adminer in a kubernetes pod

Viewed 2152

I am trying to create a pod with both phpmyadmin and adminer in it. I have the Dockerfile created but I am not sure of the entrypoint needed.

Has anyone accomplished this before? I have everything figured out but the entrypoint...

FROM phpmyadmin/phpmyadmin

ENV MYSQL_DATABASE=${MYSQL_DATABASE}
ENV MYSQL_USER=${MYSQL_USERNAME}
ENV MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
ENV MYSQL_PORT=3381
ENV PMA_USER=${MYSQL_USER}
ENV PMA_PORT=3381
ENV PMA_PASSWORD=${MYSQL_PASSWORD}
ENV PMA_HOST=${MYSQL_HOST}

EXPOSE 8081
ENTRYPOINT [ "executable" ]

FROM adminer:4

ENV POSTGRES_DB=${POSTGRES_DATABASE}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

EXPOSE 8082
ENTRYPOINT [ "?" ]

------UPDATE 1 ---------- after read some comments I spilt my Dockerfiles and will create a yml file for the kube pod

FROM phpmyadmin/phpmyadmin

ENV MYSQL_DATABASE=${MYSQL_DATABASE}
ENV MYSQL_USER=${MYSQL_USERNAME}
ENV MYSQL_ROOT_PASSWORD=${MYSQL_PASSWORD}
ENV MYSQL_PORT=3381
ENV PMA_USER=${MYSQL_USER}
ENV PMA_PORT=3381
ENV PMA_PASSWORD=${MYSQL_PASSWORD}
ENV PMA_HOST=${MYSQL_HOST}

EXPOSE 8081
ENTRYPOINT [ "executable" ]

container 2

FROM adminer:4

ENV POSTGRES_DB=${POSTGRES_DATABASE}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

EXPOSE 8082
ENTRYPOINT [ "?" ]

I am still not sure what the entrypoint script should be

3 Answers

Since you are not modifying anything in the image, you don't need to create a custom docker image for this, you could simply run 2 deployments in kubernetes passing the environment variables using a Kubernetes Secret.

See this example of how to deploy both application on Kubernetes:

  1. Create a Kubernetes secret with your connection details:

cat <<EOF >./kustomization.yaml
secretGenerator:
- name: database-conn
  literals:
  - MYSQL_DATABASE=${MYSQL_DATABASE}
  - MYSQL_USER=${MYSQL_USER}
  - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
  - MYSQL_PORT=${MYSQL_PORT}
  - POSTGRES_DB=${POSTGRES_DB}
  - POSTGRES_USER=${POSTGRES_USER}
  - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
EOF

Apply the generated file:

kubectl apply -k .
secret/database-conn-mm8ck2296m created
  1. Deploy phpMyAdmin and Adminer:

You need to create two deployment, the first for phpMyAdmin, and other to Adminer, using the secrets created above in the containers, for example:

Create a file called phpmyadmin-deploy.yml:

Note: Change the secret name from database-conn-mm8ck2296m to the generated name in the command above.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: phpmyadmin
spec:
  selector:
    matchLabels:
      app: phpmyadmin
  template:
    metadata:
      labels:
        app: phpmyadmin
    spec:
      containers:
      - name: phpmyadmin
        image: phpmyadmin/phpmyadmin
        env:
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: MYSQL_DATABASE
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: MYSQL_USER
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_PORT
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: MYSQL_PORT
        - name: PMA_HOST
          value: mysql.host
        - name: PMA_USER
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: MYSQL_USER
        - name: PMA_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: MYSQL_ROOT_PASSWORD
        - name: PMA_PORT
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: MYSQL_PORT       
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: phpmyadmin-svc
spec:
  selector:
    app: phpmyadmin
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Adminer:

Create other file named adminer-deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: adminer
spec:
  selector:
    matchLabels:
      app: adminer
  template:
    metadata:
      labels:
        app: adminer
    spec:
      containers:
      - name: adminer
        image: adminer:4
        env:
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: POSTGRES_DB
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: POSTGRES_USER
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: database-conn-mm8ck2296m
              key: POSTGRES_PASSWORD  
        ports:
        - name: http
          containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: adminer-svc
spec:
  selector:
    app: adminer
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

Deploy the yaml files with kubectl apply -f *-deploy.yaml, after some seconds type kubectl get pods && kubectl get svc to verify if everything is ok.

Note: Both services will be created as ClusterIP, it means that it will be only accessible internally. If you are using a cloud provider, you can use service type LoadBalancer to get an external ip. Or you can use kubectl port-forward (see here) command to access your service from your computer.

Access application using port-forward:

phpMyadmin:

# This command will map the port 8080 from your localhost to phpMyadmin application:
kubectl port-forward svc/phpmyadmin-svc 8080:80

Adminer

# This command will map the port 8181 from your localhost to Adminer application:
kubectl port-forward svc/adminer-svc 8181:8080

And try to access:

http://localhost:8080 <= phpMyAdmin http://localhost:8181 <= Adminer

References:

Kubernetes Secrets

Kubernetes Environment variables

Kubernetes port forward

You can't combine two docker images like that. What you've created is a multi-stage build and only the last stage is what ends up in the final image. And even if you used multi-stage copies to carefully fold both into one image, you would need to think through how you will run both things simultaneously. The upstream adminer image uses php -S under the hood.

You'd almost always run this in two separate Deployments. Since the only thing you're doing in this custom Dockerfile is setting environment variables, you don't even need a custom image; you can use the env: part of the pod spec to define environment variables at deploy time.

image: adminer:4 # without PHPMyAdmin
env:
  - name: POSTGRES_DB
    value: [...]       # fixed value in pod spec
    # valueFrom: ...   # or get it from a ConfigMap or Secret

Run two Deployments, with one container in each, and a matching Service for each. (Don't run bare pods, and don't be tempted to put both containers in a single deployment.) If the databases are inside Kubernetes too, use their Services' names and ports; I'd usually expect these to be the "normal" 3306/5432 ports.

Related