-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2-Pod
249 lines (208 loc) · 6.79 KB
/
2-Pod
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
--------------------------------------------------------------------------
command to create a pod from a image - kubectl run nginx --image=nginx
command to get the node on which pod is placesd - kubectl get pods -o wide
command to create a dryrun and get a yml from run command - kubectl run redis --image=redis123 --dry-run=client -o yaml >pod.yaml (this will not create pod but give us yaml output for deploying pod)
command with command - kubectl run static-busybox --image=buzybox --commmand sleep 1000 --dry-run=client -o yaml >pod.yaml
command to get all the options available in a pod yaml file - kubectl explain pod --recursive|less
commands to generate yml file - https://kubernetes.io/docs/reference/kubectl/conventions/
to deploy the pod from yaml (do changes to yaml file and run the command kubectl apply -f pod.yaml)
Example yaml to deploy two containers in a pod
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod (this is name of pod)
labels:
app: myapp
anykey: anyvalue
costcenter: US
spec:
containers:
- name: nginx-container (this is name of container in pod)
image: nginx (this is the image that kubernetes gets from docker hub)
- name: redis-container
image: redis
Add the above line to pod-definition.yaml (cat > pod-definition.yaml and paste the above thing)
and run the command - kubectl create -f pod-definition.yaml - this creates a pod
to see the pods - kubectl get pods
command to see the pods with labels : kubectl get pods -l name=payroll
kubectl get pods --show-labels
to see the status of a pod - kubectl describe pod <pod name>
to edit a pod- kubectl edit pod redis
before -
apiVersion: v1
kind: Pod
metadata:
name: redis
labels:
app: my-redis-app
cost-centre: US
spec:
containers:
- name: redis
image: redis123
kubectl create -f pod-definition.yaml - now as image name is wrong, pod will have error saying image name wrong
to fix this edit the image name from redis123 to redis , to edit a pod- kubectl edit pod redis (this will open the running conf of a pod, go to the bottom , edit image name from redis123 to redis and save)
to get the logs of a pod command -kubectl logs <podname>
to run a shell command on a pod use command - kubectl exec --namespace=kube-public curl -- sh -c ' <cmmand>'
yamls with args to a conatiner -
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod (this is name of pod)
labels:
app: myapp
anykey: anyvalue
costcenter: US
spec:
containers:
- name: ubuntu-sleeper
image: ubuntu
command: ["sleep"]
args: ["10"]
ports:
- conatinerPort: 8080
env:
- name: APP_COLOR (name of environmental variable)
value: blue (value to be assaigned to env vaiable)
- name: APP_CLUSTER
value: prod
OR
yamls with args to a conatiner -
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod (this is name of pod)
labels:
app: myapp
anykey: anyvalue
costcenter: US
spec:
containers:
- name: ubuntu-sleeper
image: ubuntu
command:
- "sleep"
- "1200"
ports:
- conatinerPort: 8080
env:
- name: APP_COLOR (name of environmental variable)
value: blue (value to be assaigned to env vaiable)
CONFIGMAP
to read the environmental variables from a file we create an configmap
imperative way to create a configmap - kubectl create configmap <config name> --from-literal=<key>=<value>
then run command - kubectl create configmap app-config --from-literal=APP-COLOR=blue --from-literal=APP-CLUSTER=prod
declarative way -
cat >config-map.yaml
apiVersion: v1
kind: configmap
metadata:
name: app-config
data:
APP-COLOR=blue
APP-CLUSTER=prod
kubectl create -f config-map.yaml
commad to view config maps - kubectl get configmaps
commad to describe config maps - kubectl describe configmaps
now we need to configure a pod with configmap we created
example - pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod (this is name of pod)
labels:
app: myapp
anykey: anyvalue
costcenter: US
spec:
containers:
- name: webserver
image: nginx
command: ["nginx"]
ports:
- conatinerPort: 8080
envFrom:
- configMapRef:
name: app-config
kubectl create -f pod-definition.yaml
SECRETS
data stored in a secret is encoded or hashed format
two steps to use secret -
1 -create the secret2 - inject it into pod
imperative way to create a secret
use command - kubectl create secret generic <secret name> --from-literal=<key>=<value>
create secrect from file - kubectl create secret generic <secret name> --from-file=<path to file>
data in file -
<key1>=<value1>
<key2>=<value2>
<key3>=<value3>
declarative way -
example - cat > secret-definition.yaml
apiVersion: v1
kind: Secret
metadata:
name: myapp-pod (this is name of secret)
labels:
app: myapp
anykey: anyvalue
costcenter: US
data:
<key1>=<value1>
<key2>=<value2>
<key3>=<value3>
kubectl create -f secret-definition.yaml
command to view secrets - kubectl get secrets
command to describe secret - kubectl descibe secrets - this shows keys but not values
command to see the values in a secrets - kubectl get secret <secret name> -o yaml
pod-definition file to use secret -
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod (this is name of pod)
labels:
app: myapp
anykey: anyvalue
costcenter: US
spec:
containers:
- name: webserver
image: nginx
command: ["nginx"]
ports:
- conatinerPort: 8080
envFrom:
- secretRef:
name: <secret name>
another way -
env:
- name: <secret name>
valueFrom:
secretKeyRef:
name: <key>
key: <value>
INITCONTAINER -
When a POD is first created the initContainer is run, and the process in the initContainer must run to a completion before the real container hosting the application starts.
exmple yaml for init conatiner -
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
- name: init-myservice-2
image: busybox
command: ['sh', '-c', 'git clone <some-repository-that-will-be-used-by-application> ; done;']
ref - https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
--------------------------------------------------------------------------