Managing MySQL database using PHPMyAdmin in Kubernetes
NOTE: Before you proceed you must know how to connect to kubernetes cluster, have nginx ingress controller setup with certificate manager, and have mysql database pod deployed. Follow this guide if you do not have mysql deployed. Follow this guide to setup nginx ingress and cert manager.
Phpmyadmin is a popular open source tool to manage mysql database server. In this guide you will learn how to create new deployment and expose it as a service to access phpmyadmin from internet using nginx ingress controller.
- Create a deployment file called phpmyadmin-deployment.yaml and paste below values:
apiVersion: apps/v1 kind: Deployment metadata: name: phpmyadmin-deployment labels: app: phpmyadmin spec: replicas: 1 selector: matchLabels: app: phpmyadmin template: metadata: labels: app: phpmyadmin spec: containers: - name: phpmyadmin image: phpmyadmin/phpmyadmin ports: - containerPort: 80 env: - name: PMA_HOST value: mysql-service - name: PMA_PORT value: "3306" - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secrets key: ROOT_PASSWORD
ROOT_PASSWORDvalue will be consumed from kubernetes secrets. If you want to learn more about kubernetes secrets. Follow this guide.
- Execute below command to create new deployment:
kubectl apply -f phpmyadmin-deployment.yaml
Exposing phpmyadmin via Services
- Create new file called
phpmyadmin-service.yamland paste below values:
apiVersion: v1 kind: Service metadata: name: phpmyadmin-service spec: type: NodePort selector: app: phpmyadmin ports: - protocol: TCP port: 80 targetPort: 80
- Execute below command to create service:
kubectl apply -f phpmyadmin-service.yaml
Once you are done with above configurations, its time to expose phpmyadmin service via internet.
NOTE: I'm using digital ocean managed kubernetes where I manage my own DNS, and digitalocean take cares of creating load balancer for my nginx ingress controller. Once again if you want to follow this guide it will be very helpful.
Nginx Ingress configuration
- Create new yaml file called phpmyadmin-ingress.yaml and paste below values:
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: echo-ingress annotations: cert-manager.io/cluster-issuer: "letsencrypt-prod" spec: tls: - hosts: - phpmyadmin.example.com secretName: echo-tls rules: - host: mydemo.example.com http: paths: - backend: serviceName: phpmyadmin-service servicePort: 80
- Apply the changes:
kubectl apply -f mydemo-ingress.yaml