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

  1. --image

    kubectl run test --image=busybox --dry-run=client -oyaml
  2. --labels

    kubectl run test --image=busybox --labels "app=web,env=uat" --dry-run=client -oyaml
  3. --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
  4. --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
  5. --env

    kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --dry-run=client -oyaml
  6. --restart

    kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --dry-run=client -oyaml
  7. --expose

    kubectl run test --image=busybox --env "port=6379" --env "host=127.0.0.1" --restart=OnFailure --port=6379 --expose --dry-run=client -oyaml
  8. --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
  9. --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

KubeCM

posted @   mingtian是吧  阅读(50)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示