2

CKAD Prep Part 12 - Deployments - briansdevblog

 3 years ago
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.
neoserver,ios ssh client

Kubernetes Deployments

A Kubernetes

Deployment
Deployment object provides a means of managing a group of Pod instances, known as replicas. The
Deployment
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
Deployment specifies that there should be three Pod replicas for a microservice, and one of those Pods die, Kubernetes will take corrective action and spin up a new Pod to replace the one that failed.

Defining a Deployment

The

Deployment
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
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 of Pods to create.
  • selector.matchLabels
    selector.matchLabels tells Kubernetes that this
    Deployment
    Deployment will manage Pods with the label 
    app: nginx
    app: nginx.
  • template
    template provides a template for the Pod instances that this
    Deployment
    Deployment will create.
  • template.metadata.labels
    template.metadata.labels defines the label that will match this Pod definition to the parent 
    Deployment
    Deployment.
  • spec.containers
    spec.containers defines the containers that will run in the Pod instances that are created.

After creating this

Deployment
Deployment object we should see 3 Pods created as follows.

CreateDeployment_3_Replicas-1.png

Updating a Deployment

We can update a

Deployment
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
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
replicas: 5 and take corrective action to create an additional 2 Pods.

UpdateDeployment.png

The sample code for these notes is available on Github.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK