Hello World
Deployment Mode

# 1. Create image

# Enter the project directory
docker build -t lchenlangley/hello .
        
# 2. Deposite image
docker login
docker push lchenlangley/hello
        
# 3. Deploy app on K8s cluster or minikube

# kubectl get all, search service name
kubectl create -f k8s # k8s is the directory of yaml files
        
# 4. Expore service port to user
kubectl port-forward svc/webapp-service 8082:8081 # [external port]:[service port]
        
# 5. Access app
http://localhost:8082/
        
# Cleaning Up
kubectl delete svc webapp-service
kubectl delete deploy webapp-deployment
        
# app.py
import dash
import dash_html_components as html

app = dash.Dash(__name__)

def main():
    app.layout = html.H1(children="Hello World!",
            style={"font-size": "18px", "color": "red", "font-weight": "bold", "border-style": "ridge"})

if __name__ == "__main__":
    main()
    app.run_server(debug=True, host='0.0.0.0', port=8080) # turn on Dash tools
        
# requirements.txt
dash
        
# Dockerfile
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
RUN apk add libc-dev
RUN apk add --update alpine-sdk
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 8080
COPY . .
CMD ["python", "app.py"]
        
# app-development.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp # let deployment know which pod for this deployment
  template: # configuration for pods
    metadata:
      labels:
        app: webapp # each pod has a unique name, but pods can share the same label
    spec:
      containers: # containers in a pod, usually add one container per pod
      - name: webapp
        image: lchenlangley/hello # pull image from Docker Hub
        ports:
        - containerPort: 8080
        
# app-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: webapp-service # service name accessed by app
spec:
  selector:
    app: webapp # match the pod labels
  ports:
    - protocol: TCP
      port: 8081 # service port
      targetPort: 8080 # pod port
        
Minikube on Docker Desktop

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp # let deployment know which pod for this deployment
  template: # configuration for pods
    metadata:
      labels:
        app: webapp # each pod has a unique name, but pods can share the same label
    spec:
      containers: # containers in a pod, usually add one container per pod
      - name: webapp
        image: lchenlangley/hello # pull image from local
        imagePullPolicy: Never
        ports:
        - containerPort: 8080
        
# 1. Start Minikube
minikube start
eval $(minikube docker-env) # point to inside of Minikube container
        
# 2. Create image in the registry of the Minikube container
docker build -t lchenlangley/hello .
        
# 3. Deploy app on K8s cluster or minikube
# kubectl get all, search service name
kubectl create -f k8s # k8s is the directory of yaml files
        
# 4. Expore service port to user
kubectl port-forward svc/webapp-service 8082:8081 # [external port]:[service port]
        
# 5. Access app
http://localhost:8082/
        
# Cleaning Up
kubectl delete svc webapp-service
kubectl delete deploy webapp-deployment
        
Reference
  • How to Run Locally Built Docker Images in Kubernetes