从docker-compose.yml文件创建kubernetes yml文件

时间:2020-10-23 20:47:32

标签: docker kubernetes

我有这个docker-compose.yml文件,用于运行三个微服务和一个api网关

version: '3'
services: 
    serviceone:
        container_name: serviceone
        restart: always
        build: serviceone/
        ports: 
            - '3000:3000'
    servicetwo:
        container_name: servicetwo
        restart: always
        build: servicetwo/
        ports: 
            - '3001:3001'   
    servicethree:
        container_name: servicethree
        restart: always
        build: servicethree/
        ports: 
             - '3002:3003'    
    apigateway:
        container_name: timezoneapigateway
        restart: always
        build: timezone/
        ports: 
            - '8080:8080'
        links: 
            - serviceone
            - servicetwo
            - servicethree   

现在我想将这些dockerimage部署在kubernetes中的一个pod中,以便api网关可以与所有三个微服务连接,当前版本的api网关正在运行,但是我什至没有丝毫暗示在kubernetes中这样做。我真的是kubernetes的新手,谁能告诉我如何设计kubernetes yml文件来实现这一目标

1 个答案:

答案 0 :(得分:1)

您不必在同一个Pod中运行所有服务。 Kubernetes的标准是为所有应用程序提供单独的部署和服务。这是 serviceone 的部署清单,但是您可以轻松地将其修改为 servicetwo servicethree apigateway

apiVersion: apps/v1
kind: Deployment
metadata:
  name: serviceone
  labels:
    app: serviceone
spec:
  replicas: 1
  selector:
    matchLabels:
      app: serviceone
  template:
    metadata:
      labels:
        app: serviceone
    spec:
      containers:
      - name: serviceone
        image: serviceone:latest
        ports:
        - containerPort: 3001

服务清单也是如此

apiVersion: v1
kind: Service
metadata:
  name: serviceone
spec:
  selector:
    app: serviceone
  ports:
  - protocol: TCP
    port: 3001
    targetPort: 3001

您的服务将可以通过以下方式在群集中访问:

serviceone:3001
servicetwo:3002
servicethree:3003
timezoneapigateway:8080