Kubernetes - What is a Pod?
Learn what a Kubernetes Pod is and how to create one. We will also go over what a ReplicaSet is and how they manage Pods.
Table of Contents 📖
What is a Pod?
A Kubernetes Pod is a group of one or more Docker containers. The containers in the Pod share resources and a network. Each Pod is assigned a unique IP address and hostname while the containers inside the Pod communicate with each other via localhost.
Pods are ephemeral (last for a very short time). When a Pod is restarted, all the Docker containers inside it are restarted too and the Pod is given a new IP address. This is why we communicate with Pods using Kubernetes Services. Kubernetes Services expose Pods to the outside world.
INFO: A Kubernetes Pod is the smallest deployable unit of computing that can be created and managed.
Working with Pods
Lets create a Kubernetes Pod that houses an Nginx container using a YAML file called pod.yaml.
apiVersion: v1
kind: Pod
metadata:
name: my-nginx-pod
spec:
containers:
- name: my-nginx-c
image: nginx:alpine
ports:
- containerPort: 80
Now lets create the Pod using the Kubernetes client kubectl.
kubectl apply -f pod.yaml
pod/my-nginx-pod created
We can then inspect this Pod using the kubectl get pods command and the kubectl logs command.
kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-pod 1/1 Running 0 37s
kubectl logs my-nginx-pod
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/08/20 14:58:17 [notice] 1#1: using the "epoll" event method
2024/08/20 14:58:17 [notice] 1#1: nginx/1.27.1
2024/08/20 14:58:17 [notice] 1#1: built by gcc 13.2.1 20240309 (Alpine 13.2.1_git20240309)
2024/08/20 14:58:17 [notice] 1#1: OS: Linux 6.10.0-linuxkit
2024/08/20 14:58:17 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/08/20 14:58:17 [notice] 1#1: start worker processes
2024/08/20 14:58:17 [notice] 1#1: start worker process 30
2024/08/20 14:58:17 [notice] 1#1: start worker process 31
2024/08/20 14:58:17 [notice] 1#1: start worker process 32
2024/08/20 14:58:17 [notice] 1#1: start worker process 33
2024/08/20 14:58:17 [notice] 1#1: start worker process 34
2024/08/20 14:58:17 [notice] 1#1: start worker process 35
2024/08/20 14:58:17 [notice] 1#1: start worker process 36
2024/08/20 14:58:17 [notice] 1#1: start worker process 37
2024/08/20 14:58:17 [notice] 1#1: start worker process 38
2024/08/20 14:58:17 [notice] 1#1: start worker process 39
2024/08/20 14:58:17 [notice] 1#1: start worker process 40
2024/08/20 14:58:17 [notice] 1#1: start worker process 41
ReplicaSets
Creating a Pod this way isn't a good idea as once it is destroyed we cannot recover it.
kubectl delete pod my-nginx-pod
pod "my-nginx-pod" deleted
This is where ReplicaSets come in. A ReplicaSet is a resource that maintains a stable number of Pod copies (replicas). The ReplicaSet controller guarantees that a specific number of Pods will always be running. We can set the number of Pods with the replicas key.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-replica-set
spec:
replicas: 3
selector:
matchLabels:
app: my-replica-set
template:
metadata:
labels:
app: my-replica-set
spec:
containers:
- name: my-nginx-c
image: nginx:alpine
ports:
- containerPort: 80
kubectl apply -f replica-set.yaml
replicaset.apps/my-replica-set created
kubectl get pods
NAME READY STATUS RESTARTS AGE
my-replica-set-jk6zn 1/1 Running 0 51s
my-replica-set-t752f 1/1 Running 0 51s
my-replica-set-w6jc6 1/1 Running 0 51s
Now lets delete one of these Pods and see how the ReplicaSet spins up another one instantly.
kubectl delete pod my-replica-set-jk6zn
pod "my-replica-set-jk6zn" deleted
kubectl get pods
NAME READY STATUS RESTARTS AGE
my-replica-set-t54lx 1/1 Running 0 3s
my-replica-set-t752f 1/1 Running 0 103s
my-replica-set-w6jc6 1/1 Running 0 103s