As I said in the tutorial about Overview of Kubernetes components, Pod is a concept, where containers will run in Kubernetes. A Pod may contain one or more containers with the same context, the same node, shared network namespace and volume. When a Pod is dead or in trouble, Kubernetes will automatically create a new one using Controller Manager.
We have 2 ways to create a new Pod in Kubernetes that is running the container directly from the Image using the “run” command or defining the Pod’s information using a YAML configuration file and then using the “create” command” or the “apply” command to run. You can define the information of a Pod using the file with the following basic content:
1 2 3 4 5 6 7 8 9 10 11 |
apiVersion: v1 kind: Pod metadata: name: <name> spec: containers: - name: <container_name> image: <container_image> - name: <container_name> image: <container_image> ... |
Where:
- <name> is the name of Pod
- <container_name> which is the name of the container in Pod after initializing
- <container_image> is the name of the Image used to create the container.
As you can see, we can define one or more containers running in the same Pod by adding multiple sections for container names and container images in this YAML file.
My example is as follows:
1 2 3 4 5 6 7 8 9 10 |
apiVersion: v1 kind: Pod metadata: name: huongdanjava spec: containers: - name: node-hello image: gcr.io/google-samples/node-hello:1.0 - name: nginx image: nginx |
Result when using create command to create this Pod is as follows:
You can get all Pods information using:
1 |
kubectl get pods |
command as follows:
View the information of the newly created Pod using:
1 |
kubectl describe pod <pod_name> |
command:
As you can see, the 2 containers named node-hello and nginx were created in Pod “huongdanjava”. Based on this information, we also know which node our Pod is running.
You can also use the command:
1 |
kubectl logs <pod_name> -c <container_name> |
to see the log stdout of a container if that container is having. The two containers I just created, just nginx has logs, so when I execute this command, the result is as follows:
To delete a Pod, you can use the following command:
1 |
kubectl delete pod <pod-name> |
My example is as follows:
1 |
kubectl delete pod node-hello |
Result: