您好,登錄后才能下訂單哦!
環(huán)境介紹:
#centos7.7 #kubernetes 1.16.0
#Step 1:查看證書過期時(shí)間(一年有效期)
openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not ' Not Before: May 24 03:31:50 2019 GMT Not After : May 23 03:31:50 2020 GMT
#Step 2:下載1.16.0源碼
# 下載kubernetes v1.16.0源碼,修改關(guān)鍵部分代碼的證書生產(chǎn)有效期 # github下載源碼:https://github.com/kubernetes/kubernetes # 到release下載想到的版本源碼,這里我下載v1.16.0
# step 3:解壓并且修改源代碼
查看網(wǎng)上的資料主要有兩個(gè)地方需要修改
vim ./staging/src/k8s.io/client-go/util/cert/cert.go # 這個(gè)方法里面NotAfter: now.Add(duration365d * 10).UTC() # 默認(rèn)有效期就是10年,改成50年 func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, error) { now := time.Now() tmpl := x509.Certificate{ SerialNumber: new(big.Int).SetInt64(0), Subject: pkix.Name{ CommonName: cfg.CommonName, Organization: cfg.Organization, }, NotBefore: now.UTC(), // NotAfter: now.Add(duration365d * 10).UTC(), NotAfter: now.Add(duration365d * 50).UTC(), KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, BasicConstraintsValid: true, IsCA: true, } certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &tmpl, &tmpl, key.Public(), key) if err != nil { return nil, err } return x509.ParseCertificate(certDERBytes) } docker run --rm -it -v /root/kubernetes-1.16.0:/go/src/k8s.io/kubernetes \ mirrorgooglecontainers/kube-cross:v1.12.10-1 bash vim cmd/kubeadm/app/util/pkiutil/pki_helpers.go # 這個(gè)方法里面看到NotAfter: time.Now().Add(kubeadmconstants.CertificateValidity).UTC() # 參數(shù)里面是一個(gè)常量kubeadmconstants.CertificateValidity # 所以這里可以不修改,我去看看源碼能不能找到這個(gè)常量的賦值位置 #func NewSignedCert(cfg *certutil.Config, key crypto.Signer, caCert *x509.Certificate, caKey crypto.Signer) (*x509.Certificate, error) { serial, err := cryptorand.Int(cryptorand.Reader, new(big.Int).SetInt64(math.MaxInt64)) # if err != nil { # return nil, err # } # if len(cfg.CommonName) == 0 { # return nil, errors.New("must specify a CommonName") # } # if len(cfg.Usages) == 0 { # return nil, errors.New("must specify at least one ExtKeyUsage") # } # # certTmpl := x509.Certificate{ # Subject: pkix.Name{ # CommonName: cfg.CommonName, # Organization: cfg.Organization, # }, # DNSNames: cfg.AltNames.DNSNames, # IPAddresses: cfg.AltNames.IPs, # SerialNumber: serial, # NotBefore: caCert.NotBefore, # NotAfter: time.Now().Add(kubeadmconstants.CertificateValidity).UTC(), # KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, # ExtKeyUsage: cfg.Usages, # } # certDERBytes, err := x509.CreateCertificate(cryptorand.Reader, &certTmpl, caCert, key.Public(), caKey) # if err != nil { # return nil, err # } # return x509.ParseCertificate(certDERBytes) #} 結(jié)果在這里找到kubeadmconstants.CertificateValidity的定義 vim ./cmd/kubeadm/app/constants/constants.go // 就是這個(gè)常量定義CertificateValidity,我改成*50年 const ( // KubernetesDir is the directory Kubernetes owns for storing various configuration files KubernetesDir = "/etc/kubernetes" // ManifestsSubDirName defines directory name to store manifests ManifestsSubDirName = "manifests" // TempDirForKubeadm defines temporary directory for kubeadm // should be joined with KubernetesDir. TempDirForKubeadm = "tmp" // CertificateValidity defines the validity for all the signed certificates generated by kubeadm // CertificateValidity = time.Hour * 24 * 365 CertificateValidity = time.Hour * 24 * 365 * 50 // CACertAndKeyBaseName defines certificate authority base name CACertAndKeyBaseName = "ca" // CACertName defines certificate name CACertName = "ca.crt" // CAKeyName defines certificate name CAKeyName = "ca.key" 源代碼改好了,接下來就是編譯kubeadm了
#Step 4:編譯kubeadm
剛開始嘗試服務(wù)器安裝yum -y install go環(huán)境,執(zhí)行make方法編譯,發(fā)現(xiàn)特別慢,而且編譯顯示的內(nèi)容也不對(get "gotest.tools/gotestsum":found meta tag get.metaImport{...}等內(nèi)容),(也可能是golang版本是13不支持的原因但是沒有測試)只能嘗試下載容器,在容器里面編譯,通過網(wǎng)上找資料,發(fā)現(xiàn)官網(wǎng)原來有提供一個(gè)k8s.gcr.io/kube-cross的容器用于對代碼做編譯。
由于我不能***,如果不能***的用戶,到https://hub.docker.com搜索 kube-cross關(guān)鍵字,我找了一個(gè)版本比較新的mirrorgooglecontainers/kube-cross:v1.12.10-1鏡像,v1.12.10-1應(yīng)該就是鏡像里面go環(huán)境的版本。
注:在使用mirrorgooglecontainers/kube-cross:v1.12.10-1編譯之前,我試過下載一個(gè)go版本是v.1.11.x的鏡像,編譯會報(bào)錯,大概意思是我下載的kubernetes源碼必須用v1.12.x才能編譯。
拉取鏡像 docker pull mirrorgooglecontainers/kube-cross:v1.12.10-1 # 運(yùn)行容器,并進(jìn)入到容器內(nèi)部 docker run --rm -it -v 你修改源碼后的kubernetes根目錄:/go/src/k8s.io/kubernetes \ mirrorgooglecontainers/kube-cross:v1.12.10-1 bash #例如我的源代碼放到了/root/kubernetes-1.16.0下 docker run --rm -it -v /root/kubernetes-1.16.0:/go/src/k8s.io/kubernetes \ mirrorgooglecontainers/kube-cross:v1.12.10-1 bash # cd到容器內(nèi)部的掛載路徑,可以ls -al查看一下里面的文件是不是主機(jī)掛載目錄的源碼文件 cd /go/src/k8s.io/kubernetes # 編譯kubeadm, 這里主要編譯kubeadm 即可 make all WHAT=cmd/kubeadm GOFLAGS=-v #部分編譯過程節(jié)選,用make編譯不出現(xiàn)以下過程的可能是出問題了 #k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/vsphere/vclib/diskmanagers #k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/vsphere #k8s.io/kubernetes/vendor/github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/mock #k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/gce #k8s.io/kubernetes/pkg/volume/vsphere_volume #k8s.io/kubernetes/pkg/cloudprovider/providers #k8s.io/kubernetes/pkg/volume/gcepd #。。。。。。。。 # 編譯kubelet # make all WHAT=cmd/kubelet GOFLAGS=-v # 編譯kubectl # make all WHAT=cmd/kubectl GOFLAGS=-v #編譯完產(chǎn)物在 _output/bin/kubeadm 目錄下, #其中bin是使用了軟連接 #真實(shí)路徑是_output/local/bin/linux/amd64/kubeadm 編譯成功后,可以退出容器,能看到掛載路徑中已經(jīng)有編譯好的kubeadm 路徑./_output/local/bin/linux/amd64/kubeadm #例:/root/kubernetes-1.16.0/_output/local/bin/linux/amd64
#Step 5:替換掉正在使用的kubeadm
cp /usr/bin/kubeadm /usr/bin/kubeadm.bak cp _output/local/bin/linux/amd64/kubeadm /usr/bin/kubeadm
#Step 6:執(zhí)行命令更新證書
可以先備份證書,證書在/etc/kubernetes/pki,我這里就不備份了 1、檢查證書到期時(shí)間 kubeadm alpha certs check-expiration CERTIFICATE EXPIRES RESIDUAL TIME EXTERNALLY MANAGED admin.conf Nov 20, 2069 04:30 UTC 49y no apiserver Nov 20, 2069 04:30 UTC 49y no apiserver-etcd-client Nov 20, 2069 04:30 UTC 49y no apiserver-kubelet-client Nov 20, 2069 04:30 UTC 49y no controller-manager.conf Nov 20, 2069 04:30 UTC 49y no etcd-healthcheck-client Nov 20, 2069 04:30 UTC 49y no etcd-peer Nov 20, 2069 04:30 UTC 49y no etcd-server Nov 20, 2069 04:30 UTC 49y no front-proxy-client Nov 20, 2069 04:30 UTC 49y no scheduler.conf Nov 20, 2069 04:30 UTC 49y no #使用renew命令即可更新證書,續(xù)訂全部證書(適用于1.14版本及以上:需要外網(wǎng)環(huán)境) #***如果是在斷網(wǎng)的情況下必須把有效期改好,再部署設(shè)備,否則后期證書過期手動更新證書非常麻煩,切記,切記,切記 kubeadm alpha certs renew all #如果你是舊版 kubeadm版本,那么可以用這個(gè)腳本來更新你的證書,生成證書默認(rèn)有效期為10年(3650天),你可以更改腳本里面的CAER_DAYS變量來達(dá)到你想要的證書有效期,單位是“天” #下載地址:https://github.com/yuyicai/update-kube-cert/blob/master/README.md #更新kubeadm生成的證書有效期為10年
低版本證書手動更新
#下面為低版本更新僅供參考沒有測試 #更新證書(適用于1.13版本及以下) #在1.13版本及之前,需要使用kubeadm alpha phase certs來生成新的證書 # #移動老的證書 #注意是: 必須移動,不然會使用現(xiàn)有的證書,不會重新生成! # #cd /etc/kubernetes #mkdir -p pki.bak/etcd #mkdir conf.bak #mv pki/apiserver* ./pki.bak/ #mv pki/front-proxy-client.* ./pki.bak/ #mv pki/etcd/healthcheck-client.* ./pki.bak/etcd/ #mv pki/etcd/peer.* ./pki.bak/etcd/ #mv pki/etcd/server.* ./pki.bak/etcd/ #mv ./admin.conf ./conf.bak/ #mv ./kubelet.conf ./conf.bak/ #mv ./controller-manager.conf ./conf.bak/ #mv ./scheduler.conf ./conf.bak/ #注意ca的不動! # #生成新的證書 #建議不要重新生成ca證書,因?yàn)楦铝薱a證書,集群節(jié)點(diǎn)就需要手工操作,才能讓集群正常(會涉及重新join) # #kubeadm alpha phase certs etcd-healthcheck-client --config /tmp/cluster.yaml # #kubeadm alpha phase certs etcd-peer --config /tmp/cluster.yaml # #kubeadm alpha phase certs etcd-server --config /tmp/cluster.yaml # #kubeadm alpha phase certs front-proxy-client--config /tmp/cluster.yaml # #kubeadm alpha phase certs apiserver-etcd-client --config /tmp/cluster.yaml # #kubeadm alpha phase certs apiserver-kubelet-client --config /tmp/cluster.yaml # #kubeadm alpha phase certs apiserver --config /tmp/cluster.yaml # #kubeadm alpha phase certs sa --config /tmp/cluster.yaml #更新kubeconfig文件 #生成新的配置文件 # #kubeadm alpha phase kubeconfig all --apiserver-advertise-address=${MASTER_API_SERVER_IP} #將新生成的admin配置文件覆蓋掉原本的admin文件 # #mv $HOME/.kube/config $HOME/.kube/config.old #cp -i /etc/kubernetes/admin.conf $HOME/.kube/config #chown $(id -u):$(id -g) $HOME/.kube/config #sudo chmod 777 $HOME/.kube/config #完成后重啟kube-apiserver,kube-controller,kube-scheduler,etcd這4個(gè)容器 # #如果有多臺master,則將第一臺生成的相關(guān)證書拷貝到其余master即可。 # #查看證書更新后的使用周期 ## openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -text |grep ' Not ' # Not Before: May 24 03:31:50 2019 GMT # Not After : Sep 9 02:36:46 2020 GMT # #參考 #https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-alpha/
證書更新完畢就不用擔(dān)心k8s證書過期問題了。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。