cert-manager管理內網k8s開發環境證書
阿新 • • 發佈:2020-12-04
目的
內網k8s開發環境配置HTTPS,保持與生產環境的配置的一致性,其必要性有:
- PWA開發,HTTPS是必要條件
- 網頁引入HTTP資源,如果開發環境是HTTP就不會被開發和測試人員發現,造成生產環境故障
- HTTP/2,與HTTP相差太大,必須保持環境一致
cert-manager介紹
cert-manager是Kubernetes的附加元件,用於自動管理和頒發各種發行來源的TLS證書。它將確保證書有效並定期更新,並嘗試在到期前的適當時間更新證書。
方法
開發環境在內網,做不了域名驗證,無法使用Let's Encrypt頒發和自動更新證書,所以採用自簽名CA證書+由此CA頒發證書
的方式。
- 建立自簽名發行者
- 生成CA證書
- 建立CA發行者(ClusterIssuer)
- 生成網站證書
- 將網站證書配置到Ingress
實施
前提:
- Kubernetes環境
- 開發機器已配置hosts,域名
site.example.com
指向Ingress對外ip - 站點已部署至k8s,Ingress開NodePort埠http30080、https30443,即現在可通過
http://site.example.com:30080
訪問到nginx站點
1、建立自簽名發行者
# selfsigned-issuer.issuer.yaml # 參考:https://cert-manager.io/docs/configuration/selfsigned/ apiVersion: cert-manager.io/v1 kind: Issuer metadata: name: selfsigned-issuer namespace: cert-manager spec: selfSigned: {}
2、生成CA證書
# ca-example-com.certificate.cert-manager.yaml # 參考:https://cert-manager.io/docs/usage/certificate/ # api參考:https://cert-manager.io/docs/reference/api-docs/#cert-manager.io/v1alpha3.Certificate apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: ca-example-com ### namespace: cert-manager ### 修改為cert-manager的namespace,以讓ClusterIssuer的CA Issuer可以使用此證書 spec: # Secret names are always required. secretName: ca-example-com-tls ### Secret名字 duration: 2160h # 90d renewBefore: 360h # 15d subject: organizations: - Example Inc. ### # The use of the common name field has been deprecated since 2000 and is # discouraged from being used. commonName: ca.example.com ### isCA: true ### 修改為true,isCA將將此證書標記為對證書籤名有效。這會將cert sign自動新增到usages列表中。 privateKey: algorithm: RSA encoding: PKCS1 size: 2048 #usages: ### 註釋了usages,使用情況是證書要求的x509使用情況的集合。預設為digital signature,key encipherment如果未指定。 # - server auth # - client auth # At least one of a DNS Name, URI, or IP address is required. dnsNames: - ca.example.com ### #uris: ### 註釋了uris、ipAddresses #- spiffe://cluster.local/ns/sandbox/sa/example #ipAddresses: #- 192.168.0.5 # Issuer references are always required. issuerRef: name: selfsigned-issuer ### 指定為自簽名發行人 # We can reference ClusterIssuers by changing the kind here. # The default value is Issuer (i.e. a locally namespaced Issuer) kind: Issuer # This is optional since cert-manager will default to this value however # if you are using an external issuer, change this to that issuer group. group: cert-manager.io
- ###為相對於參考的修改項
- 我們將要把CA Issuer建立為ClusterIssuer,因ClusterIssuer只能訪問
cert-manager
下的Secret,所以這個CA Certificate建立在此名字空間下,其Secret也會被建立在此名字空間下。當然也可以更改ClusterIssuer預設可訪問的名字空間,參考:https://cert-manager.io/docs/faq/cluster-resource/
3、建立CA發行者(ClusterIssuer)
# ca-issuer.clusterissuer.yaml
# 參考:https://cert-manager.io/docs/configuration/ca/
apiVersion: cert-manager.io/v1
kind: ClusterIssuer ### ClusterIssuer
metadata:
name: ca-issuer
namespace: cert-manager ### ClusterIssuer下namespace無效
spec:
ca:
secretName: ca-example-com-tls ###
- ###為相對於參考的修改項
- CA Issuer建立為ClusterIssuer,可為其他名字空間的Certificate發行證書
4、生成網站證書
# site-example-com.certificate.example-com.yaml
# 參考:https://cert-manager.io/docs/usage/certificate/
# api參考:https://cert-manager.io/docs/reference/api-docs/#cert-manager.io/v1alpha3.Certificate
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: site-example-com ###
namespace: example-com ### 站點所在名字空間
spec:
# Secret names are always required.
secretName: site-example-com-tls ### Secret名字
duration: 2160h # 90d
renewBefore: 360h # 15d
subject:
organizations:
- Example Inc. ###
# The use of the common name field has been deprecated since 2000 and is
# discouraged from being used.
commonName: site.example.com ###
isCA: false
privateKey:
algorithm: RSA
encoding: PKCS1
size: 2048
#usages: ### 註釋了usages,使用情況是證書要求的x509使用情況的集合。預設為digital signature,key encipherment如果未指定。
# - server auth
# - client auth
# At least one of a DNS Name, URI, or IP address is required.
dnsNames:
- site.example.com ###
#uris: ### 註釋了uris、ipAddresses
#- spiffe://cluster.local/ns/sandbox/sa/example
#ipAddresses:
#- 192.168.0.5
# Issuer references are always required.
issuerRef:
name: ca-issuer ### 使用CA Issuer
# We can reference ClusterIssuers by changing the kind here.
# The default value is Issuer (i.e. a locally namespaced Issuer)
kind: ClusterIssuer ### CA Issuer是ClusterIssuer
# This is optional since cert-manager will default to this value however
# if you are using an external issuer, change this to that issuer group.
group: cert-manager.io
- ###為相對於參考的修改項
5、將網站證書配置到Ingress
# site-example-com.ingress.example-com.yaml
# 參考:https://kubernetes.io/zh/docs/concepts/services-networking/ingress/#tls
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
name: site-example-com
namespace: example-com
annotations:
kubernetes.io/ingress.class: nginx
spec:
tls:
- hosts:
- site.example.com
secretName: site-example-com-tls
rules:
- host: site.example.com
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
serviceName: nginx
servicePort: 80
6、將CA證書安裝至本地
獲取CA證書——ca-example-com-tls.secret.cert-manager
裡的tls.crt
檔案,拷貝至開發機器上,windows直接開啟安裝證書至受信任的根證書頒發機構