k8s证书升级总结,修改证书时间
1. 查看证书到期时间[master]
for i in /etc/kubernetes/pki/*.crt;do echo $i; openssl x509 -in $i -text -noout|egrep "Not Before|Not After";echo "-----------";done
-----------
etc/kubernetes/pki/apiserver.crt #apiserver是1年,需要修改
Not Before: Mar 10 16:42:26 2020 GMT
Not After : Mar 10 16:42:27 2021 GMT
-----------
/etc/kubernetes/pki/apiserver-kubelet-client.crt #apiserver-kubelet-client是1年,需要修改
Not Before: Mar 10 16:42:26 2020 GMT
Not After : Mar 10 16:42:26 2021 GMT
-----------
/etc/kubernetes/pki/front-proxy-client.crt #front-proxy-client是1年,需要修改
Not Before: Mar 10 16:42:27 2020 GMT
Not After : Mar 10 16:42:27 2021 GMT
-----------
2. 更新apiserver、
apiserver-kubelet-client、ront-proxy-client
证书
1. 复制 /etc/kubernetes/pki/{ca.crt,ca.key,apiserver.key} 到新的目录,修改后 进行覆盖
mkdir /root/pki && cd /root/pki
cp /etc/kubernetes/pki/{ca.crt,ca.key,apiserver.key} /root/pki/
2. 创建ssl.conf文件
cat << EOF >> ssl.conf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name
[req_distinguished_name]
[v3_req]
keyUsage =critical, digitalSignature, keyEncipherment
extendedKeyUsage = TLS Web Server Authentication, TLS Web Client Authentication
subjectAltName = @alt_names
[alt_names]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc.cluster.local
DNS.4 = kubernetes.default.svc
IP.1 =
10.17.0.7
IP.2 =
10.96.0.1
EOF
IP.1 IP.2 可以通过下面的命令查看
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text|grep "IP Address"
3. 执行如下命令生成csr文件
openssl req -new -key apiserver.key -out apiserver.csr -subj "/CN=kube-apiserver" -config ssl.conf
4. 生成证书文件,执行如下命令
openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver.crt -days 7300 -extensions v3_req -extfile ssl.conf
5. 生成之后将apiserver.crt文件覆盖回原来目录即可
cp apiserver.crt /etc/kubernetes/pki/
6. 修改apiserver-kubelet-client证书
生成csr文件 : openssl req -new -key apiserver-kubelet-client.key -out apiserver-kubelet-client.csr -subj "/CN=apiserver-kubelet-client" -config ssl.conf
生成证书文件: openssl x509 -req -days 7300 -in apiserver-kubelet-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver-kubelet-client.crt -extensions v3_req -extfile ssl.conf
覆盖:cp apiserver-kubelet-client.crt /etc/kubernetes/pki/
7. 更新front-proxy-client证书
生成csr文件 :openssl req -new -key front-proxy-client.key -out front-proxy-client.csr -subj "/CN=front-proxy-client" -config ssl.conf
生成证书文件:openssl x509 -req -days 7300 -in front-proxy-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out front-proxy-client.crt -extensions v3_req -extfile ssl.conf
覆盖:cp front-proxy-client.crt /etc/kubernetes/pki/
8.
检查证书到期时间[master]:
for i in /etc/kubernetes/pki/*.crt;do echo $i; openssl x509 -in $i -text -noout|egrep "Not Before|Not After";echo "-----------";done
-----------
/etc/kubernetes/pki/apiserver.crt
Not Before: Mar 16 03:52:24 2020 GMT
Not After : Mar 11 03:52:24 2040 GMT
-----------
/etc/kubernetes/pki/apiserver-kubelet-client.crt
Not Before: Mar 16 03:41:39 2020 GMT
Not After : Mar 11 03:41:39 2040 GMT
-----------
/etc/kubernetes/pki/front-proxy-client.crt
Not Before: Mar 16 03:50:19 2020 GMT
Not After : Mar 11 03:50:19 2040 GMT
-----------
3. 更新controller-manager.conf和scheduler.conf
1. 使用新的kubeadm创建文件
备注:先删除/etc/kubernetes/下的四个文件
admin.conf
controller-manager.conf
kubelet.conf
scheduler.conf
2 . 使用新的kubeadm创建文件
apiVersion: kubeadm.k8s.io/v1alpha1
kind: MasterConfiguration
api:
advertiseAddress: 192.168.182.131
kubernetesVersion: v1.9.6
kubeadm alpha phase kubeconfig all --config /etc/kubeadm/kubeadm_config.yaml 或者
kubeadm --config /root/kubeadm-config.yaml alpha phase kubeconfig all
4. 重新构建api等组件
1. 重新构建api ,最好分成两次构建,一次性删除api,有可能导致一个也api pod也起不来
#调取静态api_pod
kubeadm alpha phase controlplane all --config=/etc/kubeadm/kubeadm_config.yaml
kubectl get pod -n kube-system -owide |grep api |grep -v $HOSTNAME |awk '{print $1}' |xargs kubectl delete pod -n kube-system
2. 重新构建controller-manager **同上**
kubectl get pod -n kube-system -owide |grep controller-manager |grep -v $HOSTNAME |awk '{print $1}' |xargs kubectl delete pod -n kube-system
3. 重新构建scheduler.conf **同上**
kubectl get pod -n kube-system -owide |grep scheduler |grep -v $HOSTNAME |awk '{print $1}' |xargs kubectl delete pod -n kube-system
4. 重新构建proxy **过滤的时候看下,容易删除云服的某些组件**
kubectl get pod -n kube-system -owide |grep proxy |awk '{print $1}' |xargs kubectl delete pod -n kube-system
5. 重新构建flannel (可不做)
kubectl get pod -n kube-system -owide |grep flannel |awk '{print $1}' |xargs kubectl delete pod -n kube-system
6. 重新构建dns (可不做)
kubectl get pod -n kube-system -owide |grep dns |awk '{print $1}' |xargs kubectl delete pod -n kube-system
### 上述命令也可以分为两步:
查找对应的组件: kubectl get pod -n kube-system -owide |grep
组件名称(
api、
controller-manager、
scheduler、
proxy
)
删除对应的pod: kubeclt delete pod
pod_name
-n kube-system
5. 删除所有节点的/var/lib/kubelel/pki/
rm -rf /var/lib/kubelet/pki/
7. 所有节点重启kubelet (可不做)
systemctl restart kubelet
另外修改源码的文章在我另一篇上面,参考下一篇,修改源码,编译