helm部署mongodb
介绍 (Introduction)
Kubernetes is a system for running modern, containerized applications at scale. With it, developers can deploy and manage applications across clusters of machines. And though it can be used to improve efficiency and reliability in single-instance application setups, Kubernetes is designed to run multiple instances of an application across groups of machines.
Kubernetes是一个用于大规模运行现代容器化应用程序的系统。 有了它,开发人员可以跨机器集群部署和管理应用程序。 尽管可以在单实例应用程序设置中使用它来提高效率和可靠性,但Kubernetes仍被设计为在一组机器之间运行应用程序的多个实例。
When creating multi-service deployments with Kubernetes, many developers opt to use the Helm package manager. Helm streamlines the process of creating multiple Kubernetes resources by offering charts and templates that coordinate how these objects interact. It also offers pre-packaged charts for popular open-source projects.
当使用Kubernetes创建多服务部署时,许多开发人员选择使用Helm软件包管理器。 Helm通过提供图表和模板来协调这些对象的交互方式,从而简化了创建多个Kubernetes资源的过程。 它还提供了流行的开源项目的预打包图表。
In this tutorial, you will deploy a Node.js application with a MongoDB database onto a Kubernetes cluster using Helm charts. You will use the official Helm MongoDB replica set chart to create a StatefulSet object consisting of three Pods, a Headless Service, and three PersistentVolumeClaims. You will also create a chart to deploy a multi-replica Node.js application using a custom application image. The setup you will build in this tutorial will mirror the functionality of the code described in Containerizing a Node.js Application with Docker Compose and will be a good starting point to build a resilient Node.js application with a MongoDB data store that can scale with your needs.
在本教程中,您将使用Helm图表将具有MongoDB数据库的Node.js应用程序部署到Kubernetes集群上。 您将使用官方的Helm MongoDB副本集图表创建一个StatefulSet对象 ,该对象由三个Pod ,一个Headless服务和三个PersistentVolumeClaims组成 。 您还将创建一个图表,以使用自定义应用程序映像部署多副本Node.js应用程序。 您将在本教程中构建的设置将反映使用Docker Compose容器化Node.js应用程序中描述的代码的功能,并且是使用MongoDB数据存储构建可伸缩的Node.js应用程序的良好起点。您的需求。
先决条件 (Prerequisites)
To complete this tutorial, you will need:
要完成本教程,您将需要:
A Kubernetes 1.10+ cluster with role-based access control (RBAC) enabled. This setup will use a DigitalOcean Kubernetes cluster, but you are free to create a cluster using another method.
启用了基于角色的访问控制(RBAC)的Kubernetes 1.10+集群。 此设置将使用DigitalOcean Kubernetes集群 ,但是您可以使用其他方法自由创建集群 。
The
kubectl
command-line tool installed on your local machine or development server and configured to connect to your cluster. You can read more about installingkubectl
in the official documentation.kubectl
命令行工具安装在本地计算机或开发服务器上,并配置为连接到集群。 您可以在官方文档中阅读有关安装kubectl
更多信息。Helm installed on your local machine or development server and Tiller installed on your cluster, following the directions outlined in Steps 1 and 2 of How To Install Software on Kubernetes Clusters with the Helm Package Manager.
按照如何使用Helm Package Manager在Kubernetes群集上安装软件的步骤1和2中概述的说明,将Helm安装在本地计算机或开发服务器上,并将Tiller安装在群集上 。
Docker installed on your local machine or development server. If you are working with Ubuntu 18.04, follow Steps 1 and 2 of How To Install and Use Docker on Ubuntu 18.04; otherwise, follow the official documentation for information about installing on other operating systems. Be sure to add your non-root user to the
docker
group, as described in Step 2 of the linked tutorial.将Docker安装在本地计算机或开发服务器上。 如果您正在使用Ubuntu 18.04,请遵循如何在Ubuntu 18.04上安装和使用Docker的步骤1和2; 否则,请遵循官方文档以获取有关在其他操作系统上安装的信息。 确保按照链接教程的第2步中所述将非root用户添加到
docker
组。A Docker Hub account. For an overview of how to set this up, refer to this introduction to Docker Hub.
Docker Hub帐户。 有关如何进行此设置的概述,请参阅此介绍给泊坞枢纽。
步骤1 —克隆和打包应用程序 (Step 1 — Cloning and Packaging the Application)
To use our application with Kubernetes, we will need to package it so that the kubelet
agent can pull the image. Before packaging the application, however, we will need to modify the MongoDB connection URI in the application code to ensure that our application can connect to the members of the replica set that we will create with the Helm mongodb-replicaset
chart.
要将我们的应用程序与Kubernetes一起使用,我们需要对其进行打包,以便kubelet
代理可以提取图像。 但是,在打包应用程序之前,我们将需要在应用程序代码中修改MongoDB 连接URI ,以确保我们的应用程序可以连接到将使用Helm mongodb-replicaset
图表创建的副本集的成员。
Our first step will be to clone the node-mongo-docker-dev repository from the DigitalOcean Community GitHub account. This repository includes the code from the setup described in Containerizing a Node.js Application for Development With Docker Compose, which uses a demo Node.js application with a MongoDB database to demonstrate how to set up a development environment with Docker Compose. You can find more information about the application itself in the series From Containers to Kubernetes with Node.js.
我们的第一步将是从DigitalOcean社区GitHub帐户克隆node-mongo-docker-dev存储库 。 该存储库包含容器化Node.js应用程序以进行Docker Compose开发中描述的设置代码,该示例使用具有MongoDB数据库的演示Node.js应用程序演示了如何使用Docker Compose设置开发环境。 您可以在《 使用Node.js从容器到Kubernetes 》系列中找到有关应用程序本身的更多信息。
Clone the repository into a directory called node_project
:
将存储node_project
到名为node_project
的目录中:
git clone https://github.com/do-community/node-mongo-docker-dev.git node_project
git clone https://github.com/do-community/node-mongo-docker-dev.git node_project
Navigate to the node_project
directory:
导航到node_project
目录:
cd node_project
cd node_project
The node_project
directory contains files and directories for a shark information application that works with user input. It has been modernized to work with containers: sensitive and specific configuration information has been removed from the application code and refactored to be injected at runtime, and the application’s state has been offloaded to a MongoDB database.
node_project
目录包含与用户输入配合使用的shark信息应用程序的文件和目录。 它已经过现代化的处理,可以与容器一起使用:敏感的特定配置信息已从应用程序代码中删除,并重构为在运行时注入,并且应用程序的状态已卸载到MongoDB数据库中。
For more information about designing modern, containerized applications, please see Architecting Applications for Kubernetes and Modernizing Applications for Kubernetes.
有关设计现代容器化应用程序的更多信息,请参见《 为Kubernetes设计架构应用程序》和《 为Kubernetes 现代化应用程序》 。
When we deploy the Helm mongodb-replicaset
chart, it will create:
当我们部署Helm mongodb-replicaset
图表时,它将创建:
A StatefulSet object with three Pods — the members of the MongoDB replica set. Each Pod will have an associated PersistentVolumeClaim and will maintain a fixed identity in the event of rescheduling.
具有三个Pod的StatefulSet对象-MongoDB 副本集的成员。 每个Pod都有一个关联的PersistentVolumeClaim,并且在重新计划时将保持固定的身份。
A MongoDB replica set made up of the Pods in the StatefulSet. The set will include one primary and two secondaries. Data will be replicated from the primary to the secondaries, ensuring that our application data remains highly available.
一个由StatefulSet中的Pod组成的MongoDB副本集。 集合将包括一个主要和两个次要。 数据将从主数据库复制到辅助数据库,以确保我们的应用程序数据保持高可用性。
For our application to interact with the database replicas, the MongoDB connection URI in our code will need to include both the hostnames of the replica set members as well as the name of the replica set itself. We therefore need to include these values in the URI.
为了使我们的应用程序与数据库副本进行交互,我们代码中的MongoDB连接URI将需要同时包含副本集成员的主机名以及副本集本身的名称。 因此,我们需要在URI中包含这些值。
The file in our cloned repository that specifies database connection information is called db.js
. Open that file now using nano
or your favorite editor:
我们克隆的存储库中用于指定数据库连接信息的文件称为db.js
立即使用nano
或您喜欢的编辑器打开该文件:
- nano db.js 纳米db.js
Currently, the file includes constants that are referenced in the database connection URI at runtime. The values for these constants are injected using Node’s process.env
property, which returns an object with information about your user environment at runtime. Setting values dynamically in our application code allows us to decouple the code from the underlying infrastructure, which is necessary in a dynamic, stateless environment. For more information about refactoring application code in this way, see Step 2 of Containerizing a Node.js Application for Development With Docker Compose and the relevant discussion in The 12-Factor App.
当前,该文件包括在运行时在数据库连接URI中引用的常量 。 这些常量的值是使用Node的process.env
属性注入的,该属性在运行时返回包含有关您的用户环境信息的对象。 在应用程序代码中动态设置值使我们能够将代码与底层基础结构分离,这在动态,无状态的环境中是必需的。 有关以这种方式重构应用程序代码的更多信息,请参阅使用Docker Compose容器化Node.js应用程序进行开发的 第2步 ,以及《十二要素应用程序》中的相关讨论。
The constants for the connection URI and the URI string itself currently look like this:
当前,连接URI和URI字符串本身的常量如下所示:
...
const {
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_HOSTNAME,
MONGO_PORT,
MONGO_DB
} = process.env;
...
const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;
...
In keeping with a 12FA approach, we do not want to hard code the hostnames of our replica instances or our replica set name into this URI string. The existing MONGO_HOSTNAME
constant can be expanded to include multiple hostnames — the members of our replica set — so we will leave that in place. We will need to add a replica set constant to the options
section of the URI string, however.
与12FA方法保持一致,我们不想将副本实例的主机名或副本集名称硬编码到此URI字符串中。 可以将现有的MONGO_HOSTNAME
常量扩展为包括多个主机名(副本集的成员),因此我们将其保留在原位。 但是,我们将需要向URI字符串的options
部分添加一个副本集常量。
Add MONGO_REPLICASET
to both the URI constant object and the connection string:
将MONGO_REPLICASET
添加到URI常量对象和连接字符串中:
...
const {
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_HOSTNAME,
MONGO_PORT,
MONGO_DB,
MONGO_REPLICASET
} = process.env;
...
const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?replicaSet=${MONGO_REPLICASET}&authSource=admin`;
...
Using the replicaSet
option in the options section of the URI allows us to pass in the name of the replica set, which, along with the hostnames defined in the MONGO_HOSTNAME
constant, will allow us to connect to the set members.
使用URI的options部分中的replicaSet
选项 ,我们可以传入副本集的名称,它与MONGO_HOSTNAME
常量中定义的主机名一起,将使我们能够连接到集合成员。
Save and close the file when you are finished editing.
完成编辑后,保存并关闭文件。
With your database connection information modified to work with replica sets, you can now package your application, build the image with the docker build
command, and push it to Docker Hub.
修改数据库连接信息以使用副本集后,您现在可以打包应用程序,使用docker build
命令构建映像,然后将其推送到Docker Hub。
Build the image with docker build
and the -t
flag, which allows you to tag the image with a memorable name. In this case, tag the image with your Docker Hub username and name it node-replicas
or a name of your own choosing:
使用docker build
和-t
标志构建映像,这使您可以使用令人难忘的名称标记映像。 在这种情况下,请使用您的Docker Hub用户名标记该映像,并将其命名为node-replicas
或您自己选择的名称:
docker build -t your_dockerhub_username/node-replicas .
docker build -t your_dockerhub_username / node-replicas 。
The .
in the command specifies that the