You can modify the containers in the Pods in which Ops Manager and MongoDB database resources run using the template or podTemplate setting that applies to your deployment:
MongoDB database:
spec.podSpec.podTemplateOps Manager:
spec.statefulSet.spec.template
要查看哪些字段可以添加到 template 或 podTemplate,请参阅Kubernetes文档。
当您使用template或podTemplate创建container时,Kubernetes 操作符会根据您为containers数组中每个container提供的name以不同方式处理container创建:
If the
namefield matches the name of the applicable resource image, the Kubernetes Operator updates the Ops Manager or MongoDB database container in the Pod to which thetemplateorpodTemplateapplies:Ops Manager:
mongodb-kubernetes-ops-managerBackup Daemon Service:
mongodb-backup-daemonMongoDB database:
mongodb-enterprise-database应用程序数据库:
mongodb-kubernetes-appdb
If the
namefield does not match the name of the applicable resource image, the Kubernetes Operator creates a new container in each Pod to which thetemplateorpodTemplateapplies.
为 MongoDB Kubernetes 资源定义卷挂载
On-disk files in containers in Pods don't survive container crashes or restarts. Using the spec.podSpec.podTemplate setting, you can add a volume mount to persist data in a MongoDB database resource for the life of the Pod.
要为 MongoDB database 资源创建卷挂载,请执行以下操作:
更新 MongoDB database 资源定义,以包含 Kubernetes Operator 创建的 database Pod 中的 container 的卷挂载。
例子
使用
spec.podSpec.podTemplate定义卷挂载:podSpec: podTemplate: spec: containers: - name: mongodb-enterprise-database volumeMounts: - mountPath: </new/mount/path> name: survives-restart volumes: - name: survives-restart emptyDir: {} 应用更新的资源定义:
kubectl apply -f <database-resource-conf>.yaml -n <metadata.namespace>
使用 InitContainer 调整 MongoDB Kubernetes 资源 Docker 映像
MongoDB 资源 Docker 映像在 RHEL 上运行并使用 RHEL 的默认系统配置。 要调整MongoDB 资源容器中的底层 RHEL 系统配置,请添加特权 InitContainer 初始化容器 使用以下设置之一:
spec.podSpec.podTemplate:将特权 InitContainer 添加到 MongoDB 数据库资源容器。spec.statefulSet.spec.template:将特权 InitContainer 添加到 Ops Manager 资源容器。
例子
MongoDB database 资源 Docker 映像使用 RHEL 默认keepalive时间7200 。对于数据库部署,MongoDB 建议缩短keepalive 的120 时间。
如果在客户端与数据库资源之间的通信中遇到网络超时或套接字错误,您可以调整数据库资源 Docker 映像中的keepalive时间。
提示
TCP keepalive 时间会影响 MongoDB 部署吗?在 MongoDB 手册中
要为 MongoDB database container 调整 Docker 映像,请执行以下操作:
更新 MongoDB database 资源定义,将特权 InitContainer 附加到 Kubernetes Operator 创建的数据库 pods。
例子
将
spec.podSpec.podTemplatekeepalive值更改为120的推荐值:spec: podSpec: podTemplate: spec: initContainers: - name: "adjust-tcp-keepalive" image: "busybox:latest" securityContext: privileged: true command: ["sysctl", "-w", "net.ipv4.tcp_keepalive_time=120"] 应用更新的资源定义:
kubectl apply -f <database-resource-conf>.yaml -n <metadata.namespace>
Kubernetes adds a privileged InitContainer to each Pod that the Kubernetes Operator creates using the MongoDB resource definition.
打开与数据库资源shell Pod 中正在运行的容器的 会话 并验证您的更改。
例子
要遵循前面的keepalive示例,请调用以下命令以获取当前的keepalive值:
kubectl exec -n <metadata.namespace> -it <pod-name> -- cat /proc/sys/net/ipv4/tcp_keepalive_time 120
提示
MongoDB 手册中的操作系统配置。
使用 Dockerfile 模板构建自定义映像
您可以修改 MongoDB Dockerfile 模板,以创建适合您的使用案例的自定义 Kubernetes 操作符映像。要构建自定义映像,您需要:
从 MongoDB 模板修改而来的自定义 Dockerfile。
MongoDB 为模板提供的上下文映像。
MongoDB Dockerfile 模板
用于构建容器映像的 Dockerfile 可从MongoDB Kubernetes Github存储库公开获取。
Dockerfile 目录按资源名称、版本和分布进行组织:
├── <resource name> │ └── <image version> │ └── <base distribution> │ └── Dockerfile template
将要使用的模板复制到自己的 Dockerfile 中,并根据需要进行修改。
上下文图像
要从任何 MongoDB Dockerfile 模板构建映像,您必须提供其上下文映像。
Each Dockerfile template has one associated context image, retrievable from the same Quay registry as the original images. Context image are always tagged in the format quay.io/mongodb/<resource-name>:<image-version>-context.
要向docker build提供上下文图像,请包含--build-arg选项,并将imagebase变量设置为 Quay.io 标签,其中<resource-name>和<image-version>与您的 Dockerfile 模板匹配。
例子
如果您想为任何发行版构建mongodb-enterprise-database版本 2.0.0 映像,请包括:
--build-arg imagebase=quay.io/mongodb/mongodb-kubernetes-database:2.0.0-context
docker build 例子
默认情况下, mongodb-kubernetes-operator版本 1.9.1 的 Ubuntu 发行版基于ubuntu:1604 。 在此示例中,该基本 Dockerfile 模板已修改为使用ubuntu:1804并保存为myDockerfile 。
以下命令可构建自定义映像并为其赋予标签1.9.1-ubuntu-1804 :
cat myDockerfile | docker build --build-arg imagebase=quay.io/mongodb/mongodb-kubernetes-operator:1.9.1-context \ --tag mongodb-kubernetes-operator:1.9.1-ubuntu-1804 -
注意
在docker build末尾包含连字符 ( - ) 以读取cat myDockerfile的输出,而不是提供本地目录作为构建上下文。
提示
要学习;了解有关 docker build 的详情,请参阅Docker文档。