访问k8s集群出现Unable to connect to the server: x509: certificate is valid for xxx, not xxx问题解决

查看apiserver证书

openssl x509 -noout -text -in apiserver.crt|grep DNS
                DNS:debian-1, DNS:kubernetes, DNS:kubernetes.default, DNS:kubernetes.default.svc, DNS:kubernetes.default.svc.cluster.local, IP Address:10.96.0.1, IP Address:192.168.1.195

证书中没有所需的ip地址

删除旧apiserver证书

cd /etc/kubernetes/pki
rm apiserver.*

生成新apiserver证书

kubeadm init phase certs apiserver --apiserver-advertise-address ${apiserver-ip1} --apiserver-cert-extra-sans ${apiserver-ip2} --apiserver-cert-extra-sans ${apiserver-ip3}

上边的命令中${apiserver-ip1} ${apiserver-ip2} ${apiserver-ip3}根据自己集群的实际情况进行调整。如果需要增加更多的地址,可以继续在命令里增加 --apiserver-cert-extra-sans ${apiserver-ip}参数。

此时可以看到生成了新的apiserver证书。

ls apiserver.*
apiserver.crt  apiserver.key

重启apiserver

$ docker ps|grep apiserver
c6947d3f08cb   b6d7abedde39                                        "kube-apiserver --ad…"   3 days ago       Up 42 minutes                                                          k8s_kube-apiserver_kube-apiserver-debian-1_kube-system_8c9ab1ead009c3ba3cbb640306555281_12
3d6e28c76425   registry.aliyuncs.com/google_containers/pause:3.6   "/pause"                 3 days ago       Up 42 minutes                                                          k8s_POD_kube-apiserver-debian-1_kube-system_8c9ab1ead009c3ba3cbb640306555281_1
$ docker restart c6947d3f08cb 3d6e28c76425
c6947d3f08cb
3d6e28c76425

检查

$ kubectl cluster-info
Kubernetes control plane is running at https://192.168.1.195:6443
CoreDNS is running at https://192.168.1.195:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
 To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'

你可能感兴趣的:(kubernetes,容器,云原生)