內網k8s開發環境配置HTTPS,保持與生產環境的配置的一致性,其必要性有:nginx
cert-manager是Kubernetes的附加組件,用於自動管理和頒發各類發行來源的TLS證書。它將確保證書有效並按期更新,並嘗試在到期前的適當時間更新證書。git
開發環境在內網,作不了域名驗證,沒法使用Let's Encrypt頒發和自動更新證書,因此採用自簽名CA證書+由此CA頒發證書
的方式。windows
前提:api
site.example.com
指向Ingress對外iphttp://site.example.com:30080
訪問到nginx站點# 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: {}
# 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
cert-manager
下的Secret,因此這個CA Certificate建立在此名字空間下,其Secret也會被建立在此名字空間下。固然也能夠更改ClusterIssuer默承認訪問的名字空間,參考:https://cert-manager.io/docs/faq/cluster-resource/# 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 ###
# 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
# 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
獲取CA證書——ca-example-com-tls.secret.cert-manager
裏的tls.crt
文件,拷貝至開發機器上,windows直接打開安裝證書至受信任的根證書頒發機構
測試