Kubernetes is an open-source container orchestration system that automates the deployment, scaling, and management of containerized applications. One of the key building blocks in Kubernetes is the pod, which is the smallest and simplest unit in the Kubernetes object model. In this blog post, we'll go over how to create a Kubernetes pod.
Related Articles
What is a Kubernetes Pod?
A pod is the smallest deployable unit in the Kubernetes object model. It represents a single instance of a running process in a cluster. A pod encapsulates one or more containers, storage resources, and network resources that are needed to run a specific application or service.
A pod can contain one or more containers that share the same network namespace, which means they can communicate with each other over the localhost interface. A pod also has a unique IP address, which allows it to communicate with other pods in the same cluster. This IP address is allocated by the Kubernetes API server when the pod is created.
Creating a Kubernetes Pod
To create a Kubernetes pod, we need to define a pod specification in a YAML file. The specification contains information about the pod, such as the container images to use, the commands to run, and the network settings.
Let's take a look at an example pod specification:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
This YAML file defines a pod with a single container named my-container. The container uses the nginx image and exposes port 80.
Let's break down the YAML file and explain what each field means:
- apiVersion: The version of the Kubernetes API that this object uses.
- kind: The type of object we're creating, in this case, a pod.
- metadata: Metadata about the pod, such as its name.
- spec: The specification of the pod, which includes information about its containers, volumes, and other settings.
- containers: A list of containers to run in the pod.
- name: The name of the container.
- image: The container image to use.
- ports: A list of ports to expose on the container.
- containerPort: The port number to expose on the container.
- containers: A list of containers to run in the pod.
Once you've created the YAML file, you can create the pod by running the kubectl create
command and passing the YAML file as an argument:
$ kubectl create -f my-pod.yaml
This will create the pod and start the container(s) defined in the specification.
Verifying the Pod is Running
To verify that the pod is running, you can use the kubectl get pods
command:
$ kubectl get pods
This will show a list of all the pods running in the cluster. You should see your newly created pod in the list. Example output is detailed below:
NAME READY STATUS RESTARTS AGE
my-pod 1/1 Running 0 2m
my-pod-2 1/1 Running 0 1m
my-pod-3 0/1 Pending 0 5s
This output shows a list of all the pods running in the cluster, along with their current status. Let's break down each column:
- NAME: The name of the pod.
- READY: The number of containers that are ready to serve requests out of the total number of containers defined in the pod.
- STATUS: The current status of the pod, which can be Running, Pending, Succeeded, Failed, or Unknown.
- RESTARTS: The number of times the container has been restarted.
- AGE: The amount of time since the pod was created.
If you want to get more detailed information about the pod, you can use the kubectl describe
command:
$ kubectl describe pod my-pod
This will show detailed information about the pod, including its status, events, and containers. Example output is detailed below:
Name: my-pod
Namespace: default
Priority: 0
Node: minikube/192.168.64.2
Start Time: Mon, 22 Feb 2021 13:00:51 -0800
Labels: app=my-app
Annotations:
Status: Running
IP: 172.17.0.4
IPs:
IP: 172.17.0.4
Containers:
my-container:
Container ID: docker://8e5051f181...
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:73132a981eb57fca02...
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 22 Feb 2021 13:01:24 -0800
Ready: True
Restart Count: 0
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-hwqgc (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-hwqgc:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-hwqgc
Optional: false
QoS Class: BestEffort
Node-Selectors:
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m default-scheduler Successfully assigned default/my-pod to minikube
Normal Pulling 2m kubelet Pulling image "nginx:latest"
Normal Pulled 2m kubelet Successfully pulled image "nginx:latest"
Normal Created 2m kubelet Created container my-container
Normal Started 2m kubelet Started container my-container
This output provides a more detailed view of the my-pod pod, including its status, containers, volumes, and events. Let's break down some of the key sections:
- Name: The name of the pod.
- Namespace: The namespace that the pod belongs to.
- Status: The current status of the pod.
- Containers: A list of containers running in the pod.
- Events: A list of events related to the pod, such as when it was scheduled, when containers were pulled and created, and when the pod was started.
In this example, we can see that the my-pod pod has one container (my-container) running the nginx:latest image. The container is listening on port 80, and the pod is running on the minikube node.
Updating a Pod
If you need to update the pod's configuration, such as adding a new container or changing the container image, you can edit the YAML file and apply the changes using the kubectl apply
command:
$ kubectl apply -f my-pod.yaml
This will update the pod with the new configuration.
Deleting a Pod
If you need to delete a pod, you can use the kubectl delete command:
$ kubectl delete pod my-pod
This will delete the pod and all of its resources.
Conclusion
In this blog post, we went over how to create a Kubernetes pod using a YAML file. We covered the different fields in the specification and how to use the kubectl
command-line tool to create, update, and delete pods.
Creating and managing pods is an essential skill for any Kubernetes user. Pods are the building blocks of Kubernetes, and they form the basis for higher-level objects like services, deployments, and stateful sets. By understanding how to create and manage pods, you'll be well on your way to mastering Kubernetes.