Kubectl命令
可以在你的集群中的 master node 中使用 kubectl 管理集群,也可以在本地环境中管理集群。
安装 kubectl
# 如果在 mac 上
brew install kubectl
# 或者
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
# 如果在 linux 上
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.5", GitCommit:"c285e781331a3785a7f436042c65c5641ce8a9e9", GitTreeState:"clean", BuildDate:"2022-03-16T15:51:05Z", GoVersion:"go1.17.8", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.1+k3s1", GitCommit:"75dba57f9b1de3ec0403b148c52c348e1dee2a5e", GitTreeState:"clean", BuildDate:"2021-05-21T16:12:29Z", GoVersion:"go1.16.4", Compiler:"gc", Platform:"linux/amd64"}
WARNING: version difference between client (1.23) and server (1.21) exceeds the supported minor version skew of +/-1
自动补全
# 安装自动补全插件
$ yum install bash-completion
# 添加 kubectl 自动补全的脚本至 /etc/bash_completion.d 目录下
# kubectl completion bash: 生成自动补全的脚本
$ kubectl completion bash >/etc/bash_completion.d/kubectl
# 自动补全生效
$ kubectl get n
namespaces networkpolicies.extensions networkpolicies.networking.k8s.io nodes
source <(kubectl completion zsh)
证书问题
一切就绪,准备与 API Server 友好交流时,有可能发现会有以下的证书报错。其中 47.93.26.56 是 master node 的公网 IP。
kubectl get pods
Unable to connect to the server: x509: certificate is valid for 10.96.0.1, 172.17.68.40, not 47.93.26.56
此时可以使用以下命令重新生成证书,把 IP 地址 47.93.26.56 加入白名单。以下操作均在 master node
# 以下操作均在 master node
# 删除 apiserver 的证书
$ rm /etc/kubernetes/pki/apiserver.*
rm: remove regular file ‘/etc/kubernetes/pki/apiserver.crt’? y
rm: remove regular file ‘/etc/kubernetes/pki/apiserver.key’? y
# 重新生成证书,并添加 IP 地址白名单 (当前 kubernetes 集群的版本是 v1.15)
$ kubeadm init phase certs all --apiserver-advertise-address=0.0.0.0 --apiserver-cert-extra-sans=47.93.26.56
W0902 20:38:12.102570 18119 version.go:98] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
W0902 20:38:12.102645 18119 version.go:99] falling back to the local client version: v1.15.3
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Using existing front-proxy-ca certificate authority
[certs] Using existing front-proxy-client certificate and key on disk
[certs] Using existing etcd/ca certificate authority
[certs] Using existing etcd/healthcheck-client certificate and key on disk
[certs] Using existing apiserver-etcd-client certificate and key on disk
[certs] Using existing etcd/peer certificate and key on disk
[certs] Using existing etcd/server certificate and key on disk
[certs] Using existing ca certificate authority
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [shuifeng kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.17.68.40 47.93.26.56]
[certs] Using existing apiserver-kubelet-client certificate and key on disk
[certs] Using the existing "sa" key
# 强制删除 apiserver 的容器
$ docker rm -f `docker ps -q -f 'name=k8s_kube-apiserver*'`
83fcae44bb1d
# 重启 kubelet 服务
$ systemctl restart kubelet
此时在个人的 mac (客户端)上进行操作,得以正确返回结果
# 以下操作均在客户端 (个人mac)
$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system helm-install-traefik-crd-sghkz 0/1 Completed 0 31d
kube-system helm-install-traefik-qvgj4 0/1 Completed 1 4d6h
kube-system local-path-provisioner-5ff76fc89d-jp2cr 1/1 Running 33 31d
kube-system coredns-7448499f4d-pnpm9 1/1 Running 33 31d
kubernetes-dashboard dashboard-metrics-scraper-856586f554-6nw2s 1/1 Running 33 31d
cert-manager cert-manager-webhook-65b766b5f8-mxbql 1/1 Running 18 17d
kubernetes-dashboard kubernetes-dashboard-894f59579-jwrw5 1/1 Running 33 31d
kube-system metrics-server-86cbb8457f-8qc8n 1/1 Running 33 31d
kube-system traefik-7c596ddb55-pmb7j 1/1 Running 18 17d
kube-system svclb-traefik-w2fxq 2/2 Running 66 31d
default whoami-667fc988f6-n87mj 1/1 Running 18 17d
cert-manager cert-manager-b4d6fd99b-mspxx 1/1 Running 18 17d
cert-manager cert-manager-cainjector-74bfccdfdf-ng7nd 1/1 Running 18 17d