您好,登录后才能下订单哦!
依照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安全证书的设置步骤如下:
创建自签名的秘钥与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"
[ 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
#基于修改的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对象创建完成之后,可通过3种方式调用:
在创建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/TCCAeWgAwIBAgIJALGRacBg2fWIMA0GCSqGSIb3DQEBCwUAMBUxEzARBgNVBAMMCmluZ3Jlc3MtY2EwHhcNMTgwMjI4MDM0NDI1WhcNMjgwMjI2MDM0NDI1WjAYMRYwFAYDVQQDDA1uZ2lueC1zdmMtdGxzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuZvMBYF104JPtMZFFUxCpGGODFG4rGffN1FFC98CGt99QAwVMfABGDMU8zfa21twxON1v3WK8HdJH5KRdLOIRQnhuMHsC174sb/+FuOa0GhStgmNX0f2jGETuImPQ82faXACnUkUYuvYG5odbY+tS+LQBtIormpxWRlNNTVzT3jFD6JECVZzpMyCJutkwxJC083PS1VE9ki+7mgpPWbb9BqT0Tn672x4cHI8LZ5snr1fpR8I0sqADXY+KpFQeh7UJsWZjfr00wDBsg76aF3TNK+pecXnBNYPZ6o7sOGXvagAxU58xjj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件,使用”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
apiVersion: v1
kind: Service
metadata:
name: nginx01-svc-tls
spec:
ports:
#Service服务监听的端口号
#编辑后端服务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对象
[root@kubenode1 ingress]# kubectl create -f nginx-svc-tls-ingress.yaml
[root@kubenode1 ingress]# kubectl get ingress
#或者采用-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
采用http访问,重定向自动跳转为https访问
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。