kubernetes集群证书重签
依照https://github.com/strongit/kubeadm-ha/ 安装步骤,kubeadm init安装后的集群存在证书过期问题。现修复如下:
思路如下,
1、保留ca.crt ca.key front-proxy-ca.crt front-proxy-ca.key,根证书有效期十年
2、openssl重新签注
3、kubeadm alpha phase 生成config
[root@k8s-master01 pki]# cat csr.conf
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn
[ dn ]
C = CN
ST = BeiJing
L = BeiJing
O = k8s
OU = System
CN = kubernetes
[ req_ext ]
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = kubernetes
DNS.2 = kubernetes.default
DNS.3 = kubernetes.default.svc
DNS.4 = kubernetes.default.svc.cluster
DNS.5 = kubernetes.default.svc.cluster.local
DNS.6 = k8s-master01
DNS.7 = k8s-master02
DNS.8 = k8s-master03
IP.1 = IP
IP.2 = IP
[ v3_ext ]
authorityKeyIdentifier=keyid,issuer:always
basicConstraints=CA:FALSE
keyUsage=digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment
extendedKeyUsage=serverAuth,clientAuth
subjectAltName=@alt_names
openssl genrsa -out apiserver.key 2048
openssl req -new -key apiserver.key -out apiserver.csr -config csr.conf
openssl x509 -req -in apiserver.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver.crt -days 10000 -extensions v3_ext -extfile csr.conf
openssl x509 -noout -text -in ./apiserver.crt |grep "Not"
openssl genrsa -out apiserver-kubelet-client.key 2048
openssl req -new -key apiserver-kubelet-client.key -out apiserver-kubelet-client.csr -config csr.conf
openssl x509 -req -in apiserver-kubelet-client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out apiserver-kubelet-client.crt -days 10000 -extensions v3_ext -extfile csr.conf
openssl x509 -noout -text -in ./apiserver-kubelet-client.crt |grep "Not"
openssl genrsa -out front-proxy-client.key 2048
openssl req -new -key front-proxy-client.key -out front-proxy-client.csr -config csr.conf
openssl x509 -req -in front-proxy-client.csr -CA front-proxy-ca.crt -CAkey front-proxy-ca.key -CAcreateserial -out front-proxy-client.crt -days 10000 -extensions v3_ext -extfile csr.conf
openssl x509 -noout -text -in ./front-proxy-client.crt |grep "Not"
kubeadm alpha phase certs all --config kubeadm-config.yaml
kubeadm alpha phase kubelet config write-to-disk --config kubeadm-config.yaml
kubeadm alpha phase kubelet write-env-file --config kubeadm-config.yaml
kubeadm alpha phase kubeconfig kubelet --config kubeadm-config.yaml
kubeadm alpha phase kubeconfig all --config kubeadm-config.yaml
kubeadm alpha phase controlplane all --config kubeadm-config.yaml
systemctl restart kubelet
kubeadm alpha phase mark-master --config kubeadm-config.yaml
cp /etc/kubernetes/admin.conf ~/.kube/config
重启集群后,执行kubelet logs pods XXXX -n kube-system报错如下:Error from server (Forbidden): Forbidden (user=kubernetes, verb=get, resource=nodes, subresource=proxy) ( pods/log kube-scheduler-k8s-master01)
解决方案:kubectl create clusterrolebinding system:kubernetes --clusterrole=cluster-admin --user=system:kubernetes
ingress的tls设置
对ingress中的域名进行tls安全证书的设置步骤如下:
创建自签名的秘钥与ssl证书;
将证书保存到kubernetes集群的1个Secret资源对象上;
设置Secret资源对象到ingress中。
根据网站域名是1个还是多个,前两步的操作稍有不同,第3步操作相同,下面以多域名的操作为例:
-
生成ca证书
[root@kubenode1 ~]# mkdir -p /etc/kubernetes/ingress
[root@kubenode1 ~]# cd /etc/kubernetes/ingress/
[root@kubenode1 ingress]# openssl genrsa -out ca.key 2048
[root@kubenode1 ingress]# openssl req -x509 -new -nodes -key ca.key -days 3560 -out ca.crt -subj "/CN=ingress-ca" - 修改openssl.cnf文件
#对于多域名,生成ssl证书需要使用额外的x509v3配置文件辅助;
#在[alt_names]字段中设置多域名
[root@kubenode1 ingress]# cp /etc/pki/tls/openssl.cnf .
[root@kubenode1 ingress]# vim openssl.cnf
[ req ]
#第126行,取消注释
req_extensions = v3_req # The extensions to add to a certificate request
[ v3_req ]
#Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
#第224行之后,新增部分
subjectAltName = @alt_names
[alt_names]
DNS.1 = Nginx01-svc-tls.me
DNS.2 = Nginx02-svc-tls.me
- 生成ingress ssl证书
#基于修改的openssl.cnf与ca证书生成ingress ssl证书
#生成秘钥
[root@kubenode1 ingress]# openssl genrsa -out ingress.key 2048
#生成csr文件
[root@kubenode1 ingress]# openssl req -new -key ingress.key -out ingress.csr -subj "/CN=Nginx-svc-tls" -config openssl.cnf
#生成证书
[root@kubenode1 ingress]# openssl x509 -req -in ingress.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out ingress.crt -days 3650 -extensions v3_req -extfile openssl.cnf
- 生成Secret资源对象
Secret对象的主要作用是保管私密数据,如:密码,OAuth Tokens,ssh Keys等信息。将私密信息存放在Secret对象中,比直接放在Pod或者Docker image中更安全,更便于使用与分发。
在创建Pod时,通过为Pod指定Service Account来自动使用;
通过挂载Secret到Pod来使用;
Docker image下载时使用,通过指定Pod的spc.ImagePullSecrets来引用。
#编辑secret-ingress.yaml文件,将ingress.key与ingress.crt的内容复制到yaml文件中;
#注意1:Secret的”data”域的各子域的值必须为BASE64编码;
#注意2:复制key与crt的内容时去掉换行符,变成一行
[root@kubenode1 ingress]# cd /usr/local/src/yaml/ingress/
[root@kubenode1 ingress]# touch secret-ingress.yaml
[root@kubenode1 ingress]# vim secret-ingress.yaml
apiVersion: v1
kind: Secret
Metadata:
name: secret-ingress
#1.8.x之后使用kubernetes.io/tls替换Opaque
type: kubernetes.io/tls
data:
tls.crt: MIIC/TCCAeWgAwIBAgIJALGRacBg2fWIMA0GCSqGSIb3DQEBCwUAMBUxEzARBgNVBAMMCmluZ3Jlc3MtY2EwHhcNMTgwMjI4MDM0NDI1WhcNMjgwMjI2MDM0NDI1WjAYMRYwFAYDVQQDDA1uZ2lueC1zdmMtdGxzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuZvMBYF104JPtMZFFUxCpGGODFG4rGffN1FFC98CGt99QAwVMfABGDMU8zfa21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BEpc98YikgYmZk/6kyzUP3l+MIj5i3UK/7ZG3QOpTAeTbzBQQX0s31b2Lf9M+SN2+2XJb/0OUr3RKKkuf5KgedMll7hNaEaFu9z5qPepFUlKWZz2MkirsljecbSJ8ZfGz2wCUhQoW8KLQY9ftEaz+27eEJ0FxHhuhe5+yQMpkKA==
#生成Secret资源对象
[root@kubenode1 ingress]# kubectl create -f secret-ingress.yaml
#以上编辑yaml文件,使用”kubectl create”命令生成Secret对象在步骤上更清晰;
#但可以利用”kubectl create secret tls”命令直接创建Secret对象
[root@kubenode1 ingress]# kubectl create secret tls secret-ingress --key /etc/kubernetes/ingress/ingress.key --cert /etc/kubernetes/ingress/ingress.crt
- 创建后端服务
#编辑后端服务Nginx01-svc-tls.yaml
[root@kubenode1 ingress]# touch Nginx01-svc-tls.yaml
[root@kubenode1 ingress]# vim Nginx01-svc-tls.yaml
apiVersion: extensions/v1beta1
kind: Deployment
Metadata:
name: Nginx01-tls
spec:
replicas: 1
template:
Metadata:
labels:
name: Nginx01-tls
spec:
containers:
#编辑后端服务Nginx02-svc-tls.yaml
[root@kubenode1 ingress]# cp Nginx01-svc-tls.yaml Nginx02-svc-tls.yaml
[root@kubenode1 ingress]# sed -i 's|Nginx01|Nginx02|g' Nginx02-svc-tls.yaml
#生成后端服务
[root@kubenode1 ingress]# kubectl create -f Nginx01-svc-tls.yaml
[root@kubenode1 ingress]# kubectl create -f Nginx02-svc-tls.yaml
#修改提供后端服务的Nginx容器的html文件;
#通过”kubectl exec -ti <pod-name> -c <container-name> /bin/bash”进入容器修改;pod-name可通过命令”kubectl get pods -o wide”获取;container-name即yaml文件中定义的名字
[root@kubenode1 ingress]# kubectl get pods -o wide
#Nginx官方容器的index.html文件在/usr/share/Nginx/html/目录下
[root@kubenode1 ingress]# kubectl exec -ti Nginx01-tls-59fbf6696c-qfq4k -c Nginx01-tls /bin/bash
root@Nginx01-tls-59fbf6696c-qfq4k:/# echo "<h2>Welcome to test site Nginx01-svc-tls</h2>" > /usr/share/Nginx/html/index.html
root@Nginx01-tls-59fbf6696c-qfq4k:/# cat /usr/share/Nginx/html/index.html
root@Nginx01-tls-59fbf6696c-qfq4k:/# exit
[root@kubenode1 ingress]# kubectl exec -ti Nginx02-tls-5559fd9bc7-dfbrp -c Nginx02-tls /bin/bash
root@Nginx02-tls-5559fd9bc7-dfbrp:/# echo "<h2>Welcome to test site Nginx02-svc-tls</h2>" > /usr/share/Nginx/html/index.html
root@Nginx02-tls-5559fd9bc7-dfbrp:/# cat /usr/share/Nginx/html/index.html
root@Nginx02-tls-5559fd9bc7-dfbrp:/# exit
- 创建ingress对象
复制代码
#编辑ingress对象yaml文件;
#在”spec”域下新增“tls”子域,”hosts”字段加入多域名,“secretName”字段调用对应的Secret资源;
#1个ingress对象只能使用1个Secret对象(“secretName”字段value唯一),即只能使用1个证书,该正式需要支持”hosts”字段下所有域名;
#“secretName”字段一定要置于域名列表最后的位置;
#”hosts”字段的域名需要匹配”rules”字段域名;
#ingress默认情况下,当不配置证书或者证书配置错误时,会默认给出一个tls证书;如“secretName”字段配置了2个值,则所有域名采用默认证书;如”hosts”字段少配置一个域名,缺失的域名会采用默认证书;
#更新ingress证书可能需要等待一段时间才能生效
[root@kubenode1 ingress]# touch Nginx-svc-tls-ingress.yaml
[root@kubenode1 ingress]# vim Nginx-svc-tls-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
Metadata:
name: ingress-tls
spec:
tls:
#生成ingress对象
[root@kubenode1 ingress]# kubectl create -f Nginx-svc-tls-ingress.yaml
[root@kubenode1 ingress]# kubectl get ingress
- 验证
#采用--resolve参数模拟dns解析,目标地址为域名;
#http访问时被重定向,采用https访问正常
[root@kubenode1 ingress]# curl --resolve Nginx01-svc-tls.me:80:172.30.200.21 http://Nginx01-svc-tls.me
[root@kubenode1 ingress]# curl --resolve Nginx01-svc-tls.me:443:172.30.200.21 -k https://Nginx01-svc-tls.me
#或者采用-H参数设置http头中需要访问的域名,目标地址为ip地址
[root@kubenode1 ingress]# curl -H 'Host:Nginx01-svc-tls.me' -k https://172.30.200.23
[root@kubenode1 ingress]# curl -H 'Host:Nginx02-svc-tls.me' -k https://172.30.200.23
在本地浏览器访问host主机(注意提前绑定域名):http://Nginx01-svc-tls.me
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。