-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrok8s.txt
352 lines (263 loc) · 9.45 KB
/
microk8s.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
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
#Microk8s website
microk8s.io
#Terms
- Node = kubernets master computer contols nodes, nodes do the work of running container pods
- Namespace = groups of kubernetes objects, default namespace is called "default", used for organization
- Pod = one or more containers running together to provide service
- ReplicaSet = two or more pods providing the service
- Deployment = more advanced version of ReplicaSet, controls updating better
- Service = configuration exposing a port for networking to an object
- NodePort - Pod can communicate with outside via port mapping
- Valid NodePort Ranges are from 30,000 - 32,767
- ClusterIP - Pod gets an internal IP address
- LoadBalancer - Used to distribute load of service to multiple pods
- Job - runs a job within pod and finishes
- CronJob - runs a job as a specific time
#To view snap versions of microk8s that are available to install on Ubuntu
snap info microk8s
#Using snap package to install latest stable version of Microk8s
sudo snap install microk8s --classic
#Using snap package to install specific version of Microk8s, such as 1.13
sudo snap install microk8s --classic --channel=1.13/stable
#If you want normal user to use microk8 commands without sudo
sudo usermod -a -G microk8s $USER
#Check the status of Microk8s
microk8s.status
#Enable kubernetes dashboard and kubedns services
microk8s.enable dashboard dns
microk8s.docker images
microk8s.inspect #tells you the state of the services
microk8s.reset #shuts down and restarts cluster
------------------------------------
Microk8s.kubectl
------------------------------------
#Get information about the kubernetes cluster
microk8s.kubectl cluster-info
#List all nodes that make up Kubernetes cluster
#In Microk8s it should list just your computer
microk8s.kubectl get nodes
#List all nodes that make up Kubernetes cluster, with more parameters shown
microk8s.kubectl get nodes -o wide
#Get Kubernetes componets status
microk8s.kubectl get cs
#Get Services
microk8s.kubectl get svc
To view all objects within the default namespace, prefix with "watch" to view it live
microk8s.kubectl get all -o wide
#Check the deployment process, find IP address of dashboard, prefix with "watch" to view it live
microk8s.kubectl get all --all-namespaces
#Once you have IP address of dashboard, use https to access it, you will need a token
https://<IP ADDRESS>:443
#To get the token used to login to dashboard, run the following two commands
token=$(microk8s.kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)
microk8s.kubectl -n kube-system describe secret $token
#To see events that are happening/happed run
microk8s.kubectl get events
------------------------------------
Microk8s.kubectl PODS
------------------------------------
#Create a pod for the nginx server,other examples include ansible, mongodb, redis, nodejs, etc
microk8s.kubectl run nginx --image nginx
#View the pods you have created
microk8s.kubectl get pods
#Get information about the pod, can pipe to less for reability
microk8s.kubectl describe pod nginx
#To delete the nginx pod
microk8s.kubectl delete pod nginx
#To delete all the pods
microk9s.kubectl delete pods --all
------------------------------------
Microk8s.kubectl DEPLOYMENTS
------------------------------------
#Create a deployment using Nginx
microk8s.kubectl create deployment nginx --image=nginx
#Set the deployment to autoscale between min and max replicas
microk8s.kubectl autoscale deployment nginx --min=3 --max=10
#Scale the nginx deployment to specific number of containers, rerun to change number
microk8s.kubectl scale deploy nginx --replicas=2
#View the deployment
microk8s.kubectl get deployments
#View the pods you are created with deployment
microk8s.kubectl get pods --show labels
#View the replica set you are created with this deployment
microk8s.kubectl get rs
#View the horizontal pod autoscale set you are created with this deployment
microk8s.kubectl get hpa
#Get information about the deployment, can pipe to less for reability
microk8s.kubectl describe deployment nginx
#Open local port 80 to deployment port 80 on nginx deployment
microk8s.kubectl expose deployment nginx --port 80 --target-port 80
#View exposed services
microk8s.kubectl get services
#Delete the service
microk8s.kubectl delete service nginx
#Delete the deployment, will also delete pods
microk8s.kubectl delete deployment nginx
#Delete the horizontal pod autoscale set you are created with this deployment
microk8s.kubectl delete hpa nginx
------------------------------------
Microk8s.kubectl PODS FROM FILE
------------------------------------
#Create a file named ~/mypod.yaml and paste in the following:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
#Run the configuration yaml file
microk8s.kubectl create -f ~/mypod.yaml
------------------------------------
Microk8s.kubectl DEPLOYEMENT FROM FILE
------------------------------------
#Create a file named ~/mydeployment.yaml and paste in the following
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
#Run the configuration yaml file
microk8s.kubectl create -f ~/mydeployment.yaml
------------------------------------
Microk8s.kubectl NAMESPACES
------------------------------------
#Get a list of namespaces
microk8s.kubectl get namespaces
#To get all the pods within the kube-system namespace
microk8s.kubectl --namespace kube-ctl get pods
#To create a new namespace
microk8s.kubectl create <new namespace>
------------------------------------
Microk8s.kubectl ADD/REMOVE NODES
------------------------------------
#Get a list of nodes
microk8s.kubectl get nodes
#To see all details of node
kubectl describe node <nodename> | less
#Two add more nodes, first install microk8s on second server
#On the server you want to be the master run
microk8s add-node
#The add-node command will print out join command, run that command on second node example of command:
microk8s join ip-172-31-20-243:25000/DDOkUupkmaBezNnMheTBqFYHLWINGDbf
#To remove a node, use the IP Address of node to remove
microk8s remove-node <IP ADDRESS>
#On the node that you want to leave the cluster run the command
microk8s leave
------------------------------------
Microk8s.kubectl NODE SELECTOR
------------------------------------
#Get a list of nodes with additional info columns
microk8s.kubectl get nodes -o wide
#Add label to node, will use label to spin up pods on nodes with these labels
microk8s.kubectl label node <node name> <label>=<value>
#To view the labels associated with a node
microk8s.kubectl get node <node name> --show-labels
#To remove a label from a node, use minus sign instead of equals
microk8s.kubectl label node <node name> <label>-
#Run a pod on a specific node with the label/value info
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeSelector
<label>: "<value>"
------------------------------------
Microk8s.kubectl PERSISTENT HOSTPATH VOLUMES
------------------------------------
#ReclaimPolicy options are Retain, Recycle, Delete
#Access Modes are ReadWriteOnce (RWO), ReadWriteMany (RWM), and ReadOnly (RO) - pv and pvc must match
#HostPath creates filepath on the node of the pod that claims the storage
#Hostpath can cause problem if pods run on different hosts
#Create a persistent volume (pv) using Hostpath. This will create /mnt/kube on the node it is running on
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-hostpath
labels:
type: local
spec:
storageClassName: manual
persistentVolumeReclaimPolicy: Retain
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/kube
#To view persistent volumes
microk8s.kubectl get pv
#Create/bind a claim (pvc) for some storage on the persistent volume above, links by storageClassName
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-hostpath
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
#Create a pod that uses the pvc
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
volumes:
- name: host-volume
persistentVolumeClaim:
claimName: pvc-hostpath
containers:
- name: nginx
image: nginx
volumeMounts:
- name: host-volume
mountPath: /path/on/pod
nodeSelector:
<label>: "<value>"
------------------------------------
Microk8s.kubectl PERSISTENT NFS VOLUMES
------------------------------------
#Create nfs share (/etc/exports) on server 192.168.1.2 at /var/nfs/kube
#Create a persistent volume (pv) using NFS to use nfs share
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-hostpath
labels:
type: nfs
spec:
storageClassName: manual
persistentVolumeReclaimPolicy: Retain
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
nfs
server: "192.168.1.2"
path: "/var/nfs/kube"