本文收錄在容器技術學習系列文章總目錄html
kubectl用於運行Kubernetes集羣命令的管理工具。node
kubectl [command] [TYPE] [NAME] [flags]
Operationlinux |
Descriptionnginx |
Syntaxjson |
annotateapi |
爲一個或多個資源添加註釋bash |
kubectl annotate (-f FILENAME | TYPE NAME | TYPE/NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--overwrite] [--all] [--resource-version=version] [flags]服務器 |
api-versionsapp |
列出支持的API版本。curl |
kubectl api-versions [flags] |
apply |
對文件或stdin的資源進行配置更改。 |
kubectl apply -f FILENAME [flags] |
attach |
鏈接到一個運行的容器,既能夠查看output stream,也能夠與容器(stdin)進行交互。 |
kubectl attach POD -c CONTAINER [-i] [-t] [flags] |
autoscale |
自動擴容/縮容由replication controller管理的一組pod。 |
kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU] [flags] |
cluster-info |
顯示有關集羣中master和services的終端信息。 |
kubectl cluster-info [flags] |
config |
修改kubeconfig文件。有關詳細信息,請參閱各個子命令。 |
kubectl config SUBCOMMAND [flags] |
create |
從file或stdin建立一個或多個資源。 |
kubectl create -f FILENAME [flags] |
delete |
從file,stdin或指定label 選擇器,names,resource選擇器或resources中刪除resources。 |
kubectl delete (-f FILENAME | TYPE [NAME | /NAME | -l label | --all]) [flags] |
describe |
顯示一個或多個resources的詳細狀態。 |
kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | /NAME | -l label]) [flags] |
edit |
使用默認編輯器編輯和更新服務器上一個或多個定義的資源。 |
kubectl edit (-f FILENAME | TYPE NAME | TYPE/NAME) [flags] |
exec |
對pod中的容器執行命令。 |
kubectl exec POD [-c CONTAINER] [-i] [-t] [flags] [-- COMMAND [args...]] |
explain |
獲取各類資源的文檔。例如pod,node,services等 |
kubectl explain [--include-extended-apis=true] [--recursive=false] [flags] |
expose |
將 replication controller,service或pod做爲一個新的Kubernetes service顯示。 |
kubectl expose (-f FILENAME | TYPE NAME | TYPE/NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [----external-ip=external-ip-of-service] [--type=type] [flags] |
get |
列出一個或多個資源。 |
kubectl get (-f FILENAME | TYPE [NAME | /NAME | -l label]) [--watch] [--sort-by=FIELD] [[-o | --output]=OUTPUT_FORMAT] [flags] |
label |
添加或更新一個或多個資源的flags。 |
kubectl label (-f FILENAME | TYPE NAME | TYPE/NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--overwrite] [--all] [--resource-version=version] [flags] |
logs |
在pod中打印容器的日誌。 |
kubectl logs POD [-c CONTAINER] [--follow] [flags] |
patch |
使用strategic merge 補丁程序更新資源的一個或多個字段。 |
kubectl patch (-f FILENAME | TYPE NAME | TYPE/NAME) --patch PATCH [flags] |
port-forward |
將一個或多個本地端口轉發到pod。 |
kubectl port-forward POD [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N] [flags] |
proxy |
在Kubernetes API服務器運行代理。 |
kubectl proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix] [flags] |
replace |
從file或stdin替換資源。 |
kubectl replace -f FILENAME |
rolling-update |
經過逐步替換指定的replication controller及其pod來執行滾動更新。 |
kubectl rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] --image=NEW_CONTAINER_IMAGE | -f NEW_CONTROLLER_SPEC) [flags] |
run |
在集羣上運行指定的鏡像。 |
kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [flags] |
scale |
更新指定replication controller的大小。 |
kubectl scale (-f FILENAME | TYPE NAME | TYPE/NAME) --replicas=COUNT [--resource-version=version] [--current-replicas=count] [flags] |
stop |
已棄用 |
kubectl stop |
version |
顯示客戶端和服務器上運行的Kubernetes版本。 |
kubectl version [--client] [flags] |
(1)簡單的查詢操做
查詢節點詳細信息
[root@master ~]# kubectl describe node node1 Name: node1 Roles: <none> Labels: beta.kubernetes.io/arch=amd64 ... ...
查看版本
[root@master ~]# kubectl version Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.1", GitCommit:"bb9ffb1654d4a729bb4cec18ff088eacc153c239", GitTreeState:"clean", BuildDate:"2018-08-07T23:17:28Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.1", GitCommit:"b1b29978270dc22fecc592ac55d903350454310a", GitTreeState:"clean", BuildDate:"2018-07-17T18:43:26Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}
查看集羣信息
[root@master ~]# kubectl cluster-info Kubernetes master is running at https://192.168.10.103:6443 KubeDNS is running at https://192.168.10.103:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
建立並運行一個或多個容器鏡像。
$ kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]
(1)運行一個nginx的pod
[root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1 deployment.apps/nginx-deploy created
(2)在master上查詢驗證
[root@master ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx-deploy 1 1 1 1 44s [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deploy-5b595999-6kw54 1/1 Running 0 51s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-6kw54 1/1 Running 0 1m 10.244.2.2 node2
(3)在部署的node節點上查詢
[root@node2 ~]# ifconfig |head -2 cni0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450 inet 10.244.2.1 netmask 255.255.255.0 broadcast 0.0.0.0
(4)訪問pod內的nginx服務
[root@node1 ~]# curl 10.244.2.2 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title>
$ kubectl delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)])
刪除剛纔的pod,馬上會生成一個新的pod,由於此pod是控制器管理的
[root@master ~]# kubectl delete pods nginx-deploy-5b595999-6kw54 pod "nginx-deploy-5b595999-6kw54" deleted [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deploy-5b595999-jf5gm 0/1 ContainerCreating 0 16s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-jf5gm 1/1 Running 0 47s 10.244.1.2 node1
$ kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]
(1)將nginx-deploy控制器中相關的Pod資源建立一個名爲nginx的服務
[root@master ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP service/nginx exposed [root@master ~]# kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h nginx ClusterIP 10.96.212.79 <none> 80/TCP 9s [root@master ~]# kubectl get svc #簡寫 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h nginx ClusterIP 10.96.212.79 <none> 80/TCP 13s
(2)驗證,在集羣內部節點訪問服務
[root@master ~]# curl 10.96.212.79 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title>
(3)刪除pod,當即生產新的pod,可是service仍是不變,繼續提供服務,只是指定到新的pod
---刪除pod [root@master ~]# kubectl delete pods nginx-deploy-5b595999-jf5gm ---當即生產新的pod,service繼續提供服務 [root@master ~]# curl 10.96.212.79 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> ---指定到新的pod [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-deploy-5b595999-2z769 1/1 Running 0 29m 10.244.1.2 node2 [root@master ~]# kubectl describe svc nginx Name: nginx Namespace: default Labels: run=nginx-deploy Annotations: <none> Selector: run=nginx-deploy Type: ClusterIP IP: 10.96.212.79 Port: <unset> 80/TCP TargetPort: 80/TCP Endpoints: 10.244.1.2:80 Session Affinity: None Events: <none>
$ kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)
(1)先運行名爲myapp標籤選擇器的2個pod,鏡像是我用於測試演示本身構建的
[root@master ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2 deployment.apps/myapp created [root@master ~]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE myapp 2 2 2 2 6s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE myapp-848b5b879b-58jcr 1/1 Running 0 11s 10.244.2.53 node2 myapp-848b5b879b-wpx9v 1/1 Running 0 11s 10.244.1.56 node1
(2)建立service,並驗證
[root@master ~]# kubectl expose deployment myapp --name=myapp --port=80 service/myapp exposed [root@master ~]# kubectl get svc myapp NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE myapp ClusterIP 10.104.205.158 <none> 80/TCP 8s [root@master ~]# curl 10.104.205.158 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> ---由於有2個pod,因此是隨機調度 [root@master ~]# curl 10.104.205.158/hostname.html myapp-848b5b879b-58jcr [root@master ~]# curl 10.104.205.158/hostname.html myapp-848b5b879b-wpx9v
(3)使用scale,將pod擴容到5個
[root@master ~]# kubectl scale --replicas=5 deployment myapp deployment.extensions/myapp scaled [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-2sj8n 1/1 Running 0 8s myapp-848b5b879b-58jcr 1/1 Running 0 4m myapp-848b5b879b-hp7zs 1/1 Running 0 8s myapp-848b5b879b-sfq8r 1/1 Running 0 8s myapp-848b5b879b-wpx9v 1/1 Running 0 4m
(4)使用scale,將pod縮容到3個
[root@master ~]# kubectl scale --replicas=3 deployment myapp [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-2sj8n 1/1 Running 0 1m myapp-848b5b879b-58jcr 1/1 Running 0 6m myapp-848b5b879b-wpx9v 1/1 Running 0 6m
(1)set
(2)set image
$ kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N
將鏡像版本從v1升級到v2版
[root@master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2 deployment.extensions/myapp image updated [root@master ~]# kubectl rollout status deployment myapp #動態顯示版本升級過程 ---由於版本升級,因此pod都是新建的,名字都改變了 [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE client 1/1 Running 0 3h myapp-74c94dcb8c-d5fgk 1/1 Running 0 55s myapp-74c94dcb8c-f2ntn 1/1 Running 0 21s myapp-74c94dcb8c-ht9v7 1/1 Running 0 38s ---驗證,版本升級成功 [root@master ~]# curl 10.104.205.158 Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
(1)rollout
(2)rollout undo
回滾pod到以前的版本。
$ kubectl rollout undo (TYPE NAME | TYPE/NAME) [flags]
[root@master ~]# kubectl rollout undo deployment myapp deployment.extensions/myapp [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-848b5b879b-gg9lr 1/1 Running 0 15s myapp-848b5b879b-km4t6 1/1 Running 0 16s myapp-848b5b879b-nq98c 1/1 Running 0 17s ---驗證,回滾成功 [root@master ~]# curl 10.104.205.158 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
$ kubectl edit (RESOURCE/NAME | -f FILENAME)
(1)修改service,將類型type: ClusterIP,修改成type: NodePort;修改後,能夠在集羣外訪問到pod中的服務
[root@master ~]# kubectl edit svc myapp ... ... spec: ... ... type: NodePort ... ... service/myapp edited [root@master ~]# kubectl get svc myapp NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE myapp NodePort 10.104.205.158 <none> 80:31599/TCP 23m
(2)在集羣外的主機上訪問了
更新(增長、修改或刪除)資源上的 label(標籤)。
$ kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]
(1)運行一個標籤爲app=nginx的pod
[root@master ~]# kubectl run nginx --image=nginx:1.14-alpine --replicas=1 --labels="app=nginx" deployment.apps/nginx created
(2)-l 指定標籤,實現標籤過濾
[root@master ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS myapp-848b5b879b-5blnx 1/1 Running 0 4m pod-template-hash=4046164356,run=myapp myapp-848b5b879b-6hgpp 1/1 Running 0 4m pod-template-hash=4046164356,run=myapp myapp-848b5b879b-hzj5g 1/1 Running 0 4m pod-template-hash=4046164356,run=myapp nginx-86d5b5ddb6-rwv2z 1/1 Running 0 53s app=nginx,pod-template-hash=4281618862 [root@master ~]# kubectl get pods --show-labels -l app NAME READY STATUS RESTARTS AGE LABELS nginx-86d5b5ddb6-rwv2z 1/1 Running 0 1m app=nginx,pod-template-hash=4281618862
(3)打一個新的標籤
[root@master ~]# kubectl label pods nginx-86d5b5ddb6-rwv2z release=canary pod/nginx-86d5b5ddb6-rwv2z labeled [root@master ~]# kubectl get pods --show-labels -l release NAME READY STATUS RESTARTS AGE LABELS nginx-86d5b5ddb6-rwv2z 1/1 Running 0 4m app=nginx,pod-template-hash=4281618862,release=canary
(4)修改標籤
[root@master ~]# kubectl label pods nginx-86d5b5ddb6-rwv2z release=stable --overwrite pod/nginx-86d5b5ddb6-rwv2z labeled [root@master ~]# kubectl get pods --show-labels -l release NAME READY STATUS RESTARTS AGE LABELS nginx-86d5b5ddb6-rwv2z 1/1 Running 0 5m app=nginx,pod-template-hash=4281618862,release=stable
(1)等值關係:=,==,!=
[root@master ~]# kubectl get pods -l release --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-86d5b5ddb6-rwv2z 1/1 Running 0 6m app=nginx,pod-template-hash=4281618862,release=stable [root@master ~]# kubectl get pods -l release=stable --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-86d5b5ddb6-rwv2z 1/1 Running 0 6m app=nginx,pod-template-hash=4281618862,release=stable [root@master ~]# kubectl get pods -l release!=stable --show-labels NAME READY STATUS RESTARTS AGE LABELS myapp-848b5b879b-5blnx 1/1 Running 0 10m pod-template-hash=4046164356,run=myapp myapp-848b5b879b-6hgpp 1/1 Running 0 10m pod-template-hash=4046164356,run=myapp myapp-848b5b879b-hzj5g 1/1 Running 0 10m pod-template-hash=4046164356,run=myapp
(2)集合關係
[root@master ~]# kubectl get pods -l "release in (stable,beta,alpha)" NAME READY STATUS RESTARTS AGE nginx-86d5b5ddb6-rwv2z 1/1 Running 0 9m [root@master ~]# kubectl get pods -l "release notin (stable,beta,alpha)" NAME READY STATUS RESTARTS AGE myapp-848b5b879b-5blnx 1/1 Running 0 12m myapp-848b5b879b-6hgpp 1/1 Running 0 12m myapp-848b5b879b-hzj5g 1/1 Running 0 12m
未完待續,後續還會補充更多的kubectl 應用及演示。