cert-manager simplifies and automates the management of security certificates for Kubernetes. The following procedure describes how to configure cert-manager to generate certificates for MongoDB Kubernetes Operator resources.
先决条件
要使用对象部署副本集,您必须:
拥有或安装适用于Kubernetes Operator 的MongoDB控制器。
注意
为避免在单集群Kubernetes部署中存储密钥,您可以将所有密钥迁移到密钥存储工具。多个Kubernetes集群上的部署不支持将密钥存储在密钥存储工具中,例如 HashiCorp Vault 。
为以下每个组件生成一个 TLS 证书:
您的副本集。确保为每个托管证书副本集节点的 Kubernetes 容器添加 SAN。
在您的 TLS 证书中,每个 pod 的 SAN 必须使用以下格式:
<pod-name>.<metadata.name>-svc.<namespace>.svc.cluster.local 重要
If you're using an ACME based service provider such as Let's Encrypt to issue TLS certificates, the provider might prohibit you from adding the Pod's default FQDNs (
*.svc.cluster.local) to SANs in the certificate.要使用基于 ACME 的证书,您必须为副本集资源配置证书。要学习;了解更多信息,请参阅操作步骤中有关基于 ACME 的 TLS 证书的步骤。
您项目的 MongoDB Agent。对于 MongoDB Agent 证书,请确保您满足以下要求:
TLS 证书中的公用名不为空。
每个 TLS 证书中的组合组织和组织单位与副本集节点的 TLS 证书中的组织和组织单位不同。
You must have the CA certificate file and name it
ca-pem.您必须拥有用于签署TLS证书的密钥。
重要
Kubernetes Operator 使用 kubernetes.io/tls secrets 来存储Ops Manager和MongoDB资源的 TLS 证书和私钥。从Kubernetes Operator 版本 1.17.0 开始, Kubernetes Operator 不支持存储为不透明密钥的连接 PEM 文件。
要使用对象部署副本集,您必须:
拥有或安装适用于Kubernetes Operator 的MongoDB控制器。
注意
为避免在单集群Kubernetes部署中存储密钥,您可以将所有密钥迁移到密钥存储工具。多个Kubernetes集群上的部署不支持将密钥存储在密钥存储工具中,例如 HashiCorp Vault 。
步骤
创建 CA 密钥。
注意
The following steps assume that you have already created a custom CA along with the corresponding tls.key private key and tls.crt signed certificate.
创建密钥来存储CA数据:
apiVersion: v1 kind: Secret metadata: name: ca-key-pair namespace: <namespace> data: tls.crt: <your-CA-certificate> tls.key: <your-CA-private-key>
为自定义CA证书添加其他证书。
If your Ops Manager TLS certificate is signed by a custom CA, the CA certificate must also contain additional certificates that allow Ops Manager Backup Daemon to download MongoDB binaries from the Internet. To create the TLS certificate(s), create a ConfigMap to hold the CA certificate:
重要
Kubernetes 操作符要求 Ops Manager 证书在 ConfigMap 中命名为mms-ca.crt 。
Obtain the entire TLS certificate chain for Ops Manager from
downloads.mongodb.com. The followingopensslcommand outputs the certificate in the chain to your current working directory, in.crtformat:openssl s_client -showcerts -verify 2 \ -connect downloads.mongodb.com:443 -servername downloads.mongodb.com < /dev/null \ | awk '/BEGIN/,/END/{ if(/BEGIN/){a++}; out="cert"a".crt"; print >out}' Concatenate your CA's certificate file for Ops Manager with the entire TLS certificate chain from
downloads.mongodb.comthat you obtained in the previous step:cat <custom_ca_cert.pem> cert2.crt cert3.crt cert4.crt >> mms-ca.crt 注意
将占位符
<custom_ca_cert.pem>替换为您的自定义 CA 证书 PEM文件。请勿包含
cert1.crt文件,因为不应包含来自MongoDB的服务器证书。
为MongoDB Ops Manager创建 ConfigMap:
kubectl create configmap om-http-cert-ca --from-file="mms-ca.crt"
创建 CA ConfigMap
Create a ConfigMap containing your CA. It must have two fields, ca-pem and mms-ca.crt, both pointing to your CA certificate. Replace <CA-certificate> with the path to your CA certificate.
kubectl create cm ca-issuer --from-file=ca-pem=<CA-certificate> \ --from-file=mms-ca.crt=<CA-certificate>
为 MongoDB 资源创建证书
要使用生成的证书保护 MongoDB 资源,您必须为资源本身和 MongoDB 代理创建证书。
创建 MongoDB 资源证书。 以下示例假设设置名称为
my-replica-set的副本集有三个成员:注意
spec.issuerRef.name参数引用之前创建的CA ConfigMap。apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: my-replica-set-certificate namespace: mongodb spec: dnsNames: - my-replica-set-0 - my-replica-set-0.my-replica-set-svc.mongodb.svc.cluster.local - my-replica-set-1 - my-replica-set-1.my-replica-set-svc.mongodb.svc.cluster.local - my-replica-set-2 - my-replica-set-2.my-replica-set-svc.mongodb.svc.cluster.local duration: 240h0m0s issuerRef: name: ca-issuer renewBefore: 120h0m0s secretName: mdb-my-replica-set-cert usages: - server auth - client auth 对于分片的集群,您必须为每个StatefulSet创建一个证书。要学习;了解有关分片集群配置的更多信息,请参阅部署分片集群。
创建 MongoDB 代理证书:
注意
spec.issuerRef.name参数引用之前创建的CA ConfigMap。apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: agent-certs namespace: mongodb spec: commonName: automation dnsNames: - automation duration: 240h0m0s issuerRef: name: ca-issuer renewBefore: 120h0m0s secretName: mdb-my-replica-set-agent-certs usages: - digital signature - key encipherment - client auth subject: countries: - US localities: - NY organizationalUnits: - a-1635241837-m5yb81lfnrz organizations: - cluster.local-agent provinces: - NY 创建 MongoDB 资源:
注意
如果未指定
spec.security.tls.ca参数,则默认为{replica-set}-ca。apiVersion: mongodb.com/v1 kind: MongoDB metadata: name: my-replica-set namespace: mongodb spec: type: ReplicaSet members: 3 version: 8.0.0 opsManager: configMapRef: name: my-project credentials: my-credentials security: certsSecretPrefix: mdb authentication: enabled: true modes: - X509 tls: ca: ca-issuer enabled: true
使用 TLS 为 Ops Manager 和 AppDB 创建证书
要保护 Ops Manager 资源,必须首先为 Ops Manager 和 AppDB 创建证书,然后创建 Ops Manager 资源。
创建 Ops Manager 证书:
注意
spec.issuerRef.name参数引用之前创建的CA ConfigMap。apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: cert-for-ops-manager namespace: mongodb spec: dnsNames: - om-with-https-svc.mongodb.svc.cluster.local duration: 240h0m0s issuerRef: name: ca-issuer renewBefore: 120h0m0s secretName: mdb-om-with-https-cert usages: - server auth - client auth 创建 AppDB 证书:
注意
spec.issuerRef.name参数引用之前创建的CA ConfigMap。apiVersion: cert-manager.io/v1 kind: Certificate metadata: name: appdb-om-with-https-db-cert namespace: mongodb spec: dnsNames: - om-with-https-db-0 - om-with-https-db-0.om-with-https-db-svc.mongodb.svc.cluster.local - om-with-https-db-1 - om-with-https-db-1.om-with-https-db-svc.mongodb.svc.cluster.local - om-with-https-db-2 - om-with-https-db-2.om-with-https-db-svc.mongodb.svc.cluster.local duration: 240h0m0s issuerRef: name: ca-issuer renewBefore: 120h0m0s secretName: appdb-om-with-https-db-cert usages: - server auth - client auth 创建 Ops Manager 资源:
apiVersion: mongodb.com/v1 kind: MongoDBOpsManager metadata: name: om-with-https namespace: mongodb spec: adminCredentials: ops-manager-admin-secret version: "8.0.0" applicationDatabase: members: 3 security: certsSecretPrefix: appdb tls: ca: ca-issuer version: 8.0.0-ubi8 replicas: 1 security: certsSecretPrefix: mdb tls: ca: ca-issuer
续订证书
cert-manager 将在以下情况下续订证书:
证书根据其
spec.duration和spec.renewBefore字段过期。删除持有证书的密钥。 在这种情况下,cert-经理会根据证书自定义资源中的配置重新创建密钥。
您可以更改证书自定义资源的配置。 在这种情况下,cert-经理在检测到其配置更改时会重新创建包含证书的密钥。