apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-frontend
  labels:
    app.kubernetes.io/name: web-frontend
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: web-frontend
  template:
    metadata:
      labels:
        app.kubernetes.io/name: web-frontend
    spec:
      containers:
        - name: web-frontend-container
          image: learncloudnative/helloworld:0.1.0
          ports:
            - containerPort: 3000

Comparing this deployment to ones we used in previous labs, you will notice we changed the resource names and the image we are using.

One new thing we added to the deployment is the ports section. Using the containerPort field, we set the port number website server listens on. The learncloudnative/helloworld:0.1.0 is a simple Node.js Express application.

Save the above YAML in web-frontend.yaml and create the deployment:

$ kubectl apply -f web-frontend.yaml
deployment.apps/web-frontend created

Run kubectl get pods to ensure Pod is up and running and then get the logs from the container:

$ kubectl get po
NAME                          READY   STATUS    RESTARTS   AGE
web-frontend-68f784d855-rdt97   1/1     Running   0          65s

$ kubectl logs web-frontend-68f784d855-rdt97

> [email protected] start /app
> node server.js

Listening on port 3000

From the logs, you will notice that the container is listening on port 3000. If we set the output flag to gives up the wide output (-o wide), you'll notice the Pods' IP address - 10.244.0.170:

$ kubectl get po -o wide
NAME                            READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
web-frontend-68f784d855-rdt97   1/1     Running   0          15s   172.17.0.4   minikube   <none>           <none>

If we delete this Pod, a new one will take its' place, and it will get a brand new IP address as well:

$ kubectl delete po web-frontend-68f784d855-rdt97
pod "web-frontend-68f784d855-rdt97" deleted

$ kubectl get po -o wide
NAME                            READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
web-frontend-68f784d855-8c76m   1/1     Running   0          15s   172.17.0.5   minikube   <none>           <none>

Similarly, if we scale up the deployment to four Pods, we will four different IP addresses:

$ kubectl scale deploy web-frontend --replicas=4
deployment.apps/web-frontend scaled

$ kubectl get pods -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
web-frontend-68f784d855-8c76m   1/1     Running   0          5m23s   172.17.0.5   minikube   <none>           <none>
web-frontend-68f784d855-jrqq4   1/1     Running   0          18s     172.17.0.6   minikube   <none>           <none>
web-frontend-68f784d855-mftl6   1/1     Running   0          18s     172.17.0.7   minikube   <none>           <none>
web-frontend-68f784d855-stfqj   1/1     Running   0          18s     172.17.0.8   minikube   <none>           <none>