Hi everyone, In prev post, we already known overview about Kubernetes. In this tutorial, we will learn some core concepts in Kubernetes
Pods - a group of one or more containers
Pods are the smallest deployable units in Kubernetes system. Pods locates in worker nodes where contains one or more containers. Each Pod like a logic machine ( have IP, hostname, process separate)
Why do you need Pods?
You are wondering why not run containers in worker nodes? Actually, Pod is a high level of a container. With a Pod, you can run one or more containers which will have a IP and share a volume.
Basic Pod configuration.
To create a Pod in Kubernetes, we will config with a yaml file or json file. In this tutorial, I will use a yaml config file.
Create a file with name
apiVersion: v1 kind: Pod metadata: name: nginx-name spec: containers: - image: nginx name: nginx-name ports: - containerPort: 80 protocol: TCP
Notes: Indentation is important in yaml file.
apiVersion: version of Kubernetes API
kind: The component will be created (here is Pod)
metadata: Give a name for Pod
spec: Describe about image, list containers, .. of the Pod Run below command to create a Pod:
kubectl apply -f pods.yaml
To verify Pod was created, run:
kubectl get pods
Using labels in Pods
With big system, we have many pods in Kubernetes cluster. To manage effectively, we can use Labels to collect Pods to small groups.
With Label have two attributes
- app: Example ui, order, …
- rel: Example stable, beta and canary
apiVersion: v1 kind: Pod metadata: name: nginx-name labels: app: ui rel: stable spec: containers: - image: nginx name: nginx-name ports: - containerPort: 80 protocol: TCP
Apply this change by run below command:
kubectl apply -f pods.yaml
To get all pods in Kubernetes with labels
kubectl get pods --show-labels
When a Pod run on Kubernetes, it may be shutdown or crash by many suddenly problems. ReplicaSet ensure that when Pod crashed, it will be created immediately again. ReplicaSet will maintain number of Pod which is declared in config file before. Example:
apiVersion: apps/v1 kind: ReplicaSet metadata: name: frontend labels: tier: frontend spec: # modify replicas according to your case replicas: 3 selector: matchLabels: tier: frontend template: metadata: labels: tier: frontend spec: containers: - name: php-redis image: gcr.io/google_samples/gb-frontend:v3
matchLabels must match with labels in metadata of template section.
Service like a bridge between components in Kubernetes.
As you know, the Pods have their own IP. Client can connect to the Pods by their IP or hostname. However, when you deploy a new version of Pod or ReplicaSet recreate new versions then IP of Pods will be changed. Furthermore, in Kubernetes has many Pods running at the same time, the clients should not all IPs of Pods. It only should know name of service.
Read more at here