Pod Overview
Pods
Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.
What is a Pod?
Note: While Kubernetes supports more container runtimes than just Docker, Docker is the most commonly known runtime, and it helps to describe Pods using some terminology from Docker.
The shared context of a Pod is a set of Linux namespaces, cgroups, and potentially other facets of isolation – the same things that isolate a Docker container. Within a Pod’s context, the individual applications may have further sub-isolations applied.
In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes
Using Pods
- Pods that run a single container. The “one-container-per-Pod” model is the most common Kubernetes use case; in this case, you can think of a Pod as a wrapper around a single container; Kubernetes manages Pods rather than managing the containers directly.
- Pods that run multiple containers that need to work together. A Pod can encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. These co-located containers form a single cohesive unit of service—for example, one container serving data stored in a shared volume to the public, while a separate sidecar container refreshes or updates those files.
Pod networking
Each Pod is assigned a unique IP address for each address family. Every container in a Pod shares the network namespace, including the IP address and network ports. Inside a Pod (and only then), the containers that belong to the Pod can communicate with one another using localhost. When containers in a Pod communicate with entities outside the Pod, they must coordinate how they use the shared network resources (such as ports). Within a Pod, containers share an IP address and port space, and can find each other via localhost.
To Create the pod ( with name nginx-pod by using nginx docker image) with command
kubectl run nginx-pod –image nginx
To list the pod with its IP address and NodeName
kubectl get pods -o wide
To interact with pod’s terminal(go inside the pod)
kubectl exec -it nginx-pod– bash
To Exit from the pod run “exit” command
To access the application running in the pod ( consider 192.168.135.2 is IP of the pod)
curl 192.168.135.2
To find all the properties of the pod in yaml format
kubectl get pods nginx-pod -o yaml
To find all the properties of the pod in json format
kubectl get pods nginx-pod-o json
To Delete the Pod
kubectl delete pod nginx-pod
Manifest file in Kubernetes
IDE (Integrated Development Environment)
Visual Studio Code (VS Code Download)
Notepad/Notepad++
Sublime text editor
Atom
My First Pod Manifest file (nginx-pod.yaml)
apiVersion: v1 # apiVersion for the resource
kind: Pod # type of resource (Pod)
metadata:
name: myapp # Pod name
labels: # user defined key value pair
name: myapp # name is the key and myapp is value
spec:
containers: # Containers information for the pod
– name: myapp # name of the container to access via pod
image: nginx # docker image for myapp container
# below code is optional
resources: # resources define the memory or cpu limits for a container
limits: # maximum limit for the container
memory: “128Mi” # maximum 128 mb of memory used by the container
cpu: “500m” # maximum cpu usage 500mz
ports: # Ports where the container’s app is running
– containerPort: 80 # nginx runs on port 80
To create Pod by using above file you need to run below command
kubectl create -f nginx-pod.yaml
To Verify Pod is created with its IP address and other properties
kubectl get pods -o wide
To Verify the events and other run-time properties of the pod.
kubectl describe pod myapp
If you make any change in the manifest file then use the apply(change/create) command
kubectl apply -f nginx-pod.yaml
To find the logs generated by pod
kubectl logs myapp
To interact with pod
kubectl exec -it myapp — bash
To delete the pod using manifest file
kubectl delete -f nginx-pod.yaml
To verify pod is deleted or not
kubectl get pods
MultiContainer Pod
Create 2 containers (nginx, tomcat) within a single pod multicont.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp
labels:
name: myapp
spec:
containers:
– name: nginx-container
image: nginx
# below code is optional
resources:
limits:
memory: “64Mi”
cpu: “100m”
ports:
– containerPort: 80 # nginx runs on port 80
– name: tomcat-container
image: tomcat
# below code is optional
resources:
limits:
memory: “64Mi”
cpu: “200m”
ports:
– containerPort: 8080 # tomcat runs on port 8080
To create Pod by using above file you need to run below command
kubectl create -f multicont.yaml
To Verify Pod is created with its IP address and other properties
kubectl get pods -o wide
To Verify the events and other run-time properties of the pod.
kubectl describe pod myapp
If you make any change in the manifest file then use the apply(change/create) command
kubectl apply -f multicont.yaml
To find the logs generated by pod nginx-container
kubectl logs myapp -c nginx-container
To find the logs generated by pod nginx-container
kubectl logs myapp -c tomcat-container
To interact with pod for nginx-container
kubectl exec -it myapp -c nginx-container — bash
To interact with pod for tomcat-container
kubectl exec -it myapp -c tomcat-container — bash
To access nginx app using multicontainer pod (IP 192.168.135.3)
curl 192.168.135.3:80
To access tomcat app using multicontainer pod (IP 192.168.135.3)
curl 192.168.135.3:8080
To delete the pod using manifest file
kubectl delete -f multicont.yaml
To verify pod is deleted or not
kubectl get pods
Pod Lifecycle
This page describes the lifecycle of a Pod. Pods follow a defined lifecycle, starting in the Pending phase, moving through Running if at least one of its primary containers starts OK, and then through either the Succeeded or Failed phases depending on whether any container in the Pod terminated in failure.
LAB
To create a pod with docker image nginx
Kubectl run nginx-pod –image=nginx
1.# nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
tier: dev
spec:
containers:
– name: nginx-container
image: nginx
2. Create and display Pods
# Create and display PODs
kubectl create -f nginx-pod.yaml
kubectl get pod
kubectl get pod -o wide
kubectl get pod nginx-pod -o yaml
kubectl describe pod nginx-pod
3. Test & Delete
# To get inside the pod
kubectl exec -it nginx-pod — bash
# Create test HTML page
cat <<EOF > /usr/share/nginx/html/test.html
<!DOCTYPE html>
<html>
<head>
<title>Testing..</title>
</head>
<body>
<h1 style=”color:rgb(90,70,250);”>Hello, DevopsWorld…!</h1>
<h2>Congratulations, you passed 🙂 </h2>
</body>
</html>
EOF
exit
# Expose PODS using NodePort service
kubectl expose pod nginx-pod –type=NodePort –port=80
# Display Service and find NodePort
kubectl describe svc nginx-pod
kubectl get svc
# Open Web-browser and access webapge using
http://nodeip:nodeport/test.html
# Delete pod & svc
kubectl delete svc nginx-pod
kubectl delete pod nginx-pod