0% found this document useful (0 votes)
12 views

Pdf 11-Writing YAML Files

The document provides a detailed guide on creating and managing Kubernetes deployments using YAML files. It outlines the structure of a deployment YAML, including metadata, specifications for replicas, selectors, and container definitions. Additionally, it describes the steps to apply the YAML file, verify the deployment, and filter pods by labels.

Uploaded by

chinnakumark8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Pdf 11-Writing YAML Files

The document provides a detailed guide on creating and managing Kubernetes deployments using YAML files. It outlines the structure of a deployment YAML, including metadata, specifications for replicas, selectors, and container definitions. Additionally, it describes the steps to apply the YAML file, verify the deployment, and filter pods by labels.

Uploaded by

chinnakumark8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Writing YAML

YAML Structures

1 kind:
2 apiVersion:
3 metadata:
4 name:
5 labels:
6 label:
7 spec:
8 replicas:
9 strategy:
10 selector:
11 matchLabels:
12 label:
13 template:
14 metadata:
15 labels:
16 label:
17 spec:
18 containers:
19 - name:
20 image:
21 command:
22 args:
23 workingDir:
24 ports:
25 env:
26 resources:
27 volumeMounts:
28 livenessProbe:
29 readinessProbe:
30 lifecycle:
31 terminationMessagePath:
32 imagePullPolicy:
33 securityContext:
34 stdin:
35 stdinOnce:
36 tty:

Identification of the objects we create in Kubernetes


1 kind: Deployment # What kind of Deployment?
2 apiVersion: apps/v1
3 metadata:
4 name: DeploymentName-CorpWebsite # Deployment NAME
5 labels:
6 label:
7 spec:
8 replicas: 1 # How many POD REPLICAS do we want?
9 strategy:
10 selector:
11 matchLabels:
12 label: PodLabel-CorpWebsite
13 template:
14 metadata:
15 labels:
16 label: PodLabel-CorpWebsite # Pod LABEL
17 spec:
18 containers:
19 - name: ContainerName-CorpWebsite # Container NAME
20 image: tanvisinghny/ssl-website # Docker Hub Image Pulled
21

Deploy and test:

Step-1 Create a deployment YAML file called deployment1.yaml

1 sudo nano deployment1.yaml

1 ---
2 kind: Deployment
3 apiVersion: apps/v1
4
5 # Declare the Deployment Name
6 metadata:
7 name: dep-name-ssl-website
8 labels:
9
10 # Declare the Pod behaviour interms of Replicas needed
11 # Replicas are the DESIRED STATE of the Pods
12 # Kubernetes CONTROLLER always maintains the desired number of Pods
13 # Say we wanted 5 REPLICAS, Controller always compares CURRENT STATE vs DESIRED STATE
14 # If 3 PODS sudenly terminate due to failures, CURRENT STATE = 2, DESIRED STATE =5
15 # CONTROLLER imemdiately Creates 3 more PODS to ensure DESRITED STATE 5 is maintained
16 # This is "spec1", we also have another "spec" in Container Section
17 spec:
18 replicas: 1
19 # Currentky we are not using any Pod Re-creation Strategy
20 strategy:
21 # How does Kube CONTROLLER identify the PODs belonging to this Deployment Object?
22 # By using a FIELD called SELECTOR
23 # Below in the TEMPLATE SECTION we define the POD LABEL
24 # Here in the SELCTOR SECTION, declare the SAME POD LABEL
25 # CONTROLLER reads this "spec.selector.matchLabels" field
26 # CONTROLLER will maintain DESIRED STATE of PODs Matching this LABEL
27 selector:
28 matchLabels:
29 pod-name: pod-label-ssl-website
30 # POD Definition
31 # Here we are giving 2 LAbels to POD
32 # pod-name gives a LABEL to POD
33 # we used "env" to indicate that this POD deployment is Development Type
34 template:
35 metadata:
36 labels:
37 pod-name: pod-label-ssl-website
38 env: dev
39 # CONTAINER(s) Definition
40 # Define a CONTAINER NAME
41 # Define the IMAGE we want to use
42 # Here the Image is auto pulled from Docker Hub
43 # This is "spec2"
44 spec:
45 containers:
46 - name: con-name-ssl-website
47 image: tanvisinghny/ssl-website
48 ports:
49 - containerPort: 80
50 - containerPort: 443

Step-2 Apply this file to create deployment

Verify if any Pods or Deployment Objects that are running

1 linuxadmin@master:~/mydeployments$ kubectl get pods


2 No resources found in default namespace.
3
4 linuxadmin@master:~/mydeployments$ kubectl get deployments
5 No resources found in default namespace.

Create Pods via the Deployment YAML file we created

1 linuxadmin@master:~/mydeployments$ kubectl apply -f deployment1.yaml


2 deployment.apps/dep-name-ssl-website created
3

Step-3 Verify the Pods creation

1 linuxadmin@master:~/mydeployments$ kubectl get pods


2 NAME READY STATUS RESTARTS AGE
3 dep-name-ssl-website-5694cc7758-f2gdw 1/1 Running 0 31s

Step-4 Verify Deployment Object creation

1 linuxadmin@master:~/mydeployments$ kubectl get deployments


2 NAME READY UP-TO-DATE AVAILABLE AGE
3 dep-name-ssl-website 1/1 1 1 53s

Step-5 Verify that a Replica Set is created

1 linuxadmin@master:~/mydeployments$ kubectl get rs


2 NAME DESIRED CURRENT READY AGE
3 dep-name-ssl-website-5694cc7758 1 1 1 71s

Filter Pods by Label env=dev

1 linuxadmin@master:~/mydeployments$ kubectl get pods -l env=dev


2 NAME READY STATUS RESTARTS AGE
3 dep-name-ssl-website-5694cc7758-f2gdw 1/1 Running 0 11m

POD Name
<DeploymentName>.<RandomString>

Here Deployment Name we gave in YAML file = dep-name-ssl-website

Pod Name = dep-name-ssl-website-5694cc7758-f2gdw

Random String Rules

To prevent “bad words”, vowels and the numbers 0, 1 and 3 were removed from the rand.String function
So, the <RandomString> will be composed by a combination of the following alphanumeric characters: bcdfghjklmnpqrstvwxz2456789

Is POD NAME so important?

For Kubernetes POD name is irrelevant.


Kubernetes always identifes PODs by LABEL.

You might also like