kubectl commands

安装

https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-linux/

kubectl 版本和集群版本之间的差异必须在一个小版本号内。 例如:v1.28 版本的客户端能与 v1.27、 v1.28 和 v1.29 版本的控制面通信。 用最新兼容版的 kubectl 有助于避免不可预见的问题。

shell 自动补全

bash-completion

1
2
3
apt-get install bash-completion 

yum install bash-completion

Bash、Fish 和 Zsh

1
2
3
echo $SHELL  

echo 'source <(kubectl completion bash)' >>~/.bashrc

中控机

commands

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#logs

crd

1
2
3
4
5
6
7
8
9
❯ kubectl get crd | grep traefik.containo.us | awk '{print $1}'
ingressroutes.traefik.containo.us
ingressroutetcps.traefik.containo.us
ingressrouteudps.traefik.containo.us
middlewares.traefik.containo.us
middlewaretcps.traefik.containo.us

$ kubectl delete customresourcedefinition `kubectl get crd | grep traefik.containo.us | awk '{print $1}'| tr '\n' ' '`

pod

重启

1
2
3
4
5
6
kubernetes-dashboard   kubernetes-dashboard-6495566f7b-9p5mq         0/1     ImagePullBackOff   0              9m43s
❯ kubectl rollout restart kubernetes-dashboard-6495566f7b-9p5mq -n kubernetes-dashboard
error: the server doesn't have a resource type "kubernetes-dashboard-6495566f7b-9p5mq"
❯ kubectl rollout restart deployment/kubernetes-dashboard -n kubernetes-dashboard
deployment.apps/kubernetes-dashboard restarted

rollout restart

logs

tail

1
❯ kubectl logs --tail=10 kubernetes-dashboard-6495566f7b-9p5mq -n kubernetes-dashboard

since

过去时间写入日志

1
❯ kubectl logs --since=23h09m pod/traefik-7d7459bcdc-swkl5

{“level”:”warning”,”msg”:”Error checking new version: Get "https://update.traefik.io/repos/traefik/traefik/releases\": dial tcp: lookup update.traefik.io on 10.96.1.10:53: server misbehaving”,”time”:”2023-10-16T00:11:12Z”}

since-time

指定日期的日志

1
❯ kubectl logs --since-time=2023-10-16T00:11:00Z pod/traefik-7d7459bcdc-swkl5

{“level”:”warning”,”msg”:”Error checking new version: Get "https://update.traefik.io/repos/traefik/traefik/releases\": dial tcp: lookup update.traefik.io on 10.96.1.10:53: server misbehaving”,”time”:”2023-10-16T00:11:12Z”}

krew

https://krew.sigs.k8s.io/docs/user-guide/setup/install/

export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"添加到环境变量.bashrc.zshrc

网不好,使用代理

https://github.com/521xueweihan/GitHub520 hosts文件

https://ghproxy.com

1
2
3
4
5
6
7
8
#v0.4.4
❯ wget https://ghproxy.com/https://github.com/kubernetes-sigs/krew/releases/download/v0.4.4/krew-linux_amd64.tar.gz
❯ ./krew-linux_amd64 install krew
#卡住
❯ cd ~.krew/index
❯ git clone https://ghproxy.com/https://github.com/kubernetes-sigs/krew-index.git default
#./krew-linux_amd64 install krew
❯ ./krew-linux_amd64 install --manifest=krew.yaml --archive=krew-linux_amd64.tar.gz

验证

1
2
3
4
5
6
7
8
❯ kubectl krew
krew is the kubectl plugin manager.
。。。。
❯ kubectl plugin list
The following compatible plugins are available:

/home/cs/.local/custom/kubectl/kubectl-hello
/home/cs/.krew/bin/kubectl-krew

本地安装插件

1
2
3
kubectl krew install --manifest=xx.yaml --archive=kubectl-xx_linux_amd64.zip
kubectl xx
kubectl krew uninstall xx

https://krew.sigs.k8s.io/docs/developer-guide/custom-indexes/

点击打赏
文章目录
  1. 1. 安装
    1. 1.1. shell 自动补全
  2. 2. 中控机
  3. 3. commands
  4. 4. crd
  5. 5. pod
    1. 5.1. 重启
  6. 6. logs
    1. 6.1. tail
    2. 6.2. since
    3. 6.3. since-time
  7. 7. krew
载入天数...载入时分秒... ,