Running your first Kubernetes Pods
apiVersion: v1 kind: Pod metadata: name: hello-pod labels: app.kubernetes.io/name: hello spec: containers: - name: hello-container image: busybox command: ['sh', '-c', 'echo Hello from my container! && sleep 3600']
pod.yamland use the following command to create the Pod:
kubectl apply -f pod.yaml
kubectl get podsto list all Pods running the
defaultnamespace of the cluster.
$ kubectl get pods NAME READY STATUS RESTARTS AGE hello-pod 1/1 Running 0 7s
logscommand to see the output from the container running inside the Pod:
$ kubectl logs hello-podHello from my container!
When running multiple containers running inside the same Pod, you can use the
-cflag to specify the container name whose logs you want to get. For example:
kubectl logs hello-pod -c hello-container
kubectl delete pod hello-pod, the Pod will be gone forever because nothing would automatically restart it.
kubectl get podsagain, you will notice the Pod is gone:
$ kubectl get pods No resources found in default namespace.
Keeping the Pods running with ReplicaSets
replicasfield in the resource definition.
apiVersion: apps/v1 kind: ReplicaSet metadata: name: hello labels: app.kubernetes.io/name: hello spec: replicas: 5 selector: matchLabels: app.kubernetes.io/name: hello template: metadata: labels: app.kubernetes.io/name: hello spec: containers: - name: hello-container image: busybox command: ['sh', '-c', 'echo Hello from my container! && sleep 3600']
replicaset.yamland use the following command to create the ReplicaSet:
kubectl apply -f replicaset.yaml
$ kubectl get replicaset NAME DESIRED CURRENT READY AGE hello 5 5 5 30s
$ kubectl get po NAME READY STATUS RESTARTS AGE hello-dwx89 1/1 Running 0 31s hello-fchvr 1/1 Running 0 31s hello-fl6hd 1/1 Running 0 31s hello-n667q 1/1 Running 0 31s hello-rftkf 1/1 Running 0 31s
kubectl get po -l=app.kubernetes.io/name=hello, we will get all Pods that have
app.kubernetes.io/name: hellolabel set. The output includes the five Pods we created.
ownerReferencesfield. We can use the
-o yamlflag to get the YAML representation of any object in Kubernetes. Once we get the YAML, we will search for the
$ kubectl get po hello-dwx89 -o yaml | grep -A5 ownerReferences --- ownerReferences: - apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: ReplicaSet name: hello
ownerReferences, Kubernetes sets the
hello, and the
ReplicaSet. The name corresponds to the ReplicaSet that owns the Pod.
poin the command to refer to Pods? Some Kubernetes resources have short names you can use in place of the 'full names'. You can use
powhen you mean
deploywhen you mean
deployment. To get the full list of supported short names, you can run
hello-pod, because that's what we specified in the YAML. Using the ReplicaSet, Kubernetes names the Pods using the combination of the ReplicaSet name (
hello) and a semi-random string such as
fchrvand so on.
deletekeyword followed by the resource (e.g.
pod) and the resource name
hello-dwx89(note that your Pod will have a different name):
$ kubectl delete po hello-dwx89 pod "hello-dwx89" deleted
kubectl get podsagain. Did you notice something? There are still five Pods running.
$ kubectl get po NAME READY STATUS RESTARTS AGE hello-fchvr 1/1 Running 0 14m hello-fl6hd 1/1 Running 0 14m hello-n667q 1/1 Running 0 14m hello-rftkf 1/1 Running 0 14m hello-vctkh 1/1 Running 0 48s
AGEcolumn, you will notice four Pods created 14 minutes ago and one created more recently. ReplicaSet created this new Pod. When we deleted one Pod, the number of actual replicas decreased from five to four. The replica set controller detected that and created a new Pod to match the replicas' desired number (5).
apiVersion: v1 kind: Pod metadata: name: stray-pod labels: app.kubernetes.io/name: hello spec: containers: - name: stray-pod-container image: busybox command: ['sh', '-c', 'echo Hello from stray pod! && sleep 3600']
stray-pod.yamlfile, then create the Pod by running:
$ kubectl apply -f stray-pod.yaml pod/stray-pod created
kubectl get podsyou will notice that the
stray-podhas disappeared. What happened?
stray-podwith the label
app.kubernetes.io/name=hellothat matches the selector label on the ReplicaSet, the ReplicaSet took that new Pod for its own. Remember, the manually created Pod didn't have the owner. With this new Pod under ReplicaSets' management, the number of replicas was six and not five, as stated in the ReplicaSet. Therefore, the ReplicaSet did what it's supposed to do; it deleted the new Pod to maintain the desired state of five replicas.
busybox:1.31.1. We could use
kubectl edit rs helloto open the replica set YAML in the editor, then update the image value.
$ kubectl describe po hello-fchvr | grep image Normal Pulling 14m kubelet, docker-desktop Pulling image "busybox" Normal Pulled 13m kubelet, docker-desktop Successfully pulled image "busybox"
busyboximage, but there's no sign of the
busybox:1.31.1anywhere. Let's see what happens if we delete this same Pod:
$ kubectl delete po hello-fchvr pod "hello-fchvr" deleted
hello-q8fnlin this case) to match the desired replica count from the previous test we did. If we run
describeagainst the new Pod that came up, you will notice how the image has changed:
$ kubectl describe po hello-q8fnl | grep image Normal Pulling 74s kubelet, docker-desktop Pulling image "busybox:1.31" Normal Pulled 73s kubelet, docker-desktop Successfully pulled image "busybox:1.31"
busybox). The ReplicaSet would start new Pods, and this time, the Pods would use the new image
kubectl delete rs hello.
rsis the short name for
replicaset. If you list the Pods (
kubectl get po) right after you issued the delete command, you will see the Pods getting terminated:
NAME READY STATUS RESTARTS AGE hello-fchvr 1/1 Terminating 0 18m hello-fl6hd 1/1 Terminating 0 18m hello-n667q 1/1 Terminating 0 18m hello-rftkf 1/1 Terminating 0 18m hello-vctkh 1/1 Terminating 0 7m39s