k8s Common commands


explain : stay master Operation above

0 Get into pod

kubectl exec -it settlement-5486f6cf57-9jkx2 /bin/bash

1 Delete

kubectl delete deploy/nginx delete mirror


kubectl delete svc/nginx Delete pod


2 establish

kubectl run nginx --replicas=3 --labels="app=example" --image=nginx:1.10 --port=80

kubectl get pods


3 see

kubectl get ns View namespace

kubectl get all

kubectl get deploy

kubectl get pods -l app=example

kubectl get pods -o wide # see pod On that server

kubectl get pods --show-labels ## View tab



nginx-66d64dcdf8-7cmcb 1/1 Running 0 58m app=example,pod-template-hash=2282087894

nginx-66d64dcdf8-d6cmj 1/1 Running 0 58m app=example,pod-template-hash=2282087894

nginx-66d64dcdf8-tbt2c 1/1 Running 0 58m app=example,pod-template-hash=2282087894


kubectl get pods -l app=example ## Look at the information on this tag


nginx-66d64dcdf8-7cmcb 1/1 Running 0 1h

nginx-66d64dcdf8-d6cmj 1/1 Running 0 1h

nginx-66d64dcdf8-tbt2c 1/1 Running 0 1h

kubectl label pod xxx app=label111 --overwirte=True Change the label


4 Release

kubectl expose deployment nginx --port=88 --type=NodePort --target-port=80 --name=nginx-service

Check the port

kubectl get svc


kubernetes ClusterIP <none> 443/TCP 2d

nginx-service NodePort <none> 88:44935/TCP 55m

5 Troubleshooting

kubectl describe po/nginx-66d64dcdf8-7cmcb Look at specific pod details


kubectl logs nginx-66d64dcdf8-tbt2c see pod Log

kubectl logs nginx-66d64dcdf8-tbt2c -c test # Check the log ,test It's the name of the container , If one pod There are multiple containers in it


kubectl exec -it nginx-66d64dcdf8-tbt2c bash Enter into pod Inside


6 to update , edit

kubectl set image deployment/nginx nginx=nginx:1.11 to update nginx edition


kubectl edit deploy/nginx See if the version is up to date ( You can also update the version ) After editing , Save it directly


kubectl get all First use this to find out the name , The following command can be used

kubectl describe po/nginx-56ff7b8c6d-frphs See specific details , Including using version


kubectl rollout history deploy/nginx View historical version


1 <none>

2 <none>

3 <none>

4 kubectl set image deployment/nginx nginx=nginx:1.13 --record=true


7 Roll back

kubectl rollout undo deployment/nginx Roll back to the previous image


8 Expand or delete


kubectl scale deployment nginx --replicas=5    Add to 5 individual pod
kubectl scale deployment myapp --replicas=0  # If it is 0 One is deletion 


If you want to get smaller, just change the number

9 The whole process demonstration

9.1 Run two pod

run Followed by the name of the controller

kubectl run myapp1 --replicas=2 --image=wangyanglinux/myapp:v1 --port=80

9.2 The default open port is clusterip

kubectl expose deployment myapp1 --port=30000 --target-port=80


9.3 Check out the network mode


kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE
kubernetes   ClusterIP       <none>        443/TCP           3h3m
myapp1       NodePort   <none>        30000:32649/TCP   14m


9.4 Check out the controller

kubectl get deployment


myapp    0/0     0            0           34m
myapp1   2/2     2            2           16m


9.5 Browser access (nodeip)

Be careful , If it is accessed in the browser, it must be nodeip Type of , If it's in node The above visit can be clusterip, But in master Node cannot access