在给kubernetes安装dashboard时,可能会存在CA证书问题导致dashboard pod启动失败,用kubectl logs <podname> -n kube-system查看日志会发现有这样的日志存在
Expected to load root CA config from /var/run/secrets/kubernetes.io/serviceaccount/ca.crt, but got err: open /var/run/secrets/kubernetes.io/serviceaccount/ca.crt: no such file or directory
这是因为kubernetes默认创建的secrets 资源不包含用于访问apiserver的根证书
这需要重新生产证书和秘钥
1 使用easyrsa生成证书和秘钥
wget https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz
tar zxf easy-rsa.tar.gz
cd easy-rsa/easyrsa3
./easyrsa init-pki
2 创建根证书
./easyrsa --batch "--req-cn=masterip@date +%s
" build-ca nopass
3 创建服务端证书和秘钥
./easyrsa --subject-alt-name="IP:masterip,IP:clusterip,DNS:kubernetes.default" build-server-full kubernetes-master server nopass
本文用例
./easyrsa --subject-alt-name="IP:192.168.100.20,IP:10.10.10.1,DNS:kubernetes.default" build-server-full kubernetes-master server nopass
./easyrsa --dn-mode=org --req-cn=kubecfg --req-org=system:master --req-c= --req-st= --req-city= --req-email= --req-ou= build-client-full kubecfg nopass
4 拷贝pki/ca.crt、pki/issued/server.crt和pki/private/server.key至指定的目录
mkdir /etc/kubernetes/pki
cp pki/ca.crt pki/issued/server.crt pki/private/server.key /etc/kubernetes/pki/
cp -fr pki/issued/kubecfg.crt /srv/kubernetes/
cp -fr pki/private/kubecfg.key /srv/kubernetes
chmod 644 /etc/kubernetes/pki/*
openssl x509 -noout -text -in /srv/kubernetes/server.crt
创建token
echo 123456,admin,admin > /srv/kubernetes/basic_auth.csv # 格式:密码,用户名,用户ID
5 更新apiserver服务启动参数
--client-ca-file=/etc/kubernetes/pki/ca.crt
--tls-cert-file=/etc/kubernetes/pki/server.crt
--tls-private-key-file=/etc/kubernetes/pki/server.key
--basic-auth-file=/srv/kubernetes/basic_auth.csv
6 更新controller-manager服务启动参数
--service_account_private_key_file=/etc/kubernetes/pki/server.key
--root-ca-file=/etc/kubernetes/pki/ca.crt
7 删除旧secrets资源
root@mon2:~# kubectl get secrets --all-namespaces
NAMESPACE NAME TYPE DATA AGE
default ceph-secret Opaque 1 22h
default default-token-50p5c kubernetes.io/service-account-token 3 4h
kubectl delete secret default-token-50p5c
8 重启apiserver 和controller-manager
systemcal restart apiserver controller-manager
9 检查新创建的secret是否包含根证书
root@mon2:~# kubectl get secrets --all-namespaces
NAMESPACE NAME TYPE DATA AGE
default ceph-secret Opaque 1 22h
default default-token-50p5c kubernetes.io/service-account-token 3 4h
kube-public default-token-667l9 kubernetes.io/service-account-token 3 4h
kube-system default-token-d9n7c kubernetes.io/service-account-token 3 4h
现在重新创建dashboard就不会出现证书找不到了