运维开发网

kubectl常用命令总结

运维开发网 https://www.qedev.com 2021-03-02 09:31 出处:51CTO 作者:小李子博客
get信息获取所有名称空间信息#kubectlgetnamespaceNAMESTATUSAGEdefaultActive2d2hkube-node-leaseActive2d2hkube-publicActive2d2hkube-systemActive2d2h1、获取node信息#kubectlgetnodeNAMESTATUSROLESAGEVERSIONk8s-masterReadymas

get信息

获取所有名称空间信息

# kubectl get namespace 
NAME              STATUS   AGE
default           Active   2d2h
kube-node-lease   Active   2d2h
kube-public       Active   2d2h
kube-system       Active   2d2h

1、获取node信息

# kubectl get node
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   42h   v1.15.10
k8s-node01   Ready    <none>   42h   v1.15.10
k8s-node02   Ready    <none>   42h   v1.15.10

2、获取pod信息

# 默认获取的是default名称空间的pod信息,-n可以指定名称空间

# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
myapp                           1/1     Running   0          133m
myapp-deploy-856957f86c-7dz49   1/1     Running   0          119s
myapp-deploy-856957f86c-8lp96   1/1     Running   0          125m
myapp-deploy-856957f86c-dr5hg   1/1     Running   0          119s
myapp-deploy-856957f86c-n695d   1/1     Running   0          119s
myapp-deploy-856957f86c-sv86x   1/1     Running   0          125m
nginx                           1/1     Running   0          3h15m
zhongduan                       1/1     Running   0          156m

# kubectl get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-94c4b98b-8lsjx               1/1     Running   0          2d1h
coredns-94c4b98b-bgrsk               1/1     Running   0          2d1h
etcd-k8s-master                      1/1     Running   0          2d2h
kube-apiserver-k8s-master            1/1     Running   0          2d2h
kube-controller-manager-k8s-master   1/1     Running   0          2d2h
kube-flannel-ds-amd64-66m4b          1/1     Running   0          2d1h
kube-flannel-ds-amd64-gbbnm          1/1     Running   0          2d1h
kube-flannel-ds-amd64-j2gc5          1/1     Running   0          2d1h
kube-proxy-98ttd                     1/1     Running   0          2d2h
kube-proxy-qwnxv                     1/1     Running   0          2d2h
kube-proxy-t5zbn                     1/1     Running   0          2d2h
kube-scheduler-k8s-master            1/1     Running   0          2d2h

获取service信息

# kubectl get  service 
NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes     ClusterIP   10.96.0.1      <none>        443/TCP   2d2h
myapp-deploy   ClusterIP   10.97.206.58   <none>        80/TCP    129m
nginx          ClusterIP   10.105.36.97   <none>        80/TCP    3h9m

# kubectl get  service  -o wide 
NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE    SELECTOR
kubernetes     ClusterIP   10.96.0.1      <none>        443/TCP   2d2h   <none>
myapp-deploy   ClusterIP   10.97.206.58   <none>        80/TCP    129m   app=myapp-deploy
nginx          ClusterIP   10.105.36.97   <none>        80/TCP    3h9m   run=nginx

获取控制器信息

# kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   5/5     5            5           131m
# kubectl get deployment -o wide
NAME           READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES                 SELECTOR
myapp-deploy   5/5     5            5           131m   myapp        ikubernetes/myapp:v1   app=myapp-deploy

scale 动态扩容、宿容

# kubectl get deployment
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   5/5     5            5           135m
# kubectl get deployment myapp-deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   5/5     5            5           135m
# kubectl scale --replicas=3 deployment myapp-deploy
deployment.extensions/myapp-deploy scaled
# kubectl get deployment myapp-deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   3/3     3            3           136m

labels 容器标签操作命令

查看容器标签

标签选择器:
        等值关系:=、==、!=
        集合关系:
            KEY in (VALUE1、VALUE2、...) #包含标签值得pod
            KEY notin (VALUE1、VALUE2、...) #不包含标签值得pod
#查看run标签对应值的pod,这里会把所有的pod都显示,
# kubectl get pods -L run
NAME                            READY   STATUS    RESTARTS   AGE   "RUN"
myapp                           1/1     Running   0          15d   "myapp"
myapp-deploy-856957f86c-8lp96   1/1     Running   0          15d   
myapp-deploy-856957f86c-dr5hg   1/1     Running   0          15d   
myapp-deploy-856957f86c-sv86x   1/1     Running   0          15d   
myapp-dome                      1/1     Running   0          14d   
nginx                           1/1     Running   0          15d   "nginx"
zhongduan                       1/1     Running   0          15d   "zhongduan"

只显示标签为run的的pod

# kubectl get pods -l run --show-labels 
NAME        READY   STATUS    RESTARTS   AGE   LABELS
myapp       1/1     Running   0          15d   "run=myapp"
nginx       1/1     Running   0          15d   "run=nginx"
zhongduan   1/1     Running   0          15d   "run=zhongduan"

in和notin

# kubectl get pods -l "run in (myapp,nginx)" --show-labels
NAME    READY   STATUS    RESTARTS   AGE   LABELS
myapp   1/1     Running   0          15d   run=myapp
nginx   1/1     Running   0          15d   run=nginx

# kubectl get pods -l "run notin (myapp,nginx)" --show-labels
NAME                            READY   STATUS    RESTARTS   AGE   LABELS
myapp-deploy-856957f86c-8lp96   1/1     Running   0          15d   app=myapp-deploy,pod-template-hash=856957f86c
myapp-deploy-856957f86c-dr5hg   1/1     Running   0          15d   app=myapp-deploy,pod-template-hash=856957f86c
myapp-deploy-856957f86c-sv86x   1/1     Running   0          15d   app=myapp-deploy,pod-template-hash=856957f86c
myapp-dome                      1/1     Running   0          14d   apps=myapp,tier=qianduan
zhongduan                       1/1     Running   0          15d   run=zhongduan

给pod添加新的标签

# kubectl label pod <podname> <labelname=VAULE>

例如:
    # kubectl label pod nginx relese=1.15.0

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号