Deployment⚓︎
This tutorial builds on Hello Kubernetes to create kubernetes pods using a deployment.
Create Cluster⚓︎
Create a kind cluster named tutorial with kubernetes version 1.34.0.
If you haven't done this before, see Install Kind in the previous tutorial.
kind create cluster --name tutorial --image kindest/node:v1.34.0
Create a Deployment⚓︎
Next, we'll create a deployment which creates and manages multiple pods. Each pod is similar to the one we created manually in Create a Pod.
Create Deployment Definition⚓︎
Create a new file hello-node-deployment.yaml and paste the following.
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-node #(1)!
labels: #(2)!
app: hello-node
spec:
replicas: 3 #(3)!
selector: #(4)!
matchLabels:
app: hello-node
template: #(5)!
metadata:
labels: #(6)!
app: hello-node
spec:
containers:
- name: app
image: hello-node:1.0.1
imagePullPolicy: Never
ports:
- containerPort: 3000
namecan be anything that contains only letters, numbers, and dashes. Kubernetes will name pods starting with the deployment name.- The deployment's
metadata.labelswill make it easier for us to inspect both the deployment and its pod with a single command. Otherwise, they aren't related to the deployment'sspec.selector,matchLabelsor the pod'sspec.template.metadata.labelsbelow. - Settings
replicas: 3will create3pods. - The
spec.selectortells kubernetes how to find pods managed by the deployment. It should match labels defined inspec.template.metadata.labels. spec.templatedefines how to create the deployment's pods. Notice how the deployment'sspec.template.specmatches the podspecin Create Pod Definition.- The pod
template'smetadata.labelsshould include the labels fromspec.selector.matchLabelsabove.
Apply Deployment Definition⚓︎
Apply the deployment definition to create the deployment in the cluster.
kubectl apply -f hello-node-deployment.yaml
deployment.apps/hello-node created
Get Resources⚓︎
Show all kubernetes resources with the label app: node.
kubectl get all -l app=hello-node
NAME READY STATUS RESTARTS AGE
pod/hello-node-749cb98bdb-hmqvd 1/1 Running 0 32s
pod/hello-node-749cb98bdb-v7jkj 1/1 Running 0 32s
pod/hello-node-749cb98bdb-z6s99 1/1 Running 0 32s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-node 3/3 3 3 32s
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-node-749cb98bdb 3 3 3 32s
If the pods have a STATUS of ErrImageNeverPull, see Load Image from Hello Kubernetes.
Notice 3 pods have STATUS=Running, and the deployment shows READY 3/3.
In addition to these resource types, we also see a replicaset.
You can read more about how a Replica Set works in the kubernetes documentation.
For now, it's enough to know that kubernetes uses replica sets to manage a deployment's pods.
Show Logs⚓︎
Just like we did with a single pod, we can inspect the logs for all of the pods created by the hello-node deployment.
kubectl logs -l app=hello-node --prefix
[pod/hello-node-749cb98bdb-hmqvd/app] Server running at http://0.0.0.0:3000/
[pod/hello-node-749cb98bdb-v7jkj/app] Server running at http://0.0.0.0:3000/
[pod/hello-node-749cb98bdb-z6s99/app] Server running at http://0.0.0.0:3000/
Port Forwarding⚓︎
kubectl supports forwarding a port to one of a deployment's pods.
To avoid collisions with earlier tutorials, We'll forward a different host port (3003) to container port 3000.
kubectl port-forward deployment/hello-node 3003:3000
Forwarding from 127.0.0.1:3003 -> 3000
Forwarding from [::1]:3003 -> 3000
kubectl port-forward will keep running to handle connections.
Open http://127.0.0.1:3003/ in the browser, and we can again see our Hello World greeting!

kubectl port-forward will also generate some additional output.
Handling connection for 3003
Now that we're done testing, press Ctrl+C in your terminal to stop the kubectl port-forward.
Logging Pod Name⚓︎
Now that we have more than one pod running, it would be nice to know which is handling the request.
Update Code To Log Pod Name⚓︎
We'll need to modify the source code of our node service to print the pod name.
| index.js | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
- Set the javascript constant
podNameto the value stored in thePOD_NAMEenvironment variable. IfPOD_NAMEis not set, use'unknown', which will happen until we reconfigure the deployment. - Include
podNamein the response shown in the browser. - Include
podNamein the log message printed on pod startup.
Rebuild the docker image:
docker build --tag hello-node:1.0.2 . && kind load docker-image --name tutorial hello-node:1.0.2
Update Deployment Image⚓︎
Modify the deployment to use the new version:
| hello-node-deployment-1.0.2.yaml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
- version updated to
1.0.2to match the new docker image
Re-apply the deployment definition.
kubectl apply -f hello-node-deployment-1.0.2.yaml
deployment.apps/hello-node configured
Check the startup logs.
kubectl logs -l app=hello-node --prefix
[pod/hello-node-b84c9b567-pjcfj/app] Server running on pod unknown at http://0.0.0.0:3000/
[pod/hello-node-b84c9b567-v6pgj/app] Server running on pod unknown at http://0.0.0.0:3000/
[pod/hello-node-b84c9b567-d86sd/app] Server running on pod unknown at http://0.0.0.0:3000/
Forward a port to the deployment again.
kubectl port-forward deployment/hello-node 3003:3000
Open http://127.0.0.1:3003/ in the browser, and we can see the Hello World from pod unknown greeting!

Press Ctrl+C in your terminal to stop the kubectl port-forward.
Update Deployment Environment⚓︎
Modify the deployment to set the POD_NAME environment variable.
| hello-node-deployment-1.0.2-pod-name.yaml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
- Set the
POD_NAMEenvironment variable to the pod'smetadata.nameSee Information available viafieldRefin the kubernetes documentation.
Re-apply the deployment definition.
kubectl apply -f hello-node-deployment-1.0.2-pod-name.yaml
deployment.apps/hello-node configured
Check the startup logs.
kubectl logs -l app=hello-node --prefix
[pod/hello-node-7f746ffc8d-4v4dq/app] Server running on pod hello-node-7f746ffc8d-4v4dq at http://0.0.0.0:3000/
[pod/hello-node-7f746ffc8d-8xfts/app] Server running on pod hello-node-7f746ffc8d-8xfts at http://0.0.0.0:3000/
[pod/hello-node-7f746ffc8d-zjqbc/app] Server running on pod hello-node-7f746ffc8d-zjqbc at http://0.0.0.0:3000/
Forward a port to the deployment again.
kubectl port-forward deployment/hello-node 3003:3000
Open http://127.0.0.1:3003/ in the browser, and we can see the Hello World from pod ... greeting!

Note that if you press refresh a few times, the message won't change because the same pod handles every request.
Because kubectl port-forward selects the deployment's first pod, there's no way to choose another with this method.
In the next tutorial, we'll learn how to create a Service which sends traffic to the pods.
Press Ctrl+C in your terminal to stop the kubectl port-forward.