设定自签证书,证书生成工具有很多,如openssl,这里使用cfssl证书生成工具。
cfssl是一个开源的证书管理工具,使用json文件生成证书,相比openssl更方便使用,找任意一台主机操作即可,这里在master节点操作。

一、安装CFSSL
由于CFSSL系列的工具都是独立的二进制文件,所以下载相应的二进制文件并赋予权限即可。

[root@master1 cert]# wget -c  https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 
[root@master1 cert]# curl -s -L -o cfssljson https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
[root@master1 cert]# curl -s -L -o cfssl-certinfo https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
[root@master1 cert]# ll
total 18808
-rw-r--r--. 1 root root 10376657 Oct 13 19:42 cfssl
-rw-r--r--. 1 root root  6595195 Apr 17 03:17 cfssl-certinfo
-rw-r--r--. 1 root root  2277873 Oct 13 19:50 cfssljson
[root@master1 cert]# chmod +x cfssl*
[root@master1 cert]# ll
total 18808
-rwxr-xr-x. 1 root root 10376657 Oct 13 19:42 cfssl
-rwxr-xr-x. 1 root root  6595195 Apr 17 03:17 cfssl-certinfo
-rwxr-xr-x. 1 root root  2277873 Oct 13 19:50 cfssljson
[root@master1 cert]# 
#确认
[root@master1 cert]# ./cfssl version
Version: 1.2.0
Revision: dev
Runtime: go1.6
[root@master1 cert]# 

根据认证对象可以将证书分成三类:服务器证书server cert,客户端证书client cert,对等证书peer cert(表示既是server cert又是client cert)。
在k8s 集群中需要的证书种类一般有:
ETCD , Apiserver ,controller-manager,kube-proxy等等。

二、生成ETCD证书
1、创建根证书配置文件

[root@master1 cert]# mkdir work
[root@master1 cert]# ls
cfssl  cfssl-certinfo  cfssljson  work
[root@master1 cert]# cd work
[root@master1 work]# ../cfssl print-defaults config > config.json
[root@master1 work]# ls
config.json
[root@master1 work]# ../cfssl print-defaults csr > csr.json
#复制一份作为CA-配置文件。
[root@master1 work]# cp config.json  ca-config.json  
#修改如下:
[root@master1 work]# cat ca-config.json 
{
    "signing": {
        "default": {
            "expiry": "876000h"
        },
        "profiles": {
            "www": {
                "expiry": "876000h",
                "usages": [
                    "signing",
                    "key encipherment",
                    "server auth",
                    "client auth"
                ]
            }
        }
    }
}
[root@master1 work]# ls
ca-config.json  config.json  csr.json
[root@master1 work]# 

signing: 表示该证书可用于签名其它证书;生成的ca.pem 证书中CA=TRUE;
server auth: 表示client 可以用该CA 对server 提供的证书进行校验;
client auth: 表示server 可以用该CA 对client 提供的证书进行验证;
expiry:表示证书有效期

#复制一份作为CA的证书签名请求文件
[root@master1 work]# cp csr.json ca-csr.json
#修改如下:
[root@master1 work]# cat  ca-csr.json 
{
    "CN": "etcd",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "beijing",
            "L": "beijing",
            "O": "k8s",
            "OU": "System"
        }
    ],
    "ca": {
        "expiry": "876000h"
 }
}
[root@master1 work]# 
[root@master1 work]# ls
ca-config.json  ca-csr.json  config.json  csr.json

证书初始化,生成自签CA密钥(ca-key.pem)和CA证书(ca.pem)

[root@master1 work]# ../cfssl gencert -initca ca-csr.json | ../cfssljson -bare ca
2021/10/13 21:14:09 [INFO] generating a new CA key and certificate from CSR
2021/10/13 21:14:09 [INFO] generate received request
2021/10/13 21:14:09 [INFO] received CSR
2021/10/13 21:14:09 [INFO] generating key: rsa-2048
2021/10/13 21:14:09 [INFO] encoded CSR
2021/10/13 21:14:09 [INFO] signed certificate with serial number 10012960205882602354646000558020006147460777742
[root@master1 work]# ls
ca-config.json  ca.csr  ca-csr.json  ca-key.pem  ca.pem  config.json  csr.json
[root@master1 work]# ls -s *.pem
4 ca-key.pem  4 ca.pem
[root@master1 work]# 

2、使用自签CA签发ETCD 证书

vim server-csr.json
#增加:
{
  "CN": "etcd",
  "hosts": [
  "192.168.139.130",
  "192.168.139.131",
  "192.168.139.132"
  ],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "L": "BeiJing",
      "ST": "BeiJing"
    }
  ]
}

#生成etcd证书文件,会生成server.pem 和server-key.pem 文件。
[root@master1 work]# ../cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | ../cfssljson -bare server
2021/10/14 00:10:12 [INFO] generate received request
2021/10/14 00:10:12 [INFO] received CSR
2021/10/14 00:10:12 [INFO] generating key: rsa-2048
2021/10/14 00:10:12 [INFO] encoded CSR
2021/10/14 00:10:12 [INFO] signed certificate with serial number 583190765873858524452419671166187543028791035227
2021/10/14 00:10:12 [WARNING] This certificate lacks a "hosts" field. This makes it unsuitable for
websites. For more information see the Baseline Requirements for the Issuance and Management
of Publicly-Trusted Certificates, v.1.1.6, from the CA/Browser Forum (https://cabforum.org);
specifically, section 10.2.3 ("Information Requirements").
[root@master1 work]# ls
ca-config.json  ca.csr  ca-csr.json  ca-key.pem  ca.pem  config.json  csr.json  server.csr  server-csr.json  server-key.pem  server.pem
[root@master1 work]# 

至此,ETCD证书生成完成。
注:上述文件hosts 字段中IP 为所有etcd 节点的集群内部通信IP,一个都不能少!为了方便后期扩容可以多写几个预留的IP。

总结:
安装cfssh
生成自签CA :ca.pem和ca-key.pem
使用自签CA签发ETCD证书 server.pem和server-key.pem.

三、生成kube-apiserver证书
方法同上。

四、生成kbe-scheduler证书
方法同上

五、生成kubectl连接集群证书
方法同上。

六、生成kube-proxy证书,方法同上。

Logo

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

更多推荐