CKAD Prep Part 12 - Deployments - briansdevblog
source link: https://www.briansdevblog.com/2021/06/ckad-prep-part-12-deployments/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Kubernetes Deployments
A Kubernetes
Deployment
object provides a means of managing a group of Pod
instances, known as replicas. The Deployment
tells Kubernetes the type of Pod
you want to run and the number of Pod
instances. This is known as the desired state.
Kubernetes will actively monitor the number of active Pod
replicas and take action to ensure it is the same as the desired state. For example, if your
Deployment
specifies that there should be three Pod
replicas for a microservice, and one of those Pod
s die, Kubernetes will take corrective action and spin up a new Pod
to replace the one that failed.
Defining a Deployment
The
Deployment
below creates 3 nginx Pod
replicas.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx imagePullPolicy: Always
- spec.replicas
spec.replicas
specifies the number ofPod
s to create. - selector.matchLabels
selector.matchLabels
tells Kubernetes that thisDeploymentDeployment
will managePod
s with the labelapp: nginxapp: nginx
. - template
template
provides a template for the Pod instances that thisDeploymentDeployment
will create. - template.metadata.labels
template.metadata.labels
defines the label that will match thisPod
definition to the parentDeploymentDeployment
. - spec.containers
spec.containers
defines the containers that will run in thePod
instances that are created.
After creating this
Deployment
object we should see 3 Pod
s created as follows.
Updating a Deployment
We can update a
Deployment
to change the state of our Pod
replicas. For example, we can increase the number of replicas from 3 to 5 as follows.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 5 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx imagePullPolicy: Always
Remember that we currently have 3 Pod
replicas running in the cluster. When we apply this update, Kubernetes will see the updated
replicas: 5
and take corrective action to create an additional 2 Pod
s.
The sample code for these notes is available on Github.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK