Kubernetes | Pods
Pods
📌 Kubernetes doesn’t deploy containers directly on the worker node. The containers are encapsulated into “pods”.
📌 A pod is a single instance of an application. A pod is the smallest object that you can create in Kubernetes.
Why does Kubernetes use pods? 🤔
Pods are designed to have one (common use case) or more (advanced use cases) containers.
Grouping containers in this way allows them to communicate with each other, while still remaining isolated to some degree. Containers in a single pod are designed to share common compute resources, such as same storage, same network, same namespace, and be created together and destroyed together.
This is a good thing in the long run because your application is now ready to handle future architectural changes and scale. Organizing containers into pods allows horizontal scalability of applications. Pods can be replicated across different nodes, enabling resistance to failure.
The simplest case
1 | Big > ----------- > --- > --------- > Small |
The image below contains the following components:
- A Kubernetes cluster
- A single worker node
- A single pod
- A single container
- A single application
Explanation:A single-node Kubernetes cluster
with a single instance of your application
running in a single Docker container
encapsulated in a single pod
.
Best Practice
📌 Pods usually have a one-to-one relationship with containers running your application. Multiple containers running in a Pod is a rare use case.
📌 Don’t add additional (same kind) containers to an existing pod to scale your application!
A Pod contains at most one container of the same type.
📌 當連線到應用程式的使用者增加時,該如何擴展你的應用程式來分攤負載(share the load)?
[ X ]
在同一個 Pod 中,啟動新的 Container。
(A cluster, a worker node, two containers in a pod)
[ O ]
新增一個 Pod ,並在其中啟動新的 Container。
(A cluster, a worker node, two pods, a container in a pod)
[ O ]
若當前 worker node 沒有足夠容量時,新增一個 worker node,並在該 worker node 上新增一個 Pod,接著在 Pod 上啟動新的 container。
(A cluster, two worker nodes, a worker node can have multiple pods, a container in a pod)
圖解 Kubectl Command
📌 Kubectl Command 會自動創建一個 Pod,並在其內部署一個 Container。
1 | kubectl run nginx --image nginx |
1 | kubectl get pods |
YAML in Kubernetes
- Note:
- apiVersion: string
- kind: string
- metadata: dictionary
- containers: list/array
- Check “kind” for pod/replicationcontroller/replicaset/deployment/service
1
kubectl explain [pod | replicationcontroller | replicaset | deployment | service]
- Help
1
kubectl create deployment --help
definition.yml
1 | apiVersion: [v1 | apps/v1] |
👍 Quickly create Kubernetes YAML Templates
1 | kubectl run redis --image=redis --dry-run=client -o yaml > redis_template.yaml |
Pods with YAML
pod-definition.yml
1 | apiVersion: v1 |
Run pod-definition.yml
1 | kubectl create -f pod-definition.yml |
View Pod details
1 | kubectl describe pod [myapp-pod] |
Delete Pod
1 | kubectl delete pod [myapp-pod] |
Service with YAML
service-definition.yml
1 | apiVersion: v1 |
ReplicaSet with YAML
replicaset-definition.yml
1 | apiVersion: apps/v1 |
Deployment with YAML
deployment-definition.yml
1 | apiVersion: apps/v1 |