这个东西的作用有:

1. 在新部署k8s集群时,就生成30年证书。这样就不用1年后为kubelet更换证书。

2. 在证书快到期时,手动生成新证书,替换原证书(不需要替换key),这样只需要重启(服务器或kubelet服务)就能使用新证书,不用等到证书过期,节点被踢出k8s集群,再重新生成。

 

1. 沿用kubelet原有的ecc算法生成的 key,假设为kubelet-client.key。生成一个csr

2. 生成csr之前,通过ini格式,配置一个req配置文件。

cat req.conf

[req]

prompt = no

distinguished_name = dn

input_password = 123456  #这个密码好像没啥用

 

[dn]

O = system:nodes

CN = system:node:192.168.1.93

生成csr的命令为:

openssl req -new -sha256 -key kubelet-client.key -config req.conf -out newcsr

其中,kubelet-client.key是原本的kubelet使用的key。新生成的csr是newcsr。

 

生成csr后,可以用命令看一下其内容:

openssl req -in newcsr -text

Certificate Request:

Data:

Version: 1 (0x0)

Subject: O = system:nodes, CN = system:node:192.168.1.93

Subject Public Key Info:

Public Key Algorithm: id-ecPublicKey

Public-Key: (256 bit)

pub:
 

04:0b:d4:09:62:a5:d5:bb:aa:bf:2a:9a:7e:f7:01:

xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

xx:xx:xx:xx:xx

ASN1 OID: prime256v1

NIST CURVE: P-256

Attributes:

a0:00

Signature Algorithm: ecdsa-with-SHA256

31:47:12:21:00:dd:9f:05:fd:82:7e:53:b7:f3:0c:d5:02:fb:

71:f9:cb:ec:bd:0b:f4:c5:b4:b1:62:21:e0:f9:92:6d:9d:be:

xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:x:
 

 

3. 根据csr,使用ca的key和证书颁发新证书

由于kubelet使用v3版本的x509格式。因此v3的额外参数文件v3.ext如下:

cat v3.ext

 

keyUsage=critical,Digital Signature, Key Encipherment

extendedKeyUsage=TLS Web Client Authentication

basicConstraints=critical,CA:FALSE

subjectKeyIdentifier=hash

authorityKeyIdentifier=keyid,issuer

 

最后颁发证书的命令为:

openssl x509 -req -in newcsr -CA  ca.pem -CAkey ca-key.pem -out kubelet-c.crt --days 10950 -extfile v3.ext

其中,10950 days是表示30年有效期。

 

如果报错ca.srl: No such file or directory

那么 echo "01" > ca.srl 即可,这个文件影响到ca颁发的证书的序号,而证书序号应该是唯一的,所以这点需要控制好。

 

用命令查看生成的证书:

openssl x509 -in kubelet-c.crt -text

Certificate:

Data:

Version: 3 (0x2)

Serial Number: 6 (0x6)

Signature Algorithm: sha256WithRSAEncryption

Issuer: C = CN, ST = ST, L = L, O = example, OU = System, CN = www.example.com

Validity

Not Before: Mar 27 08:48:42 2020 GMT

Not After : Mar 25 08:48:42 2050 GMT

Subject: O = system:nodes, CN = system:node:192.168.1.99

Subject Public Key Info:

Public Key Algorithm: id-ecPublicKey

Public-Key: (256 bit)

pub:

04:0b:d4:09:62:a5:d5:bb:aa:bf:2a:9a:7e:f7:01:

16:34:27:4e:79:32:05:8b:de:d3:d3:37:9a:ff:39:

ASN1 OID: prime256v1

NIST CURVE: P-256

X509v3 extensions:

X509v3 Key Usage: critical

Digital Signature, Key Encipherment

X509v3 Extended Key Usage:

TLS Web Client Authentication

X509v3 Basic Constraints: critical

CA:FALSE

X509v3 Subject Key Identifier:

58:E3:90:CB:75:E5:C1:78:42:4

X509v3 Authority Key Identifier:

keyid:68:FF:FE:FE:57:87:9B:08

Signature Algorithm: sha256WithRSAEncryption

53:a8:72:1d:62:1f:9b:b3:3e:a8:c1:4a:6e:91:ea:20:30:d0:

51:7d:78:4a:8f:a4:72:1a:98:12:51:b1:63:c4:3a:c9:c0:7c:

 

其他:

用现有证书和key生成csr的命令为:

openssl x509 -x509toreq -in kubelet-client.crt -out req.pem -signkey kubelet-client.key

这个req.pem就和上文中的newcsr是一样的。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐