• 云原生|kubernetes|kubeadm部署的集群的100年证书


    前言:

    首先,先看看minikube这样的开发或者测试使用的kubernetes集群的证书时间:

    1. [root@node3 ~]# kubeadm certs check-expiration
    2. [check-expiration] Reading configuration from the cluster...
    3. [check-expiration] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
    4. [check-expiration] Error reading configuration from the Cluster. Falling back to default configuration
    5. CERTIFICATE EXPIRES RESIDUAL TIME CERTIFICATE AUTHORITY EXTERNALLY MANAGED
    6. admin.conf Dec 04, 2023 12:07 UTC 363d ca no
    7. apiserver Dec 03, 2025 12:02 UTC 2y ca no
    8. apiserver-etcd-client Dec 04, 2023 12:07 UTC 363d etcd-ca no
    9. apiserver-kubelet-client Dec 04, 2023 12:07 UTC 363d ca no
    10. controller-manager.conf Dec 04, 2023 13:00 UTC 363d ca no
    11. etcd-healthcheck-client Dec 04, 2023 12:07 UTC 363d etcd-ca no
    12. etcd-peer Dec 04, 2023 12:07 UTC 363d etcd-ca no
    13. etcd-server Dec 04, 2023 12:07 UTC 363d etcd-ca no
    14. front-proxy-client Dec 04, 2023 12:07 UTC 363d front-proxy-ca no
    15. scheduler.conf Dec 04, 2023 13:00 UTC 363d ca no
    16. CERTIFICATE AUTHORITY EXPIRES RESIDUAL TIME EXTERNALLY MANAGED
    17. ca Dec 01, 2032 12:02 UTC 9y no
    18. etcd-ca Dec 01, 2032 12:07 UTC 9y no
    19. front-proxy-ca Dec 01, 2032 12:07 UTC 9y no

    OK,我们可以看到,这些证书的时间大部分都是一年期的。对于minikube这样的集群,无所谓喽,集群本来就是测试性质的,大不了重新部署了,也是非常快的,但,在生产环境下,我们追求的是稳定高效,当然可以使用kubeadm certs renew all来续订证书,但是证书更新了那些服务如果要重启就很麻烦,并且如果不是一个集群的证书要续订,而是有N个集群的证书续订,那可就有得忙了。

    因此,我们在生产环境部署集群的时候,如果提前就把证书的时间修改为10年或者更长的100年,会规避掉一些麻烦,也算是提前解决一个可能会对生产造成影响的问题。

    那么,如何在部署阶段就修改证书的时间呢?

    其实也比较简单,在部署前就利用kubernetes的源码编译出一个新的kubeadm即可了。

    实操:

    工具原材料:

    1,kubernetes的源码

    2,go语言环境

    目标:

    假设生产环境使用的kubernetes版本是1.23.12版本,通过go语言环境,利用kubernetes-1.23.12的源码,重新编译出一个新的kubeadm程序,在kubeadm init 初始化前,用新的kubeadm替换原有的kubeadm,使得kubernetes的证书期限是100年。

    一,

    go语言环境的安装部署

    首先申明,其它版本的编译安装没有试过,反正1.23.12版本的kubernetes需要go-1.17版本以上,否则不能正常编译,会报错:

    1. [root@node4 kubernetes-1.23.12]# make all WHAT=cmd/kubeadm GOFLAGS=-v
    2. Detected go version: go version go1.16.12 linux/amd64.
    3. Kubernetes requires go1.17.0 or greater.
    4. Please install go1.17.0 or later.
    5. !!! [1205 23:21:50] Call tree:
    6. !!! [1205 23:21:50] 1: hack/run-in-gopath.sh:31 kube::golang::setup_env(...)
    7. Detected go version: go version go1.16.12 linux/amd64.
    8. Kubernetes requires go1.17.0 or greater.
    9. Please install go1.17.0 or later.
    10. !!! [1205 23:21:50] Call tree:
    11. !!! [1205 23:21:50] 1: /root/kubernetes-1.23.12/hack/lib/golang.sh:794 kube::golang::setup_env(...)
    12. !!! [1205 23:21:50] 2: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
    13. !!! [1205 23:21:50] Call tree:
    14. !!! [1205 23:21:50] 1: hack/make-rules/build.sh:27 kube::golang::build_binaries(...)
    15. make[1]: *** [_output/bin/prerelease-lifecycle-gen] Error 1
    16. make: *** [generated_files] Error 2

    因此,go语言版本选择的是1.17.1,部署步骤如下;

    1,

    下载go语言安装包

    wget https://studygolang.com/dl/golang/go1.17.1.linux-amd64.tar.gz

    2,

    解压,解压后的文件移动到/usr/local/目录下:

    1. tar zxf go1.17.1.linux-amd64.tar.gz
    2. mv go /usr/local/

    3,

    设置环境变量并激活变量

    编辑/etc/profile 文件,在末尾添加如下内容:

    1. export GOROOT=/usr/local/go
    2. export PATH=$PATH:/usr/local/go/bin
    3. export GOPATH=/go

    激活环境变量:

    source /etc/profile

    4,

    验证go语言环境

    1. [root@node3 ~]# go version
    2. go version go1.17.1 linux/amd64

    二,

    下载kubernetes-1.23.12源码

    https://codeload.github.com/kubernetes/kubernetes/zip/refs/tags/v1.23.12

    下载下来的文件上传到服务器解压后,进入解压目录:

    1. [root@node3 kubernetes-1.23.12]# pwd
    2. /root/kubernetes-1.23.12
    3. [root@node3 kubernetes-1.23.12]# ls
    4. api CHANGELOG cluster code-of-conduct.md docs go.sum LICENSE logo Makefile.generated_files OWNERS pkg README.md staging test vendor
    5. build CHANGELOG.md cmd CONTRIBUTING.md go.mod hack LICENSES Makefile _output OWNERS_ALIASES plugin SECURITY_CONTACTS SUPPORT.md third_party

    三,

    修改源码的证书相关文件(两个文件):

    vim cmd/kubeadm/app/constants/constants.go

    以关键字time.Hour 搜索,修改成如下(加个100,原来是只有time.Hour * 24 * 365):

    1. // CertificateValidity defines the validity for all the signed certificates generated by kubeadm
    2. CertificateValidity = time.Hour * 24 * 365 * 100
    vim staging/src/k8s.io/client-go/util/cert/cert.go

    以关键字KeyUsageDigitalSignatur 搜索,修改成如下(10改成100,原来是now.Add(duration365d * 10)): 

    1. func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, error) {
    2. now := time.Now()
    3. tmpl := x509.Certificate{
    4. SerialNumber: new(big.Int).SetInt64(0),
    5. Subject: pkix.Name{
    6. CommonName: cfg.CommonName,
    7. Organization: cfg.Organization,
    8. },
    9. DNSNames: []string{cfg.CommonName},
    10. NotBefore: now.UTC(),
    11. NotAfter: now.Add(duration365d * 100).UTC(),
    12. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
    13. BasicConstraintsValid: true,
    14. IsCA: true,
    15. }

    以上修改请务必以关键字准确定位。

    四,

    重新编译kubeadm

    make all WHAT=cmd/kubeadm GOFLAGS=-v

    输出如下:

    1. k8s.io/kubernetes/vendor/github.com/spf13/pflag
    2. k8s.io/kubernetes/hack/make-rules/helpers/go2make
    3. +++ [1205 23:50:42] Building go targets for linux/amd64:
    4. ./vendor/k8s.io/code-generator/cmd/prerelease-lifecycle-gen
    5. > non-static build: k8s.io/kubernetes/./vendor/k8s.io/code-generator/cmd/prerelease-lifecycle-gen
    6. k8s.io/kubernetes/vendor/golang.org/x/mod/semver
    7. k8s.io/kubernetes/vendor/golang.org/x/sys/execabs
    8. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/event/label
    9. k8s.io/kubernetes/vendor/golang.org/x/xerrors/internal
    10. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/event/keys
    11. k8s.io/kubernetes/vendor/golang.org/x/xerrors
    12. k8s.io/kubernetes/vendor/golang.org/x/mod/module
    13. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/event/core
    14. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/event
    15. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/gocommand
    16. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/typeparams
    17. k8s.io/kubernetes/vendor/golang.org/x/tools/go/ast/astutil
    18. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/fastwalk
    19. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/gopathwalk
    20. k8s.io/kubernetes/vendor/k8s.io/gengo/types
    21. k8s.io/kubernetes/vendor/k8s.io/gengo/namer
    22. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/imports
    23. k8s.io/kubernetes/vendor/github.com/go-logr/logr
    24. k8s.io/kubernetes/vendor/k8s.io/klog/v2
    25. k8s.io/kubernetes/vendor/k8s.io/gengo/parser
    26. k8s.io/kubernetes/vendor/k8s.io/gengo/examples/set-gen/sets
    27. k8s.io/kubernetes/vendor/golang.org/x/tools/imports
    28. k8s.io/kubernetes/vendor/golang.org/x/tools/go/internal/gcimporter
    29. k8s.io/kubernetes/vendor/k8s.io/gengo/generator
    30. k8s.io/kubernetes/vendor/k8s.io/gengo/args
    31. k8s.io/kubernetes/vendor/k8s.io/code-generator/cmd/prerelease-lifecycle-gen/prerelease-lifecycle-generators
    32. k8s.io/kubernetes/vendor/k8s.io/code-generator/cmd/prerelease-lifecycle-gen/args
    33. k8s.io/kubernetes/vendor/golang.org/x/tools/go/internal/packagesdriver
    34. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/packagesinternal
    35. k8s.io/kubernetes/vendor/golang.org/x/tools/internal/typesinternal
    36. k8s.io/kubernetes/vendor/golang.org/x/tools/go/gcexportdata
    37. k8s.io/kubernetes/vendor/golang.org/x/tools/go/packages
    38. k8s.io/kubernetes/vendor/k8s.io/code-generator/pkg/util
    39. k8s.io/kubernetes/vendor/k8s.io/code-generator/cmd/prerelease-lifecycle-gen
    40. Generating prerelease lifecycle code for 28 targets
    41. +++ [1205 23:50:52] Building go targets for linux/amd64:
    42. ./vendor/k8s.io/code-generator/cmd/deepcopy-gen
    43. 后面的略略略

    这个编译还是比较快的,等编译完成后,echo $? 看看有没有报错,如果是0,表示编译完成了。

    那么,编译出的kubeadm在 _output/bin 目录下:

    1. /root/kubernetes-1.23.12/_output/bin
    2. [root@node4 bin]# ll
    3. total 79020
    4. -rwxr-xr-x 1 root root 6270976 Dec 5 23:51 conversion-gen
    5. -rwxr-xr-x 1 root root 5996544 Dec 5 23:50 deepcopy-gen
    6. -rwxr-xr-x 1 root root 6000640 Dec 5 23:51 defaulter-gen
    7. -rwxr-xr-x 1 root root 3375951 Dec 5 23:50 go2make
    8. -rwxr-xr-x 1 root root 45191168 Dec 5 23:56 kubeadm
    9. -rwxr-xr-x 1 root root 8114176 Dec 5 23:52 openapi-gen
    10. -rwxr-xr-x 1 root root 5963776 Dec 5 23:50 prerelease-lifecycle-gen

    五,

    测试编程出来的kubeadm 初始化集群,集群的证书是否变为了100年

    将以上生成的kubeadm文件拷贝到一个新的服务器上,随便怎么拷贝吧,scp也好,直接lsrzs也可以。

    添加yum源,安装kubeadm:

    1. cat >/etc/yum.repos.d/kubernetes.repo <<EOF
    2. [kubernetes]
    3. name=Kubernetes
    4. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
    5. enabled=1
    6. gpgcheck=0
    7. repo_gpgcheck=0
    8. gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
    9. EOF

    安装命令:

    yum install kubeadm-1.23.12 kubelet-1.23.12 kubectl-1.23.12 -y

    安装完毕后,将原来的kubeadm备份一哈;

    cp /usr/bin/kubeadm{,.bak}

    将新的kubeadm拷贝到/usr/bin/下:

    cp -f kubeadm /usr/bin/

    因为是测试性质,因此,随便初始化一下:

    1. kubeadm init \
    2. --image-repository registry.aliyuncs.com/google_containers \
    3. --apiserver-advertise-address=192.168.217.24 \
    4. --service-cidr=10.96.0.0/16 \
    5. --pod-network-cidr=10.244.0.0/16 \
    6. --kubernetes-version=1.23.12

    等待初始化完成后,再次查看证书期限:

    1. [root@node4 yum.repos.d]# kubeadm certs check-expiration
    2. [check-expiration] Reading configuration from the cluster...
    3. [check-expiration] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
    4. CERTIFICATE EXPIRES RESIDUAL TIME CERTIFICATE AUTHORITY EXTERNALLY MANAGED
    5. admin.conf Nov 11, 2122 14:48 UTC 99y ca no
    6. apiserver Nov 11, 2122 14:48 UTC 99y ca no
    7. apiserver-etcd-client Nov 11, 2122 14:48 UTC 99y etcd-ca no
    8. apiserver-kubelet-client Nov 11, 2122 14:48 UTC 99y ca no
    9. controller-manager.conf Nov 11, 2122 14:48 UTC 99y ca no
    10. etcd-healthcheck-client Nov 11, 2122 14:48 UTC 99y etcd-ca no
    11. etcd-peer Nov 11, 2122 14:48 UTC 99y etcd-ca no
    12. etcd-server Nov 11, 2122 14:48 UTC 99y etcd-ca no
    13. front-proxy-client Nov 11, 2122 14:48 UTC 99y front-proxy-ca no
    14. scheduler.conf Nov 11, 2122 14:48 UTC 99y ca no
    15. CERTIFICATE AUTHORITY EXPIRES RESIDUAL TIME EXTERNALLY MANAGED
    16. ca Nov 11, 2122 14:48 UTC 99y no
    17. etcd-ca Nov 11, 2122 14:48 UTC 99y no
    18. front-proxy-ca Nov 11, 2122 14:48 UTC 99y no

    OK,证书的过期时间都是100年了,说明前面的编译工作是有效果的,可行的。

    如果是在生产上,在也不用担心证书过期的问题了,也算是提前解决了一个暴雷问题。

  • 相关阅读:
    【推荐系统】推荐基础算法-基于稀疏自编码的推荐方法
    Conflux国产公链注册流程
    深度解析SpringBoot内嵌Web容器
    不改平面不加层,微调走线抬电平
    AI带你省钱旅游!精准预测民宿房源价格! ⛵
    Allegro Design Entry HDL(OrCAD Capture HDL)RF-PCB菜单详细介绍
    Android 性能优化(六):启动优化的详细流程
    前嗅百科 | 这10个科学常识竟然都不是真的?
    Alter database open fails with ORA-00600 kcratr_nab_less_than_odr
    11.11 - 每日一题 - 408
  • 原文地址:https://blog.csdn.net/alwaysbefine/article/details/128194904