Do you know how pod is created on Kubernetes?

Murari Saran Vikas
4 min readMay 25, 2021

I don’t waste much time of your time in introducing myself. Let’s dive in.

Simple YAML file to create a pod on kubernetes cluster.

apiVersion: v1
kind: Pod
metadata:
name: init-demo
spec:
containers:
— name: nginx
image: nginx
ports:
— containerPort: 80

By looking at above PodSpec we are asking kubernetes to create a container on one of kubernetes node. Lets dive into it

Here comes….. the magic

In order to get better understanding what I did was

Open 4 terminals in parallel.

1st terminal run

$ kubectl proxy — port=8080

running above command gives you access to list kubernetes apis.

I am just listing out first 10 apis just to test if it is working or not

$ curl http://127.0.0.1:8080/ | head -10
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 5494 0 5494 0 0 22424 0 — : — : — — : — : — — : — : — 22424
{
“paths”: [
“/api”,
“/api/v1”,
“/apis”,
“/apis/”,
“/apis/admissionregistration.k8s.io”,
“/apis/admissionregistration.k8s.io/v1”,
“/apis/admissionregistration.k8s.io/v1beta1”,
“/apis/apiextensions.k8s.io”

On the second terminal watch for the events that triggers to the API events, using below command.

curl -s 127.0.0.1:8080/api/v1/watch/events

Ok, I will do it. Whats next and why did you say open 4 terminals and still two terminals are empty on my system which are consuming process, memory etc

OMG,

Open 3rd terminal and create a pod using above YAML file.

$ kubectl apply -f my-pod.yaml

On your 2nd terminal you can see the some of the events being listed out.

This is 1st event which got triggered

{“type”:”ADDED”,”object”:{“kind”:”Event”,”apiVersion”:”v1",”metadata”:{“name”:”init-demo.1682611c9fc08a2d”,”namespace”:”default”,”selfLink”:”/api/v1/namespaces/default/events/init-demo.1682611c9fc08a2d”,”uid”:”e908dbaa-24b4–4f0d-9ad9–939ec65b93cd”,”resourceVersion”:”16561280",”creationTimestamp”:”2021–05–25T17:59:51Z”,”managedFields”:[{“manager”:”kube-scheduler”,”operation”:”Update”,”apiVersion”:”events.k8s.io/v1",”time”:”2021–05–25T17:59:51Z”,”fieldsType”:”FieldsV1",”fieldsV1":{“f:action”:{},”f:eventTime”:{},”f:note”:{},”f:reason”:{},”f:regarding”:{“f:apiVersion”:{},”f:kind”:{},”f:name”:{},”f:namespace”:{},”f:resourceVersion”:{},”f:uid”:{}},”f:reportingController”:{},”f:reportingInstance”:{},”f:type”:{}}}]},”involvedObject”:{“kind”:”Pod”,”namespace”:”default”,”name”:”init-demo”,”uid”:”e26be962-c3a2–45b4–84d7–8529d27be215",”apiVersion”:”v1",”resourceVersion”:”16561274"},”reason”:”Scheduled”,”message”:”Successfully assigned default/init-demo to 172.16.2.131",”source”:{},”firstTimestamp”:null,”lastTimestamp”:null,”type”:”Normal”,”eventTime”:”2021–05–25T17:59:51.373917Z”,”action”:”Binding”,”reportingComponent”:”default-scheduler”,”reportingInstance”:”default-scheduler-control-plane-host-10–64–221–231"}}

Now lets concentrate on

“managedFields”: [{“manager”: “kube-scheduler”,
“operation”: “Update”,

it says that the manager is kube-scheduler that is trying to do some operation of Object called `Pod`.

“involvedObject”: {
“kind”: “Pod”,
“namespace”: “default”,
“name”: “init-demo”,

which is being Scheduled on the kubernetes node 172.16.2.131 using Filter and sorting mechanism. For more details refer to kubernetes scheduler doc.

“reason”: “Scheduled”,
“message”: “Successfully assigned default/init-demo to 172.16.2.131”,
“eventTime”: “2021–05–25T17:59:51.373917Z”,
“action”: “Binding”,
“reportingComponent”: “default-scheduler”,

Sorry, due to large contents of the events I am pasting only important contents.

Lets see 2nd event it says kubelet update event

“managedFields”: [
{
“manager”: “kubelet”,
“operation”: “Update”,

and Object type is Pod

“involvedObject”: {
“kind”: “Pod”,
“namespace”: “default”,
“name”: “init-demo”,

More detail here — kubelet which is running on the node 172.16.2.131 is trying to pull image nginx. Basically docker pull nginx.

“reason”: “Pulling”,
“message”: “Pulling image \”nginx\””,
“source”: {
“component”: “kubelet”,
“host”: “172.16.2.131”

Next message/event from the kubelet says that image was pulled successfully.

“reason”: “Pulled”,
“message”: “Successfully pulled image \”nginx\” in 5.414647872s”,

Next, kubelet is successfully created the container on the node 172.16.2.131.

“reason”: “Created”,
“message”: “Created container nginx”,
“source”: {
“component”: “kubelet”,
“host”: “172.16.2.131”

Next, kubelet dropped a event saying that it started the container nginx on the Node.

“reason”: “Started”,
“message”: “Started container nginx”,
“source”: {
“component”: “kubelet”,
“host”: “172.16.2.131”

Thats all, now container is running on the kubernetes cluster happily.

Image: Pod Creation, Do you know how pod is created on Kubernetes?

You might be seeing this picture every where but thought to posting it again.

I want to modify a Pod, lets say I want to add labels to the Pod. This event got triggered when I added labels t

{“type”:”MODIFIED”,”object”:{“kind”:”Event”,”apiVersion”:”v1",”metadata”:{“name”:”tekton-pipelines-webhook.168250be2d31bcfc”,”namespace”:”tekton-pipelines”,”selfLink”:”/api/v1/namespaces/tekton-pipelines/events/tekton-pipelines-webhook.168250be2d31bcfc”,”uid”:”764a1275-ac2b-42d1-a920–14171bfe0093",”resourceVersion”:”16992463",”creationTimestamp”:”2021–05–25T12:59:53Z”,”managedFields”:[{“manager”:”kube-controller-manager”,”operation”:”Update”,”apiVersion”:”v1",”time”:”2021–05–25T12:59:53Z”,”fieldsType”:”FieldsV1",”fieldsV1":{“f:count”:{},”f:firstTimestamp”:{},”f:involvedObject”:{“f:apiVersion”:{},”f:kind”:{},”f:name”:{},”f:namespace”:{},”f:resourceVersion”:{},”f:uid”:{}},”f:lastTimestamp”:{},”f:message”:{},”f:reason”:{},”f:source”:{“f:component”:{}},”f:type”:{}}}]},”involvedObject”:{“kind”:”HorizontalPodAutoscaler”,”namespace”:”tekton-pipelines”,”name”:”tekton-pipelines-webhook”,”uid”:”06eed5f4–93dd-47f4–835d-5edae7604221",”apiVersion”:”autoscaling/v2beta2",”resourceVersion”:”1051846"},”reason”:”FailedGetResourceMetric”,”message”:”unable to get metrics for resource cpu: unable to fetch metrics from resource metrics API: the server could not find the requested resource (get pods.metrics.k8s.io)”,”source”:{“component”:”horizontal-pod-autoscaler”},”firstTimestamp”:”2021–05–25T12:59:53Z”,”lastTimestamp”:”2021–05–26T05:30:01Z”,”count”:3902,”type”:”Warning”,”eventTime”:null,”reportingComponent”:””,”reportingInstance”:””}}

Question:

Let’s say if you have modified Pod resource, example lets say you want to add labels to the Pod.

Will the event be triggered?

a) Yes

b) No

Test it out and let me know in the comments section.

Also read about the resourceVersion helps in understanding the updation events.

Before adding pod labels resourceVersion is 16574507.

"metadata": {
"name": "init",
"namespace": "default",
"selfLink": "/api/v1/namespaces/default/pods/init",
"uid": "f6744840-dce7-4a93-babf-a4f01b85e17f",
"resourceVersion": "16574507",
"creationTimestamp": "2021-05-25T18:20:57Z",
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"name\":\"init\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"image\":\"nginx\",\"name\":\"nginx\",\"ports\":[{\"containerPort\":80}]}]}}\n"
}

After adding pod labels resourceVersion is 16992401.

"metadata": {
"name": "init",
"namespace": "default",
"selfLink": "/api/v1/namespaces/default/pods/init",
"uid": "f6744840-dce7-4a93-babf-a4f01b85e17f",
"resourceVersion": "16992401",
"creationTimestamp": "2021-05-25T18:20:57Z",
"labels": {
"env": "production",
"owner": "michael"
},
"annotations": {
"kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"labels\":{\"env\":\"production\",\"owner\":\"michael\"},\"name\":\"init\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"image\":\"nginx\",\"name\":\"nginx\",\"ports\":[{\"containerPort\":80}]}]}}\n"
},

Don’t forget I said 4 terminals and you have used only 3, what is the use of 4th one.

Please run : echo “Bye”

on the 4th terminal.

See you in next article.

--

--