Sergey Filipovich
Sergey Filipovich

Reputation: 77

PhpMyAdmin throws 404 error response in Kubernetes

I have an error trying to deploy the official phpmyadmin image locally in Kubernetes cluster. Please look at my yaml configs. I haven't any idea what I did wrong. I tried phpmyadmin/phpmyadmin image but the 404 error stays. I also viewed configs from other people but it doesn't differ from mine. This is my first experience in Kubernetes so maybe I don't know some development approaches.

enter image description here

ingress-service.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - http:
        paths:
          - path: /phpmyadmin/?(.*)
            pathType: Prefix
            backend:
              service:
                name: phpmyadmin-cluster-ip-service
                port:
                  number: 80

phpmyadmin-cluster-ip-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: phpmyadmin-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    app: phpmyadmin
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP

phpmyadmin-deployment.yaml Ip 192.168.64.7 is given by minikube.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: phpmyadmin-deployment
  labels:
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: phpmyadmin
      tier: backend
  template:
    metadata:
      labels:
        app: phpmyadmin
        tier: backend
    spec:
      restartPolicy: Always
      containers:
        - name: phpmyadmin
          image: phpmyadmin:latest
          ports:
            - name: phpmyadmin
              containerPort: 80
              protocol: TCP
          imagePullPolicy: Always
          env:
            - name: PMA_ABSOLUTE_URI
              value: "http://192.168.64.7/phpmyadmin/"
            - name: PMA_VERBOSE
              value: "PhpMyAdmin"
            - name: PMA_HOST
              value: mysql-service
            - name: PMA_PORT
              value: "3306"
            - name: UPLOAD_LIMIT
              value: "268435456"
            - name: PMA_ARBITRARY
              value: "0"
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: MYSQL_ROOT_PASSWORD

I omitted MySQL yaml configs thinking it doesn't related to phpmyadmin issue but if they can help I will pushlish it too.

Upvotes: 0

Views: 486

Answers (1)

Ryan
Ryan

Reputation: 96

In this case, adding the annotation nginx.ingress.kubernetes.io/rewrite-target: /$1 will fix it. How? (NOTE: I will change service port to 8080 to better distinguish the ports of the container and Service).

  1. You visit http://<MINIKUBE IP>/phpmyadmin/ on your browser.
  2. The NGINX Ingress controller receives your request, and rewrites the path /phpmyadmin/ to /. The NGINX Ingress controller creates the request to the Service in phpmyadmin-cluster-ip-service at port 8080 (service port) which has the targetPort at 80 (container port) for the pods containing the label app: phpmyadmin. One of the matching pods happens to be at 172.17.0.4:
"GET /phpmyadmin/favicon.ico HTTP/1.1" 200 22486 "-" ... 492 0.001 [default-phpmyadmin-cluster-ip-service-8080] [] 172.17.0.4:80 22486 0.000 200 ...
  1. Because the request is now using the correct path for the phpmyadmin server, it responds with 200 and the requested resource. We can also see the corresponding logs in phpmyadmin:
172.17.0.3 - - [14/Nov/2022:21:58:43 +0000] "GET /favicon.ico HTTP/1.1" 200 22733 "-" ...

The IP 172.17.0.3 is of the NGINX Ingress Controller.

There is also a similar question with an even more detailed answer.

Upvotes: 1

Related Questions