Skip to content

Commit

Permalink
Fix up kube image to use alpine after merge for 0.0.0.0 address & rem…
Browse files Browse the repository at this point in the history
…ove the need for nginx
  • Loading branch information
chris-cmsoft committed Mar 26, 2019
1 parent b87e41f commit e7166c6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
/.DS_store
/.DS_store
port-log.txt
22 changes: 13 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
FROM ubuntu:16.04
FROM alpine:3.9

WORKDIR /code
ENV KUBE_LATEST_VERSION="v1.13.0"

RUN apt-get update && \
apt-get install -y apt-transport-https curl python3-dev nginx && \
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \
touch /etc/apt/sources.list.d/kubernetes.list && \
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list && \
apt-get update && \
apt-get install -y kubectl=1.11.1-00
RUN echo 'http://nl.alpinelinux.org/alpine/edge/testing' >> /etc/apk/repositories \
&& apk add --update kubernetes python3 \
&& mkdir -p /root/.kube \
&& rm -rf /usr/bin/kubelet \
/usr/bin/kubeadm \
/usr/bin/kube-scheduler \
/usr/bin/kube-proxy \
/usr/bin/kube-controller-manager \
/usr/bin/kube-apiserver \
/usr/bin/hyperkube

COPY . /code
WORKDIR /code

RUN chmod +x map_services.py entrypoint.sh

Expand Down
7 changes: 3 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
version: '2'
version: '3'

services:
kube_forwards:
build: .
entrypoint: ['tail', '-f', '/dev/null']
environment:
- SERVICES=authenticationservice:8000, srservice:8001:8000
- SERVICES=stock-service:3000
ports:
- 8011:8000
- 8012:8001
- 3000:3000
volumes:
- ~/.kube/config:/root/.kube/config
- .:/code
Expand Down
62 changes: 22 additions & 40 deletions map_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,42 +57,35 @@ def validate_service_spec(service_spec):


def port_forward_service(service_spec):
# kubectl get pods --selector=app=authenticationservice --output=jsonpath={.items..metadata.name}

service_parts = service_spec.strip().split(':')

output = run(['kubectl', 'get', 'pods', '--selector=app={}'.format(service_parts[0].strip()),
'--output=jsonpath={.items..metadata.name}'], stdout=PIPE)
# kubectl get pods --selector=app=health --output=jsonpath={.items..metadata.name} | | |
# TODO: Provide better mechanism to specify selector as everything might not be app=
# This utility needs to provide forwarding

pods_output = Popen([
'kubectl',
'get',
'pods',
'--selector=app={selector}'.format(selector=service_parts[0].strip()),
'--output=jsonpath={.items..metadata.name}',
], stdout=PIPE)

pod_names = pods_output.communicate()[0].decode().split()

if not output.stdout.decode().strip():
if len(pod_names) <= 0:
logging.error('No pods found for selector {}'.format(service_parts[0]))
exit(1)

port_forwarding_pod = pod_names[0]

local_port = service_parts[1]
local_proxy_port = int(local_port) + 1000
remote_port = local_port if (len(service_parts) == 2) else service_parts[2]
global nginx_directives
nginx_directives += """
server {{
listen {listen_port};
server_name _;
location / {{
proxy_pass http://127.0.0.1:{proxy_port};
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-Proto https;
}}
}}
""".format(listen_port=local_port, proxy_port=local_proxy_port)

Popen('kubectl port-forward {} {}:{} >> port-log.txt &'.format(
output.stdout.decode().strip(),
local_proxy_port,
service_parts[1] if (len(service_parts) == 2) else service_parts[2]

Popen('kubectl port-forward --address 0.0.0.0 {} {}:{} >> port-log.txt &'.format(
port_forwarding_pod,
local_port,
remote_port,
), close_fds=True, shell=True)

print('Port Forwarding: {} to local port {} from port {}'.format(
Expand All @@ -101,13 +94,6 @@ def port_forward_service(service_spec):
remote_port,
))

def update_nginx_configs():
file = open('/etc/nginx/sites-enabled/default', 'w+')
file.truncate(0)
file.write(nginx_directives)
file.close()


if __name__ == "__main__":
services = os.environ.get('SERVICES', None)

Expand All @@ -118,12 +104,8 @@ def update_nginx_configs():
service_list = services.split(',')

for service in service_list:
# Validate will throw error and exit if services i oncorrectly defined.
# Validate will throw error and exit if services incorrectly defined.
validate_service_spec(service)

for service in service_list:
port_forward_service(service)

update_nginx_configs()

Popen('service nginx restart', shell=True)

0 comments on commit e7166c6

Please sign in to comment.