kubernetes常用命令

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章