How to convert docker-compose to kubernetes?

2/22/2019

I have an example project with nginx, php-fpm, mysql: https://gitlab.com/x-team-blog/docker-compose-php

This is the docker-compose file:

version: '3'

services:
  php-fpm:
    build:
      context: ./php-fpm
    volumes:
      - ../src:/var/www

  nginx:
    build:
      context: ./nginx
    volumes:
      - ../src:/var/www
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/sites/:/etc/nginx/sites-available
      - ./nginx/conf.d/:/etc/nginx/conf.d
    depends_on:
      - php-fpm
    ports:
      - "80:80"
      - "443:443"

  database:
    build:
      context: ./database
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=myuser
      - MYSQL_PASSWORD=secret
      - MYSQL_ROOT_PASSWORD=docker
    volumes:
      - ./database/data.sql:/docker-entrypoint-initdb.d/data.sql

I'd like to use Kubernetes in Google Cloud and for this I have created several files:

Persistent Volume database-claim0-persistentvolumeclaim.yaml:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: database-claim0
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 400Mi

Also I created secrets:

kubectl create secret generic mysql --from-literal=MYSQL_DATABASE=mydb --from-literal=MYSQL_PASSWORD=secret --from-literal=MYSQL_ROOT_PASSWORD=docker --from-literal=MYSQL_USER=myuser

Then I created a service:

apiVersion: v1
kind: Service
metadata:
  name: database
  labels:
    app: database
spec:
  type: ClusterIP
  ports:
    - port: 3306
  selector:
    app: database

And now I'd like to create database-deployment.yaml, but I don't know what I have to write in the volumeMounts section for copy sql file like docker-compose:

- ./database/data.sql:/docker-entrypoint-initdb.d/data.sql

database-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: database
  labels:
    app: database
spec:
  replicas: 1
  selector:
    matchLabels:
      app: database
  template:
    metadata:
      labels:
        app: database
    spec:
      containers:
        - image: mariadb
          name: database
          env:
            - name: MYSQL_DATABASE
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_DATABASE
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_PASSWORD
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_ROOT_PASSWORD
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: MYSQL_USER
          ports:
            - containerPort: 3306
              name: database
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: database-claim0

UPD: My mistake. I did not push images first.

-- noc
docker-compose
kubernetes

2 Answers

3/11/2020

This is an Online tool here which help generating most of the Docker compose file converted to the Kubernetes Resources enter image description here

-- anish
Source: StackOverflow

2/22/2019

Use kompose

INFO Kubernetes file "nginx-service.yaml" created
INFO Kubernetes file "database-deployment.yaml" created
INFO Kubernetes file "database-claim0-persistentvolumeclaim.yaml" created
INFO Kubernetes file "nginx-deployment.yaml" created
INFO Kubernetes file "nginx-claim0-persistentvolumeclaim.yaml" created
INFO Kubernetes file "nginx-claim1-persistentvolumeclaim.yaml" created
INFO Kubernetes file "nginx-claim2-persistentvolumeclaim.yaml" created
INFO Kubernetes file "nginx-claim3-persistentvolumeclaim.yaml" created
INFO Kubernetes file "php-fpm-deployment.yaml" created
INFO Kubernetes file "php-fpm-claim0-persistentvolumeclaim.yaml" created
-- jaxxstorm
Source: StackOverflow