-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathKubernetes Notes
290 lines (190 loc) · 5.68 KB
/
Kubernetes Notes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
Kubernetes Notes:
=======================
1. Kubectl get nodes
2. kubectl get nodes -o wide
Object 1 : POD
=============================
Create a file Pod-defitnion.yml
apiVersion: v1
kind: Pod
metadata:
name: pod1
labels:
app: java
author: sonal
spec:
containers:
- image: nginx
name: c1
# kubectl create -f Pod-defitnion.yml
# kubectl get pods
# kubectl describe pod pod1 | less
Object 2 : ReplicasSet
===========================
ReplicaSet
A ReplicaSet's purpose is to maintain a stable set of replica Pods running at any given time.
As such, it is often used to guarantee the availability of a specified number of identical Pods.
A ReplicaSet is defined with fields, including
a selector that specifies how to identify Pods it can acquire,
a number of replicas indicating how many Pods it should be maintaining,
and a pod template specifying the data of new Pods it should create to meet the number of replicas criteria.
A ReplicaSet then fulfills its purpose by creating and deleting Pods as needed to reach the desired number. When a ReplicaSet needs to create new Pods, it uses its Pod template.
LAB Execution Steps:
Step1:
Delete all exisitng Pods
kubectl delete pods --all
Step2:
Create Replicas using ReplicaSet YAML file
kubectl create -f https://raw.githubusercontent.com/Sonal0409/Container-Orchestration-using-Kubernetes/main/Day2-Notes/ReplicaSet-Demo/ReplicaSet.yml
Step3:
Validate the creation of Replicas
kubectl get all
kubectl get pods
kubectl get pods -o wide
Scale up the replicas for the current replicaSet
kubectl scale --replicas=5 replicaset myrs
kubectl get pods -o wide
Scale down the replicas for the current replicaSet
kubectl scale --replicas=2 replicaset myrs
kubectl get pods -o wide
Delete Replicaset
kubectl delete replicaset myrs
==================================================================
Lab execution steps:
Execute below steps:
Create the deployment object and Nodeport service:
kubectl create -f https://raw.githubusercontent.com/Sonal0409/Container-Orchestration-using-Kubernetes/main/Day2-Notes/Deployment/deployment.yml
Validate:
kubectl get all
kubectl get deployment kubeserve -o wide
kubectl set image deploy kubeserve app=leaddevops/kubeserve:v2
kubectl rollout status deploy kubeserve
kubectl get all
kubectl get deployment kubeserve -o wide
kubectl set image deploy kubeserve app=leaddevops/kubeserve:v3
lets rollback to older version of image:
kubectl rollout undo deploy kubeserve
Delete the deployment:
kubectl delete deploy kubeserve
kubectl get all
===================================================
Step 1: Run the below pod with multiple containers:
kind: Pod # kubectl api-resources
apiVersion: v1
metadata:
name: multi-cont-pod1
namespace: default
labels: # are mandatory # helps to indentify a group of pods in cluster # in kube if you do not provide a label kube will assing one automatically
role: dev # here both key & value are your choice
spec:
#restrtPolicy: Always # Never
containers:
- name: cont1
image: httpd
- name: cont2
image: nginx
- name: ucont
image: ubuntu
# kubectl create -f multi-containerpod.yml
# kubectl get pods
You will see that only one container is up and rinning, 2 containers have failed
# kubectl describe pod multi-cont-pod1
You will see that cont 2 and ucont have failed and kubernetes(pod) tried to
restart the containers again and again due to restart policy as Always
Now check the logs for error of cont2
kubectl logs multi-cont-pod1 -c cont2
Now check logs for error of ucont
No logs will be there
**************************************
Now lets correct the YAMl
Instead of nginx, lets take tomcat image and second container as it has different port number
and lets pass a command with ubuntu container, that will invoke a process in the container
command: ["bash", "-c", "sleep 6000"]
Invoke the bash shell process and run a specific command as sleep 6000,
which means we are running a specific sleep process in the ubuntu container this time
Create a new YAML file:
# vim multi-containerpod2.yml
kind: Pod # kubectl api-resources
apiVersion: v1
metadata:
name: multi-cont-pod2
namespace: default
labels: # are mandatory # helps to indentify a group of pods in cluster # in kube if you do not provide a label kube will assing one automatically
role: dev # here both key & value are your choice
spec:
#restrtPolicy: Always # Never
containers:
- name: cont1
image: httpd
- name: cont2
image: tomcat
- name: ucont
image: ubuntu
command: ["bash", "-c", "sleep 6000"]
# kubectl create -f multi-containerpod2.yml
# kubectl get pods
===================================================
Springboot and Mongo DB deployment:
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mongodb
name: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
creationTimestamp: null
labels:
app: mongodb
spec:
containers:
- image: lerndevops/samples:mongodb
name: mongo
---
apiVersion: v1
kind: Service
metadata:
name: mongo
spec:
type: ClusterIP
ports:
- port: 27017
targetPort: 27017
selector:
app: mongodb
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-app
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: lerndevops/samples:springboot-app
name: springboot-app
---
apiVersion: v1
kind: Service
metadata:
name: springboot-app-svc
spec:
type: NodePort
ports:
- port: 80
targetPort: 8080
selector:
app: myapp