k8s常見命令及參數(shù)
2.1 命令語法格式
kubectl特性豐富且功能強大暖混,是kubernetes管理員最常用的集群管理工具蓬蝶,其最基本的語法格式如下:
kubectl [command] [TYPE] [NAME] [flages]
說明:
command:對資源執(zhí)行相應(yīng)操作的子命令,例如 get问麸、create膀捷、delete、run 等鸟蟹;
TYPE:要操作的資源類型,例如pods使兔、services等建钥;類型名稱大小寫敏感,但支持使用單數(shù)虐沥、復(fù)數(shù)或簡寫格式熊经;
NAME:要操作的資源對象名稱,大小寫敏感欲险;省略時镐依,則表示指定TYPE的所有資源對象;同一類型的資源名稱可于TYPE后同時給出多個天试,也可以直接使用TYPE/NAME的格式來為每個資源對象分別指定類型槐壳;
flags:命令行選項,例如-s或--server等喜每;另外get等命令在輸出時還有一個常用的標(biāo)志 -o <format> 用于指定輸出格式
2.2 常見命令(command)
2.2.1 get
作用:顯示一個或多個資源信息
例1:查看節(jié)點狀態(tài)
[root@hostname /root]?
#kubectl get node
NAME? ? ? ? ? ? ? ? ? ? ? ? ? STATUS? ROLES? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? AGE? VERSION
?"xx.xx.xx.xx"? Ready? ? worker? 32d? v1.14.8-aliyun.1
?"xx.xx.xx.xx"? Ready? ? master,worker? ? ? ? 32d? v1.14.8-aliyun.1
例2:查看velero命名空間(namespace)下的所有pod及svc資源
[root@?"xx.xx.xx.xx" /root]?
#kubectl get po,svc -n 命名空間
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE
pod/xx-xx-xxx-md58g? ? ? ? ? ? ? ? ? ? 2/2? ? Running? 0? ? ? ? ? 30d
pod/xxxx-xxx-4fwqc? ? ? ? ? ? ? ? ? ? ? 2/2? ? Running? 0? ? ? ? ? 30d
NAME? ? ? ? ? ? ? ? ? ? ? ? ? TYPE? ? ? ? CLUSTER-IP? ? EXTERNAL-IP? PORT(S)? ? ? ? ? ? AGE
service/xx-svc? ?ClusterIP? ?"xx.xx.xx.xx"? <none>? ? ? ?xx/UDP,xx/TCP? 33d
service/velero? ? ? ? ? ? ? ? ClusterIP? ?"xx.xx.xx.xx"? ? <none>? ? ? ? ? ? xx/TCP? ? ? ? ? ? 33d
2.2.2 describe
作用:顯示指定的資源或資源組的詳細(xì)信息
例:查看kube-system命名空間下metrics-server這個pod的詳細(xì)信息
第一步:根據(jù)ns及關(guān)鍵字獲取到pod名稱
[root@hostname /root]?
#kubectl get po -n 命名空間 | grep pod名稱
pod名稱? ? ? ? ? ? ? ? ? ? ? ? ?1/1? ? Running? ? 2? ? ? ? ? 30d
第二步:查看describe信息
[root@hostname /root]?
#kubectl describe po -n 命名空間 pod名稱
Events:
? Type? ? Reason? ? Age? ? ? ? ? ? ? ? ? ? From? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Message
? ----? ? ------? ? ----? ? ? ? ? ? ? ? ? ----? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? -------
xxx
ps:在實際運維中宏粤,如遇pod狀態(tài)異常,那首要任務(wù)就是查看pod describe中最下方Events字段中的信息
2.2.3 delete
作用:基于資源對象名稱刪除相關(guān)資源
例:刪除ark-system命名空間下名為xxx的pod
[root@hostname /root]?
#kubectl delete po -n 命名空間 xxx
pod "xxx" deleted
2.2.4 edit
作用:編輯資源(執(zhí)行命令后將會打開一個vim編輯器灼卢,操作同vim)
例:想要修改xxx ns下控制器名稱的ds控制器中l(wèi)imit字段里memory的大小
第一步:編輯控制器
kubectl edit ds -n xxx 控制器名稱
第二步:找到對應(yīng)字段,修改完成保存退出
? ? ? ? resources:
? ? ? ? ? limits:
? ? ? ? ? ? cpu: 10m
? ? ? ? ? ? memory: 80Mi? ? ## 修改該字段的數(shù)值
? ? ? ? ? requests:
? ? ? ? ? ? cpu: 1m
? ? ? ? ? ? memory: 27996979200m
2.2.5 logs
作用:顯示一個pod內(nèi)容器的日志
例:查看 xxx.cloud.xx.amtest.xx 節(jié)點上rama-daemon組件的pod最后五行日志
第一步:根據(jù)節(jié)點hostname與pod名稱關(guān)鍵詞精確查找到podname
[root@hostname /root]?
#kubectl get pod -A -o wide | grep xxx.cloud.xx.amtestxx | grep rama-daemon
xxx? ? ? ? ? ? ? rama-xx-xx? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1/1? ? Running? ? ? ? ? ? ? ? 316? ? ? ? 33d? ? ?"xx.xx.xx.xx"? ? ?xxx.cloud.xx.amtestxx? ?<none>? ? ? ? ? <none>
第二步:查看pod日志
[root@hostname /root]?
#kubectl logs -n 命名空間 pod名稱 --tail 5
補充:如果要查看容器上一次退出時候的日志来农,可以加上 -p 參數(shù)(previous)
kubectl logs -n 命名空間 pod名稱 -p
2.2.6 apply
作用:基于文件或stdin將配置應(yīng)用于資源
例:需將當(dāng)前目錄下名為job.yaml的文件創(chuàng)建為k8s資源
[root@hostname /root]?
#kubectl apply -f job.yaml
job.batch/xxx-xx created
2.2.7 exec
作用:對 Pod 中的容器執(zhí)行命令
例1:進入default命名空間下 xxx pod鞋真,查看根下有哪些目錄及文件
[root@hostname /root]?
#kubectl exec -it xxx bash
[root@xxx /home/admin/bin]
#ls /
例2:直接列出(不進入pod)default命名空間下 xxx pod 根下有哪些目錄及文件
[root@hostname /root]?
#kubectl exec -it xxx -- ls /
2.2.8 (un)cordon
作用:(解除)限制節(jié)點調(diào)度pod
例1:設(shè)置節(jié)點為不可調(diào)度調(diào)度狀態(tài)
kubectl cordon ${節(jié)點名}
例2:取消節(jié)點不可調(diào)度狀態(tài)
kubectl uncordon ${節(jié)點名}
ps:當(dāng)設(shè)置節(jié)點為cordon(不可調(diào)度)狀態(tài)后,執(zhí)行 kubectl get node 時候可以看到節(jié)點狀態(tài)被標(biāo)記為了 SchedulingDisabled
2.2.9 taint
作用:更新一個或多個節(jié)點上的污點
污點設(shè)置格式:kubectl taint node {nodename} {key}={value}:{effect}
例1:給節(jié)點 xxx.cloud.d02.amtestxx 添加污點 a=b:NoSchedule
[root@xxx.cloud.d02.amtestxx /root] 69E_OPS1
#kubectl taint node xxx.cloud.d02.amtestxx a=b:NoSchedule
node/xxx.cloud.d02.amtestxx tainted
例2:將節(jié)點 xxx.cloud.d02.amtestxx 上key為a的污點去除
[root@hostname /root]?
#kubectl taint node xxx.cloud.d02.amtestxx a-
node/xxx.cloud.d02.amtestxx untainted
例3:將節(jié)點 xxx.cloud.d02.amtestxx 上key為a的原有污點(a=b:NoSchedule)進行更新沃于,更新后為(a=c:NoSchedule)
[root@hostname /root]?
#kubectl taint node xxx.amtest11 a=c:NoSchedule --overwrite
node/xxx.cloud.d02.amtestxx modified
2.2.10 label
作用:添加或更新一個或多個資源的標(biāo)簽
設(shè)置格式:kubectl label {TYPE} {key}={value}
例:給節(jié)點 xxx.cloud.d02.amtestxx 添加標(biāo)簽 a=b
[root@hostname /root]?
#kubectl label node xxx.cloud.d02.amtestxx a=b
node/xxx.cloud.d02.amtestxx labeled
去除標(biāo)簽涩咖、覆蓋(overwrite)標(biāo)簽與taint操作方法基本一致故不再贅述
2.3 常見資源類型(TYPE)
2.3.1 原生資源類型
2.3.1.1 node
簡介:Kubernetes 通過將容器放入在節(jié)點(Node)上運行的 Pod 中來執(zhí)行你的工作負(fù)載
[root@hostname /root]?
#kubectl get node -o wide
2.3.1.2 pods
簡介:Pod 是可以在 Kubernetes 中創(chuàng)建和管理的、最小的可部署的計算單元(可以由1個或多個容器組成)
[root@hostname /root]?
#kubectl get po -n 命名空間 -o wide
2.3.1.3 service
簡介:Service 是將運行在一個或一組 Pod 上的網(wǎng)絡(luò)應(yīng)用程序公開為網(wǎng)絡(luò)服務(wù)的方法
[root@hostname /root]?
#kubectl get service -n 命名空間
2.3.1.4 ingress
簡介:Ingress 是對集群中服務(wù)的外部訪問進行管理的 API 對象
[root@hostname /root]?
#kubectl get ing -n 命名空間
2.3.1.5 PersistentVolumeClaim(pvc)
簡介:PersistentVolumeClaim 是用戶針對一個持久卷的請求和申領(lǐng)
[root@hostname /root]?
#kubectl get pvc -n ark-system
2.3.1.6 PersistentVolume(pv)
簡介:PersistentVolume 是管理員制備的一個存儲資源
[root@hostname /root]?
#kubectl get pv
2.3.2 阿里專有云二次開發(fā)的資源類型
2.3.2.1 appset
簡介:AppSets 是一組應(yīng)用的定義, 即AppD 的列表, 對應(yīng)一個產(chǎn)品Feature
2.3.2.2 appinstance
簡介:AppInstance 是AppD 的運行態(tài)實例, 它記錄了AppD chart 的parameterValues繁莹、resources檩互、workload-Settings
2.3.2.3 ipinstance
簡介:rama使用ipam為pod分配ip,每個被分配的ip使用ipinstance CR進行保存
2.3.2.4 subnet
簡介:每個subnet 表示一個容器網(wǎng)段咨演,
2.3.2.5 network
簡介:network 表示一個pod 的調(diào)度域闸昨,屬于特定network的pod,只能被調(diào)度到network指定的節(jié)點
[root@hostname /root]?
2.4 常見命令選項(flags)
2.3.1 -A
作用:列出所有命名空間下的某個和多個資源
在專有云敏捷PaaS環(huán)境中由于k8s版本較低(1.12),故不支持 -A 選項饵较,若需要查看所有ns下資源拍嵌,可以使用 --all-namespaces;-A等價于 --all-namespaces
2.3.2 -o wide
作用:查看資源更多詳細(xì)信息
示例:如果要查看某個pod ip及所調(diào)度的節(jié)點循诉,僅使用 get pod 輸出的結(jié)果是無法直接獲取的横辆,還需要加上 -o wide 選項才可以完整顯示
[root@hostname /root]?
#kubectl get po xx
NAME? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE
xx? ?1/1? ? Running? 0? ? ? ? ? 38d
[root@hostname /root]?
#kubectl get po xx -o wide
NAME? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE? IP? ? ? ? ? ? NODE? ? ? ? ? ? ? ? ? ? ? ? ? NOMINATED NODE? READINESS GATES
xx? ?1/1? ? Running? 0? ? ? ? ? 38d? ?xx.xx.xx.xx? ?xxx.cloud.d05.amtest11? <none>? ? ? ? ? <none>
2.3.3 -n
作用:namespace
ps:如果不指定 -n 則默認(rèn)顯示default命名空間下的資源,等價于 -n default
2.3.4 -o yaml
作用:指定結(jié)果輸出結(jié)果顯示格式以yaml格式輸出
[root@hostname /root]?
#kubectl get svc kubernetes -o yaml
apiVersion: v1
kind: Service
metadata:
? creationTimestamp: "2023-03-28T03:22:20Z"
? labels:
? ? component: apiserver
? ? provider: kubernetes
? name: kubernetes
? namespace: default
? resourceVersion: "182"
? selfLink: /api/v1/namespaces/default/services/kubernetes
? uid: xxx-xx-xxx-xx
spec:
? clusterIP: ?xx.xx.xx.xx
? ports:
? - name: https
? ? port: 443
? ? protocol: TCP
? ? targetPort: 6443
? sessionAffinity: None
? type: ClusterIP
status:
? loadBalancer: {}
2.3.5 -c
作用:指定pod中的容器名茄猫,可以是init容器狈蚤,也可以是main容器
場景:當(dāng)一個pod中包含多個容器時(非1/1)或者pod處于init0/1時需要查看日志時,直接執(zhí)行 kubectl logs 是會報錯的(如下圖)划纽,會提示你需要指定其中一個容器查看日志脆侮,可以選擇的容器列表在 [xxx] 中顯示,例如nginx-ingress這個pod的中就包含2個主容器阿浓,分別為 nginx-ingress-controller 和 ingress-nginx-sidecar
如果需要查看其中一個容器日志就可以這樣:
[root@hostname /root]?
#kubectl logs -n 命名空間 pod名稱 -c 容器名稱
2.3.6 -o json
作用:指定結(jié)果輸出結(jié)果顯示格式以json格式輸出
[root@hostname /root]?
#kubectl get svc kubernetes -o json
2.4 k8s命令補齊與簡寫
2.4.1 命令補齊
說明:k8s命令簡寫是基于 bash-completion 這個包實現(xiàn)他嚷,在專有云PaaS底座中為默認(rèn)安裝,可以為簡寫相關(guān)k8s命令節(jié)省很多時間
使用方法:
例如你想輸入 kubectl describe 這條命令芭毙,可以直接執(zhí)行 kube <tab>? des<tab> 即可達(dá)到同樣效果而不用一個個字母鍵入筋蓖,工具會自動給你補齊,具體使用可自行去摸索體驗
注意:如果kubectl做了alias(別名)則補全功能無法使用退敦!必須使用kubectl的全寫
2.4.2 命令簡寫
運維過程中很多命令我們可能無法清晰記得粘咖,比如 pvc 的全稱為 persistentvolumeclaims 全部完整書寫難度大且還不一定敲的正確,這時我們就可以使用該TYPE的簡寫成 pvc侈百,省時省力又好記瓮下。在輸入命令時候可以使用 kubectl api-resources 查看哪些命令支持縮寫,在 SHORTNAMES 這一列對應(yīng)的就是資源名的簡寫钝域,例如:nodes 可以簡寫為 no讽坏,在執(zhí)行命令的時候可以直接執(zhí)行 kubectl get no 即可查看到節(jié)點列表及狀態(tài),等價于 kubectl get nodes
三例证、總結(jié)及小技巧
1.在查看pod資源信息時候請加上 -o wide 要不然看到節(jié)點/ip等信息我還得讓你重新拍照
2.同一個命名空間(namespace)下的資源可以在一條命令同時操作路呜,示例如下
kubectl delete po -n 命名空間 pod1 pod2 pod3
3.在刪除sts類型的pod時,如果要同時刪除該控制器下所屬的全部pod织咧,可以如下操作
[root@hostname /root]?
#kubectl get po -n 命名空間 | grep xxxx
xxxx-0? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? ? ? ? ? ? 0? ? ? ? ? 37d
xxxx-1? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? ? ? ? ? ? 3? ? ? ? ? 35d
xxxx-2? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1/1? ? Running? ? ? ? ? ? 0? ? ? ? ? 37d
[root@hostname /root]?
#kubectl delete po -n 命名空間 xxxx-{0..2}
以上簡單列舉幾個胀葱,更多快捷操作方法等你發(fā)現(xiàn)~
四、補充
3.1 kubectl支持的command有哪些笙蒙?
kubectl支持的所有命令可以使用 kubectl --help 命令進行查看
[root@xxx.cloud.d02.amtestxx /root]?
#kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
? create? ? ? ? Create a resource from a file or from stdin.
? expose? ? ? ? Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
? run? ? ? ? ? ? Run a particular image on the cluster
? set? ? ? ? ? ? Set specific features on objects
Basic Commands (Intermediate):
? explain? ? ? ? Documentation of resources
? get? ? ? ? ? ? Display one or many resources
? edit? ? ? ? ? Edit a resource on the server
? delete? ? ? ? Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy Commands:
? rollout? ? ? ? Manage the rollout of a resource
? scale? ? ? ? ? Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
? autoscale? ? ? Auto-scale a Deployment, ReplicaSet, or ReplicationController
Cluster Management Commands:
? certificate? ? Modify certificate resources.
? cluster-info? Display cluster info
? top? ? ? ? ? ? Display Resource (CPU/Memory/Storage) usage.
? cordon? ? ? ? Mark node as unschedulable
? uncordon? ? ? Mark node as schedulable
? drain? ? ? ? ? Drain node in preparation for maintenance
? taint? ? ? ? ? Update the taints on one or more nodes
Troubleshooting and Debugging Commands:
? describe? ? ? Show details of a specific resource or group of resources
? logs? ? ? ? ? Print the logs for a container in a pod
? attach? ? ? ? Attach to a running container
? exec? ? ? ? ? Execute a command in a container
? port-forward? Forward one or more local ports to a pod
? proxy? ? ? ? ? Run a proxy to the Kubernetes API server
? cp? ? ? ? ? ? Copy files and directories to and from containers.
? auth? ? ? ? ? Inspect authorization
Advanced Commands:
? diff? ? ? ? ? Diff live version against would-be applied version
? apply? ? ? ? ? Apply a configuration to a resource by filename or stdin
? patch? ? ? ? ? Update field(s) of a resource using strategic merge patch
? replace? ? ? ? Replace a resource by filename or stdin
? wait? ? ? ? ? Experimental: Wait for a specific condition on one or many resources.
? convert? ? ? ? Convert config files between different API versions
? kustomize? ? ? Build a kustomization target from a directory or a remote url.
Settings Commands:
? label? ? ? ? ? Update the labels on a resource
? annotate? ? ? Update the annotations on a resource
? completion? ? Output shell completion code for the specified shell (bash or zsh)
Other Commands:
? api-resources? Print the supported API resources on the server
? api-versions? Print the supported API versions on the server, in the form of "group/version"
? config? ? ? ? Modify kubeconfig files
? plugin? ? ? ? Provides utilities for interacting with plugins.
? version? ? ? ? Print the client and server version information
Usage:
? kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
參考文檔:命令行工具 (kubectl)· kubernetes docs
3.2 其他常用命令
限于篇幅原因抵屿,上面僅介紹了運維過程中最常用、最基礎(chǔ)的操作命令捅位,還要很多進階命令需要學(xué)習(xí)和掌握轧葛,我哦再次列舉幾個搂抒,大家可以抽空進行學(xué)習(xí)和練習(xí):
scale(伸縮Deployment、ReplicaSet朝群、RC或job的規(guī)模)
cp(從容器中拷貝文件至宿主機或反之)
proxy(可以使kube-spiserver監(jiān)聽在本地的8001端口燕耿,進而直接訪問apiserver無需配置token等步驟)
api-resources(可以查看k8s集群中所有的api資源信息)
version(打印kubernetes服務(wù)端和客戶端的版本信息)
3.3 學(xué)習(xí)資料
本文僅涉及kubernetes核心基礎(chǔ)操作命令,還遠(yuǎn)遠(yuǎn)不能滿足k8s運維的需要姜胖,況且k8s版本迭代快誉帅;如果你想更深入的進階學(xué)習(xí)k8s,歡迎參考學(xué)習(xí)以下鏈接及視頻教程右莱,希望對各位有所幫助:
1.kubernetes官方document(核心蚜锨!學(xué)k8s不看官方Doc怎么行?)
https://kubernetes.io/zh-cn/docs/home/
2.kubernetes源碼(適合會Go的大牛學(xué)習(xí)哦~新手略過)
https://github.com/kubernetes/kubernetes
3.尚硅谷 kubernetes教程(初學(xué)入門必備慢蜓!)