-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkubernetes.txt
More file actions
481 lines (351 loc) · 13.1 KB
/
Copy pathkubernetes.txt
File metadata and controls
481 lines (351 loc) · 13.1 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
>>>>>>> Kubernetes <<<<<<<<<
Basic Commands
Minikube
KubeCTL
Create YML File
Microk8s CMDS
-------------------------------------------------
Basic Commands :
0. Display all list (pods,services,deployments & riplicaSets)
kubectl get all --all-namespaces
microk8s enable dns storage //Use add-ons
1. Run a yamal file
//Output redirect to new yaml file
kubectl run secondpod --dry-run --generator=run-pod/v1 --image=coolgaurab147/nginx-custom -o yaml > mysecondpd.yml
kubectl create -f firstpod.yml --dry-run
kubectl apply -f firstpod.yml
cat myfirstpod.yml
kubectl create -f firstpod.yml
2. Pods
kubectl get pods
kubectl delete pod myfirstpod
kubectl explain pod --recursive !less
kubectl run secondpod --generator=run-pod/v1 --image=coolgaurab147/nginx-custom
//Client side run
kubectl run secondpod --dry-run --generator=run-pod/v1 --image=coolgaurab147/nginx-custom
kubectl run secondpod --dry-run --generator=run-pod/v1 --image=coolgaurab147/nginx-custom -o yaml
3. Services
kubectl get services
4. Deployment
kubectl get pods
kubectl create deployment <deploymentName> --image=nginx
kubectl create deployment <deploymentName> --image=mysql --replicas=2
kubectl scale deployment <deploymentName> --replicas=1
kubectl get deployments
kubectl get deployments -o wide
kubectl get deployments -o yaml
kubectl get deployments -o json
kubectl describe deployment
kubectl describe deployment <deploymentName>
kubectl rollout undo deployment <deploymentName> --to-revision=1
-------------------------------------------------
What is Minicube
Minikube installation steps in windows
How to deploy first spring boot application to Kubernetes
-------------------------------------------------
-------------------------------------------------
Installatioin:
1. Minikube ( )
2. KubeCTL ( )
Requirements:
1. Virtual Box Or Hiper-V or docker ( as a driver )
--------------------------------------------------
--------------------------------------------------
1. Minikube Installation ( minikube.exe )
Usually used to run k8s cluster in your local machine/computer.
Minikube runs a single-node Kubernetes cluster on your machine so that you can try out Kubernetes for your daily development work.
Minikube is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node.
2. KubeCtl Installation ( Kubectl.exe )
kubectl. The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs. For more information including a complete list of kubectl operations, see the kubectl reference documentation.
# Download minikube & kubectl
# Install first minikube second kubectl
# Path to environment variable ( kubectl & minikube )
#
# Open cmd write command
> minikube start --driver=docker
> minikube status
> kubectl cluster-info
> kubectl get node
Now install spring boot project
> minikube docker-env
> copy the above command showing @FOR /F %1 ('minikube -p minikube docker-env') Do @i
> docker images
> ........... more
-------------------------------------------------------------------
-------------------------------------------------------------------
//Creating first yml file
MyFirst Yaml File:
apiVersion:
kind:
metadata:
spec:
apiVersion: v1
kind: Pod
metadata:
name: myfirstpod
labels:
env:prod
spec:
containers:
- name: containername
image: coolgaurab147/nginx-custom
Run Yamal File:
kubectl create -f firstpod.yml --dry-run
cat myfirstpod.yml
kubectl create -f firstpod.yml
kubectl get pods
kubectl explain pod --recursive !less
//Create k8s pod
kubectl run secondpod --generator=run-pod/v1 --image=coolgaurab147/nginx-custom
//Client side run
kubectl run secondpod --dry-run --generator=run-pod/v1 --image=coolgaurab147/nginx-custom
kubectl run secondpod --dry-run --generator=run-pod/v1 --image=coolgaurab147/nginx-custom -o yaml
//Output redirect to new yaml file
kubectl run secondpod --dry-run --generator=run-pod/v1 --image=coolgaurab147/nginx-custom -o yaml > mysecondpd.yml
Commands:
kubectl get pods
kubectl delete pods myfirstpod
ls
kubectl apply -f mysecond.yml //Create pod
Example YML File:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
--------------------------------------------
>>> Microk8s CMDS <<<<<<
sudo snap install microk8s --classic --channel=1.25 //Install MicroK8s
sudo usermod -a -G microk8s $USER //Join the group
sudo chown -f -R $USER ~/.kube
su - $USER // Password
microk8s status --wait-ready //Check the status
microk8s kubectl get nodes //Access Kubernetes
microk8s kubectl get services
alias kubectl='microk8s kubectl'
microk8s kubectl create deployment nginx --image=nginx //Deploy an app
microk8s kubectl get pods
microk8s enable dns storage //Use add-ons
//microk8s
microk8s stop
microk8s start
microk8s status
microk8s kubectl get all --all-namespaces
microk8s kubectl get pods
microk8s kubectl create deployment <deploymentName> --image=nginx
microk8s kubectl create deployment <deploymentName> --image=mysql --replicas=2
microk8s kubectl scale deployment <deploymentName> --replicas=1
microk8s kubectl get deployments
microk8s kubectl get deployments -o wide
microk8s kubectl get deployments -o yaml
microk8s kubectl get deployments -o json
microk8s kubectl describe deployment
microk8s kubectl describe deployment <deploymentName>
microk8s kubectl rollout undo deployment <deploymentName> --to-revision=1
--------------------------------------------
//In Pod's container
kubectl get pods
kubectl exec <podName> env
kubectl exec <podName> -c <containerName> env
kubectl exec <podName>
kucbctl get pods
kubectl exec <podName> <containerName> -it bash
kubectl exec <podName> -c <containerName> -it bash
kubectl exec <podName> -c <containerName> -it ls /
Some CMDS:
kubectl expose pod <podName>
kubectl get pod
kubectl expose pod <podname> --port=8000 --target=80 --name <serviceName>
kubectl get services
kubectl get svc
curl 10.88.20e.56:8000
//Mulit containers in a pod (shared network)
kubectl get pods
kubectl apply -f firstpod.yml
kubectl get pods
kubectl grep name firstpod.yml
kubectl exec myfirstpod -c firstcontainer -it bash
netstat -nltp
netcat -l -p 8000 //Port listen
telnet localhost 8000
ifconfig
//initContainer (Add initContainer in above yml file)
initContainers:
- name: initcontainer
image: coolgourav147/nginx-custom
env:
- name: myname
value: Gaurav
- name: City
value: Jaipur
args: ["sleep","3600"]
- name: secondcontainer
image: coolgourav147/nginx-custom
kubectl apply -f firtpod.yml
kubectl get pods -w
--------------------------------------------
Kubernetes Short Commands: (Copied on github to kubernetes.txt page)
Minikube vs Kind vs K3s
kubectl get all
kubectl get rs
kubectl get ns
kubectl get pods -n kube-system
Minikube:
minikube kubectl get all
minikube kubectl get rs
minikube kubectl get ns
minikube kubectl get pods -n kube-system
kind kubectl get all
kind kubectl get rs
kind kubectl get ns
kind kubectl get pods -n kube-system
microk8s kubectl get all
microk8s kind kubectl get rs
microk8s kind kubectl get ns
microk8s kind kubectl get pods -n kube-system
//Installation Minikube on ubuntu
Minikube System Requirements
2 GB RAM or more
2 CPU / vCPU or more
20 GB free hard disk space or more
Docker / Virtual Machine Manager – KVM & VirtualBox
Prerequisites for minikube
Minimal Ubuntu 20.04 LTS / 21.04
Sudo User with root privileges
Stable Internet Connection
Step 1) Apply updates:
Apply all updates of existing packages of your system by executing the following apt commands,
sudo apt update -y
sudo apt upgrade -y
Once all the updates are installed then reboot your system once.
sudo reboot
Step 2) Install Minikube dependencies
Install the following minikube dependencies by running beneath command,
sudo apt install -y curl wget apt-transport-https
Step 3) Download Minikube Binary
Download Minikube latest minikube binary
https://www.linuxtechi.com/wget-command-practical-examples/
wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
Once the binary is downloaded, copy it to the path /usr/local/bin and set the executable permissions on it
sudo cp minikube-linux-amd64 /usr/local/bin/minikube
sudo chmod +x /usr/local/bin/minikube
Verify the minikube version
minikube version
minikube version: v1.27.0 //cmd
commit: 4243041b7a72319b9be7842a7d34b6767bbdac2b //cmd
Step 4) Install Kubectl utility
Kubectl is a command line utility which is used to interact with Kubernetes cluster. It is used for managing deployments, service and pods etc. Use below curl command to download latest version of kubectl.
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl //cmd
Once kubectl is downloaded then set the executable permissions on kubectl binary and move it to the path /usr/local/bin.
chmod +x kubectl //cmd
sudo mv kubectl /usr/local/bin/ //cmd
Now verify the kubectl version
kubectl version -o yaml //cmd
Step 5) Start minikube
As we are already stated in the beginning that we would be using docker as base for minikue, so start the minikube with the docker driver, run
minikube start --driver=docker //cmd
In case you want to start minikube with customize resources and want installer to automatically select the driver then you can run following command,
minikube start --addons=ingress --cpus=2 --cni=flannel --install-addons=true --kubernetes-version=stable --memory=6g //cmd
//Run below minikube command to check status,
ajay@sisaudiya:~$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
pkumar@linuxtechi:~$
-------------------------------------------------
Docker:
Containers can not communicate each other
Auto scaling and load balancing
Was not possible
container had to be manage carefully
Features of Kubernetes :
Orchestration (Clustering of any no. of containers running of different network)
Auto Scaling (Vertically and Horizontally)
Auto Healing
Load Balancing
Plateform Independent (Cloud, Virtual, Physical)
Fault Tolerance (node/pod failure)
Rollback (Going back to pervious version)
Health monitoring of container
Batch Execution
ubuntu:
netstat -nltp
netcat -l -p 8000 //To listen
telnet localhost 8000
ifconfig
Enter into container
grep name abcd.yml
------------------------------------------
------------------------------------------
Kubernetes: (Working)
Pod with HostPath:
apiVersion: v1
kind: Pod
metadata:
name: hostpath2
labels:
app: php-vol1
spec:
containers:
- name: hostpath2
image: astechutube/custom-php8.0-apache
#command: ["/bin/bash", "-c", "sleep 15000"]
volumeMounts:
- name: testvol2
mountPath: /var/www/html
volumes:
- name: testvol2
hostPath:
path: /tmp/php1
------------------------------------------
Kubernetes: (Working)
Deployment with hostpath
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-hostpath
labels:
app: host-vol1
spec:
replicas: 1
selector:
matchLabels:
app: host-vol1
template:
metadata:
labels:
app: host-vol1
spec:
containers:
- name: hostpath
image: astechutube/custom-php8.0-apache
volumeMounts:
- name: testvolume
mountPath: /var/www/html
ports:
- containerPort: 80
volumes:
- name: testvolume
hostPath:
path: /tmp/var/www/html
CMDS:
microk8s kubectl apply -f deployment3.yml
microk8s kubectl get deployment
---------------------------------------------