自建K8S集群认证过期

诗林  金牌会员 | 2025-1-25 12:46:32 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 578|帖子 578|积分 1734

本日使用kubectl下令查看pod信息时,不停正常运行的k8s集群突然不能访问了,输入任何下令都提示以下报错:
Unable to connect to the server: x509: certificate has expired or is not yet valid: current time 2025-01-25T11:35:45+08:00 is after 2024-11-22T23:44:48Z
解决方案:

参考官方文档: kubeadm证书管理 使用下令kubeadm alpha certs 来管理证书:

  • 使用下令kubeadm alpha certs renew all更新证书,返回
  1. [renew] Reading configuration from the cluster...
  2. [renew] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
  3. certificate embedded in the kubeconfig file for the admin to use and for kubeadm itself renewed
  4. certificate for serving the Kubernetes API renewed
  5. certificate the apiserver uses to access etcd renewed
  6. certificate for the API server to connect to kubelet renewed
  7. certificate embedded in the kubeconfig file for the controller manager to use renewed
  8. certificate for liveness probes to healthcheck etcd renewed
  9. certificate for etcd nodes to communicate with each other renewed
  10. certificate for serving etcd renewed
  11. certificate for the front proxy client renewed
  12. certificate embedded in the kubeconfig file for the scheduler manager to use renewed
复制代码

  • 使用如下下令拷贝新生成的设置文件
  1. sudo kubeadm alpha kubeconfig user --client-name=admin --org=system:masters > /tmp/admin.conf
  2. sudo cp /tmp/admin.conf $HOME/.kube/config
  3. sudo chown $(id -u):$(id -g) $HOME/.kube/config
复制代码

  • 重启kubeletsystemctl restart kubelet 即可正常使用K8S集群
  1. [root@k8smaster k8s]# kubectl get po
  2. NAME                             READY   STATUS      RESTARTS   AGE
  3. cron-job-test-1732318920-k2g76   0/1     Completed   0          63d
  4. cron-job-test-1732318980-kcr4x   0/1     Completed   0          63d
  5. cron-job-test-1732319040-b88rf   0/1     Completed   0          63d
复制代码

  • 再次查看证书到期环境
  1. [root@k8smaster k8s]# kubeadm alpha 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 -oyaml'
  4. CERTIFICATE                EXPIRES                  RESIDUAL TIME   CERTIFICATE AUTHORITY   EXTERNALLY MANAGED
  5. admin.conf                 Jan 25, 2026 05:55 UTC   364d                                    no
  6. apiserver                  Jan 25, 2026 05:55 UTC   364d            ca                      no
  7. apiserver-etcd-client      Jan 25, 2026 05:55 UTC   364d            etcd-ca                 no
  8. apiserver-kubelet-client   Jan 25, 2026 05:55 UTC   364d            ca                      no
  9. controller-manager.conf    Jan 25, 2026 05:55 UTC   364d                                    no
  10. etcd-healthcheck-client    Jan 25, 2026 05:55 UTC   364d            etcd-ca                 no
  11. etcd-peer                  Jan 25, 2026 05:55 UTC   364d            etcd-ca                 no
  12. etcd-server                Jan 25, 2026 05:55 UTC   364d            etcd-ca                 no
  13. front-proxy-client         Jan 25, 2026 05:55 UTC   364d            front-proxy-ca          no
  14. scheduler.conf             Jan 25, 2026 05:55 UTC   364d                                    no
  15. CERTIFICATE AUTHORITY   EXPIRES                  RESIDUAL TIME   EXTERNALLY MANAGED
  16. ca                      Nov 20, 2033 23:44 UTC   8y              no
  17. etcd-ca                 Nov 20, 2033 23:44 UTC   8y              no
  18. front-proxy-ca          Nov 20, 2033 23:44 UTC   8y              no
复制代码
留意事项:

官网上给的下令是kubeadm certs check-expiration,标识的k8s版本是V1.15,直接在本地执行该下令报错:
  1. [root@k8smaster k8s]# kubeadm certs check-expiration
  2. unknown command "certs" for "kubeadm"
  3. To see the stack trace of this error execute with --v=5 or higher
复制代码
查了下,我本地的k8s版本是1.19,certs下令放在了 kubeadm alpha下,须要将kubeadm certs 更换为   kubeadm aplha certs 执行即可
  1. [root@k8smaster k8sh]# kubeadm version
  2. kubeadm version: &version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.5", GitCommit:"e338cf2c6d297aa603b50ad3a301f761b4173aa6", GitTreeState:"clean", BuildDate:"2020-12-09T11:16:40Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"linux/amd64"}
复制代码
  1. [root@k8smaster k8s]# kubeadm --help
  2.     ┌──────────────────────────────────────────────────────────┐
  3.     │ KUBEADM                                                  │
  4.     │ Easily bootstrap a secure Kubernetes cluster             │
  5.     │                                                          │
  6.     │ Please give us feedback at:                              │
  7.     │ https://github.com/kubernetes/kubeadm/issues             │
  8.     └──────────────────────────────────────────────────────────┘
  9. Example usage:
  10.     Create a two-machine cluster with one control-plane node
  11.     (which controls the cluster), and one worker node
  12.     (where your workloads, like Pods and Deployments run).
  13.     ┌──────────────────────────────────────────────────────────┐
  14.     │ On the first machine:                                    │
  15.     ├──────────────────────────────────────────────────────────┤
  16.     │ control-plane# kubeadm init                              │
  17.     └──────────────────────────────────────────────────────────┘
  18.     ┌──────────────────────────────────────────────────────────┐
  19.     │ On the second machine:                                   │
  20.     ├──────────────────────────────────────────────────────────┤
  21.     │ worker# kubeadm join <arguments-returned-from-init>      │
  22.     └──────────────────────────────────────────────────────────┘
  23.     You can then repeat the second step on as many other machines as you like.
  24. Usage:
  25.   kubeadm [command]
  26. Available Commands:
  27.   alpha       Kubeadm experimental sub-commands
  28.   completion  Output shell completion code for the specified shell (bash or zsh)
  29.   config      Manage configuration for a kubeadm cluster persisted in a ConfigMap in the                   cluster
  30.   help        Help about any command
  31.   init        Run this command in order to set up the Kubernetes control plane
  32.   join        Run this on any machine you wish to join an existing cluster
  33.   reset       Performs a best effort revert of changes made to this host by 'kubeadm init                  ' or 'kubeadm join'
  34.   token       Manage bootstrap tokens
  35.   upgrade     Upgrade your cluster smoothly to a newer version with this command
  36.   version     Print the version of kubeadm
  37. Flags:
  38.       --add-dir-header           If true, adds the file directory to the header of the lo                  g messages
  39.   -h, --help                     help for kubeadm
  40.       --log-file string          If non-empty, use this log file
  41.       --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)
  42.       --rootfs string            [EXPERIMENTAL] The path to the 'real' host root filesyst                  em.
  43.       --skip-headers             If true, avoid header prefixes in the log messages
  44.       --skip-log-headers         If true, avoid headers when opening log files
  45.   -v, --v Level                  number for the log level verbosity
  46. Use "kubeadm [command] --help" for more information about a command.
  47. [root@k8smaster k8s]# kubectl alpha --help
  48. These commands correspond to alpha features that are not enabled in Kubernetes
  49. clusters by default.
  50. Available Commands:
  51.   debug       Attach a debug container to a running pod
  52. Use "kubectl <command> --help" for more information about a given command.
  53. [root@k8smaster k8s]# kubeadm alpha --help
  54. Kubeadm experimental sub-commands
  55. Usage:
  56.   kubeadm alpha [command]
  57. Available Commands:
  58.   certs       Commands related to handling kubernetes certificates
  59.   kubeconfig  Kubeconfig file utilities
  60.   selfhosting Make a kubeadm cluster self-hosted
  61. Flags:
  62.   -h, --help   help for alpha
  63. Global Flags:
  64.       --add-dir-header           If true, adds the file directory to the header of the log messages
  65.       --log-file string          If non-empty, use this log file
  66.       --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)
  67.       --rootfs string            [EXPERIMENTAL] The path to the 'real' host root filesystem.
  68.       --skip-headers             If true, avoid header prefixes in the log messages
  69.       --skip-log-headers         If true, avoid headers when opening log files
  70.   -v, --v Level                  number for the log level verbosity
  71. Additional help topics:
  72.   kubeadm alpha phase Invoke subsets of kubeadm functions separately for a manual install
  73. Use "kubeadm alpha [command] --help" for more information about a command.
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

诗林

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表