Kubernetes php-fpm missing ingress nginx configuration with minikube

3/18/2019

I'm missing some kubernetes ingress-nginx configuration here in order to run a php-fpm app on k8s in minikube.

The k8s configuration files:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: server-cluster-ip-service
              servicePort: 5000

---
apiVersion: v1
kind: Service
metadata:
  name: server-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: server
  ports:
    - port: 5000
      targetPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      component: server
  template:
    metadata:
      labels:
        component: server
    spec:
      containers:
        - name: server
          image: xxx/phpfpm
          ports:
            - containerPort: 5000
          env:
            - name: APP_ENV
              value: dev
            - name: APP_DEBUG
              value: '1'
            - name: APP_SECRET
              value: 74bd83726749616f29166ef53c5f0557
            - name: MYSQL_HOST
              value: mysql-cluster-ip-service
            - name: MYSQL_ROOT_PASSWORD
              value: root
            - name: MYSQL_DATABASE
              value: db
            - name: MYSQL_DATABASE_TEST
              value: test
            - name: MYSQL_USER
              value: user
            - name: MYSQL_PASSWORD
              value: secret
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: mysql
  ports:
    - port: 3306
      targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: mysql
  template:
    metadata:
      labels:
        component: mysql
    spec:
      volumes:
        - name: mysql-storage
          persistentVolumeClaim:
            claimName: database-persistent-volume-claim
      containers:
        - name: mysql
          image: mysql:5.7.22
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-storage
              mountPath: /var/lib/mysql
              subPath: mysql
          env:
            - name: MYSQL_HOST
              value: mysql-cluster-ip-service
            - name: MYSQL_ROOT_PASSWORD
              value: root
            - name: MYSQL_DATABASE
              value: db
            - name: MYSQL_USER
              value: user
            - name: MYSQL_PASSWORD
              value: secret
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-persistent-volume-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

Nginx server working configuration file used on docker nginx image for previous docker development environment (nginx is in docker a separated container from php container):

server {

    listen 443 default_server ssl;

    root   /opt/app/public;
    index  index.php;

    ...

    location / {
       try_files $uri $uri/ /index.php$is_args$args;
    }

    location /healthcheck {
        return 200 'ok';
        add_header Content-Type text/plain;
    }

    error_page 404 /index.php;

    location ~ ^/(index)\.php(/|$) {

        fastcgi_pass php:9000;
        fastcgi_index index.php;

        include fastcgi_params;
    }
}

when accessing minikube ip on browser I get 502 error.

I've installed ingress-nginx running following commands (as explained in their installation guide)

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml
$ minikube addons enable ingress
$ make dev-env

I assume I need to map somehow the root (nginx conf) to /opt/app/public and somehow configure the fastcgi_pass in the ingress-nginx which I'm still honestly a bit lost on how all together works.

-- jj-aa
kubernetes
kubernetes-ingress
minikube
nginx-ingress
php

1 Answer

3/18/2019

This might be one of the following problems.Your ingress seems fine.

  • Did you mean to run an nginx container in the same pod as your php container? Once you have nginx running in the same pod, you will have to replace "php" in your fastci_pass to "127.0.0.1".

  • Check your pod logs, if you are getting connection refused it's problem with your php container, it's not exposing the ports properly. Try doing port-forwarding on your php pod and see if you can access 5000 through localhost.

  • I see you have your fastcgi_pass on php:9000, is this supposed to be 5000?

-- cookiedough
Source: StackOverflow