您好,登錄后才能下訂單哦!
依照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+pecXnBNYPZ6o7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aml
#以上編輯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
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。