kubectl 速查手册
资源对象文件
--- kind: Pod apiVersion: v1 metadata: name: myweb labels: app: nginx spec: containers: - name: webserver image: nginx status: {}
annotate
# 更新资源所关联的注释信息 #-----------------------------------------# [root@master k8s]# kubectl apply -f mypod.yaml --record [root@master k8s]# kubectl get pod mypod -o custom-columns=podName:.metadata.name,annotations:.metadata.annotations."kubernetes\.io/change-cause" podName annotations mypod kubectl apply --filename=mypod.yaml --record=true [root@master k8s]# kubectl annotate pods mypod kubernetes.io/change-cause='my description' pod/mypod annotated [root@master k8s]# kubectl get pod mypod -o custom-columns=podName:.metadata.name,annotations:.metadata.annotations."kubernetes\.io/change-cause" podName annotations mypod my description
api-resources
# 显示服务器上所支持的 API 资源 # -o wide 可以用来查询资源权限 #-----------------------------------------# [root@master k8s]# kubectl api-resources -o wide NAME SHORTNAMES APIVERSION NAMESPACED KIND VERBS pods po v1 true Pod [get list patch ...] namespaces ns v1 false Namespace [create get ...]
api-versions
# 显示服务端所支持的 API 版本 #-----------------------------------------# [root@master k8s]# kubectl api-versions admissionregistration.k8s.io/v1 apps/v1 ... ... v1
apply
# 读取资源文件,将新的配置应用到资源上 #-----------------------------------------# [root@master k8s]# kubectl apply -f mypod.yaml pod/mypod created [root@master k8s]# sed 's,mypod,myweb,g' mypod.yaml |kubectl apply -f - pod/myweb created [root@master k8s]# kubectl get pods NAME READY STATUS RESTARTS AGE mypod 1/1 Running 0 36s myweb 1/1 Running 0 4s
attach
# 连接一个正在运行的容器的启动进程 #-----------------------------------------# [root@master k8s]# kubectl attach mypod -c linux If you don't see a command prompt, try pressing enter. 10.244.219.64:44372: response:200
auth
# 检查授权信息 #-----------------------------------------# [root@master k8s]# kubectl --kubeconfig=admin.conf auth can-i get pods yes [root@master k8s]# kubectl --kubeconfig=auth.conf auth can-i get pods no
autoscale
# 创建一个HPA控制器,对资源对象进行自动扩缩 #-----------------------------------------# [root@master k8s]# kubectl apply -f myDeploy.yaml deployment.apps/myweb created [root@master k8s]# kubectl autoscale deployment myweb --min=1 --max=10 --cpu-percent=80 horizontalpodautoscaler.autoscaling/myweb autoscaled [root@master k8s]# kubectl get horizontalpodautoscalers.autoscaling NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE myweb Deployment/myweb 10%/80% 1 10 1 27m #-----------------------------------------# --- apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: myweb spec: minReplicas: 1 maxReplicas: 10 scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: myweb targetCPUUtilizationPercentage: 80
certificate
# 修改证书资源 #-----------------------------------------# [root@master k8s]# kubectl get certificatesigningrequests NAME AGE REQUESTOR CONDITION csr-wsfz7 8s system:node:master Pending [root@master k8s]# kubectl certificate approve csr-wsfz7 [root@master k8s]# kubectl get certificatesigningrequests NAME AGE REQUESTOR CONDITION csr-wsfz7 86s system:node:master Approved,Issued
cluster-info
# 显示集群信息 #-----------------------------------------# [root@master k8s]# kubectl cluster-info Kubernetes control plane is running at https://192.168.1.10:6443 CoreDNS is running at https://192.168.1.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
completion
# 根据已经给出的 Shell 输出 补全代码 #-----------------------------------------# [root@master k8s]# source 8080 Forwarding from 0.0.0.0:80 -> 80 #-----------------------------------------# [root@master local]# curl http://master:8080 hello world.
proxy
# 运行一个 kubernetes API 服务器代理 #-----------------------------------------# [root@master k8s]# kubectl proxy --port=80 Starting to serve on 127.0.0.1:80 #-----------------------------------------# [root@master k8s]# curl http://127.0.0.1/version { "major": "1", "minor": "22", "gitVersion": "v1.22.5", "gitCommit": "c92036820499fedefec0f847e2054d824aea6cd1", "gitTreeState": "clean", "buildDate": "2021-10-27T18:35:25Z", "goVersion": "go1.16.9", "compiler": "gc", "platform": "linux/amd64" }
replace
# 基于文件名或标准输入替换资源 #-----------------------------------------# [root@master k8s]# kubectl replace --force -f mypod.yaml pod "mypod" deleted pod/mypod replaced
rollout
set
# 为资源对象设置功能特性
#-----------------------------------------#
[root@master k8s]# kubectl set env pods --all --list
# Pod mypod, container linux
[root@master k8s]# kubectl set env deployment/myweb myEnv=prod
deployment.apps/myweb env updated
[root@master k8s]# kubectl exec -i -t myweb-6c645646c9-pqjc7 -- sh -c 'echo ${myEnv}'
prod
[root@master k8s]# kubectl get deployments.apps myweb -o wide
NAME READY AGE CONTAINERS IMAGES SELECTOR
myweb 1/1 25s httpd registry:5000/myos:httpd app=apache
[root@master k8s]# kubectl set image deployment/myweb httpd=registry:5000/myos:nginx
deployment.apps/myweb image updated
[root@master k8s]# kubectl get deployments.apps myweb -o wide
NAME READY AGE CONTAINERS IMAGES SELECTOR
myweb 1/1 45s httpd registry:5000/myos:nginx app=apache
taint
# 在一个或者多个节点上更新污点配置
#-----------------------------------------#
[root@master k8s]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName Taints
master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001
[root@master k8s]# kubectl taint node node-0001 k=v:PreferNoSchedule
node/node-0001 tainted
[root@master k8s]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName Taints
master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
node-0001 [map[effect:PreferNoSchedule key:k value:v]]
[root@master k8s]# kubectl taint node node-0001 k-
node/node-0001 untainted
[root@master k8s]# kubectl get nodes -o custom-columns=NodeName:.metadata.name,Taints:.spec.taints
NodeName Taints
master [map[effect:NoSchedule key:node-role.kubernetes.io/master]]
# 管理资源的上线 #-----------------------------------------# [root@master k8s]# kubectl rollout history deployment deployment.apps/myweb REVISION CHANGE-CAUSE 1 httpd.v1 2 httpd.v2 [root@master k8s]# kubectl rollout undo deployment myweb --to-revision=1 deployment.apps/myweb rolled back [root@master k8s]# kubectl rollout history deployment deployment.apps/myweb REVISION CHANGE-CAUSE 2 httpd.v2 3 httpd.v1 run # 在集群中使用指定镜像启动容器 #-----------------------------------------# [root@master k8s]# kubectl run mypod --image=registry:5000/myos:httpd #-----------------------------------------# --- apiVersion: v1 kind: Pod metadata: labels: run: mypod name: mypod spec: containers: - image: registry:5000/myos:httpd name: mypod restartPolicy: Always scale # 为可扩充资源设置一个新副本数量 #-----------------------------------------# [root@master k8s]# kubectl apply -f myDeploy.yaml deployment.apps/myweb created [root@master ~]# kubectl get deployments.apps NAME READY UP-TO-DATE AVAILABLE AGE myweb 1/1 1 1 21m [root@master ~]# kubectl scale deployment myweb --replicas=3 deployment.apps/myweb scaled [root@master ~]# kubectl get deployments.apps NAME READY UP-TO-DATE AVAILABLE AGE myweb 3/3 3 3 21m
top
# 显示资源(CPU /内存/存储)使用率 #-----------------------------------------# [root@master k8s]# kubectl top nodes NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% master 90m 4% 1210Mi 14% node-0001 45m 2% 931Mi 11% [root@master k8s]# kubectl top pods NAME CPU(cores) MEMORY(bytes) mypod 5m 8Mi [root@master k8s]#
uncordon
# 解除(cordon、drain)资源不可调度标记 #-----------------------------------------# [root@master k8s]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 15h v1.22.5 node-0001 Ready,SchedulingDisabled node 15h v1.22.5 [root@master k8s]# kubectl uncordon node-0001 node/node-0001 uncordoned [root@master k8s]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 15h v1.22.5 node-0001 Ready node 15h v1.22.5
version
# 显示客户端和服务器的版本信息 #-----------------------------------------# [root@master k8s]# kubectl version -o yaml clientVersion: buildDate: "2021-10-27T18:41:28Z" compiler: gc gitCommit: c92036820499fedefec0f847e2054d824aea6cd1 gitTreeState: clean gitVersion: v1.22.5 goVersion: go1.16.9 major: "1" minor: "22" platform: linux/amd64 serverVersion: buildDate: "2021-10-27T18:35:25Z" compiler: gc gitCommit: c92036820499fedefec0f847e2054d824aea6cd1 gitTreeState: clean gitVersion: v1.22.5 goVersion: go1.16.9 major: "1" minor: "22" platform: linux/amd64 wait # 等待一个或多个资源达到某种状态 #-----------------------------------------# --- kind: Pod apiVersion: v1 metadata: name: mypod spec: terminationGracePeriodSeconds: 0 initContainers: - name: myinit image: registry:5000/busybox:latest imagePullPolicy: IfNotPresent command: ["sleep", "10"] containers: - name: linux image: registry:5000/busybox:latest imagePullPolicy: IfNotPresent command: ["sh", "-c"] args: - | echo "hello world !!!" >/var/www/index.html httpd -v -f -p 0.0.0.0:80 -h /var/www restartPolicy: Always #-----------------------------------------# [root@master k8s]# kubectl replace --force -f mypod.yaml pod "mypod" deleted pod/mypod replaced [root@master k8s]# time kubectl wait --for=condition=Ready pod/mypod pod/mypod condition met real 0m10.335s user 0m0.034s sys 0m0.008s()()
The End