Count

Start Minikube
# ingress on minikube on Docker Desktop does not work
# may due to that it is a container running on Docker Desktop
minikube start --kubernetes-version=v1.22.1 --driver=hyperkit --container-runtime=docker
        
Enable Ingress
minikube addons enable ingress
        
Deploy App
# app.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: count-ns

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
  namespace: count-ns
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/count # pull image from local
        imagePullPolicy: Never
        ports:
        - containerPort: 5000
        env:
        - name: MYREDIS_HOST
          value: redis-service

---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service # service name accessed by app
  namespace: count-ns
spec:
  selector:
    app: webapp # match the pod labels
  ports:
    - protocol: TCP
      port: 5000 # service port
      targetPort: 5000 # pod port

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
  labels:
    app: redis
  namespace: count-ns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis # let deployment know which pod for this deployment
  template: # configuration for pods
    metadata:
      labels:
        app: redis # 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: redisdb
        image: redis:alpine # pull image from Docker Hub
        ports:
        - containerPort: 6379

---
apiVersion: v1
kind: Service
metadata:
  name: redis-service # service name accessed by app
  namespace: count-ns
spec:
  selector:
    app: redis # match the pod labels
  ports:
    - protocol: TCP
      port: 6379 # service port
      targetPort: 6379 # pod port

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app-ingress # ingress name
  namespace: count-ns
spec:
  rules:
    - host: count.org
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: webapp-service # service name
                port:
                  number: 5000 # service port
        
kubectl apply -f app.yaml
        
Connect Ingress IP and Domain
# get ip of app-ingress
kubectl -n count-ns get ingress

# add ip and domain to /etc/hosts
...
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost
192.168.64.5    count.org
...
        
Access App
http://count.org/
        
Reference
  • Documentation