-
Notifications
You must be signed in to change notification settings - Fork 0
/
K8s.txt
213 lines (159 loc) · 5.77 KB
/
K8s.txt
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
minikube:
---------
This is local a Kubernetes tool which will create a node on our local machine & it focus on making it easy to learn and develop for Kubernetes.
kubectl:
--------
This is a program to control/manage the containers inside the node.
Node:
-----
Node is a physical or virtual machine where we will run the containers.
YAML:
-----
It is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted.
# Lines starts with # will be ignored as comments
1) Simplest data structure like key/value pair can be written like,
<Key>:<Space><Value>
Fruit: Apple
Here space is mandatory after colon.
2) Arrays can be written like,
Fruits:
- Apple
- Banana
- Grape
3) Dict/Hash can be written like,
Fruits:
Apple: Red
Banana: Yellow
Grape: Green
--------------------------
Development setups macOS:
--------------------------
1) Install homebrew
2) Install kubectl
- brew install kubectl
3) Install VM driver for virtualbox
- Download virtualbox from virtualbox.org
4) Install minikube
- brew cask install minikube
5) Start minikube
- minikube start
Note:
-----
In K8s, We do not create containers out of config files instead we create something called Objects, for example Pod, Service, StatufulSet, ReplicaController.
Each object servers different purpose like running a container, monitoring a container, setting up the network etc.
---------
Commands:
---------
1) Check the status of minikube
- minikube status
- kubectl cluster-info
2) Run a config file
- kubectl apply -f <path_to_config_file>
3) Get the status/details of specific object
- kubectl get <object-type>
eg: kubectl get pods OR kubectl get services
4) Get the IP address of minikube, we can accress Pod using this ip address
- minikube ip
5) Get nodes status
kubectl get nodes
6) Get pods status
kubectl get pods
7) Stop running minikube
- minikube stop
8) Get some extra columns like IP and Node name on which Pod is running
- kubectl get pods -o wide
9) Get detailed description of pods
- kubectl describe pods
10) Run the pod and fetch the nginx image from DockerHub
- kubectl run nginx --image=nginx:alpine
11) Delete the pod/object
- kubectl delete <object-type> <object-name>
- kubectl delete pod nginx
12) To create a Pod using YAML file
Create file: sample-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
name: myapp-pod
country: DHD-GJ-IN
spec:
containers:
- name: nginx-pod
image: nginx:alpine
# Now save the file and run it using kubectl
- kubectl create -f ~/Documents/projects/Kubernaties-Examples/sample-pod.yaml
# Check the status of newly created pod after above command
- kubectl describe pod myapp-pod
13) To create a ReplicationController, Below is the code
Create a file: sample-replication.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp
spec:
replicas: 3
selector:
app: myapp
template:
metadata:
name: myapp
labels:
app: myapp
spec:
containers:
- name: myapp-replication
image: nginx:alpine
ports:
- containerPort: 8080
# Now save the file and run it using kubectl
- kubectl create -f ~/Documents/projects/Kubernaties-Examples/sample-replication.yaml
# Get list of the replication controllers
- kubectl get replicationController
# Get list of the pods created by replication controllers
- kubectl get pods
14) In we want to update the replication instance when using ReplicaSet then use
- kubectl replace -f ~/Documents/projects/Kubernaties-Examples/sample-replica-set.yaml
# OR
- kubectl scale --replicas=6 -f ~/Documents/projects/Kubernaties-Examples/sample-replica-set.yaml
# OR (Without having to update the file - on the fly - adhoc)
- kubectl scale --replicas=6 replicaset <ReplicaSet-name>
15) Get list of the ReplicaSet
- kubectl get replicaset
16) Delete the ReplicaSet
- kubectl delete replicaset <ReplicaSet-name>
17) Create the Deployment
- kubectl create -f ~/Documents/projects/Kubernaties-Examples/sample-deployment.yaml
# Get list of the deployment
- kubectl get deployment
# Get list of the replication controllers created by deployment
- kubectl get replicaset
# Get list of the pods created by deployment
- kubectl get pods
18) To see all the objects using single comand
- kubectl get all
19) To update the existing deployment
- kubectl apply -f ~/Documents/projects/Kubernaties-Examples/sample-deployment.yaml
20) Check the status of rollouts in deplyment
- kubectl rollout status deplyment/<Deployment-name>
21) Check the history status of rollouts, revisions in deplyment
- kubectl rollout history deplyment/<Deployment-name>
# Note: There are two deplyment strategy
# * ReCreate - Take down all the instance and create new instance
# * Rolling Update - Take one instance down at a time and create new instance to avoid any downtime
22) To Rollback/Undo the changes after upgrade
- kubectl rollout undo deployment/<Deployment-name>
23) To fetch the default YAML configuration
- kubectl create deploy --image=nginx:alpine --dry-run=client -o yaml > config.yml
24) To fetch the YAML of running pod
- kubectl get pods/<Pod-name> -o yaml
25) To fetch the logs from cluster
- kubectl logs <object>
26) To scale up the replicas in deployment
- kubectl scale deplyment/<Deployment-name> --replica=5
27) To update the deployment (ad-hoc)
- kubectl set image deplyment/<Deployment-name> nginx=nginx:1.17-alpine
28) To edit the deploment on the fly, this will open VI/VIM or default editor
- kubectl edit deplyment/<Deployment-name>