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
-
--imagekubectl run test --image=busybox --dry-run=client -oyaml -
--labelskubectl run test --image=busybox --labels "app=web,env=uat" --dry-run=client -oyaml -
--annotationskubectl run test --image=busybox --labels "app=web,env=uat" --annotations "describe=this is a test pod create by kubectl" --dry-run=client -oyaml -
--portkubectl 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 -
--envkubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --dry-run=client -oyaml -
--restartkubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --dry-run=client -oyaml -
--exposekubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --port=6379 --expose --dry-run=client -oyaml -
--image-pull-policykubectl 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 -
--privilegedkubectl 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
浙公网安备 33010602011771号