[root@master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 4d13h v1.19.3 node01 Ready <none> 4d13h v1.19.3 node02 Ready <none> 4d13h v1.19.3 node03 Ready <none> 4d13h v1.19.3
查看名称空间
1 2 3 4 5 6 7 8
[root@master ~]# kubectl get ns NAME STATUS AGE default Active 4d13h kube-flannel Active 4d12h kube-node-lease Active 4d13h kube-public Active 4d13h kube-system Active 4d13h luay Active 4h17m
查看pod (默认default名称空间)
1 2 3 4
[root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-565785f75c-tpjmj 1/1 Running 0 6h8m nginx-pod 1/1 Running 0 5h43m
查看pod (指定名称空间)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[root@master ~]# kubectl get pods -n luay NAME READY STATUS RESTARTS AGE busybox-pod 2/2 Running 0 3m30s
[root@master ~]# kubectl get pod -n luay -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES busybox-pod 2/2 Running 0 4m14s 10.2.2.6 node03 <none> <none>
[root@master231:1 pods]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS xiuxian 1/1 Running 0 20m app=xiuxian,level=jindan xiuxian-v1 1/1 Running 0 101s app=xiuxian
基于标签过滤资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@master231:1 pods]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS xiuxian 1/1 Running 0 25m app=xiuxian,level=jindan xiuxian-v1 1/1 Running 0 6m41s app=xiuxian,name=wanglin
#过滤key里含有name的标签 [root@master231:1 pods]# kubectl get pods --show-labels -l name NAME READY STATUS RESTARTS AGE LABELS xiuxian-v1 1/1 Running 0 6m21s app=xiuxian,name=wanglin
#过滤level=jindan的标签 [root@master231:1 pods]# kubectl get pods --show-labels -l level=jindan NAME READY STATUS RESTARTS AGE LABELS xiuxian 1/1 Running 0 26m app=xiuxian,level=jindan
#过滤level不等于jindan的标签 [root@master231:1 pods]# kubectl get pods --show-labels -l level!=jindan NAME READY STATUS RESTARTS AGE LABELS xiuxian-v1 1/1 Running 0 8m54s app=xiuxian,name=wanglin
7.基于标签删除资源
1 2 3 4 5 6 7 8 9 10 11
[root@master231:1 pods]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS xiuxian 1/1 Running 0 30m app=xiuxian,level=jindan xiuxian-v1 1/1 Running 0 11m app=xiuxian,name=wanglin
[root@master231:1 pods]# kubectl delete pods -l name pod "xiuxian-v1" deleted
[root@master231:1 pods]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS xiuxian 1/1 Running 0 31m app=xiuxian,level=jindan
#资源清单修改后要应用一下 [root@master231:1 pods]# kubectl apply -f single-pods.yaml pod/xiuxian created
2.查看资源清单
1 2 3
[root@master231:1 pods]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS xiuxian 1/1 Running 0 76s level=jindan,name=wanglin
修改标签和删除标签直接在资源清单里修改即可,修改后应用一下
响应式和声明式的区别
1.相同点
都可以管理资源
2.不同点
响应式修改后立即生效,但是资源被重新创建后又得重新执行相关命令;
声明式修改需要先修改资源清单,在使用apply命令更新应用配置;
连接容器
1.连接指定pod执行命令
1 2 3 4 5
kubectl exec -it 资源名 command [root@master231:0 kubernetes]# kubectl exec -it mysql80 -- mysql ··· Type 'help;' or '\h'forhelp. Type '\c' to clear the current input statement. mysql>
2.连接同一个Pod的不同容器执行命令
1 2 3 4 5 6
[root@master231 pods]# kubectl exec -it multiple-wordpress -c db -- ls / -l