kubernetes常用命令

本文详细介绍Kubernetes中常用命令的操作方法,包括查询节点、组件状态、名命空间等,还介绍了如何进行更新维护、问题定位等操作,并提供了具体的命令示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

kubectl get

1.查询节点,nodes or node or no

$ kubectl get no
NAME      STATUS    ROLES     AGE       VERSION
master1   Ready       master     16h         v1.9.2
master2   Ready       master     16h         v1.9.2
master3   Ready       master     16h         v1.9.2

2.查询组件状态,componentstatuses or cs

$ kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-2               Healthy   {"health":"true"}   
etcd-1               Healthy   {"health":"true"}   
etcd-0               Healthy   {"health":"true"}

3.查询名命空间,namespaces or ns

$ kubectl get ns
NAME          STATUS    AGE
default       Active    18h
kube-public   Active    18h
kube-system   Active    18h

下面所有查询,缺省defalut名命空间,-n 指定名命空间,指定全部名命空间 --all-namespaces
4.查询pod,pods or pod or po

$ kubectl get pod -n kube-system
NAME                              READY     STATUS    RESTARTS   AGE
kube-apiserver-master1            1/1       Running   0          18h
kube-apiserver-master2            1/1       Running   0          17h
kube-apiserver-master3            1/1       Running   0          17h

加参数 -o wide 可显示运行节点

$ kubectl get pod -n kube-system -o wide
NAME                              READY     STATUS    RESTARTS   AGE       IP              NODE
kube-apiserver-master1            1/1       Running   0          18h       192.168.6.101   master1
kube-apiserver-master2            1/1       Running   0          17h       192.168.6.102   master2
kube-apiserver-master3            1/1       Running   0          17h       192.168.6.103   master3

5.查询service,services or service or svc

$ kubectl  get service --all-namespaces
NAMESPACE     NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
default       kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP         18h
kube-system   kube-dns     ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP   18h

6.kind分类查询,replicationcontrollers or rc , deployments or deploy, daemonsets or ds

$ kubectl  get deployments --all-namespaces 
NAMESPACE     NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kube-system   kube-dns   1         1         1            1           18h

7.查询所有 all

$ kubectl  get all --all-namespaces

kubectl delete

1.删除节点

$ kubectl delete node node1

2.删除pod

$ kubectl delete pod $(pod_name) -n $(namespace-name)

3.删除services

$ kubectl delete svc $(svc_name) -n $(namespace-name)

更新维护

1.手动动态伸宿副本, scale

$ kubectl get Deployment -n test-app
NAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-world   3               3                  3                       3                   13h

$ kubectl scale deployments hello-world --replicas=2 -n test-app
deployment "hello-world" scaled

$ kubectl get Deployment -n test-app                            
NAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-world   2                2                  2                      2                   14h

2.根据pod负载自动伸宿副本,autoscale

$ kubectl autoscale deployments hello-world --min=2 --max=5 -n test-app

3.更新回滚RC,rolling-update
rolling-update确保新版本有不同的metadate.name,spec.selector.version和spec.template.metadate.labels,否则会报错

$ ls |grep hello-world*
hello-world-v0.1.yaml     hello-world-v0.2.yaml

创建pod

$ kubectl create -f  hello-world-v0.1.yaml 

更新pod

$ kubectl rolling-update hello-world -f hello-world-v0.2.yaml

回滚

$ kubectl rolling-update hello-world --rollback

问题定位

1.查询对象详细信息

$ kubectl describe pod kube-proxy-ftkx5 -n kube-system

2.查询日志

$ kubectl logs -f  kube-proxy-ftkx5 -n kube-system

3.进入容器运行命令,如果pod有多个容器,可用-c指定容器

$ kubectl exec kube-dns-6f4fd4bdf-b7txf  -c dnsmasq hostname -n kube-system
kube-dns-6f4fd4bdf-b7txf

转载于:https://blog.51cto.com/2759492/2083300

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值