我正在努力拥有多个服务(NodeJS,Spring-boot),每个服务都有自己的MongoDB每服务数据库服务器(最终针对GCP和K8),以便我可以将数据分开。我将使用Docker compose一起启动服务和数据库。但是,当我运行多个服务时,自然会遇到端口冲突。这是一个典型的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"
我正在寻找如何执行此操作的示例。我的想法是每个撰写文件都将拥有自己的端口,每个服务都将在内部映射到那些端口?
答案 0 :(得分:0)
您可以制作yaml进行部署,并在一个pod中解释所有容器(Pod是一组容器)。您的部署可能如下所示:
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
这只是在集群内部进行部署。您需要将其公开到集群之外。我建议您为此使用Ingress。
在这里,您将在pod内拥有数据库。另外,您可以在相同的名称空间中为数据库和应用程序创建2个部署。
此外,您需要手动构建Docker映像或为此使用CI工具。您可以通过名称空间管理环境(prod,pre-prod,dev,test)。一个环境的一个名称空间将为您提供完全隔离。另外,要管理所有这些,我建议您使用Helm或kops之类的工具。
Kubernetes和Docker-compose之间有很多区别,但是主要区别是设计。在Kubernetes中,每个应用程序级别都有更多实体,您可以对其进行管理。在Docker-compose中,您将所有服务都配置在一个地方,并且通常很难管理某些特定的东西。