k8s证书配置过期时间(默认1年,修改源码解决)

延长k8s master 证书时间

参考文档: https://blog.51cto.com/11889458/2323328


  • 下载源码并切换分支
git clone https://github.com/kubernetes/kubernetes.git -b release-1.17  v1.17.3

  • 安装go
mkdir /data/soft
cd /data/soft/
wget https://dl.google.com/go/go1.13.6.linux-amd64.tar.gz
tar zxvf go1.13.6.linux-amd64.tar.gz -C /usr/local/

vim /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

source /etc/profile
go version

  • 修改源码
vim /data/v1.17.3/staging/src/k8s.io/client-go/util/cert/cert.go
96         maxAge := time.Hour * 24 * 365 * 10         // one year self-signed certs

vim /data/v1.17.3/cmd/kubeadm/app/constants/constants.go
47         CertificateValidity = time.Hour * 24 * 365 * 10

  • 编译
cd /data/v1.17.3/
KUBE_BUILD_PLATFORMS=linux/amd64 make WHAT=cmd/kubeadm GOFLAGS=-v GOGCFLAGS="-N -l"

  • 将编译出的二进制也执行文件替换到现有系统的kubeadm目录
ls -l /data/v1.17.3/_output/bin/kubeadm
mv /usr/bin/kubeadm /usr/bin/kubeadm_backup
ln -s /data/v1.17.3/_output/bin/kubeadm /usr/bin/kubeadm

  • 重新初始化集群(第一种方法)
kubeadm reset
kubeadm init --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.log
rm -rf $HOME/.kube
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

  • 更新配置(第二种方法)
kubeadm alpha certs renew all --config=/root/kubeadm-config.yaml


  • 验证结果
cd /etc/kubernetes/pki/
openssl x509 -in front-proxy-client.crt   -noout -text  |grep Not
openssl x509 -in apiserver.crt   -noout -text  |grep Not


master重新生成token和证书key

  • master生成token

    默认有效期24小时,若想久一些可以结合--ttl参数,设为0则用不过期

kubeadm token create --print-join-command


  • master生成证书
kubeadm init phase upload-certs --upload-certs


  • 如果是加入master, 需要拼接以上的token和证书的key

    在其他从master上运行如下命令

kubeadm join 172.16.240.100:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:765ed49fc560742ad9e83340bc1264e0caf33fcc0cc11a4d82ad292ceaed6ad7  --control-plane --certificate-key abe665cf4c25f5f9b3f234ca7ac36264dd88aafe2fb337feb32a5e048782daaa

  • 查看token
kubeadm token list

posted @ 2020-03-03 00:40  cjw1219  阅读(2052)  评论(0编辑  收藏  举报