kubernetes配置私有仓库证书

目录


公司内部有个Kubernetes集群,镜像服务使用私有化部署的Harbor,突然一天发现域名没有续费… 接下来面临的问题就是TLS证书过期无法通过权威CA证书机构签发新证书,但如果更换域名CI/CD、K8s workload 都需要修改,修改量比较大。有没有简单处理方案呢,当然有,改由本地DNS解析域名,自己签发证书。下面的内容就是操作步骤,相对还算是简单的。

一、 OpenSSL 生成证书

  1. 生成根证书
1
2
3
4
5
6
7
8
9
10
11
# 创建 CA 目录
mkdir -p ~/ssl/example/{certs,newcerts,crl,private}
cd ~/ssl/example
Touch index.txt
echo "01" > serial

# 配置openssl.conf
cp /etc/ssl/openssl.cnf ~/ssl/example/openssl.cnf

#生成根证书及密钥
openssl req -new -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -config openssl.cnf -days 3650 #CA证书建议设置比较长的时间,比如10年
  1. 签发客户端证书
1
2
3
4
5
6
7
8
9
10
11
12
# 参考如下配置修改 ~/ssl/openssl.cnf
[req]
req_extensions = v3_req
[ v3_req ]
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = registry.example.com
DNS.2 = reg.example.com
1
2
3
4
5
6
7
cd example
# 客户端证书私钥, 如果希望浏览器能直接访问建议 使用4096 bit
openssl genrsa -out registry.example.com.key 4096
# 用客户端私钥生成证书签名请求
openssl req -new -key registry.example.com.key -out registry.example.com.csr -config openssl.cnf -utf8
# 签发证书
openssl ca -in registry.example.com.csr -out registry.example.com.crt -cert cacert.crt -keyfile ca.key -config openssl.cnf -days 365 -utf8 #Chrome 不接受超长有效期证书, -utf8 可接受中文描述信息

二、 部署证书

  1. 替换Harbor证书
    1
    2
    3
    4
    5
    6
    7
    # docker-compose安装方式
    mv ~/ssl/example/registry.example.com.key /opt/harbor/common/config/nginx/cert/
    mv ~/ssl/example/registry.example.com.cert /opt/harbor/common/config/nginx/cert/
    # 其他安装方式道理一样
    # 重启harbor
    docker-compose restart registry

三、 客户端安装CA证书

  1. Kubernetes Node信任证书
    可以直接将ca.crt复制到所有节点的 /etc/docker/certs.d/registry.example.com/ 目录下

    也可使用Kubernetes的DaemonSet 自动部署到所有节点上 参考脚本 Daemonset

  2. 为了能直接访问registry.example.com ca.crt 添加到本地系统信任

评论