Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.
A Pod is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.
Think of a Pod as a wrapper around containers, ensuring they work as a single unit.
Single Container Pod
We can start or run a particular image in a pod using kubectl run command.
➜ kubectl run nginx --image=nginx --port=80
pod/nginx created
This command will create a pod named nginx will single container named nginx using image nginx and open port 80. We can see the details using command kubectl describe.
metadata.name: nginx: The name of the Pod (must be unique in the namespace).
spec: The specification of the Pod.
containers: List of containers that will run inside this Pod.
- name: nginx: The name of the container inside the Pod.
image: nginx: The Docker image used to run the container (pulled from Docker Hub by default).
ports: Defines which ports are opened on the container.
containerPort: 80: The container listens on port 80.
To apply it as usual we can use kubectl apply -f <file> command.
Multiple Container Pod
To create a pod that have multiple containers we just need to add new entries in containers section. Create new file pod.yaml and put example configuration below.
The command above will print logs from pod nginx-with-logger specifically in container logger. To print logs from all container we can add option --all-containers=true.
Access the Pod
To access the pod we can use kubectl port-forward command.
➜ kubectl port-forward nginx-with-logger 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
This command will forward traffic from port 8080 in our local machine to pod named nginx-with-logger port 80.
Lets try to access it using curl and we should see default response from nginx server.
➜ curl http://localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Delete Pod
To delete pod we can use kubectl delete pods <pod-name> command.