Count

  1. # app.py
  2. import os
  3. import time
  4. import redis
  5. from flask import Flask
  6.  
  7. def get_ip():
  8. return os.getenv('MYREDIS_HOST', 'localhost')
  9.  
  10. app = Flask(__name__)
  11. cache = redis.Redis(host=get_ip(), port=6379)
  12.  
  13. def get_hit_count():
  14. retries = 5
  15. while True:
  16. try:
  17. return cache.incr('hits')
  18. except redis.exceptions.ConnectionError as exc:
  19. if retries == 0:
  20. raise exc
  21. retries -= 1
  22. time.sleep(0.5)
  23.  
  24. @app.route('/')
  25. def hello():
  26. count = get_hit_count()
  27. return 'Hello from Docker! I have been seen {} times.\n'.format(count)
  28.  
  29. if __name__ == '__main__':
  30. app.run(host='0.0.0.0', port=5000)
  1. # requirements.txt
  2. flask
  3. redis
  1. # Dockerfile
  2. FROM python:3.7-alpine
  3. WORKDIR /code
  4. ENV FLASK_APP=app.py
  5. ENV FLASK_RUN_HOST=0.0.0.0
  6. RUN apk add --no-cache gcc musl-dev linux-headers
  7. COPY requirements.txt requirements.txt
  8. RUN pip install -r requirements.txt
  9. EXPOSE 5000
  10. COPY . .
  11. CMD ["python", "app.py"]
  1. # app-deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: webapp-deployment
  6. labels:
  7. app: webapp
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: webapp # let deployment know which pod for this deployment
  13. template: # configuration for pods
  14. metadata:
  15. labels:
  16. app: webapp # each pod has a unique name, but pods can share the same label
  17. spec:
  18. containers: # containers in a pod, usually add one container per pod
  19. - name: webapp
  20. image: lchenlangley/count # pull image from local
  21. imagePullPolicy: Never
  22. ports:
  23. - containerPort: 5000
  24. env:
  25. - name: MYREDIS_HOST
  26. value: redis-service
  1. # app-service.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: webapp-service # service name accessed by app
  6. spec:
  7. selector:
  8. app: webapp # match the pod labels
  9. ports:
  10. - protocol: TCP
  11. port: 5000 # service port
  12. targetPort: 5000 # pod port
  1. # redis-deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: redis-deployment
  6. labels:
  7. app: redis
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. app: redis # let deployment know which pod for this deployment
  13. template: # configuration for pods
  14. metadata:
  15. labels:
  16. app: redis # each pod has a unique name, but pods can share the same label
  17. spec:
  18. containers: # containers in a pod, usually add one container per pod
  19. - name: redisdb
  20. image: redis:alpine # pull image from Docker Hub
  21. ports:
  22. - containerPort: 6379
  1. # redis-service.yaml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: redis-service # service name accessed by app
  6. spec:
  7. selector:
  8. app: redis # match the pod labels
  9. ports:
  10. - protocol: TCP
  11. port: 6379 # service port
  12. targetPort: 6379 # pod port
Minikube
  1. # select Kubernetes/minikube on Docker Desktop
  2.  
  3. # 1. Start minikube
  4. minikube start
  5. eval $(minikube docker-env) # point to inside of Minikube container
  1. # 2. Create image in the registry of the Minikube container
  2. docker build -t lchenlangley/count .
  1. # 3. Deploy app on K8s cluster or minikube
  2.  
  3. # kubectl get all, search service name
  4. kubectl create -f k8s # k8s is the directory of yaml files
  1. # 4. Expore service port to user
  2. kubectl port-forward svc/webapp-service 8000:5000 # [external port]:[service port]
  1. # 5. Access app
  2. http://localhost:8000/
  1. # Cleaning Up
  2. kubectl delete -f k8s
Reference
  • How to Run Locally Built Docker Images in Kubernetes