Kubernetes Pods
What is a Pod?
A Pod is the smallest execution unit of an application and means something like a virtual host grouped as the basic management unit in Kubernetes.
In other words, it is the name Kubernetes uses for a container or a group of containers. You can think of it simply as a container.
Because a Pod shares a virtual network interface, the same IP, and filesystem, it acts like a virtual host.
It contains information such as one or more application containers, storage, and networking.
Characteristics of Pods
- Each Pod is assigned a unique private IP.
- Containers inside a Pod share the Pod IP as localhost.
- A Pod can practically be considered a virtual machine that contains Pods.
Controlling Pods
Let’s look at how to create these Pods and check their information.
Starting a Pod: kubectl run
First, let’s see how to start a Pod. Running the following command starts a Pod.
kubectl run –-image [image name]:[tag name] (options) [Pod name]
Start nginx as a web server with the following command.
kubectl run webserver --image=nginx:1.14 --port 80
Execution result:
% kubectl run webserver --image=nginx:1.14 --port 80
pod/webserver created
As with docker run, various options can also be specified for kubectl run. For example, you can set environment variables with the -e option.
Refer to the following page for other options.
Listing Pods: kubectl get
Run the following command to see the Pod list.
kubectl get pods
Execution result:
% kubectl get pods
NAME READY STATUS RESTARTS AGE
webserver 0/1 ContainerCreating 0 4s
If you run it immediately after creation, you will see that the state is ContainerCreating.
% kubectl get pods
NAME READY STATUS RESTARTS AGE
webserver 1/1 Running 0 16s
After a little more time, the state becomes Running.
To see the Pod list in more detail, run the following command.
kubectl get pods -o wide
Execution result:
% kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
webserver 0/1 Running 0 9s <none> minikube <none> <none>
Viewing Pod details: kubectl describe
To see Pod metadata, run the following command.
kubectl describe pod [Pod name]
Execution result:
% kubectl describe pod webserver
Name: webserver
Namespace: default
Priority: 0
Service Account: default
Node: minikube/192.168.49.2
Start Time: Fri, 09 Sep 2022 08:36:18 +0900
Labels: run=webserver
Annotations: <none>
Status: Running
IP: 172.17.0.3
IPs:
IP: 172.17.0.3
Containers:
webserver:
Container ID: docker://dddee2b98f8badd1a09208d99e02ba881f79091a9670e42862a7aa5f45ec4200
Image: nginx:1.14
Image ID: docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 09 Sep 2022 08:36:30 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-xmw8p (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-xmw8p:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
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 22s default-scheduler Successfully assigned default/webserver to minikube
Normal Pulling 22s kubelet Pulling image "nginx:1.14"
Normal Pulled 11s kubelet Successfully pulled image "nginx:1.14" in 11.180480547s
Normal Created 11s kubelet Created container webserver
Normal Started 10s kubelet Started container webserver
Checking Pod logs: kubectl logs
If you want to see Pod logs, run the following command.
kubectl logs [Pod name]
Execution result:
% kubectl logs webserver
172.17.0.4 - - [08/Sep/2022:23:38:30 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.85.0-DEV" "-"
Checking Pod execution result: curl
Check whether it can be accessed with the curl command.
curl [IP]
Execution result:
$ curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
Entering a Pod: kubectl exec
To enter the created Pod, run the following command.
kubectl exec -it [Pod name] [image name]
Execution result:
Enter the Pod.
% kubectl exec webserver -it -- /bin/bash
root@webserver:/#
Move to the path where nginx’s first page file exists.
root@webserver:/# cd /usr/share/nginx/html/
Check the contents.
root@webserver:/usr/share/nginx/html# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
Modify the contents.
root@webserver:/usr/share/nginx/html# echo "Hello, devkuma" > index.html
Access the server to check whether the modified contents were applied.
$ curl 172.17.0.3
Hello, devkuma
Port forwarding from a Pod: kubectl port-forward
kubectl port-forward 8080:80
Create a YAML file and start a Pod
Here, create YAML for starting a Pod and then start the Pod using that YAML.
-
Create the command
First, use the--dry-runflag to create a command that is not actually executed.kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=clientWith the
--dry-runflag, the object to be sent to the cluster can be previewed without actually executing it.Execution result:
% kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client pod/webserver created (dry run) -
Check the YAML configuration
kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client -o yamlExecution result:
% kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client -o yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: null labels: run: webserver name: webserver spec: containers: - image: nginx:1.14 name: webserver ports: - containerPort: 80 resources: {} dnsPolicy: ClusterFirst restartPolicy: Always status: {} -
Create the YAML file
Create YAML with this file content under the file namewebserver-pod.yaml.% kubectl run webserver --image=nginx:1.14 --port 80 --dry-run=client -o yaml > webserver-pod.yaml -
Delete the existing Pod Delete the existing
webserverPod with the same name.% kubectl delete pod webserver pod "webserver" deleted -
Create a new Pod from YAML Create a new Pod with the YAML file.
kubectl create -f webserver-pod.yamlCheck whether it was created correctly.
% kubectl get pods NAME READY STATUS RESTARTS AGE webserver 1/1 Running 0 12s
Delete a Pod: kubectl delete
To delete a created Pod, run the following command.
kubectl delete pod [Pod name]
Execution result:
% kubectl delete pod webserver
pod "webserver" deleted
Deploy Pods
kubectl create deployment mainui --image=httpd: --replicas=3
kubectl get deployment.apps
kubectl describe deployment.apps mainui
kubectl get pods
kubectl get pods -o wide
kubectl edit deployment.apps maini