kubectl
安装kubectl
kubectl 版本和集群版本之间的差异必须在一个小版本号内。 例如:v1.32 版本的客户端能与 v1.31、 v1.32 和 v1.33 版本的控制面通信
安装
curl -LO https://dl.k8s.io/release/v1.32.0/bin/linux/amd64/kubectl chmod +x kubectl mv kubectl /usr/bin/kubectl
查看版本信息
kubectl version --client
补全功能
kubectl 的补全功能依赖于操作系统的
bash-completion
包
yum install bash-completion # 安装后生成了/usr/share/bash-completion/bash_completion 文件 source /usr/share/bash-completion/bash_completion # 通过该命令验证功能是否正常 type _init_completion
# 加入到环境profile中 echo 'source <(kubectl completion bash)' >>/etc/profile source /etc/profile
查看api 组
kubectl api-versions
kubectl get apiservice
查看k8s中的资源类型
kubectl api-resources kubectl api-resources --namespaced=false kubectl api-resources --namespaced=true
查看集群信息
[root@master01 ~]# kubectl cluster-info Kubernetes control plane is running at https://192.168.0.244:6443 CoreDNS is running at https://192.168.0.244:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
kubectl run
-
--image
kubectl run test --image=busybox --dry-run=client -oyaml -
--labels
kubectl run test --image=busybox --labels "app=web,env=uat" --dry-run=client -oyaml -
--annotations
kubectl run test --image=busybox --labels "app=web,env=uat" --annotations "describe=this is a test pod create by kubectl" --dry-run=client -oyaml -
--port
kubectl run test --image=busybox --labels "app=web,env=uat" --annotations "describe=this is a test pod create by kubectl" --port=80 --dry-run=client -oyaml -
--env
kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --dry-run=client -oyaml -
--restart
kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --dry-run=client -oyaml -
--expose
kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --port=6379 --expose --dry-run=client -oyaml -
--image-pull-policy
kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --port=80 --expose --image-pull-policy=Never --dry-run=client -oyaml -
--privileged
kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --port=80 --expose --image-pull-policy=Never --dry-run=client -oyaml --privileged
kubectl 自定义插件
kubectl 支持使用任何编程语言定义插件,插件位置需要在$PATH 路径中,需要可执行权限,命令需要以kubectl
为前缀
示例:
编写脚本并放在PATH 中
root@master01:~# cat /usr/local/bin/kubectl-hello #!/bin/bash echo hello world
添加执行权限
chmod +x /usr/local/bin/kubectl-hello
调用插件
root@master01:~# kubectl hello hello world
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本