如何在kubernetes上运行docker-compose.yaml?

时间:2019-05-07 16:32:22

标签: docker kubernetes docker-compose

我的由tomcat7服务器和mysql数据库组成的docker-compose文件


version: '3'
services:
      mysql:
        image: suji165475/vignesh:latest
        ports:
          - "3066:3066"

      tomcat:
        image: suji165475/vignesh:tomcatserver
        container_name: tomcat7hope
        ports:
          - "8080:8080"

我使用docker文件构建了图像

FROM mysql
ENV MYSQL_ROOT_PASSWORD=root
ADD init.sql /docker-entrypoint-initdb.d
FROM picoded/tomcat7
COPY data-core-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/data-core-0.0.1-SNAPSHOT.war

如何在kubernetes集群上运行它?我已经尝试过kompose convert并且war文件不会在tomcats应用程序管理器中启动。但是war文件使用docker-compose up成功启动了。

为什么我只在kubernetes中遇到此问题,而直接运行docker-compose up却没有,请让我知道我应该对kubernetes yaml文件进行哪些更改。

mysql-deployment.yaml

 apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.18.0 (06a2e56)
  creationTimestamp: null
  labels:
    io.kompose.service: mysql
  name: mysql
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: mysql
    spec:
      containers:
      - image: suji165475/vignesh:latest
        name: mysql
        ports:
        - containerPort: 3066
        resources: {}
      restartPolicy: Always
status: {}

tomcat-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.18.0 (06a2e56)
  creationTimestamp: null
  labels:
    io.kompose.service: tomcat
  name: tomcat
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: tomcat
    spec:
      containers:
      - image: suji165475/vignesh:tomcatserver
        name: tomcat7hope
        ports:
        - containerPort: 8080
        resources: {}
      restartPolicy: Always
status: {}

mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.18.0 (06a2e56)
  creationTimestamp: null
  labels:
    io.kompose.service: mysql
  name: mysql
spec:
  ports:
  - name: "3066"
    port: 3066
    targetPort: 3066
  selector:
    io.kompose.service: mysql
status:
  loadBalancer: {}

tomcat-service.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert
    kompose.version: 1.18.0 (06a2e56)
  creationTimestamp: null
  labels:
    io.kompose.service: tomcat
  name: tomcat
spec:
  ports:
  - name: "8080"
    port: 8080
    targetPort: 8080
  selector:
    io.kompose.service: tomcat
status:
  loadBalancer: {}

1 个答案:

答案 0 :(得分:2)

让我说清楚点,您不能在Kubernetes上“运行docker-compose”。如果您想这样做,则不应使用Kubernetes。

话虽如此,使用kompose并不是一个好主意。 Docker撰写的Yaml文件都不同,kompose只是猜测相关联的Kubernetes清单是什么样子。如果您的应用程序非常简单并且很幸运,kompose可能会为您准备好要部署的清单,但是通常情况并非如此。

可能对您不起作用的原因有几个:

  • 您的tomcat应用未正确引用您的mysql服务
  • 您的mysql部署缺少某些变量或已装入的卷/文件

您的tomcat应用可以通过mysql来引用您的mysql:3066数据库,并且您可能需要在tomcat部署中添加一些环境变量,例如数据库名称,数据库用户名和db密码进行身份验证。这是您编辑过的tomcat清单:

deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tomcat
  namespace: default
  labels:
    app: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - image: suji165475/vignesh:tomcatserver
        name: tomcat7hope
        ports:
        - containerPort: 8080

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
  labels:
    app: tomcat
spec:
  ports:
  - name: "8080"
    port: 8080
    targetPort: 8080
  selector:
    app: tomcat

您的mysql部署可能需要PersistentVolumeClaimemptyDir才能保留数据。查看附加的卷,并确认您没有丢失任何需要的卷安装或环境变量。这是您编辑过的mysql清单:

deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql
  namespace: default
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: suji165475/vignesh:latest
        name: mysql
        ports:
        - containerPort: 3066
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: root

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: default
  labels:
    app: mysql
spec:
  ports:
  - name: "3066"
    port: 3066
    targetPort: 3066
  selector:
    app: mysql