Deployment Basics
(this post is part of the material I cover in my devops course)
The main power of kubernetes is running things at scale.
Deployments are meant to run multiple pods, on multiple nodes.
About Deployments
- A kuberneteis deployment is a kubernetes object that is meant to define and run pods at scale.
- It contains another kubernetes object called ReplicaSet which is responsible on running multiple pods as part of a deployment.
- The deployment can rollout versions, by creating a new ReplicaSet, and then gradually creating new pods in it, while terminating pods in the old ReplicaSet.
- If your ReplicaSet is part of a Deployment, you should not try to manage it directly.
Creating a basic Deployment
- A deployment YAML definition contains a template of a pod.
- It means that all of the pods that are going to be created will be based on this template.
- If you name your pod (inside the template)
- Here's an example:
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: my-deployment
5spec:
6 replicas: 3
7 selector:
8 matchLabels:
9 app: my-deployment
10 template:
11 metadata:
12 labels:
13 app: my-deployment
14 spec:
15 containers:
16 - name: nginx
17 image: nginx:1.19.1
18 ports:
19 - containerPort: 80
- Here's the creation of the deployment:
1$>
2$> kubectl apply -f my-deployment.yaml
3deployment.apps/my-deployment created
4$> kubectl get deployments
5NAME READY UP-TO-DATE AVAILABLE AGE
6my-deployment 3/3 3 3 9s
7$> kubectl get replicaset
8NAME DESIRED CURRENT READY AGE
9my-deployment-979fbc74d 3 3 3 21s
10$> kubectl get pods -o wide
11NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
12my-deployment-979fbc74d-5ljk8 1/1 Running 0 28s 10.244.3.5 five-m04 <none> <none>
13my-deployment-979fbc74d-86lhp 1/1 Running 0 28s 10.244.2.6 five-m03 <none> <none>
14my-deployment-979fbc74d-kbmjl 1/1 Running 0 28s 10.244.4.5 five-m05 <none> <none>
15$>
- You can edit a deployment directly. You'll get the look and feel of the vi editor.
Try to change the number of replicas:
1kubectl edit deployments my-deployment
- You can also scale your deployment directly by using the kubectl scale command:
1kubectl scale deployment.v1.apps/my-deployment --replicas=2