-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote
71 lines (66 loc) · 4.34 KB
/
note
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
1. create pod yaml file from kubectl
$ kubectl run reids --image=redis --dry-run -o yaml > pod.yaml
2 .Edit Pods
In any of the practical quizzes if you are asked to edit an existing POD, please note the following:
2.1 If you are given a pod definition file, edit that file and use it to create a new pod.
2.2 If you are not given a pod definition file, you may extract the definition to a file using the below command:
$ kubectl get pod <pod-name> -o yaml > pod-definition.yaml
Then edit the file to make the necessary changes, delete and re-create the pod.
2.3 Use the kubectl edit pod <pod-name> command to edit pod properties.
3. create deployment by command
$ kubectl create deployment httpd-frontend --image=httpd:2.4-alpine --replicas=3 --dry-run -o yaml > my-depoy.yaml
4. scale deployment,replicatset with cmd
$ kubectl scale --relplicas=3 deployment(replicatset) [deployment(deployment) name]
5. imperative commands
5.1 POD
5.1.1 Create an NGINX Pod
$ kubectl run nginx --image=nginx
5.1.2 Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
$ kubectl run nginx --image=nginx --dry-run=client -o yaml
5.2 Deployment
5.2.1 Create a deployment
$ kubectl create deployment --image=nginx nginx
5.2.2 Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)
$ kubectl create deployment --image=nginx nginx --dry-run -o yaml
5.2.3 Generate Deployment with 4 Replicas
$ kubectl create deployment nginx --image=nginx --replicas=4
5.2.4 scale a deployment using the kubectl scale command
$ kubectl scale deployment nginx --replicas=4
5.2.5 Another way to do this is to save the YAML definition to a file and modify
$ kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml
5.3 Service
5.3.1 Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379
$ kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
(This will automatically use the pod's labels as selectors)
or
$ kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
(This will not use the pods labels as selectors, instead it will assume selectors as app=redis.
You cannot pass in selectors as an option. So it does not work very well if your pod has a different label set.
So generate the file and modify the selectors before creating the service))
5.3.2 Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes
$ kubectl expose pod nginx --port=80 --name nginx-service --type=NodePort --dry-run=client -o yaml
(This will automatically use the pod's labels as selectors, but you cannot specify the node port.
You have to generate a definition file and then add the node port in manually before creating the service
with the pod.)
or
$ kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
(This will not use the pods labels as selectors)
PS: Both the above commands have their own challenges. While one of it cannot accept a selector
the other cannot accept a node port. I would recommend going with the `kubectl expose` command.
If you need to specify a node port, generate a definition file using the same command and
manually input the nodeport before creating the service.
6. Namespace
6.1 Refer to service in other Namespace
<service>.<namespace>.svc.cluster.local
e.q. mysql.connect("db-service.dev.svc.cluster.local")
6.2 get/create resource in assign namespace
add --namespace=<namespace>
e.q. kubectl get pods --namespace=kube-system
e.q. kubectl create -f pod-definition.yml --namespce=dev
(also can add namespace: dev in the metadata section in pod-definition.yml)
6.3 switch from default namespace to another
$ kubectl config set-context $(kubectl config current-context) --namespace=dev
6.4 get resource in all namespaces
$ kubectl get pods --all-namespaces
6.5 create namespace
$ kubectl create ns dev-ns