====== K8s(rke) 內服務設定 SSL 憑證的方式 ======
在一個使用 [[tech/rke-k8s|rke 建置好的 K8s]] 內, 設定使用 [[tech/ssl_letsencrypt|Let's Encrypt 產生的 SSL 憑證]] 讓裡面的服務 https 可以正確使用
===== 基本處理步驟 =====
- DNS 的設定
- 產生 SSL 憑證 : 參考 [[tech/ssl_letsencrypt]]
- 將憑證匯入 K8s 內 (建立 secret tls)
- 設定 K8s 使用 secret tls
===== 更新憑證步驟 =====
* 可透過 kubectl edit secret 方式進行人工編輯 Exp. devops-tls
kubectl edit secret devops-tls
* 可先刪除 secret 後馬上 create 方式處理 Exp. devops-tls
kubectl delete secret devops-tls
kubectl create secret tls devops-tls --cert=cert1.pem --key=privkey1.pem
===== Exp1. 以 *.ingress-devops.ichiayi.com 來設定為預設服務使用的 SSL 憑證 =====
- 確認 DNS 已設定好 *.ingress-devops.ichiayi.com
$ ping ttt.ingress-devops.ichiayi.com
PING ttt.ingress-devops.ichiayi.com (172.16.0.190) 56(84) bytes of data.
64 bytes from iiiDevOps.unassigned-domain (172.16.0.190): icmp_seq=1 ttl=64 time=0.025 ms
64 bytes from iiiDevOps.unassigned-domain (172.16.0.190): icmp_seq=2 ttl=64 time=0.042 ms
- 透過 Let's Encrypt 產生 *.ingress-devops.ichiayi.com 憑證
sudo certbot \
-d *.ingress-devops.ichiayi.com \
--manual --preferred-challenges dns certonly --server https://acme-v02.api.letsencrypt.org/directory
產生的憑證檔(**cert.pem privkey.pem**)應該會出現在 /etc/letsencrypt/live/ingress-devops.ichiayi.com 內
- 將憑證匯入 K8s (建立 secret tls -> **ingress-wildcard-tls**)
kubectl create secret tls ingress-wildcard-tls --cert=cert.pem --key=privkey.pem
- 設定 K8s 預設憑證為 ingress-wildcard-tls (RKE 安裝的 K8s + Rancher v2.4)
- 透過 Rancher Web admin 登入的管理介面
- cluster -> System -> Resources -> Workloads 找到 nginx-ingress-controller -> View/Edit YAML
- 加上指定預設憑證為 ingress-wildcard-tls : --default-ssl-certificate=default/ingress-wildcard-tls ++看 Rancher 的編輯畫面|{{:tech:2021060901.png|}}++
- SAVE 之後 K8s 的 ingress 服務會重新啟動, 等 Ingress 服啟動後就會將預設憑證改成 *.ingress-devops.ichiayi.com
- 檢查之前已在 K8s 內透過 ingress 的 https 連線網頁, 應該可以透過瀏覽器看到 SSL 憑證已經更換
===== Exp2. 對 sonarqube-devops.ichiayi.com 這服務來設定 SSL 憑證 =====
- 確認 DNS 已經設定好 sonarqube-devops.ichiayi.com
$ ping sonarqube-devops.ichiayi.com
PING sonarqube-devops.ichiayi.com (172.16.0.190) 56(84) bytes of data.
64 bytes from iiiDevOps.unassigned-domain (172.16.0.190): icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from iiiDevOps.unassigned-domain (172.16.0.190): icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from iiiDevOps.unassigned-domain (172.16.0.190): icmp_seq=3 ttl=64 time=0.023 ms
- 透過 Let's Encrypt 產生 SSL 憑證
sudo certbot \
-d sonarqube-devops.ichiayi.com \
--manual --preferred-challenges dns certonly --server https://acme-v02.api.letsencrypt.org/directory
產生的憑證檔(**cert.pem privkey.pem**)應該會出現在 /etc/letsencrypt/live/sonarqube-devops.ichiayi.com 內
- 將憑證匯入 K8s 內 (建立 secret tls-> **sonarqube-devops-tls** , namespace -> **my-devops**)
kubectl create secret tls sonarqube-devops-tls --cert=cert.pem --key=privkey.pem -n my-devops
- 設定 K8s 使用 secret tls (修改 sonarqube-devops.ichiayi.com 的 ingress 設定) Exp. sonar-server-ingress-ssl.yaml
* 將 nginx.ingress.kubernetes.io/force-ssl-redirect: -> **true**
* spec: 增加 tls: 設定
tls:
- hosts:
- "sonarqube-devops.ichiayi.com"
secretName: sonarqube-devops-tls
* 修改後類似以下的結果
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: sonarqube-ing
namespace: my-devops
annotations:
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
tls:
- hosts:
- "sonarqube-devops.ichiayi.com"
secretName: sonarqube-devops-tls
rules:
- host: "sonarqube-devops.ichiayi.com"
http:
paths:
- backend:
serviceName: sonarqube-server-service
servicePort: 9000
path: "/"
pathType: "ImplementationSpecific"
* 透過 kubectl 讓 ingress 生效
kubectl apply -f sonar-server-ingress-ssl.yaml
* 檢查 https://sonarqube-devops.ichiayi.com 所看到的憑證應該就會是設定的憑證
===== 參考網址 =====
* https://kubernetes.io/docs/tasks/tls/managing-tls-in-a-cluster/
* https://kubernetes.io/docs/tasks/tls/manual-rotation-of-ca-certificates/
* https://github.com/ietf-wg-acme/acme/
* https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-kubectl/
{{tag>k8s cert}}