简体   繁体   English

如何使用Docker为每个服务创建mongo数据库

[英]How to create a mongo database per service with Docker

I am working towards having multiple services (NodeJS, Spring-boot) that each have their own MongoDB Database-server-per-service (eventually targeting GCP & K8s) so that I can keep the data separate. 我正在努力拥有多个服务(NodeJS,Spring-boot),每个服务都有自己的MongoDB每服务数据库服务器(最终针对GCP和K8),以便我可以将数据分开。 I will be using Docker compose to launch both the service and database together. 我将使用Docker compose一起启动服务和数据库。 However, when I run multiple services, naturally I get port collision. 但是,当我运行多个服务时,自然会遇到端口冲突。 Here is a typical docker-compose file: 这是一个典型的docker-compose文件:

version: '3'

# Define the services/containers to be run
services:
  myapp: #name of your service
    build: ./ # specify the directory of the Dockerfile
    ports:
      - "3000:3000" #specify ports forwarding
    links:
      - database # link this service to the database service
    volumes:
      - .:/usr/src/app
    depends_on:
      - database

  database: # name of the service
    image: mongo # specify image to build container from
    volumes:
        - ./data:/data/db
    ports:
        - "27017:27017"

I am looking for an example of how to do this. 我正在寻找如何执行此操作的示例。 My thinking is that each compose file will have it's own ports and each service will map to those ports internally? 我的想法是,每个撰写文件都将拥有自己的端口,每个服务都将在内部映射到那些端口?

You can make yaml for deployment and explain all your containers in one pod (a Pod is a group of containers). 您可以YAML部署和解释一个吊舱所有的容器(一个吊舱是一组容器)。 Your deployment may look like this: 您的部署可能如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: application-deployment
  labels:
    app: application
spec:
  selector:
    matchLabels:
      app: application
  template:
    metadata:
      labels:
        app: application
    spec:
      containers:
      - name: application
        image: application:version
        ports:
        - containerPort: 3000
      name: database
        image: database:version
        ports:
        - containerPort: 27017

It is just deployment inside your cluster. 它只是在集群内部部署。 You need to expose it outside the cluster. 您需要将其公开到集群之外。 I recommend you to use Ingress for that. 我建议您为此使用Ingress

Here you will have the database inside the pod. 在这里,您将在pod内拥有数据库。 Also you can create 2 deployments for database and your app in the same namespace. 您也可以在同一名称空间中为数据库和您的应用程序创建2个部署。

Also, you need to build Docker images manually or use the CI tool for that. 另外,您需要手动构建Docker映像或为此使用CI工具。 You can manage environments ( prod, pre-prod, dev, test ) by namespaces. 您可以通过名称空间管理环境(prod,pre-prod,dev,test)。 One namespace for one environment will give you full isolation. 一个环境的一个名称空间将为您提供完全隔离。 Also, to manage all this, I recommend you to use tools like Helm or kops . 另外,要管理所有这些,我建议您使用Helmkops之类的工具。

There are a lot of differences between Kubernetes and Docker-compose, but the main difference is design. Kubernetes和Docker-compose之间有很多区别,但主要区别是设计。 In Kubernetes, you have more entities for each level of application, and you can manage them. 在Kubernetes中,每个应用程序级别都有更多实体,您可以对其进行管理。 In Docker-compose, you configure all as one service in one place and usually it is hard to manage some specific things. 在Docker-compose中,您将所有服务都配置在一处,通常很难管理某些特定的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM