附008.Kubernetes TLS证书介绍及创建,


一 Kubernetes证书

1.1 TLS

Kubernetes系统的各个组件需要使用TLS证书对其通信加密以及授权认证,建议在部署之前先生成相关的TLS证书。

1.2 CA证书创建方式

kubernetes 系统各个组件需要使用TLS证书对通信进行加密,通常可通过以下工具生产自建证书:
  • openssl
  • cfssl
  • easyrsa

1.3 Kubernetes组件证书

部署kubernetes组件建议使用TLS双向认证的,相关组件涉及的主要证书有:
  • etcd证书:etcd集群之间通信加密使用的TLS证书。
  • kube-apiserver证书:配置kube-apiserver组件的证书。
  • kube-controller-manager证书:用于和kube-apiserver通信认证的证书。
  • kube-scheduler证书:用于和kube-apiserver通信认证的证书。
  • kubelet证书【可选,非必需】:用于和kube-apiserver通信认证的证书,如果使用TLS Bootstarp认证方式,将没有必要配置。
  • kube-proxy证书【可选,非必需】:用于和kube-apiserver通信认证的证书,如果使用TLS Bootstarp认证方式,将没有必要配置。

二 openssl生成证书

2.1 openssl创建证书

  1 [root@master ~]# MASTER_IP=172.24.8.71			#定义MASTER_IP
  2 [root@master ~]# mkdir cert				        #建议创建独立存储证书的目录
  3 [root@master ~]# cd cert
  4 [root@master cert]# openssl genrsa -out ca.key 2048	        #生成一个 2048 bit的ca.key
  5 [root@master cert]# openssl req -x509 -new -nodes -key ca.key -subj "/CN=${MASTER_IP}" -days 10000 -out ca.crt	#根据 ca.key 生成一个 ca.crt(使用 -days 设置证书的有效时间)
  6 [root@master cert]# openssl genrsa -out server.key 2048	#生成一个 2048 bit 的 server.key
  7 [root@master cert]# openssl req -new -key server.key -subj "/CN=${MASTER_IP}" -out server.csr	#根据 server.key 生成一个 server.csr
  8 [root@master cert]# openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 10000	                                                #根据 ca.key、ca.crt 和 server.csr 生成 server.crt
  9 [root@master cert]# openssl x509  -noout -text -in ./server.crt
 

三 cfssl生成证书

3.1 cfssl创建证书

  1 [root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl         #下载cfssl软件
  2 [root@master ~]# chmod u+x /usr/local/bin/cfssl
  3 [root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson #下载json模板
  4 [root@master ~]# chmod u+x /usr/local/bin/cfssljson
  5 [root@master ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo
  6 [root@master ~]# chmod u+x /usr/local/bin/cfssl-certinfo
  7 [root@master ~]# mkdir cert
  8 [root@master ~]# cd cert/
  9 [root@master cert]# cfssl print-defaults config > config.json
 10 [root@master cert]# cfssl print-defaults csr > csr.json			#创建模版配置json文件
 11 [root@master cert]# cp config.json ca-config.json			        #复制一份作为CA的配置文件
 12 [root@master cert]# vi ca-config.json
 13 {
 14     "signing": {
 15         "default": {
 16             "expiry": "168h"
 17         },
 18         "profiles": {
 19             "kubernetes": {
 20                 "expiry": "8760h",
 21                 "usages": [
 22                     "signing",
 23                     "key encipherment",
 24                     "server auth"
 25                     "client auth"
 26                 ]
 27             }
 28         }
 29     }
 30 }
  字段解释: config.json:可以定义多个profiles,分别指定不同的过期时间、使用场景等参数;后续在签名证书时使用某个profile;
  • signing: 表示该证书可用于签名其它证书;生成的ca.pem 证书中CA=TRUE;
  • server auth: 表示client 可以用该CA 对server 提供的证书进行校验;
  • client auth: 表示server 可以用该CA 对client 提供的证书进行验证。
  1 [root@master cert]# cp csr.json ca-csr.json					#复制一份作为CA的配置文件
  2 [root@master cert]# vi ca-csr.json
  3 {
  4     "CN": "kubernetes",
  5     "key": {
  6         "algo": "rsa",
  7         "size": 2048
  8     },
  9     "names": [
 10         {
 11             "C": "CN",
 12             "ST": "Shanghai",
 13             "L": "Shanghai",
 14             "O": "k8s",
 15             "OU": "System"
 16         }
 17     ]
 18 }
  字段解释:
  • CN: Common Name,kube-apiserver 从证书中提取该字段作为请求的用户名(User Name);浏览器使用该字段验证网站是否合法;
  • C:country;
  • ST:state;
  • L:city;
  • O: Organization,kube-apiserver 从证书中提取该字段作为请求用户所属的组(Group);
  • OU:organization unit。
  1 [root@master cert]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca	#生成CA密钥(ca-key.pem)和证书(ca.pem)
提示:生成证书后,Kubernetes集群需要双向TLS认证,则可将ca-key.pem和ca.pem拷贝到所有要部署的机器的/etc/kubernetes/ssl目录下。

四 easyrsa生成证书

4.1 easyrsa创建证书

  1 [root@master ~]# mkdir cert
  2 [root@master ~]# curl -LO https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz	#下载easyrsa软件
  3 [root@master ~]# tar xzf easy-rsa.tar.gz
  4 [root@master ~]# cd easy-rsa-master/easyrsa3
  5 [root@master easyrsa3]# ./easyrsa init-pki
  6 [root@master easyrsa3]# MASTER_IP=172.24.8.71			                             #定义MASTER_IP
  7 [root@master easyrsa3]# ./easyrsa --batch "--req-cn=${MASTER_IP}@`date +%s`" build-ca nopass     #生成 CA
  解释: --batch:设置为自动模式; --req-cn:设置默认的 CN
  1 [root@master easyrsa3]# ./easyrsa --subject-alt-name="IP:${MASTER_IP}" build-server-full server nopass	#生成服务器证书和密钥
解释: build-server-full [文件名]:生成一个键值对,在本地为客户端和服务器签名。
  1 [root@master easyrsa3]# cp pki/ca.crt pki/issued/server.crt pki/private/server.key /root/cert/		#复制相关证书
提示:生成证书后,Kubernetes集群可通过如下配置使用证书:
  • --client-ca-file=/root/cert/ca.crt
  • --tls-cert-file=/root/cert/server.crt
  • --tls-private-key-file=/root/cert/server.key

五 相关证书及配置项

5.1 API Server 证书

API Server 证书配置为如下两个选项:
  • --tls-cert-file string
File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated after server cert). If HTTPS serving is enabled, and --tls-cert-file and --tls-private-key-file are not provided, a self-signed certificate and key are generated for the public address and saved to the directory specified by --cert-dir.  
  • --tls-private-key-file string
File containing the default x509 private key matching --tls-cert-file.

5.2 Client CA 证书

  • --client-ca-file string
If set, any request presenting a client certificate signed by one of the authorities in the client-ca-file is authenticated with an identity corresponding to the CommonName of the client certificate. 该配置明确了 Clent 连接 API Server 时,API Server 应当确保其证书源自哪个 CA 签发;如果其证书不是由该 CA 签发,则拒绝请求;事实上,这个 CA 不必与 HTTPS 端点所使用的证书 CA 相同;同时这里的 Client 是一个泛指的,可以是 kubectl,也可能是你自己开发的应用

5.3 请求头证书

API Server 支持多种认证方式的,其中一种就是使用 HTTP 头中的指定字段来进行认证,相关配置如下:
  • --requestheader-allowed-names stringSlice
List of client certificate common names to allow to provide usernames in headers specified by --requestheader-username-headers. If empty, any client certificate validated by the authorities in --requestheader-client-ca-file is allowed.
  • --requestheader-client-ca-file string
Root certificate bundle to use to verify client certificates on incoming requests before trusting usernames in headers specified by --requestheader-username-headers. WARNING: generally do not depend on authorization being already done for incoming requests.

5.4 kubelet证书

对于 Kubelet 组件,API Server 单独提供了证书配置选项,从而指定 API Server 与 Kubelet 通讯所使用的证书以及其签署的 CA。同时这个 CA 可以完全独立与上述其他CA。同时 Kubelet 组件也提供了反向设置的相关选项: # API Server
  • --kubelet-certificate-authority string
Path to a cert file for the certificate authority.
  • --kubelet-client-certificate string
Path to a client cert file for TLS.
  • --kubelet-client-key string
Path to a client key file for TLS.   # Kubelet
  • --client-ca-file string
If set, any request presenting a client certificate signed by one of the authorities in the client-ca-file is authenticated with an identity corresponding to the CommonName of the client certificate.
  • --tls-cert-file string
File containing x509 Certificate used for serving HTTPS (with intermediate certs, if any, concatenated after server cert). If --tls-cert-file and --tls-private-key-file are not provided, a self-signed certificate and key are generated for the public address and saved to the directory passed to --cert-dir.
  • --tls-private-key-file string
File containing x509 private key matching --tls-cert-file. 5.5 Service Account 证书 在 API Server 配置中,对于 Service Account 同样有两个证书配置:
  • --service-account-key-file stringArray
File containing PEM-encoded x509 RSA or ECDSA private or public keys, used to verify ServiceAccount tokens. The specified file can contain multiple keys, and the flag can be specified multiple times with different files. If unspecified, --tls-private-key-file is used. Must be specified when --service-account-signing-key is provided
  • --service-account-signing-key-file string
Path to the file that contains the current private key of the service account token issuer. The issuer will sign issued ID tokens with this private key. (Requires the 'TokenRequest' feature gate.) 这两个配置描述了对 Service Account 进行签名验证时所使用的证书;不过需要注意的是这里并没有明确要求证书 CA,所以这两个证书的 CA 理论上也是可以完全独立的。 Kubernetes相关证书及配置项参考: https://mritd.me/2018/08/26/kubernetes-certificate-configuration/ 提示:以上证书创建示例参考:https://notes.doublemine.me/2018-03-26-Kubernetes%E9%9B%86%E7%BE%A4%E4%B9%8B%E8%B7%AF%E4%B9%8BTLS%E8%AF%81%E4%B9%A6%E9%85%8D%E7%BD%AE.html

相关内容

    暂无相关文章