简体   繁体   中英

Kubernetes + MySQL : Creating custom database and user in a Kubernetes container

I am trying to create a Django + MySQL app using Google Container Engine and Kubernetes. Following the docs from official MySQL docker image and Kubernetes docs for creating MySQL container I have created the following replication controller

apiVersion: v1
kind: ReplicationController
metadata:
  labels:
    name: mysql
  name: mysql
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - image: mysql:5.6.33
        name: mysql
        env:
          #Root password is compulsory
        - name: "MYSQL_ROOT_PASSWORD"
          value: "root_password"
        - name: "MYSQL_DATABASE"
          value: "custom_db"
        - name: "MYSQL_USER"
          value: "custom_user"
        - name: "MYSQL_PASSWORD"
          value: "custom_password"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
          # This name must match the volumes.name below.
          - name: mysql-persistent-storage
            mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          gcePersistentDisk:
            # This disk must already exist.
            pdName: mysql-disk
            fsType: ext4

According to the docs, passing the environment variables MYSQL_DATABASE. MYSQL_USER, MYSQL_PASSWORD, a new user will be created with that password and assigned rights to the newly created database. But this does not happen. When I SSH into that container, the ROOT password is set. But neither the user, nor the database is created.

I have tested this by running locally and passing the same environment variables like this

docker run -d --name some-mysql \
    -e MYSQL_USER="custom_user" \
    -e MYSQL_DATABASE="custom_db" \
    -e MYSQL_ROOT_PASSWORD="root_password" \
    -e MYSQL_PASSWORD="custom_password" \
    mysql

When I SSH into that container, the database and users are created and everything works fine.

I am not sure what I am doing wrong here. Could anyone please point out my mistake. I have been at this the whole day.

EDIT: 20-sept-2016

As Requested @Julien Du Bois The disk is created. it appears in the cloud console and when I run the describe command I get the following output

Command : gcloud compute disks describe mysql-disk

Result: 
creationTimestamp: '2016-09-16T01:06:23.380-07:00'
id: '4673615691045542160'
kind: compute#disk
lastAttachTimestamp: '2016-09-19T06:11:23.297-07:00'
lastDetachTimestamp: '2016-09-19T05:48:14.320-07:00'
name: mysql-disk
selfLink: https://www.googleapis.com/compute/v1/projects/<details-withheld-by-me>/disks/mysql-disk
sizeGb: '20'
status: READY
type: https://www.googleapis.com/compute/v1/projects/<details-withheld-by-me>/diskTypes/pd-standard
users:
- https://www.googleapis.com/compute/v1/projects/<details-withheld-by-me>/instances/gke-cluster-1-default-pool-e0f09576-zvh5
zone: https://www.googleapis.com/compute/v1/projects/<details-withheld-by-me>

I referred to lot of tutorials and google cloud examples. To run the mysql docker container locally my main reference was the official image page on docker hub https://hub.docker.com/_/mysql/

This works for me and locally the container created has a new database and user with right privileges.

For kubernetes, my main reference was the following

https://cloud.google.com/container-engine/docs/tutorials/persistent-disk/

I am just trying to connect to it using Django container.

You set mysql-disk in your deployment and the disk you have is custom-disk. Change pdName to custom-disk and it will work.

I was facing the same issue when I was using volumes and mounting them to mysql pods.

As mentioned in the documentation of mysql's docker image :

When you start the mysql image, you can adjust the configuration of the MySQL instance by passing one or more environment variables on the docker run command line. Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.

So after spinning wheels I managed to solve the problem by changing the hostPath of the volume that I was creating from "/data/mysql-pv-volume" to "/var/lib/mysql"

Here is a code snippet that might help create the volumes

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  persistentVolumeReclaimPolicy: Delete /* For development Purposes only */
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/var/lib/mysql"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Hope that helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM