Kubernetes Docker/MySQL Multiple Databases in one Service

7/26/2018

I need multiple databases in one mysql service.

apiVersion: v1
kind: Service
metadata:
  name: database
  labels:
    app: keyshake
spec:
  ports:
    - port: 3306
  selector:
    app: keyshake
    tier: database
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-pv-claim
  labels:
    app: keyshake
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: database
  labels:
    app: keyshake
spec:
  selector:
    matchLabels:
      app: keyshake
      tier: database
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: keyshake
        tier: database
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_RANDOM_ROOT_PASSWORD
          value: "yes"
        - name: MYSQL_USER
          value: keyshake
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database-password
              key: password
        ports:
        - containerPort: 3306
          name: database
        volumeMounts:
        - name: database-persistent-storage
          mountPath: /var/lib/mysql
        - name: mysql-initdb
          mountPath: /docker-entrypoint-initdb.d
      volumes:
      - name: database-persistent-storage
        persistentVolumeClaim:
          claimName: database-pv-claim
      - name: mysql-initdb
        configMap:
          name: mysql-initdb-config

This is my configuration. Right now I'm using a Config Map with this content:

init.sql:

CREATE DATABASE IF NOT EXISTS keyshake;
CREATE DATABASE IF NOT EXISTS pdns;

When I create the service it logs this /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql but when I connect to it and list the databases I see nothing.

I guess that my configuration is not properly setup but I don't know what the problem is.

Thanks in advance.

Theo

-- Theo Bouwman
docker
kubernetes
mysql

1 Answer

7/26/2018

Add the same configuration in docker-compose file like:

services:
 mysql1:
    container_name: mysqlserver
    image: mysql:5.6
    ports:
        - 3310:3306
    volumes:
        - ./sqlserver:/docker-entrypoint-initdb.d
    environment:
        MYSQL_ROOT_PASSWORD: "root"
        MYSQL_DATABASE: <dbname1>
        MYSQL_USER: <db1user>
        MYSQL_PASSWORD: <db1pass>

mysql2:
    container_name: mysqlserver2
    image: mysql:5.6
    environment:
    volumes:
        - ./sqlserver:/docker-entrypoint-initdb2.d
    ports:
        - 3311:3306
        MYSQL_ROOT_PASSWORD: "root"
        MYSQL_DATABASE: <dbname2>
        MYSQL_USER: <db2user>
        MYSQL_PASSWORD: <db2pass>

If you need this in one service, If what you mean is correct, then You need to load this docker volume into your application. If you want application to be configured with 2 databases, add In Loadbalancer, the properties like Config.services.mysqlserver1(host, port, pass) .

If you arent using a DB Accumulator (like eg.JPA), Then You have to add a schema.sql seprately with something like

CREATE DATABASE IF NOT EXISTS keyshake;
CREATE DATABASE IF NOT EXISTS pdns;

And Add path to schema.sql in docker entrypoint for both image instances in place of docker-entrypoint-initdb.sql---->schema.sql

Configure differrent In ports, and see "docker -ti mysql1 /bin/bash" and "docker -ti server2 /bin/bash", whether both database instances have desired tables.

Hope this helps!!!.

-- Pavan Kate
Source: StackOverflow