查看幫助
[root@Master1 ~]# kubectl --help
查看版本
[root@Master1 ~]# kubectl --version Kubernetes v1.5.2
get命令
get命令用於獲取集群的一個或一些resource信息。 使用–help查看詳細信息。 Ps:kubectl的幫助信息、示例相當詳細,而且簡單易懂。建議大家習慣使用幫助信息。kubectl可以列出集群所有resource的詳細。resource包括集群節點、運行的pod,ReplicationController,service等。
查看所有的pods
[root@Master1 ~]# kubectl get pod NAME READY STATUS RESTARTS AGE pod-redis 1/1 Running 1 4h rc-nginx-3-k962j 1/1 Running 0 13m rc-nginx-3-sfw9n 1/1 Running 0 13m rc-nginx-3-v32k0 1/1 Running 0 12m
查看所有的nodes
[root@Master1 ~]# kubectl get nodes NAME STATUS AGE node1 Ready 2d node2 Ready 2d
查看所有的pods(詳細)
[root@Master1 ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE pod-redis 1/1 Running 1 4h 10.0.78.2 node1 rc-nginx-3-k962j 1/1 Running 0 15m 10.0.64.2 node2 rc-nginx-3-sfw9n 1/1 Running 0 15m 10.0.64.4 node2 rc-nginx-3-v32k0 1/1 Running 0 15m 10.0.78.3 node1 [root@Master1 ~]# kubectl get nodes -o wide NAME STATUS AGE EXTERNAL-IP node1 Ready 2d <none> node2 Ready 2d <none>
查看所有的namespace
[root@Master1 ~]# kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default pod-redis 1/1 Running 1 4h default rc-nginx-3-k962j 1/1 Running 0 17m default rc-nginx-3-sfw9n 1/1 Running 0 17m default rc-nginx-3-v32k0 1/1 Running 0 16m kube-system kube-dns-2025727478-x6qns 0/2 ImagePullBackOff 0 33m
以yaml文件形式顯示一個pod詳細信息
[root@Master1 ~]# kubectl get pod pod-redis -o yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: 2018-05-07T01:47:20Z labels: name: redis name: pod-redis namespace: default resourceVersion: "88366" selfLink: /api/v1/namespaces/default/pods/pod-redis uid: 94d4daa5-5198-11e8-8660-000c29a28b16 spec: containers: - image: docker.io/redis:latest imagePullPolicy: Always name: pod-redis ports: - containerPort: 6379 hostPort: 6379 protocol: TCP resources: {} terminationMessagePath: /dev/termination-log dnsPolicy: ClusterFirst nodeName: node1 nodeSelector: zone: node1 restartPolicy: Always securityContext: {} terminationGracePeriodSeconds: 30 status: conditions: - lastProbeTime: null lastTransitionTime: 2018-05-07T01:47:20Z status: "True" type: Initialized - lastProbeTime: null lastTransitionTime: 2018-05-07T01:51:32Z status: "True" type: Ready - lastProbeTime: null lastTransitionTime: 2018-05-07T01:47:20Z status: "True" type: PodScheduled containerStatuses: - containerID: docker://193a93d80f61d80a963d19d85afe370b950b2b4f9321bc5abd7187b85d806104 image: docker.io/redis:latest imageID: docker-pullable://docker.io/redis@sha256:4aed8ea5a5fc4cf05c8d5341b4ae4a4f7c0f9301082a74f6f9a5f321140e0cd3 lastState: terminated: containerID: docker://78869119cf6ce869df4f92814e6c0e22a61885af9e7e2d1ca65a2e814eb80157 exitCode: 1 finishedAt: 2018-05-07T01:51:45Z reason: Error startedAt: 2018-05-07T01:51:45Z name: pod-redis ready: true restartCount: 1 state: running: startedAt: 2018-05-07T01:51:31Z hostIP: 192.168.13.163 phase: Running podIP: 10.0.78.2 startTime: 2018-05-07T01:47:20Z
以jison格式顯示一個pod的詳細信息
[root@Master1 ~]# kubectl get pod pod-redis -o json
以describe顯示一個pod的詳細信息
describe類似於get,同樣用於獲取resource的相關信息。不同的是,get獲得的是更詳細的resource個性的詳細信息,describe獲得的是resource集群相關的信息。describe命令同get類似,但是describe不支持-o選項,對於同一類型resource,describe輸出的信息格式,內容域相同。 注:如果發現是查詢某個resource的信息,使用get命令能夠獲取更加詳盡的信息。但是如果想要查詢某個resource的狀態,如某個pod並不是在running狀態,這時需要獲取更詳盡的狀態信息時,就應該使用describe命令。
[root@Master1 ~]# kubectl describe pod pod-redis
create
使用create則可以基於rc-nginx-3.yaml文件創建出ReplicationController(rc),rc會創建兩個副本:
[root@Master1 ~]# kubectl create -f rc-nginx.yaml
replace
replace命令用於對已有資源進行更新、替換。如前面create中創建的nginx,當我們需要更新resource的一些屬性的時候,如果修改副本數量,增加、修改label,更改image版本,修改端口等。都可以直接修改原yaml文件,然后執行replace命令。 注:名字不能被更更新。另外,如果是更新label,原有標簽的pod將會與更新label后的rc斷開聯系,有新label的rc將會創建指定副本數的新的pod,但是默認並不會刪除原來的pod。所以此時如果使用get po將會發現pod數翻倍,進一步check會發現原來的pod已經不會被新rc控制,此處只介紹命令不詳談此問題,好奇者可自行實驗。
[root@Master1 ~]# kubectl replace -f rc-nginx.yaml
patch
如果一個容器已經在運行,這時需要對一些容器屬性進行修改,又不想刪除容器,或不方便通過replace的方式進行更新。kubernetes還提供了一種在容器運行時,直接對容器進行修改的方式,就是patch命令。 如前面創建pod的label是app=nginx-2,如果在運行過程中,需要把其label改為app=nginx-3,這patch命令如下:
[root@Master1 ~]# kubectl patch pod rc-nginx-3-k962j -p '{"metadata":{"labels":{"app":"nginx-3"}}}' "rc-nginx-3-k962j" patched
edit
edit提供了另一種更新resource源的操作,通過edit能夠靈活的在一個common的resource基礎上,發展出更過的significant resource。例如,使用edit直接更新前面創建的pod的命令為:
[root@Master1 ~]# kubectl edit pod rc-nginx-3-k962j
上面命令的效果等效於:
[root@Master1 ~]# kubectl get pod rc-nginx-3-k962j -o yaml >> /tmp/nginx-tmp.yaml [root@Master1 ~]# vim /tmp/nginx-tmp.yaml
Delete
根據resource名或label刪除resource。
[root@Master1 ~]# kubectl delete -f rc-nginx.yaml replicationcontroller "rc-nginx-3" deleted [root@Master1 ~]# kubectl delete pod -lapp=nginx-2
apply
apply命令提供了比patch,edit等更嚴格的更新resource的方式。通過apply,用戶可以將resource的configuration使用source control的方式維護在版本庫中。每次有更新時,將配置文件push到server,然后使用kubectl apply將更新應用到resource。kubernetes會在引用更新前將當前配置文件中的配置同已經應用的配置做比較,並只更新更改的部分,而不會主動更改任何用戶未指定的部分。 apply命令的使用方式同replace相同,不同的是,apply不會刪除原有resource,然后創建新的。apply直接在原有resource的基礎上進行更新。同時kubectl apply還會resource中添加一條注釋,標記當前的apply。類似於git操作。
logs
logs命令用於顯示pod運行中,容器內程序輸出到標准輸出的內容。跟docker的logs命令類似。如果要獲得tail -f 的方式,也可以使用-f選項。
[root@Master1 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-redis 1/1 Running 1 5h
[root@Master1 ~]# kubectl logs pod-redis 1:C 07 May 01:51:31.414 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 07 May 01:51:31.415 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 07 May 01:51:31.415 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 07 May 01:51:31.416 * Running mode=standalone, port=6379. 1:M 07 May 01:51:31.416 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:M 07 May 01:51:31.416 # Server initialized 1:M 07 May 01:51:31.416 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 1:M 07 May 01:51:31.416 * Ready to accept connections
rolling-update
rolling-update是一個非常重要的命令,對於已經部署並且正在運行的業務,rolling-update提供了不中斷業務的更新方式。rolling-update每次起一個新的pod,等新pod完全起來后刪除一個舊的pod,然后再起一個新的pod替換舊的pod,直到替換掉所有的pod。 rolling-update需要確保新的版本有不同的name,Version和label,否則會報錯 。
[root@Master1 ~]# kubectl rolling-update rc-nginx-2 -f rc-nginx.yaml
如果在升級過程中,發現有問題還可以中途停止update,並回滾到前面版本
[root@Master1 ~]# kubectl rolling-update rc-nginx-2 -rollback
rolling-update還有很多其他選項提供豐富的功能,如—update-period指定間隔周期,使用時可以使用-h查看help信息。
scale
scale用於程序在負載加重或縮小時副本進行擴容或縮小,如前面創建的nginx有兩個副本,可以輕松的使用scale命令對副本數進行擴展或縮小。 擴展副本數到4:
[root@Master1 ~]# kubectl scale rc rc-nginx-3 --replicas=4 replicationcontroller "rc-nginx-3" scaled
重新縮減副本數到2:
[root@Master1 ~]# kubectl scale rc rc-nginx-3 --replicas=2 replicationcontroller "rc-nginx-3" scaled
autoscale
scale雖然能夠很方便的對副本數進行擴展或縮小,但是仍然需要人工介入,不能實時自動的根據系統負載對副本數進行擴、縮。autoscale命令提供了自動根據pod負載對其副本進行擴縮的功能。 autoscale命令會給一個rc指定一個副本數的范圍,在實際運行中根據pod中運行的程序的負載自動在指定的范圍內對pod進行擴容或縮容。如前面創建的nginx,可以用如下命令指定副本范圍在1~4
[root@Master1 ~]# kubectl autoscale rc rc-nginx-3 --min=1 --max=4
attach
類似於docker attach的功能,用於取得實時的類似於kubectl logs的信息
[root@Master1 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-redis 1/1 Running 1 5h rc-nginx-3-6bjmk 1/1 Running 0 20m rc-nginx-3-d9ntz 1/1 Running 0 20m
[root@Master1 ~]# kubectl attach pod-redis If you don't see a command prompt, try pressing enter. 1:C 07 May 01:51:31.414 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 07 May 01:51:31.415 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 07 May 01:51:31.415 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 07 May 01:51:31.416 * Running mode=standalone, port=6379. 1:M 07 May 01:51:31.416 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:M 07 May 01:51:31.416 # Server initialized 1:M 07 May 01:51:31.416 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 1:M 07 May 01:51:31.416 * Ready to accept connection
port-forward
轉發一個本地端口到容器端口,博主一般都是使用yaml的方式編排容器,所以基本不使用此命令。
proxy
只嘗試過使用nginx作為kubernetes多master HA方式的代理,沒有使用過此命令為kubernetes api server運行過proxy。
run
類似於docker的run命令,直接運行一個image。
label
為kubernetes集群的resource打標簽,如前面實例中提到的為rc打標簽對rc分組。還可以對nodes打標簽,這樣在編排容器時,可以為容器指定nodeSelector將容器調度到指定lable的機器上,如如果集群中有IO密集型,計算密集型的機器分組,可以將不同的機器打上不同標簽,然后將不同特征的容器調度到不同分組上。 在1.2之前的版本中,使用kubectl get nodes則可以列出所有節點的信息,包括節點標簽,1.2版本中不再列出節點的標簽信息,如果需要查看節點被打了哪些標簽,需要使用describe查看節點的信息。
cp
kubectl cp 用於pod和外部的文件交換,比如如下示例了如何在進行內外文件交換。 在pod中創建一個文件message.log
[root@Master1 ~]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-redis 1/1 Running 1 5h rc-nginx-3-6bjmk 1/1 Running 0 32m rc-nginx-3-d9ntz 1/1 Running 0 32m [root@Master1 ~]# kubectl exec -it rc-nginx-3-6bjmk sh # pwd / # cd /tmp # echo "this is a message from `hostname`" >message.log # cat message.log this is a message from rc-nginx-3-6bjmk 拷貝出來並確認 [root@Master1 ~]# kubectl cp rc-nginx-3-6bjmk:/tmp/message.log message.log tar: Removing leading `/' from member names [root@Master1 ~]# cat message.log this is a message from rc-nginx-3-6bjmk
更改message.log並拷貝回pod
[root@Master1 ~]# echo "information added in `hostname`" >>message.log [root@Master1 ~]# cat message.log this is a message from rc-nginx-3-6bjmk information added in Master1 [root@Master1 ~]# kubectl cp message.log rc-nginx-3-6bjmk:/tmp/message.log
確認更改后的信息
[root@Master1 ~]# kubectl exec rc-nginx-3-6bjmk cat /tmp/message.log this is a message from rc-nginx-3-6bjmk information added in Master1
kubectl cluster-info
使用cluster-info和cluster-info dump也能取出一些信息,尤其是你需要看整體的全部信息的時候一條命令一條命令的執行不如kubectl cluster-info dump來的快一些
[root@Master1 ~]# kubectl cluster-info Kubernetes master is running at http://localhost:8080 KubeDNS is running at http://localhost:8080/api/v1/proxy/namespaces/kube-system/services/kube-dns To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.