This post is the basic guide on installing the MongoDB replica set and providing a UI access to it on a Kubernetes cluster.
这篇文章是有关安装MongoDB副本集并在Kubernetes集群上提供对它的UI访问的基本指南。
I assume that you are already familiar with Kubernetes. I will use Google Kubernetes Engine as the Kubernetes environment, but you should be able to use any other environment. Before starting, make sure you are connected to the Kubernetes cluster.
我假设您已经熟悉Kubernetes。 我将使用Google Kubernetes Engine作为Kubernetes环境,但是您应该可以使用任何其他环境。 在开始之前,请确保您已连接到Kubernetes集群。
安装MongoDB Kubernetes运算符 (Install MongoDB Kubernetes Operator)
To install MongoDB replica set we will need to install an operator first. We will use the MongoDB Community Kubernetes Operator. For the latest and full version of installation steps, visit the official repository. Short summary:
要安装MongoDB副本集,我们需要首先安装一个操作员。 我们将使用MongoDB社区Kubernetes运营商 。 有关安装步骤的最新和完整版本,请访问官方存储库 。 简短的摘要:
# Create a namespace
kubectl create
# Download and install the operator
git clone https://github.com/mongodb/mongodb-kubernetes-operator.git
cd mongodb-kubernetes-operator
kubectl create -f deploy/crds/mongodb.com_mongodb_crd.yaml
kubectl create -f deploy/ --namespace mongodb
To make sure that the operator is installed, check if all resources are in the Ready state:
要确保已安装操作员,请检查所有资源是否都处于“就绪”状态:
kubectl get all
The output should look like this:
输出应如下所示:
![Image for post](https://miro.medium.com/max/9999/1*zG0P97VAdQkI0tL2Kceshg.png)
部署测试副本集 (Deploy a test replica set)
Now you have the operator installed. Let’s deploy a test replica set:
现在,您已经安装了操作员。 让我们部署一个测试副本集:
kubectl apply -f deploy/crds/mongodb.com_v1_mongodb_cr.yaml --namespace mongodb
The deployment of the replica set will take some time (up to 5 minutes). Let’s wait until its phase is ‘Running’. To check the current state, run this command:
副本集的部署将花费一些时间(最多5分钟)。 让我们等到其阶段为“正在运行”。 要检查当前状态,请运行以下命令:
kubectl get mdb
![Image for post](https://miro.medium.com/max/9999/1*Dk-KxTApCH4CwZPneX4W7Q.png)
Now your replica set is ready to operate. You can connect to it from inside the cluster using the following connection string:
现在,您的副本集可以使用了。 您可以使用以下连接字符串从集群内部连接到它:
mongodb://example-mongodb-0.example-mongodb-svc.mongodb.svc.cluster.local:27017,example-mongodb-1.example-mongodb-svc.mongodb.svc.cluster.local:27017,example-mongodb-2.example-mongodb-svc.mongodb.svc.cluster.local:27017/?replicaSet=example-mongodb
部署mongo-express (Deploy mongo-express)
Let’s continue and deploy a mongo-express to get the UI access to the replica set:
让我们继续并部署mongo-express以获得对副本集的UI访问:
kubectl apply -f https://gist.githubusercontent.com/StMarian/6b8aa3366ee481d9c2bc79d8ddf0bacc/raw/0a949ad9cad50d3ec8b2df40283efb9633e3e1da/mongo-express.yaml
This will apply the mongo-express deployment and expose it using a LoadBalancer.
这将应用mongo-express部署,并使用LoadBalancer公开它。
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mongo-express
name: mongo-express
namespace: mongodb
spec:
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- env:
- name: ME_CONFIG_MONGODB_SERVER
value: "example-mongodb-0.example-mongodb-svc.mongodb.svc.cluster.local,example-mongodb-1.example-mongodb-svc.mongodb.svc.cluster.local,example-mongodb-2.example-mongodb-svc.mongodb.svc.cluster.local"
- name: ME_CONFIG_MONGODB_ENABLE_ADMIN
value: "true"
- name: ME_CONFIG_BASICAUTH_USERNAME
value: "admin-username"
- name: ME_CONFIG_BASICAUTH_PASSWORD
value: "admin-password"
image: mongo-express:latest
imagePullPolicy: Always
name: mongo-express
ports:
- containerPort: 8081
protocol: TCP
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
labels:
app: mongo-express
name: mongo-express
namespace: mongodb
spec:
ports:
- name: http
port: 8081
protocol: TCP
selector:
app: mongo-express
type: LoadBalancer
As you can see from the code above, we set up some configuration variables for the mongo-express. The most important one is the ME_CONFIG_MONGODB_SERVER — here we provide a comma-delimited list of the hostnames of the pods, where the replica set instances are located. For more details on the configuration of the mongo-express, visit the official documentation.
从上面的代码中可以看到,我们为mongo-express设置了一些配置变量。 最重要的一个是ME_CONFIG_MONGODB_SERVER-在这里,我们提供了以逗号分隔的Pod主机名列表,副本集实例位于该Pod的主机名中。 有关mongo-express的配置的更多详细信息,请访问官方文档 。
For any non-test projects, you should update the username and password.
对于任何非测试项目,您应该更新用户名和密码。
Note, that this will not run on clusters that do not provide LoadBalancer support (minikube, kubeadm). In order to run this deployment on such clusters, change the type of the mongo-express Service from ‘LoadBalancer’ to ‘NodePort’.
请注意,这不会在不提供LoadBalancer支持的群集(minikube,kubeadm)上运行。 为了在此类群集上运行此部署,请将mongo-express Service的类型从“ LoadBalancer”更改为“ NodePort”。
Now let’s wait until the mongo-express service receives the external IP:
现在,让我们等到mongo-express服务收到外部IP:
![Image for post](https://miro.medium.com/max/9999/1*qPQN_iQLPB9cjXAZ4abELw.png)
Once the mongo-express service got its IP, you can go ahead and browse its 8081 port. You will see the login window where you should provide credentials from the mongo-express.yaml:
mongo-express服务获得其IP后,就可以继续浏览其8081端口。 您将看到登录窗口,您应该在其中提供mongo-express.yaml的凭据:
![Image for post](https://miro.medium.com/max/9999/1*0o-996ARqss-dHzX9egPzw.png)
After the login you’ll see the mongo-express UI:
登录后,您将看到mongo-express UI:
![Image for post](https://miro.medium.com/max/9999/1*7dsjdXFaDlzgFPHMRaVyvQ.png)
翻译自: https://medium.com/@stychuk.marian/mongodb-replica-set-on-kubernetes-mongo-express-ui-3ee96804252