未完待测…………………………..

kubeadm集群默认证书是1年,如果在部署前更换的直接修改源代码,然后编译即可。如果已经部署了,需要更新证书,则可以参考下面的方法。

集群信息,单master

  1. # kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready master 118d v1.17.2
  4. k8s-node01 Ready node01 118d v1.17.2
  5. k8s-node02 Ready node02 118d v1.17.2

获取现在集群的证书信息

获取apiserver的证书信息
  1. # openssl x509 -noout -text -in /etc/kubernetes/pki/apiserver.crt
  2. ....
  3. X509v3 extensions:
  4. X509v3 Key Usage: critical
  5. Digital Signature, Key Encipherment
  6. X509v3 Extended Key Usage:
  7. TLS Web Server Authentication
  8. X509v3 Subject Alternative Name:
  9. DNS:k8s-master, DNS:kubernetes, DNS:kubernetes.default, DNS:kubernetes.default.svc, DNS:kubernetes.default.svc.cluster.local, IP Address:10.96.0.1, IP Address:10.1.10.128
  10. ....

从上面得到签发的DNS和IP详情,如下:

  1. DNS.1=k8s-master
  2. DNS.2=kubernetes
  3. DNS.3=kubernetes.default
  4. DNS.4=kubernetes.default.svc
  5. DNS.5=kubernetes.default.svc.cluster.local
  6. IP.1=10.96.0.1
  7. IP.2=10.1.10.128

获取ectd的证书详情
  1. # openssl x509 -noout -text -in /etc/kubernetes/pki/etcd/server.crt
  2. .....
  3. X509v3 extensions:
  4. X509v3 Basic Constraints:
  5. CA:FALSE
  6. X509v3 Key Usage: critical
  7. Digital Signature, Key Encipherment
  8. X509v3 Extended Key Usage:
  9. TLS Web Server Authentication, TLS Web Client Authentication
  10. X509v3 Subject Alternative Name:
  11. DNS:k8s-master, DNS:localhost, IP Address:127.0.0.1, IP Address:10.1.10.128, IP Address:0:0:0:0:0:0:0:1
  12. .....

从上面得到签发的DNS和IP详情,如下:

  1. DNS.1=k8s-master
  2. DNS.2=localhost
  3. IP.1=127.0.0.1
  4. IP.2=10.1.10.128
  5. IP.3=0:0:0:0:0:0:0:1

创建证书

我们只需要在一个节点上进行证书生成,生成的证书分发到其他节点即可。 我们创建一个目录用于保存新建的证书文件,mkdir /root/k8s/newssl -p && cd `` /root/k8s/newssl

创建CA服务端证书签名请求配置文件openssl.conf
  1. [ req ]
  2. default_bits = 2048
  3. default_md = sha256
  4. distinguished_name = req_distinguished_name
  5. [req_distinguished_name]
  6. [ v3_ca ]
  7. basicConstraints = critical, CA:TRUE
  8. keyUsage = critical, digitalSignature, keyEncipherment, keyCertSign
  9. [ v3_req_server ]
  10. basicConstraints = CA:FALSE
  11. keyUsage = critical, digitalSignature, keyEncipherment
  12. extendedKeyUsage = serverAuth
  13. [ v3_req_client ]
  14. basicConstraints = CA:FALSE
  15. keyUsage = critical, digitalSignature, keyEncipherment
  16. extendedKeyUsage = clientAuth
  17. [ v3_req_apiserver ]
  18. basicConstraints = CA:FALSE
  19. keyUsage = critical, digitalSignature, keyEncipherment
  20. extendedKeyUsage = serverAuth
  21. subjectAltName = @alt_names_cluster
  22. [ v3_req_etcd ]
  23. basicConstraints = CA:FALSE
  24. keyUsage = critical, digitalSignature, keyEncipherment
  25. extendedKeyUsage = serverAuth, clientAuth
  26. subjectAltName = @alt_names_etcd
  27. [ alt_names_cluster ]
  28. DNS.1=k8s-master
  29. DNS.2=kubernetes
  30. DNS.3=kubernetes.default
  31. DNS.4=kubernetes.default.svc
  32. DNS.5=kubernetes.default.svc.cluster.local
  33. IP.1=10.96.0.1
  34. IP.2=10.1.10.128
  35. [ alt_names_etcd ]
  36. DNS.1=k8s-master
  37. DNS.2=localhost
  38. IP.1=127.0.0.1
  39. IP.2=10.1.10.128
  40. IP.3=0:0:0:0:0:0:0:1

注意替换alt_names_cluster和alt_names_etcd的内容

创建集群的key和CA

需要创建的CA信息如下:

路径 Common Name 描述
ca.crt,key kubernetes Kubernetes general CA
etcd/ca.crt,key kubernetes For all etcd-related functions
front-proxy-ca.crt,key kubernetes For the front-end proxy

要注意 CA 中 CN(Common Name) 与 O(Organization) 等内容是会影响Kubernetes组件认证的。

  • CA (Certificate Authority) 是自签名的根证书,用来签名后续创建的其它证书
  • CN (Common Name), apiserver 会从证书中提取该字段作为请求的用户名 (User Name)
  • O (Organization), apiserver 会从证书中提取该字段作为请求用户所属的组 (Group)

(1)、创建kubernetes CA

  1. openssl genrsa -out ca.key 2048
  2. openssl req -x509 -new -nodes -key ca.key \
  3. -subj "/CN=kubernetes" -config openssl.conf \
  4. -extensions v3_ca -out ca.crt -days 3560

(2)、创建etcd CA

  1. mkdir -p etcd
  2. openssl genrsa -out etcd/ca.key 2048
  3. openssl req -x509 -new -nodes -key etcd/ca.key \
  4. -subj "/CN=kubernetes" -config openssl.conf \
  5. -extensions v3_ca -out etcd/ca.crt -days 3560

(3)、创建front-proxy CA

  1. openssl genrsa -out front-proxy-ca.key 2048
  2. openssl req -x509 -new -nodes -key front-proxy-ca.key \
  3. -subj "/CN=kubernetes" -config openssl.conf \
  4. -extensions v3_ca -out front-proxy-ca.crt -days 3560

创建 Certificates

将要创建的 Certificates有:

Name Key Certificates Common Name Organization
etcd/server etcd/server.key etcd/server.crt master
etcd/peer etcd/peer.key etcd/peer.crt master
etcd/healthcheck-client etcd/healthcheck-client.key etcd/healthcheck-client.crt kube-etcd-healthcheck-client system:masters
apiserver-etcd-client apiserver-etcd-client.key apiserver-etcd-client.crt kube-apiserver-etcd-client system:masters
apiserver apiserver.key apiserver.crt kube-apiserver
apiserver-kubelet-client apiserver-kubelet-client.key apiserver-kubelet-client.crt kube-apiserver-kubelet-client system:masters
front-proxy-client front-proxy-client.key front-proxy-client.crt front-proxy-client
kube-scheduler kube-scheduler.key kube-scheduler.crt system:kube-scheduler
sa(kube-controller-manager) sa.key(sa.pub) kube-controller-manager.crt system:kube-controller-manager
admin(kubectl) admin.key admin.crt kubernetes-admin system:masters
kubelet kubelet.key kubelet.crt system:node:master system:nodes

(1)、创建etcd/server

  1. openssl genrsa -out etcd/server.key 2048
  2. openssl req -new -key etcd/server.key \
  3. -subj "/CN=master" -out etcd/server.csr
  4. openssl x509 -in etcd/server.csr -req -CA etcd/ca.crt \
  5. -CAkey etcd/ca.key -CAcreateserial -extensions v3_req_etcd \
  6. -extfile openssl.conf -out etcd/server.crt -days 3560

(2)、创建etcd/peer

  1. openssl genrsa -out etcd/peer.key 2048
  2. openssl req -new -key etcd/peer.key \
  3. -subj "/CN=master" -out etcd/peer.csr
  4. openssl x509 -in etcd/peer.csr -req -CA etcd/ca.crt \
  5. -CAkey etcd/ca.key -CAcreateserial -extensions v3_req_etcd \
  6. -extfile openssl.conf -out etcd/peer.crt -days 3560

(3)、创建etcd/healthcheck-client

  1. openssl genrsa -out etcd/healthcheck-client.key 2048
  2. openssl req -new -key etcd/healthcheck-client.key \
  3. -subj "/CN=kube-etcd-healthcheck-client/O=system:masters" \
  4. -out etcd/healthcheck-client.csr
  5. openssl x509 -in etcd/healthcheck-client.csr -req -CA etcd/ca.crt \
  6. -CAkey etcd/ca.key -CAcreateserial -extensions v3_req_etcd \
  7. -extfile openssl.conf -out etcd/healthcheck-client.crt -days 3560

(4)、创建apiserver-etcd-client

  1. openssl genrsa -out apiserver-etcd-client.key 2048
  2. openssl req -new -key apiserver-etcd-client.key \
  3. -subj "/CN=kube-apiserver-etcd-client/O=system:masters" \
  4. -out apiserver-etcd-client.csr
  5. openssl x509 -in apiserver-etcd-client.csr -req -CA etcd/ca.crt \
  6. -CAkey etcd/ca.key -CAcreateserial -extensions v3_req_etcd \
  7. -extfile openssl.conf -out apiserver-etcd-client.crt -days 3560

(5)、创建apiserver

  1. openssl genrsa -out apiserver.key 2048
  2. openssl req -new -key apiserver.key \
  3. -subj "/CN=kube-apiserver" -config openssl.conf \
  4. -out apiserver.csr
  5. openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key \
  6. -CAcreateserial -extensions v3_req_apiserver \
  7. -extfile openssl.conf -out apiserver.crt -days 3560

(6)、创建apiserver-kubelet-client

  1. openssl genrsa -out apiserver-kubelet-client.key 2048
  2. openssl req -new -key apiserver-kubelet-client.key \
  3. -subj "/CN=kube-apiserver-kubelet-client/O=system:masters" \
  4. -out apiserver-kubelet-client.csr
  5. openssl x509 -req -in apiserver-kubelet-client.csr -CA ca.crt -CAkey ca.key \
  6. -CAcreateserial -extensions v3_req_client \
  7. -extfile openssl.conf -out apiserver-kubelet-client.crt -days 3560

(7)、创建front-proxy-client

  1. openssl genrsa -out front-proxy-client.key 2048
  2. openssl req -new -key front-proxy-client.key \
  3. -subj "/CN=front-proxy-client" \
  4. -out front-proxy-client.csr
  5. openssl x509 -req -in front-proxy-client.csr -CA front-proxy-ca.crt -CAkey front-proxy-ca.key \
  6. -CAcreateserial -extensions v3_req_client \
  7. -extfile openssl.conf -out front-proxy-client.crt -days 3560

(8)、创建kube-scheduler

  1. openssl genrsa -out kube-scheduler.key 2048
  2. openssl req -new -key kube-scheduler.key \
  3. -subj "/CN=system:kube-scheduler" \
  4. -out kube-scheduler.csr
  5. openssl x509 -req -in kube-scheduler.csr -CA ca.crt -CAkey ca.key \
  6. -CAcreateserial -extensions v3_req_client \
  7. -extfile openssl.conf -out kube-scheduler.crt -days 3560

(9)、创建sa(kube-controller-manager)

  1. openssl genrsa -out sa.key 2048
  2. openssl rsa -in sa.key -pubout -out sa.pub
  3. openssl req -new -key sa.key \
  4. -subj "/CN=system:kube-controller-manager" \
  5. -out kube-controller-manager.csr
  6. openssl x509 -req -in kube-controller-manager.csr -CA ca.crt -CAkey ca.key \
  7. -CAcreateserial -extensions v3_req_client \
  8. -extfile openssl.conf -out kube-controller-manager.crt -days 3560

(10)、创建admin(kubectl)

  1. openssl genrsa -out admin.key 2048
  2. openssl req -new -key admin.key \
  3. -subj "/CN=kubernetes-admin/O=system:masters" \
  4. -out admin.csr
  5. openssl x509 -req -in admin.csr -CA ca.crt -CAkey ca.key \
  6. -CAcreateserial -extensions v3_req_client \
  7. -extfile openssl.conf -out admin.crt -days 3560

(11)、创建kubelet

  1. openssl genrsa -out kubelet.key 2048
  2. # 此处为 master 节点 nodeName,每个 master 生成对应的证书
  3. openssl req -new -key kubelet.key \
  4. -subj "/CN=system:node:k8s-master/O=system:nodes" \
  5. -out kubelet.csr
  6. openssl x509 -req -CA ca.crt -CAkey ca.key \
  7. -CAcreateserial -extensions v3_req_client \
  8. -extfile openssl.conf -days 3560 -in kubelet.csr -out kubelet.crt

生成kubernetes各组件配置文件并应用

需要生成的配置文件列表

配置文件名称 组件证书文件名称 组件秘钥文件名称 根证书文件名称
admin.conf(kubectl) admin.crt admin.key ca.crt
kubelet.conf kubelet.crt kubelet.key ca.crt
scheduler.conf kube-scheduler.crt kube-scheduler.key ca.crt
controller-manager.conf kube-controller-manager.crt sa.key ca.crt

注意:

  • 操作前请先备份原有配置文件
  • 除了kubelet.conf文件需注意配置为对应节点的nodeName,其余配置文件可通用
  • 以下操作请先在一台 master 节点上操作确认没有问题后再进行配置其他节点
  • –certificate-authority:指定根证书
  • –client-certificate、–client-key:指定组件证书及秘钥
  • –embed-certs=true:将组件证书内容嵌入到生成的配置文件中(不加时,写入的是证书文件路径)

(1)、admin.conf(kubectl)

  1. KUBE_APISERVER="https://10.1.10.128:6443"
  2. CLUSTER_NAME="kubernetes"
  3. KUBE_USER="kubernetes-admin"
  4. KUBE_CERT="admin"
  5. KUBE_CONFIG="admin.conf"
  6. # 设置集群参数
  7. kubectl config set-cluster ${CLUSTER_NAME} \
  8. --certificate-authority=ca.crt \
  9. --embed-certs=true \
  10. --server=${KUBE_APISERVER} \
  11. --kubeconfig=${KUBE_CONFIG}
  12. # 设置客户端认证参数
  13. kubectl config set-credentials ${KUBE_USER} \
  14. --client-certificate=${KUBE_CERT}.crt \
  15. --client-key=${KUBE_CERT}.key \
  16. --embed-certs=true \
  17. --kubeconfig=${KUBE_CONFIG}
  18. # 设置上下文参数
  19. kubectl config set-context ${KUBE_USER}@${CLUSTER_NAME} \
  20. --cluster=${CLUSTER_NAME} \
  21. --user=${KUBE_USER} \
  22. --kubeconfig=${KUBE_CONFIG}
  23. # 设置当前使用的上下文
  24. kubectl config use-context ${KUBE_USER}@${CLUSTER_NAME} --kubeconfig=${KUBE_CONFIG}
  25. # 查看生成的配置文件
  26. kubectl config view --kubeconfig=${KUBE_CONFIG}

(2)、kubelet.conf(注意配置对应的nodeName)

  1. KUBE_APISERVER="https://10.1.10.128:6443"
  2. CLUSTER_NAME="default-cluster"
  3. # 此处为 master 节点 nodeName,每个 master 生成对应的 kubelet.conf
  4. KUBE_USER="default-auth"
  5. KUBE_CERT="kubelet"
  6. KUBE_CONFIG="kubelet.conf"
  7. # 设置集群参数
  8. kubectl config set-cluster ${CLUSTER_NAME} \
  9. --certificate-authority=ca.crt \
  10. --embed-certs=true \
  11. --server=${KUBE_APISERVER} \
  12. --kubeconfig=${KUBE_CONFIG}
  13. # 设置客户端认证参数
  14. kubectl config set-credentials ${KUBE_USER} \
  15. --client-certificate=${KUBE_CERT}.crt \
  16. --client-key=kubelet.key \
  17. --embed-certs=true \
  18. --kubeconfig=${KUBE_CONFIG}
  19. # 设置上下文参数
  20. kubectl config set-context ${KUBE_USER}@${CLUSTER_NAME} \
  21. --cluster=${CLUSTER_NAME} \
  22. --user=${KUBE_USER} \
  23. --kubeconfig=${KUBE_CONFIG}
  24. # 设置当前使用的上下文
  25. kubectl config use-context ${KUBE_USER}@${CLUSTER_NAME} --kubeconfig=${KUBE_CONFIG}
  26. # 查看生成的配置文件
  27. kubectl config view --kubeconfig=${KUBE_CONFIG}

(3)、scheduler.conf

  1. KUBE_APISERVER="https://10.1.10.128:6443"
  2. CLUSTER_NAME="kubernetes"
  3. KUBE_USER="system:kube-scheduler"
  4. KUBE_CERT="kube-scheduler"
  5. KUBE_CONFIG="scheduler.conf"
  6. # 设置集群参数
  7. kubectl config set-cluster ${CLUSTER_NAME} \
  8. --certificate-authority=ca.crt \
  9. --embed-certs=true \
  10. --server=${KUBE_APISERVER} \
  11. --kubeconfig=${KUBE_CONFIG}
  12. # 设置客户端认证参数
  13. kubectl config set-credentials ${KUBE_USER} \
  14. --client-certificate=${KUBE_CERT}.crt \
  15. --client-key=${KUBE_CERT}.key \
  16. --embed-certs=true \
  17. --kubeconfig=${KUBE_CONFIG}
  18. # 设置上下文参数
  19. kubectl config set-context ${KUBE_USER}@${CLUSTER_NAME} \
  20. --cluster=${CLUSTER_NAME} \
  21. --user=${KUBE_USER} \
  22. --kubeconfig=${KUBE_CONFIG}
  23. # 设置当前使用的上下文
  24. kubectl config use-context ${KUBE_USER}@${CLUSTER_NAME} --kubeconfig=${KUBE_CONFIG}
  25. # 查看生成的配置文件
  26. kubectl config view --kubeconfig=${KUBE_CONFIG}

(4)、controller-manager.conf

  1. KUBE_APISERVER="https://10.1.10.128:6443"
  2. CLUSTER_NAME="kubernetes"
  3. KUBE_USER="system:kube-controller-manager"
  4. KUBE_CERT="kube-controller-manager"
  5. KUBE_CONFIG="controller-manager.conf"
  6. # 设置集群参数
  7. kubectl config set-cluster ${CLUSTER_NAME} \
  8. --certificate-authority=ca.crt \
  9. --embed-certs=true \
  10. --server=${KUBE_APISERVER} \
  11. --kubeconfig=${KUBE_CONFIG}
  12. # 设置客户端认证参数
  13. kubectl config set-credentials ${KUBE_USER} \
  14. --client-certificate=${KUBE_CERT}.crt \
  15. --client-key=sa.key \
  16. --embed-certs=true \
  17. --kubeconfig=${KUBE_CONFIG}
  18. # 设置上下文参数
  19. kubectl config set-context ${KUBE_USER}@${CLUSTER_NAME} \
  20. --cluster=${CLUSTER_NAME} \
  21. --user=${KUBE_USER} \
  22. --kubeconfig=${KUBE_CONFIG}
  23. # 设置当前使用的上下文
  24. kubectl config use-context ${KUBE_USER}@${CLUSTER_NAME} --kubeconfig=${KUBE_CONFIG}
  25. # 查看生成的配置文件
  26. kubectl config view --kubeconfig=${KUBE_CONFIG}

更新证书

Master节点

(1)、先备份原目录

  1. tar zcvf ~/backup/kubernetes-cert.tar.gz /etc/kubernetes/

(2)、将新的证书文件拷贝到原目录

  1. \cp -rf /root/k8s/newssl/* /etc/kubernetes/pki

(3)、将conf文件移动到上层目录

  1. \mv /etc/kubernetes/pki/*.conf /etc/kubernetes

(4)、重启docker和kubelet

  1. systemctl restart docker
  2. systemctl restart kubelet

(5)、更新准入kubeconfig

  1. cp /etc/kubernetes/admin.conf ~/.kube/config

(6)、查看集群状态

  1. # kubectl get node
  2. NAME STATUS ROLES AGE VERSION
  3. k8s-master Ready master 118d v1.17.2
  4. k8s-node01 NotReady node01 118d v1.17.2
  5. k8s-node02 NotReady node02 118d v1.17.2

Node节点