行业知识
kubectl 速查手册
2026-11-10 10:10:43
来源:互联网转载
资源对象文件
---kind: PodapiVersion: v1metadata: name: myweb labels: app: nginxspec: containers: - name: webserver image: nginxstatus: {} 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 annotationsmypod 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 annotationsmypod my description
api-resources
# 显示服务器上所支持的 API 资源# -o wide 可以用来查询资源权限#-----------------------------------------#[root@master k8s]# kubectl api-resources -o wideNAME SHORTNAMES APIVERSION NAMESPACED KIND VERBSpods po v1 true Pod [get list patch ...]namespaces ns v1 false Namespace [create get ...]
api-versions
# 显示服务端所支持的 API 版本#-----------------------------------------#[root@master k8s]# kubectl api-versionsadmissionregistration.k8s.io/v1apps/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 podsNAME READY STATUS RESTARTS AGEmypod 1/1 Running 0 36smyweb 1/1 Running 0 4s
attach
# 连接一个正在运行的容器的启动进程#-----------------------------------------#[root@master k8s]# kubectl attach mypod -c linuxIf 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.yamldeployment.apps/myweb created[root@master k8s]# kubectl autoscale deployment myweb --min=1 --max=10 --cpu-percent=80horizontalpodautoscaler.autoscaling/myweb autoscaled[root@master k8s]# kubectl get horizontalpodautoscalers.autoscaling NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGEmyweb Deployment/myweb 10%/80% 1 10 1 27m#-----------------------------------------#---apiVersion: autoscaling/v1kind: HorizontalPodAutoscalermetadata: name: mywebspec: minReplicas: 1 maxReplicas: 10 scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: myweb targetCPUUtilizationPercentage: 80
certificate
# 修改证书资源#-----------------------------------------#[root@master k8s]# kubectl get certificatesigningrequestsNAME AGE REQUESTOR CONDITIONcsr-wsfz7 8s system:node:master Pending[root@master k8s]# kubectl certificate approve csr-wsfz7[root@master k8s]# kubectl get certificatesigningrequestsNAME AGE REQUESTOR CONDITIONcsr-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:6443CoreDNS is running at https://192.168.1.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
completion
# 根据已经给出的 Shell 输出 补全代码#-----------------------------------------#[root@master k8s]# source 8080Forwarding from 0.0.0.0:80 -> 80#-----------------------------------------#[root@master local]# curl http://master:8080hello world.
proxy
# 运行一个 kubernetes API 服务器代理#-----------------------------------------#[root@master k8s]# kubectl proxy --port=80Starting 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" deletedpod/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-CAUSE1 httpd.v12 httpd.v2[root@master k8s]# kubectl rollout undo deployment myweb --to-revision=1deployment.apps/myweb rolled back[root@master k8s]# kubectl rollout history deployment deployment.apps/myweb REVISION CHANGE-CAUSE2 httpd.v23 httpd.v1run# 在集群中使用指定镜像启动容器#-----------------------------------------#[root@master k8s]# kubectl run mypod --image=registry:5000/myos:httpd#-----------------------------------------#---apiVersion: v1kind: Podmetadata: labels: run: mypod name: mypodspec: containers: - image: registry:5000/myos:httpd name: mypod restartPolicy: Alwaysscale# 为可扩充资源设置一个新副本数量#-----------------------------------------#[root@master k8s]# kubectl apply -f myDeploy.yamldeployment.apps/myweb created[root@master ~]# kubectl get deployments.apps NAME READY UP-TO-DATE AVAILABLE AGEmyweb 1/1 1 1 21m[root@master ~]# kubectl scale deployment myweb --replicas=3deployment.apps/myweb scaled[root@master ~]# kubectl get deployments.apps NAME READY UP-TO-DATE AVAILABLE AGEmyweb 3/3 3 3 21m
top
# 显示资源(CPU /内存/存储)使用率#-----------------------------------------#[root@master k8s]# kubectl top nodesNAME CPU(cores) CPU% MEMORY(bytes) MEMORY% master 90m 4% 1210Mi 14% node-0001 45m 2% 931Mi 11% [root@master k8s]# kubectl top podsNAME CPU(cores) MEMORY(bytes) mypod 5m 8Mi[root@master k8s]#
uncordon
# 解除(cordon、drain)资源不可调度标记#-----------------------------------------#[root@master k8s]# kubectl get nodesNAME STATUS ROLES AGE VERSIONmaster Ready master 15h v1.22.5node-0001 Ready,SchedulingDisabled node 15h v1.22.5[root@master k8s]# kubectl uncordon node-0001 node/node-0001 uncordoned[root@master k8s]# kubectl get nodesNAME STATUS ROLES AGE VERSIONmaster Ready master 15h v1.22.5node-0001 Ready node 15h v1.22.5
version
# 显示客户端和服务器的版本信息#-----------------------------------------#[root@master k8s]# kubectl version -o yamlclientVersion: 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/amd64serverVersion: 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/amd64wait# 等待一个或多个资源达到某种状态#-----------------------------------------#---kind: PodapiVersion: v1metadata: name: mypodspec: 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" deletedpod/mypod replaced[root@master k8s]# time kubectl wait --for=condition=Ready pod/mypodpod/mypod condition metreal 0m10.335suser 0m0.034ssys 0m0.008s()()