[root@k8s-master~]# kubectl get namespace NAME STATUS AGE default Active 24h dev Active 45m kube-node-lease Active 24h kube-public Active 24h kube-system Active 24h
[root@k8s-master~]# kubectl get namespace NAME STATUS AGE default Active 24h dev Active 60m kube-node-lease Active 24h kube-public Active 24h kube-system Active 24h
缩写
1 2 3 4 5 6 7
[root@k8s-master~]# kubectl get ns NAME STATUS AGE default Active 24h dev Active 60m kube-node-lease Active 24h kube-public Active 24h kube-system Active 24h
查看指定某个命名空间
1 2 3
[root@k8s-master ~]# kubectl get ns default NAME STATUS AGE default Active 24h
[root@k8s-master ~]# kubectl run nginx --image=nginx:1.17.1 --port=80 --namespace=dev pod/nginx created
示例:查询名称为dev的namespace下的所有Pod的基本信息
1 2 3
[root@k8s-master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE nginx 1/1 Running 02m
语法:查看Pod的详细信息
1 2 3
[root@k8s-master~]# kubectl get pod -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODENOMINATEDNODEREADINESS GATES nginx 1/1 Running 03m19s 10.244.1.7 k8s-node1 <none><none>
[root@k8s-master~]# kubectl get pods -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODENOMINATEDNODEREADINESS GATES nginx 1/1 Running 017m10.244.1.7 k8s-node1 <none><none>
[root@k8s-master ~]# curl 10.244.1.7:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <ahref="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <ahref="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
语法:删除指定的Pod
1
kubectl delete pod pod的名称 [-n 命名空间]
示例:删除Nginx的Pod
1 2
[root@k8s-master ~]# kubectl delete pod nginx -n dev pod "nginx" deleted
[root@k8s-master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1Running038s <none>
示例:为Nginx的Pod打上标签
1 2 3 4 5 6
[root@k8s-master ~]# kubectl label pod nginx version=1.0 -n dev pod/nginx labeled
[root@k8s-master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 02m24s version=1.0
语法:更新资源的标签
1
kubectl label pod xxx key=value [-n 命名空间]--overwrite
示例:为Nginx的Pod更新标签
1 2 3 4 5 6
[root@k8s-master ~]# kubectl label pod nginx version=2.0 -n dev --overwrite pod/nginx labeled
[root@k8s-master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 06m12s version=2.0
语法:查看标签
1
kubectl get pod xxx [-n 命名空间] --show-labels
示例:显示Nginx的Pod的标签
1
kubectl get pod nginx -n dev --show-labels
示例:筛选版本号是2.0的在名称为dev的namespace下的Pod
1 2 3 4 5 6 7 8 9 10 11 12
[root@k8s-master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 10m version=2.0 nginx1 1/1 Running 0 89s version=1.0
[root@k8s-master ~]# kubectl get pod -l "version=2.0" -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 0 10m version=2.0
[root@k8s-master ~]# kubectl get pod -l "version!=2.0" -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx1 1/1 Running 0 2m version=1.0
语法:删除标签
1
kubectl label pod xxx key- [-n 命名空间]
示例:删除名称为dev的namespace下的Nginx的Pod上的标签
1 2 3 4 5 6 7
[root@k8s-master~]# kubectl label pod nginx version- -n dev pod/nginx labeled
[root@k8s-master~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 013m<none> nginx1 1/1 Running 04m50s version=1.0
[root@k8s-master ~]# kubectl apply -f pod-nginx.yaml Warning: kubectl apply should be used on resource created by either kubectl create--save-config or kubectl apply pod/nginx configured
[root@k8s-master ~]# kubectl get pod -n dev --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx 1/1 Running 019m env=test,version=3.0 nginx1 1/1 Running 010m version=1.0
特别注意:在v1.18版之后,kubectl run nginx –image=nginx –replicas=2 –port=80,会反馈Flag –replicas has been deprecated, has no effect and will be removed in the future,并且只会创建一个Nginx容器实例。
语法:创建指定名称的deployement
1 2 3
kubectl create deployment xxx [-n 命名空间]
kubectl create deploy xxx [-n 命名空间]
示例:在名称为test的命名空间下创建名为nginx的deployment
1
kubectl create deployment nginx --image=nginx:1.17.1 -n test