004.Kubernetes二进制部署创建证书
一 创建CA证书和密钥1.1 安装cfssl工具集
1 [root@k8smaster01 ~]# mkdir -p /opt/k8s/cert 2
3 [root@k8smaster01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /opt/k8s/bin/cfssl #下载cfssl软件
4 [root@k8smaster01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /opt/k8s/bin/cfssljson #下载json模板
5 [root@k8smaster01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /opt/k8s/bin/cfssl-certinfo
6 [root@k8smaster01 ~]# chmod u+x /opt/k8s/bin/*
7 [root@k8smaster01 ~]# export PATH=/opt/k8s/bin:$PATH
1.2 创建根证书
1 [root@k8smaster01 ~]# mkdir -p /opt/k8s/work 2 [root@k8smaster01 ~]# cd /opt/k8s/work
3 [root@k8smaster01 work]# cfssl print-defaults config > config.json
4 [root@k8smaster01 work]# cfssl print-defaults csr > csr.json #创建模版配置json文件
5 [root@k8smaster01 work]# cp config.json ca-config.json #复制一份作为CA的配置文件
6 [root@k8smaster01 work]# cat > ca-config.json <<EOF
7 {
8 "signing": {
9 "default": {
10 "expiry": "168h"
11 },
12 "profiles": {
13 "kubernetes": {
14 "expiry": "87600h",
15 "usages": [
16 "signing",
17 "key encipherment",
18 "server auth",
19 "client auth"
20 ]
21 }
22 }
23 }
24 }
25 EOF
字段解释:config.json:可以定义多个profiles,分别指定不同的过期时间、使用场景等参数;后续在签名证书时使用某个profile;- signing: 表示该证书可用于签名其它证书;生成的ca.pem 证书中CA=TRUE;
- server auth: 表示client 可以用该CA 对server 提供的证书进行校验;
- client auth: 表示server 可以用该CA 对client 提供的证书进行验证。
1 [root@k8smaster01 work]# cp csr.json ca-csr.json #复制一份作为CA的证书签名请求文件 2 [root@k8smaster01 work]# cat > ca-csr.json <<EOF
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 }
19 EOF
字段解释:- CN: Common Name,kube-apiserver 从证书中提取该字段作为请求的用户名(User Name);浏览器使用该字段验证网站是否合法;
- C:country;
- ST:state;
- L:city;
- O: Organization,kube-apiserver 从证书中提取该字段作为请求用户所属的组(Group);
- OU:organization unit。
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# 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目录下。更多TLS证书创建方式参考《附008.Kubernetes TLS证书介绍及创建》。1.3 分发证书
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for all_ip in ${ALL_IPS[@]}
4 do
5 echo ">>> ${all_ip}"
6 ssh root@${all_ip} "mkdir -p /etc/kubernetes/cert"
7 scp ca*.pem ca-config.json root@${all_ip}:/etc/kubernetes/cert
8 done
2
3 [root@k8smaster01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /opt/k8s/bin/cfssl #下载cfssl软件
4 [root@k8smaster01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /opt/k8s/bin/cfssljson #下载json模板
5 [root@k8smaster01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /opt/k8s/bin/cfssl-certinfo
6 [root@k8smaster01 ~]# chmod u+x /opt/k8s/bin/*
7 [root@k8smaster01 ~]# export PATH=/opt/k8s/bin:$PATH
2 [root@k8smaster01 ~]# cd /opt/k8s/work
3 [root@k8smaster01 work]# cfssl print-defaults config > config.json
4 [root@k8smaster01 work]# cfssl print-defaults csr > csr.json #创建模版配置json文件
5 [root@k8smaster01 work]# cp config.json ca-config.json #复制一份作为CA的配置文件
6 [root@k8smaster01 work]# cat > ca-config.json <<EOF
7 {
8 "signing": {
9 "default": {
10 "expiry": "168h"
11 },
12 "profiles": {
13 "kubernetes": {
14 "expiry": "87600h",
15 "usages": [
16 "signing",
17 "key encipherment",
18 "server auth",
19 "client auth"
20 ]
21 }
22 }
23 }
24 }
25 EOF
2 [root@k8smaster01 work]# cat > ca-csr.json <<EOF
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 }
19 EOF
2 [root@k8smaster01 work]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca #生成CA密钥(ca-key.pem)和证书(ca.pem)
2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh
3 [root@k8smaster01 work]# for all_ip in ${ALL_IPS[@]}
4 do
5 echo ">>> ${all_ip}"
6 ssh root@${all_ip} "mkdir -p /etc/kubernetes/cert"
7 scp ca*.pem ca-config.json root@${all_ip}:/etc/kubernetes/cert
8 done
以上是 004.Kubernetes二进制部署创建证书 的全部内容, 来源链接: utcz.com/z/510806.html