如何使用kubeadm管理證書?

kubeadm管理證書

在管理證書以前,你須要瞭解kubernetes如何使用PKI證書的相關知識:官方文檔node

檢查證書到期時間

check-expiration 可用於檢查證書過時時間:api

kubeadm alpha certs check-expiration

輸出以下內容;安全

CERTIFICATE                EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
admin.conf                 Oct 06, 2020 03:56 UTC   364d            no      
apiserver                  Oct 06, 2020 10:41 UTC   364d            no      
apiserver-etcd-client      Oct 06, 2020 03:55 UTC   364d            no      
apiserver-kubelet-client   Oct 06, 2020 03:55 UTC   364d            no      
controller-manager.conf    Oct 06, 2020 03:56 UTC   364d            no      
etcd-healthcheck-client    Oct 02, 2020 12:14 UTC   361d            no      
etcd-peer                  Oct 02, 2020 12:14 UTC   361d            no      
etcd-server                Oct 02, 2020 12:14 UTC   361d            no      
front-proxy-client         Oct 06, 2020 03:55 UTC   364d            no      
scheduler.conf             Oct 06, 2020 03:56 UTC   364d            no

該命令顯示了 全部證書的到期/剩餘時間,包括在etc/kubernetes/pki目錄下的客戶端證書及由kubeadm嵌入到KUBECONFIG文件中的客戶端證書(admin.conf,controller-manager.conf和scheduler.conf)。app

注意:ide

  1. kubelet.conf未包含在上面的列表中,由於kubeadm將已將其配置爲自動更新。
  2. kubeadm沒法管理由外部CA簽名的證書。

自動續訂證書

自動續訂指的是,在用kubeadm升級控制平面時 自動更新全部證書。ui

若是對證書續約沒有要求,並按期升級kubernetes版本,每次升級間隔時間少於1年,最佳作法是常常升級集羣以確保安全。this

若是不想在升級集羣時續約證書,則給 kubeadm upgrade apply 或 kubeadm upgrade node 傳遞參數:--certificate-renewal=falserest

手動續訂證書

使用 kubeadm alpha certs renew 命令 能夠隨時手動續訂證書,該命令使用存儲在/etc/kubernetes/pki中的 CA (or front-proxy-CA)證書和密鑰來更新證書。code

若是是HA集羣,則在全部控制平面執行server

kubeadm alpha certs 命令詳解:

Available Commands:
  certificate-key  生成證書和key
  check-expiration  檢測證書過時時間
  renew            續訂Kubernetes集羣的證書

用的最多的續訂證書的 renew子命令,如今來看下該命令幫助:

root@k8s-master:~# kubeadm  alpha certs renew -h
This command is not meant to be run on its own. See list of available subcommands.

Usage:
  kubeadm alpha certs renew [flags]
  kubeadm alpha certs renew [command]

Available Commands:
  admin.conf               Renew the certificate embedded in the kubeconfig file for the admin to use and for kubeadm itself
  all                      Renew all available certificates
  apiserver                Renew the certificate for serving the Kubernetes API
  apiserver-etcd-client    Renew the certificate the apiserver uses to access etcd
  apiserver-kubelet-client Renew the certificate for the API server to connect to kubelet
  controller-manager.conf  Renew the certificate embedded in the kubeconfig file for the controller manager to use
  etcd-healthcheck-client  Renew the certificate for liveness probes to healtcheck etcd
  etcd-peer                Renew the certificate for etcd nodes to communicate with each other
  etcd-server              Renew the certificate for serving etcd
  front-proxy-client       Renew the certificate for the front proxy client
  scheduler.conf           Renew the certificate embedded in the kubeconfig file for the scheduler manager to use

Flags:
  -h, --help   help for renew

Global Flags:
      --log-file string          If non-empty, use this log file
      --log-file-max-size uint   Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
      --rootfs string            [EXPERIMENTAL] The path to the 'real' host root filesystem.
      --skip-headers             If true, avoid header prefixes in the log messages
      --skip-log-headers         If true, avoid headers when opening log files
  -v, --v Level                  number for the log level verbosity

如上所知,指定某個證書就能續訂該證書,指定 all
則續訂全部證書。

命令執行後,注意:

  1. 不管證書的到期時間如何,都會無條件地續訂一年。
  2. 證書的SAN等信息基於原證書,無需再次提供。
  3. renew執行後,爲使更改生效,須要重啓控制平面組件。

kubeadm alpha certs命令僅支持v1.15及其以上的版本。

示例: 手動續訂apiserver的證書-apiserver.crt

從上面檢測中知道,當前 apiserver.crt 到期時間是 Oct 06, 2020 03:55 UTC ,剩餘364d。

1. 執行renew更新:

root@k8s-master:~# kubeadm  alpha certs renew apiserver
certificate for serving the Kubernetes API renewed

2. 重啓控制平面:

重啓kubelet會自動從新建立核心組件

systemctl restart kubelet

3. 驗證:

root@k8s-master:~# kubeadm alpha certs check-expiration
CERTIFICATE                EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
apiserver                  Oct 06, 2020 10:41 UTC   364d            no

apiserver證書到期時間發生了變化, 不過不是順延一年, 而是 從你 執行renew成功的時間開始續簽一年。

若是要將全部證書續簽一年,則執行:

kubeadm  alpha certs renew all

使用外部CA續訂證書

經過外部CA簽發證書,須要kubeadm 生成一個CSR提交給CA。

1. 生成CSR和私鑰:

kubeadm alpha certs renew apiserver --csr-only  --csr-dir /tmp/apiserver.csr
  • --csr-only:僅生成CSR。
  • --csr-dir:生成的CSR和私鑰文件保存在哪裏,默認保存在/etc/kubernetes/pki

2. 查看CSR和私鑰:

命令輸出結果中提供了CSR和私鑰。

root@k8s-master:~# ls /tmp/apiserver.csr/
apiserver.csr  apiserver.key

3. 使用該私鑰到CA上請求籤發證書。

將頒發的證書及私鑰複製到PKI目錄/etc/kubernetes/pki中。

相關文章
相關標籤/搜索