学习空间K8s

二进制安装-k8s高可用集群02-创建CA证书和秘钥

2021-08-25  本文已影响0人  Chris0Yang

1) 安装 cfssl 工具集

从现在开始所有的操作,都要在 master机器下执行
可以参考上一章文档:4)无密码 ssh 登录其它节点

mkdir -p /data/application && cd /data/application
wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64

cp /data/application/cfssljson_linux-amd64 /opt/k8s/bin/cfssljson
cp /data/application/cfssl_linux-amd64 /opt/k8s/bin/cfssl
cp /data/application/cfssl-certinfo_linux-amd64 /opt/k8s/bin/cfssl-certinfo
chmod +x /opt/k8s/bin/*

2) 创建根证书 (CA)

CA 证书是集群所有节点共享的,只需要创建一个 CA 证书,后续创建的所有证书都由它签名

1、创建配置文件

CA 配置文件用于配置根证书的使用场景 (profile) 和具体参数 (usage,过期时间、服务端认证、客户端认证、加密等),后续在签名其它证书时需要指定特定场景

cat > /data/cert/ca-config.json <<EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
        "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ],
        "expiry": "87600h"
      }
    }
  }
}
EOF

2、创建证书签名请求文件

cat > /data/cert/ca-csr.json <<EOF
{ 
  "CN": "kubernetes",
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "4Paradigm"
    }
  ]
}
EOF

3、生成 CA 证书和私钥

[root@master cert]# cfssl gencert -initca /data/cert/ca-csr.json | cfssljson -bare ca
20XX/XX/XX XX:XX:XX [INFO] generating a new CA key and certificate from CSR
20XX/XX/XX XX:XX:XX [INFO] generate received request
20XX/XX/XX XX:XX:XX [INFO] received CSR
20XX/XX/XX XX:XX:XX [INFO] generating key: rsa-2048
20XX/XX/XX XX:XX:XX [INFO] encoded CSR
20XX/XX/XX XX:XX:XX [INFO] signed certificate with serial number 652945881726401134885162916242742430723518895911

[root@master cert]# ls ca*
ca-config.json  ca.csr  ca-csr.json  ca-key.pem  ca.pem

3) 分发证书文件

将生成的 CA 证书、秘钥文件、配置文件拷贝到所有节点的 /etc/kubernetes/cert 目录下

cat > magic09_cert.sh << "EOF"
#!/bin/bash
# 分发证书文件
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}"
    scp /data/cert/ca* root@${node_ip}:/etc/kubernetes/cert
done
EOF

4) 参考

各种 CA 证书类型:https://github.com/kubernetes-sigs/apiserver-builder-alpha/blob/master/docs/concepts/auth.md

上一篇 下一篇

猜你喜欢

热点阅读