lifecycle of a pod
TRANSCRIPT
![Page 1: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/1.jpg)
Lifecycle of a Pod
Kubernetes Pune Meetup #7
08-July-2017
![Page 2: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/2.jpg)
About Me
![Page 3: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/3.jpg)
Contents
Kubernetes Architecture recap
Introduction to Pod
Interaction with Pod
Birth of a Pod
Stages of a Pod’s lifecycle
Probes
Handlers for Lifecycle Events
![Page 4: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/4.jpg)
Recap : Kubernetes Architecture
Image Courtesy: https://blog.heptio.com/core-kubernetes-jazz-improv-over-orchestration-a7903ea92ca
kubeproxy
kubeproxy
![Page 5: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/5.jpg)
Introduction to Pod
Pod is the smallest schedulable unit in Kubernetes
Containers within a Pod share:
IP address space
Port space
Mounted Volumes
Containers within a Pod can communicate via
Localhost
IPC mechanism such as Semaphores or Shared Memory
Pods are never created directly. Higher level constructs such as RC’s,
![Page 6: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/6.jpg)
Interaction with Pods
Kubectl create -f <POD Definition YAML>
Kubectl get pods
Kubectl describe pod <POD NAME>
Kubectl logs <POD NAME>
Kubectl exec <POD NAME> -- <COMMAND>
Kubectl top <POD NAME>
![Page 7: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/7.jpg)
Birth of a Pod
Image Courtesy: Joe Beda
https://blog.heptio.com/core-kubernetes-
jazz-improv-over-orchestration-
a7903ea92ca
![Page 8: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/8.jpg)
Phases of a Pod
Pending: Accepted by Kubernetes but container not created yet.
Running: Pod bound to a node, all containers created and at least one
container is running/starting/restarting
Succeeded: Container(s) exited with status 0
Failed: All containers exit and at least one exited with non-zero status.
Unknown: State of Pod could not be determined due to communication issues
with its node
CrashLoopBackOff: Container fails to start and is retried again and again.
![Page 9: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/9.jpg)
Probes
Probe is a diagnostic performed periodically by kubelet on a container. There are
three types of handlers which can be used to probe containers:
ExecAction: Executes specified command inside container.
TCPSocketAction: Performs check against TCP socket. Returns success if port
is open
HTTPGetAction: Performs HTTP GET against specified port and path.
Successful if 200 > status code > 400.
![Page 10: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/10.jpg)
Liveness and Readiness probes
livenessProbe: Indicates whether the Container is running. If the liveness probe
fails, the kubelet kills the Container, and the Container is subjected to its restart
policy.
readinessProbe: Indicates whether the Container is ready to service requests. If
the readiness probe fails, the endpoints controller removes the Pod’s IP address
from the endpoints of all Services that match the Pod.
![Page 11: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/11.jpg)
Termination of a Pod
![Page 12: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/12.jpg)
Handlers to Lifecycle events
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: nginx
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:
exec:
command: ["/usr/sbin/nginx","-s","quit"]
![Page 13: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/13.jpg)
Init Containers
Out of Beta since 1.6
Init containers allow some pre-actions to run before the actual container is started.
Init containers run to completion sequentially.
Example Use Cases:
Copy source code from a repo
Generate config file for application (update pod specific variables etc)
Check readiness of dependent services
![Page 14: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/14.jpg)
Init Containers Example
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
- name: init-mydb
image: busybox
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
![Page 15: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/15.jpg)
Init Containers Example (continued)kind: Service
apiVersion: v1
metadata:
name: myservice
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9376
---
kind: Service
apiVersion: v1
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
![Page 16: Lifecycle of a pod](https://reader036.vdocument.in/reader036/viewer/2022092617/5aaba46c7f8b9ac7658b4619/html5/thumbnails/16.jpg)