mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署

mongodb分布式部署

Storytelling is one of the parts of being a Developer Advocate that I enjoy. Sometimes the stories are about the special moments when the team comes together to keep a system running or build it faster. But there are less than glorious tales to be told about the software deployments I’ve been involved in. And for situations where we needed to deploy several times a day, now we are talking nightmares.

讲故事是成为我所喜欢的开发者倡导者的一部分。 有时,这些故事是关于团队聚集在一起以保持系统运行或更快构建的特殊时刻。 但是,关于我所参与的软件部署,没有比这更辉煌的故事了。对于我们每天需要部署几次的情况,现在我们正在做噩梦。

For some time, I worked at a company that believed that deploying to production several times a day was ideal for project velocity. Our team was working to ensure that advertising software across our media platform was always being updated and released. One of the issues was a lack of real automation in the process of applying new code to our application servers.

一段时间以来,我在一家公司工作,我认为一天进行几次生产部署是项目速度的理想选择。 我们的团队正在努力确保整个媒体平台上的广告软件始终得到更新和发布。 问题之一是在将新代码应用于我们的应用服务器的过程中缺乏真正的自动化。

What both ops and development teams had in common was a desire for improved ease and agility around application and configuration deployments. In this article, I’ll present some of my experiences and cover how MongoDB Atlas and Kubernetes can be leveraged together to simplify the process of deploying and managing applications and their underlying dependencies.

运维团队和开发团队的共同点是希望围绕应用程序和配置部署提高易用性和敏捷性。 在本文中,我将介绍一些经验,并介绍如何将MongoDB Atlas和Kubernetes一起使用,以简化部署和管理应用程序及其底层依赖项的过程。

Let's talk about how a typical software deployment unfolded:

让我们讨论一下典型的软件部署是如何进行的:

  1. The developer would send in a ticket asking for the deployment

    开发人员将发送票证以要求部署
  2. The developer and I would agree upon a time to deploy the latest software revision

    我和开发人员会商定时间来部署最新的软件版本
  3. We would modify an existing bash script with the appropriate git repository version info

    我们将使用适当的git仓库版本信息修改现有的bash脚本
  4. We’d need to manually back up the old deployment

    我们需要手动备份旧的部署
  5. We’d need to manually create a backup of our current database

    我们需要手动创建当前数据库的备份
  6. We’d watch the bash script perform this "Deploy" on about six servers in parallel

    我们将观看bash脚本在大约六台服务器上并行执行此“部署”
  7. Wave a dead chicken over my keyboard

    在我的键盘上挥动一只死鸡

Some of these deployments would fail, requiring a return to the previous version of the application code. This process to "rollback" to a prior version would involve me manually copying the repository to the older version, performing manual database restores, and finally confirming with the team that used this system that all was working properly. It was a real mess and I really wasn't in a position to change it.

其中一些部署将失败,需要返回到应用程序代码的先前版本。 这个“回滚”到先前版本的过程将涉及到我手动将存储库复制到较早的版本,执行手动数据库还原,最后与使用该系统的团队确认所有工作正常。 这真是一团糟,我真的无能为力。

I eventually moved into a position which gave me greater visibility into what other teams of developers, specifically those in the open source space, were doing for software deployments. I noticed that — surprise! — people were no longer interested in doing the same work over and over again.

最终,我进入了一个职位,使我对其他开发人员团队(尤其是开源领域的其他团队)在进行软件部署方面的工作有了更大的了解。 我注意到了-感到惊讶! -人们不再对一遍又一遍地进行相同的工作感兴趣。

Developers and their supporting ops teams have been given keys to a whole new world in the last few years by utilizing containers and automation platforms. Rather than doing manual work required to produce the environment that your app will live in, you can deploy applications quickly thanks to tools like Kubernetes.

通过使用容器和自动化平台,在过去的几年中,开发人员及其支持的操作团队已获得了通往全新世界的钥匙。 借助Kubernetes之类的工具,您可以快速部署应用程序,而无需执行产生应用程序将要生存的环境所需的手动工作。

什么是Kubernetes? ( What's Kubernetes? )

Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications. Kubernetes can help reduce the amount of work your team will have to do when deploying your application. Along with MongoDB Atlas, you can build scalable and resilient applications that stand up to high traffic or can easily be scaled down to reduce costs. Kubernetes runs just about anywhere and can use almost any infrastructure. If you're using a public cloud, a hybrid cloud or even a bare metal solution, you can leverage Kubernetes to quickly deploy and scale your applications.

Kubernetes是一个开源系统,用于自动化容器化应用程序的部署,扩展和管理。 Kubernetes可以帮助减少团队在部署应用程序时必须要做的工作量。 与MongoDB Atlas一起,您可以构建可扩展且具有弹性的应用程序,这些应用程序可以承受高流量,也可以轻松缩小规模以降低成本。 Kubernetes几乎可以在任何地方运行,并且可以使用几乎任何基础架构。 如果您正在使用公共云,混合云甚至是裸机解决方案,则可以利用Kubernetes快速部署和扩展应用程序。

The Google Kubernetes Engine is built into the Google Cloud Platform and helps you quickly deploy your containerized applications.

Google Kubernetes Engine内置在Google Cloud Platform中,可帮助您快速部署容器化的应用程序。

For the purposes of this tutorial, I will upload our image to GCP and then deploy to a Kubernetes cluster so I can quickly scale up or down our application as needed. When I create new versions of our app or make incremental changes, I can simply create a new image and deploy again with Kubernetes.

在本教程中,我将映像上传到GCP,然后部署到Kubernetes集群,以便可以根据需要快速扩展或缩小应用程序。 当我创建应用程序的新版本或进行增量更改时,我可以简单地创建一个新映像并再次使用Kubernetes进行部署。

为什么选择Kubernetes的Atlas? ( Why Atlas with Kubernetes? )

By using these tools together for your MongoDB Application, you can quickly produce and deploy applications without worrying much about infrastructure management. Atlas provides you with a persistent data-store for your application data without the need to manage the actual database software, replication, upgrades, or monitoring. All of these features are delivered out of the box, allowing you to build and then deploy quickly.

通过将这些工具一起用于MongoDB应用程序, 您可以快速生成和部署应用程序,而不必担心基础架构管理。 Atlas为您的应用程序数据提供了持久的数据存储,而无需管理实际的数据库软件,复制,升级或监视。 所有这些功能都是开箱即用的,使您可以构建然后快速部署。

In this tutorial, I will build a MongoDB Atlas cluster where our data will live for a simple Node.js application. I will then turn the app and configuration data for Atlas into a container-ready image with Docker.

在本教程中,我将构建一个MongoDB Atlas集群,其中的数据将存储在一个简单的Node.js应用程序中。 然后,我将使用Docker将Atlas的应用程序和配置数据转换为容器就绪的映像。

MongoDB Atlas is available across most regions on GCP so no matter where your application lives, you can keep your data close by (or distributed) across the cloud.

MongoDB Atlas在GCP的大多数区域均可使用,因此无论您的应用程序位于何处,都可以在云中保持(或分散)数据。

mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第1张图片

要求 ( Requirements )

To follow along with this tutorial, users will need some of the following requirements to get started:

要继续学习本教程,用户将需要满足以下一些要求才能开始使用:

  • Google Cloud Platform Account (billing enabled or credits)

    Google Cloud Platform帐户 (已启用计费或信用)
  • MongoDB Atlas Account (M10+ dedicated cluster)

    MongoDB Atlas帐户(M10 +专用集群)
  • Docker

    码头工人
  • Node.js

    Node.js
  • Kubernetes

    Kubernetes
  • Git

    吉特

First, I will download the repository for the code I will use. In this case, it's a basic record keeping app using MongoDB, Express, React, and Node (MERN).

首先,我将下载要使用的代码的存储库。 在这种情况下,它是使用MongoDB,Express,React和Node( MERN )的基本记录保存应用程序。

bash-3.2$git clone [email protected]:cefjoeii/mern-crud.git
Cloning into 'mern-crud'...
remote: Counting objects: 326, done.
remote: Total 326 (delta 0), reused 0 (delta 0), pack-reused 326
Receiving objects: 100% (326/326), 3.26 MiB | 2.40 MiB/s, done.
Resolving deltas: 100% (137/137), done.

cd mern-crud

Next, I will npm install and get all the required npm packages installed for working with our app:

接下来,我将npm installnpm install所有必需的npm软件包以使用我们的应用程序:

> [email protected] install /Users/jaygordon/work/mern-crud/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0

选择Atlas的GCP区域 ( Selecting your GCP Region for Atlas )

Each GCP region includes a set number of independent zones. Each zone has power, cooling, networking, and control planes that are isolated from other zones. For regions that have at least three zones (3Z), Atlas deploys clusters across three zones. For regions that only have two zones (2Z), Atlas deploys clusters across two zones.

每个GCP区域都包含一定数量的独立区域。 每个区域都具有与其他区域隔离的电源,冷却,网络和控制平面。 对于具有至少三个区域(3Z)的区域,Atlas在三个区域中部署群集。 对于只有两个区域(2Z)的区域,Atlas在两个区域之间部署群集。

The Atlas Add New Cluster form marks regions that support 3Z clusters as Recommended, as they provide higher availability. If your preferred region only has two zones, consider enabling cross-region replication and placing a replica set member in another region to increase the likelihood that your cluster will be available during partial region outages.

“ Atlas 添加新群集”表单将支持3Z群集的区域标记为“ 推荐” ,因为它们提供了更高的可用性。 如果您的首选区域只有两个区域,请考虑启用跨区域复制并将副本集成员放在另一个区域中,以增加部分区域中断期间群集可用的可能性。

The number of zones in a region has no effect on the number of MongoDB nodes Atlas can deploy. MongoDB Atlas clusters are always made of replica sets with a minimum of three MongoDB nodes.

区域中的区域数量不会影响Atlas可以部署的MongoDB节点的数量。 MongoDB Atlas群集始终由副本集组成,副本集至少包含三个MongoDB节点。

For general information on GCP regions and zones, see the Google documentation on regions and zones.

有关GCP区域和区域的一般信息,请参阅有关区域和区域的Google文档。

创建集群并添加用户 ( Create Cluster and Add a User )

In the provided image below you can see I have selected the Cloud Provider "Google Cloud Platform." Next, I selected an instance size, in this case an M10. Deployments using M10 instances are ideal for development. If I were to take this application to production immediately, I may want to consider using an M30 deployment. Since this is a demo, an M10 is sufficient for our application. For a full view of all of the cluster sizes, check out the Atlas pricing page. Once I’ve completed these steps, I can click the "Confirm & Deploy" button. Atlas will spin up my deployment automatically in a few minutes.

在下面提供的图像中,您可以看到我选择了云提供商“ Google Cloud Platform”。 接下来,我选择了一个实例大小,在本例中为M10。 使用M10实例进行部署非常适合开发。 如果要立即将此应用程序投入生产,则可能要考虑使用M30部署。 由于这是一个演示,因此M10对于我们的应用程序就足够了。 有关所有群集大小的完整视图,请查看Atlas定价页面 。 完成这些步骤后,我可以单击“确认并部署”按钮。 Atlas将在几分钟内自动启动我的部署。

mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第2张图片

Let’s create a username and password for our database that our Kubernetes deployed application will use to access MongoDB.

让我们为数据库创建一个用户名和密码,我们的Kubernetes部署应用程序将使用该用户名和密码来访问MongoDB。

  • Click "Security" at the top of the page.

    点击页面顶部的“安全性”。
  • Click "MongoDB Users"

    点击“ MongoDB用户”
  • Click "Add New User"

    点击“添加新用户”
  • Click "Show Advanced Options"

    点击“显示高级选项”
  • We'll then add a user "mernuser" for our mern-crud app that only has access to a database named "mern-crud" and give it a complex password. We'll specify readWrite privileges for this user:

    然后,我们mern-crud应用程序添加一个用户“ mernuser ”,该用户只能访问名为“ mern-crud ”的数据库,并为其提供复杂的密码。 我们将为此用户指定readWrite特权:
mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第3张图片

Click "Add User"

点击“添加用户”

Your database is now created and your user is added. You still need our connection string and to whitelist access via the network.

现在已创建数据库并添加了用户。 您仍然需要我们的连接字符串并通过网络将访问权限列入白名单 。

连接字符串 ( Connection String )

Get your connection string by clicking "Clusters" and then clicking "CONNECT" next to your cluster details in your Atlas admin panel. After selecting connect, you are provided several options to use to connect to your cluster. Click "connect your application."

通过单击“群集”,然后在Atlas管理面板中的群集详细信息旁边单击“连接”,获取连接字符串。 选择连接后,将为您提供几个用于连接到集群的选项。 单击“连接您的应用程序”。

mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第4张图片

Options for the 3.6 or the 3.4 versions of the MongoDB driver are given. I built mine using the 3.4 driver, so I will just select the connection string for this version.

提供了3.6或3.4版本的MongoDB驱动程序的选项。 我使用3.4驱动程序构建了我的驱动程序,因此我只选择此版本的连接字符串。

mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第5张图片

I will typically paste this into an editor and then modify the info to match my application credentials and my database name:

我通常将其粘贴到编辑器中,然后修改信息以匹配我的应用程序凭据和数据库名称:

mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第6张图片

I will now add this to the app's database configuration file and save it.

现在,我将其添加到应用程序的数据库配置文件中并保存。

mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第7张图片

Next, I will package this up into an image with Docker and ship it to Google Kubernetes Engine!

接下来,我将使用Docker将其打包成一个映像,并将其发送到Google Kubernetes Engine!

Docker和Google Kubernetes引擎 ( Docker and Google Kubernetes Engine )

Get started by creating an account at Google Cloud, then follow the quickstart to create a Google Kubernetes Project.

首先在Google Cloud上创建一个帐户,然后按照快速入门创建一个Google Kubernetes Project 。

Once your project is created, you can find it within the Google Cloud Platform control panel:

创建项目后,您可以在Google Cloud Platform控制面板中找到它:

mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第8张图片

It's time to create a container on your local workstation:

现在是在本地工作站上创建容器的时候了:

Set the PROJECT_ID environment variable in your shell by retrieving the pre- configured project ID on gcloud with the command below:

通过使用以下命令在gcloud上检索预配置的项目ID,在您的Shell中设置PROJECT_ID环境变量:

export PROJECT_ID="jaygordon-mongodb"
gcloud config set project $PROJECT_ID
gcloud config set compute/zone us-central1-b

Next, place a Dockerfile in the root of your repository with the following:

接下来,使用以下命令将Dockerfile在存储库的根目录中:

FROM node:boron

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY . /usr/src/app

EXPOSE 3000

CMD [npm, start]

To build the container image of this application and tag it for uploading, run the following command:

要构建此应用程序的容器映像并将其标记为要上传,请运行以下命令:

bash-3.2$ docker build -t gcr.io/${PROJECT_ID}/mern-crud:v1 .
Sending build context to Docker daemon  40.66MB
Successfully built b8c5be5def8f
Successfully tagged gcr.io/jgordon-gc/mern-crud:v1

Upload the container image to the Container Registry so we can deploy to it:

将容器映像上传到Container Registry,以便我们对其进行部署:

Successfully tagged gcr.io/jaygordon-mongodb/mern-crud:v1
bash-3.2$ gcloud docker -- push gcr.io/${PROJECT_ID}/mern-crud:v1The push refers to repository [gcr.io/jaygordon-mongodb/mern-crud]

Next, I will test it locally on my workstation to make sure the app loads:

接下来,我将在工作站上对其进行本地测试以确保应用程序加载:

docker run --rm -p 3000:3000 gcr.io/${PROJECT_ID}/mern-crud:v1
> [email protected] start /usr/src/app
> node server
Listening on port 3000
mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第9张图片

Great — pointing my browser to http://localhost:3000 brings me to the site. Now it's time to create a kubernetes cluster and deploy our application to it.

太好了-将浏览器指向http:// localhost:3000,将我带到该站点。 现在是时候创建一个kubernetes集群并将我们的应用程序部署到它了。

使用Google Kubernetes引擎构建集群 ( Build Your Cluster With Google Kubernetes Engine )

I will be using the Google Cloud Shell within the Google Cloud control panel to manage my deployment. The cloud shell comes with all required applications and tools installed to allow you to deploy the Docker image I uploaded to the image registry without installing any additional software on my local workstation.

我将使用Google Cloud控制面板中的Google Cloud Shell管理我的部署。 云外壳随附了所有必需的应用程序和工具,以允许您部署我上传到映像注册表的Docker映像,而无需在本地工作站上安装任何其他软件。

Now I will create the kubernetes cluster where the image will be deployed that will help bring our application to production. I will include three nodes to ensure uptime of our app.

现在,我将创建kubernetes集群,在该集群中将部署映像,这将有助于将我们的应用程序投入生产。 我将包括三个节点,以确保我们的应用程序正常运行。

Set up our environment first:

首先设置我们的环境:

export PROJECT_ID="jaygordon-mongodb"
gcloud config set project $PROJECT_ID
gcloud config set compute/zone us-central1-b

Launch the cluster

启动集群

gcloud container clusters create mern-crud --num-nodes=3

When completed, you will have a three node kubernetes cluster visible in your control panel. After a few minutes, the console will respond with the following output:

完成后,您将在控制面板中看到一个三个节点的kubernetes集群。 几分钟后,控制台将响应以下输出:

Creating cluster mern-crud...done.
Created [https://container.googleapis.com/v1/projects/jaygordon-mongodb/zones/us-central1-b/clusters/mern-crud].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-b/mern-crud?project=jaygordon-mongodb
kubeconfig entry generated for mern-crud.
NAME       LOCATION       MASTER_VERSION  MASTER_IP       MACHINE_TYPE   NODE_VERSION  NUM_NODES  STATUS
mern-crud  us-central1-b  1.8.7-gke.1     35.225.138.208  n1-standard-1  1.8.7-gke.1   3          RUNNING
mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第10张图片

Just a few more steps left. Now we'll deploy our app with kubectl to our cluster from the Google Cloud Shell:

仅剩几步了。 现在,我们将带有kubectl的应用程序从Google Cloud Shell部署到集群中:

kubectl run mern-crud --image=gcr.io/${PROJECT_ID}/mern-crud:v1 --port 3000

The output when completed should be:

完成后的输出应为:

jay_gordon@jaygordon-mongodb:~$ kubectl run mern-crud --image=gcr.io/${PROJECT_ID}/mern-crud:v1 --port 3000
deployment "mern-crud" created

Now review the application deployment status:

现在查看应用程序部署状态:

jay_gordon@jaygordon-mongodb:~$ kubectl get pods
NAME                         READY     STATUS    RESTARTS   AGE
mern-crud-6b96b59dfd-4kqrr   1/1       Running   0          1m
jay_gordon@jaygordon-mongodb:~$

We'll create a load balancer for the three nodes in the cluster so they can be served properly to the web for our application:

我们将为集群中的三个节点创建一个负载平衡器,以便可以为我们的应用程序将它们正确地提供给网络:

jay_gordon@jaygordon-mongodb:~$ kubectl expose deployment mern-crud --type=LoadBalancer --port 80 --target-port 3000 
service "mern-crud" exposed

Now get the IP of the loadbalancer so if needed, it can be bound to a DNS name and you can go live!

现在获取负载均衡器的IP,因此,如果需要,可以将其绑定到DNS名称,然后开始使用!

jay_gordon@jaygordon-mongodb:~$ kubectl get service
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
kubernetes   ClusterIP      10.27.240.1              443/TCP        11m
mern-crud    LoadBalancer   10.27.243.208   35.226.15.67   80:30684/TCP   2m

A quick curl test shows me that my app is online!

快速卷曲测试显示我的应用程序在线!

bash-3.2$ curl -v 35.226.15.67
* Rebuilt URL to: 35.226.15.67/
*   Trying 35.226.15.67...
* TCP_NODELAY set
* Connected to 35.226.15.67 (35.226.15.67) port 80 (#0)
> GET / HTTP/1.1
> Host: 35.226.15.67
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
mongodb分布式部署_使用Kubernetes和MongoDB Atlas进行现代分布式应用程序部署_第11张图片

I have added some test data and as we can see, it's part of my deployed application via Kubernetes to GCP and storing my persistent data in MongoDB Atlas.

我已经添加了一些测试数据,正如我们所看到的,它是我通过Kubernetes部署到GCP的应用程序的一部分,并将我的持久数据存储在MongoDB Atlas中。

When I am done working with the Kubernetes cluster, I can destroy it easily:

使用Kubernetes集群完成工作后,可以轻松销毁它:

gcloud container clusters delete mern-crud

gcloud container clusters delete mern-crud

下一步是什么? ( What's Next? )

You've now got all the tools in front of you to build something HUGE with MongoDB Atlas and Kubernetes.

现在,您已经拥有了使用MongoDB Atlas和Kubernetes构建巨大功能的所有工具。

Check out the rest of the Google Kubernetes Engine's tutorials for more information on how to build applications with Kubernetes. For more information on MongoDB Atlas, click here.

请查看Google Kubernetes Engine的其余教程 ,以获取有关如何使用Kubernetes构建应用程序的更多信息。 有关MongoDB Atlas的更多信息, 请单击此处 。

Have more questions? Join the MongoDB Community Slack!

还有其他问题吗? 加入MongoDB社区Slack !

Continue to learn via high quality, technical talks, workshops, and hands-on tutorials. Join us at MongoDB World.

继续通过高质量的技术讲座,讲习班和动手教程进行学习。 加入我们的MongoDB世界。

翻译自: https://scotch.io/tutorials/modern-distributed-application-deployment-with-kubernetes-and-mongodb-atlas

mongodb分布式部署

你可能感兴趣的:(数据库,java,docker,大数据,python)