内网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直接打开安装证书至受信任的根证书颁发机构
测试