How to connect PhpMyAdmin with Mysql both hosted in Kubernetes cluster in Gcloud

5/2/2018

I have deployed MySql and PhpMyAdmin in Kubernetes cluster. I want to log in to PMA with the username and password and connect to Mysql. I kind of got stuck. I have external IP for PMA which should connect to my database. Here is my deployment script:

For mysql:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    name: mysqldb
  name: mysqldb
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: mysqldb
    spec:
      containers:
      - env:
        - name: DB_NAME
          value: metrics
        - name: DB_USER
          value: metrics
        - name: DB_PASS
          value: ****
        image: mysql:5.7
        name: mysqldb
        ports:
        - containerPort: 3306
        resources: {}
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: data-volume
          subPath: data
      restartPolicy: Always
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: data-volume
status: {}

For PMA:

apiVersion: v1
kind: Pod
metadata:
  name: myadmin
  labels:
    name: myadmin
spec:
  containers:
    - name: phpmyadmin
      image: phpmyadmin/phpmyadmin
      env:
        - name: PMA_HOST
          value: mysqldb
        - name: DB_NAME
          value: metrics
        - name: DB_USER
          value: *****
        - name: DB_PASS
          value: *****
      ports:
        - containerPort: 80
          name: myadmin

Am I doing something wrong.

Is it even possible to deploy Php, Mysql, PMA, React and Nginx all in Kubernetes? Kind of struggling to figure-out how all things are connected and can't find any meaningful resources related to this topic. My app works fine in local machine with docker-compose and trying to convert this file to Kompose resources.

Here is my docker-compose file:

version: '3'
services:
    web:
        image: nginx:alpine
        volumes:
            - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf"
            - "./etc/ssl:/etc/ssl"
            - "./web:/var/www/html"
            - "./etc/nginx/default.template.conf:/etc/nginx/conf.d/default.template"
        ports:
            - "8000:80"
            - "3000:443"
        environment:
            - NGINX_HOST=${NGINX_HOST}
        command: /bin/sh -c "envsubst '$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
        restart: always
        depends_on:
            - php
            - mysqldb
    app:
      build: ./app
      environment:
        - NODE_ENV=$NODE_ENV
      ports:
          - "5000:5000"
      volumes:
        - ./app:/usr/src/web
        # Use a data volume to store all `node_modules` to prevent the directory
        # being removed when `./app` is mounted into the docker instance
        - /usr/src/web/node_modules
      #uncomment to run a local prod build
      #command: yarn build-start-local-prod
    php:
        image: nanoninja/php-fpm:${PHP_VERSION}
        restart: always
        env_file:
            - ".env"
        environment:
            - MYSQL_DATABASE=${MYSQL_DATABASE}
            - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_USER=${MYSQL_USER}
            - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        volumes:
            - "./etc/php/php.ini:/usr/local/etc/php/conf.d/php.ini"
            - "./web:/var/www/html"
    composer:
        image: "composer"
        volumes:
            - "./web/app:/app"
        command: install
    myadmin:
        image: phpmyadmin/phpmyadmin
        container_name: phpmyadmin
        ports:
            - "8080:80"
        environment:
            - PMA_ARBITRARY=1
            - PMA_HOST=${MYSQL_HOST}
        restart: always
        depends_on:
            - mysqldb
    mysqldb:
        image: mysql:5.7
        container_name: ${MYSQL_HOST}
        restart: always
        env_file:
            - ".env"
        environment:
            - MYSQL_DATABASE=${MYSQL_DATABASE}
            - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
            - MYSQL_USER=${MYSQL_USER}
            - MYSQL_PASSWORD=${MYSQL_PASSWORD}
        ports:
            - "8989:3306"
        volumes:
            - "data-volume:/var/lib/mysql"
volumes:
  data-volume:

I would appreciate if you can help. Thanks!

-- surajnew55
docker-compose
google-kubernetes-engine
kubernetes
php
phpmyadmin

1 Answer

5/20/2020

The way to do it is:

  1. Stateful set with PHP My Admin
  2. Service pointing to stateful set
  3. Ingress pointing to the service
  4. your MySQL Stateful set
-- Andres Leon Rangel
Source: StackOverflow