skip to Main Content

Im trying to put a php symfony project on kubernetes !
It work but my php-fpm is slow to start

I build this as one pod with one php container and one nginx container

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-deployment
  namespace: {{ .Values.namespace }}
  labels:
    app: php
spec:
  replicas: {{ .Values.php.replicas }}
  selector:
    matchLabels:
      app: php
  minReadySeconds: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%    # Specifies the maximum number of Pods that can be unavailable during the update process (in percentage or number).
      maxSurge: 25%          # Specifies the maximum number of additional Pods that can be created above the desired number of Pods during the update process (in percentage or number).
  template:
    metadata:
      labels:
        app: php
    spec:
      terminationGracePeriodSeconds: 15
      initContainers:
        - name: init-symfony
          image: {{ .Values.php.image }}  
          command: ["/bin/sh", "-c"]
          args:
            - |
              cp -R /var/www/symfony/. /var/www/share 
              cd /var/www/share
              php bin/console c:c || true
              chown www-data:www-data -R . 
          env:
          - name: DATABASE_URL
            value: 'postgresql://dummy:dummy@dummy:5432/dummy?serverVersion=14'
          volumeMounts:
            - name: www-storage
              mountPath: /var/www/share
      containers:
      - name: php
        image: {{ .Values.php.image }}
        command: ["/bin/sh", "-c"]
        args:
          - |
            php-fpm
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh", "-c", "sleep 15"]
        # resources:
        #   requests:
        #     memory: "1024Mi"
        #     cpu: "50m"
        #   limits:
        #     memory: "2048Mi"
        #     cpu: "100m"
        readinessProbe:
          tcpSocket:
            port: 9000
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 2
          failureThreshold: 3
        ports:
        - containerPort: 9000
        volumeMounts:
        - name: php-config-volume
          mountPath: /var/www/symfony/.env.local
          subPath: php.conf
        - name: www-storage
          mountPath: /var/www/symfony
        - name: key-storage
          mountPath: /var/www/symfony/config/jwt

      - name: nginx
        image: {{ .Values.php.image }} 
        resources:
          requests:
            memory: "64Mi"
            cpu: "10m"
        readinessProbe:
          httpGet:
            path: /nginx/heal
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 2
          failureThreshold: 3
        ports:
        - containerPort: 80
        volumeMounts:
        - name: www-storage
          mountPath: /var/www/symfony      
      volumes:
        - name: php-config-volume
          configMap:
            name: php-config 
        - name: www-storage
          emptyDir: {}
        - name: key-storage
          persistentVolumeClaim:
            claimName: pv-key-claim

So with this chart when i restart or do a rolling update my first container wait the second to be ready (so php-fpm is running on port 9000 and nginx on 80)

On my nginx i created a /nginx/health to check when it is ready
I tried to run my pod without runing php-fpm and start it by hand with the command php-fpm (it write php-fpm is ready to handle connection) and it take a lot of time ‘arround 30 seconds’ to show my page
One of the solution that i get is to put the nginx readyness probe to smt high that fpm can start without terminating the old pod

but i wonder why php-fpm tell me that he is listening for request but is not fully ready

Thanks 😀 !

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution ! With my first Deployement. In the php container cmd i added before the php-fpm, a cache clear and a cache warmup and it fixed all my problems


  2. PHP-FPM’s delayed readiness can be due to several factors, including the readiness probe configuration, resource allocation, or caching/warming up issues in Symfony.
    Try increasing the initialDelaySeconds and periodSeconds values. Setting initialDelaySeconds to around 20–30 seconds could give PHP-FPM time to initialize fully before Kubernetes checks readiness.

    Assuming your Dockerfile for PHP-FPM and NGINX for Symfony is ready and pushed

    enter image description here

    Configure your yamls accordingly

    pvc

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: symfony-storage
      namespace: symfony
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
    

    enter image description here

    deployment

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: symfony-deployment
      namespace: symfony
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: symfony
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxUnavailable: 25%
          maxSurge: 25%
      template:
        metadata:
          labels:
            app: symfony
        spec:
          containers:
          - name: php-fpm
            image: arkoacr.azurecr.io/symfony-php-fpm:latest
            command: ["php-fpm"]
            ports:
              - containerPort: 9000
            readinessProbe:
              tcpSocket:
                port: 9000
              initialDelaySeconds: 20
              periodSeconds: 15
              timeoutSeconds: 5
              failureThreshold: 3
            resources:
              requests:
                memory: "512Mi"
                cpu: "100m"
              limits:
                memory: "1024Mi"
                cpu: "500m"
            volumeMounts:
              - mountPath: /var/www/symfony
                name: symfony-storage
    
          - name: nginx
            image: arkoacr.azurecr.io/symfony-nginx:latest
            command: ["nginx", "-g", "daemon off;"]
            ports:
              - containerPort: 80
            readinessProbe:
              httpGet:
                path: /nginx/health
                port: 80
              initialDelaySeconds: 30
              periodSeconds: 10
              timeoutSeconds: 5
              failureThreshold: 3
            resources:
              requests:
                memory: "64Mi"
                cpu: "10m"
            volumeMounts:
              - mountPath: /var/www/symfony
                name: symfony-storage
    
          volumes:
          - name: symfony-storage
            persistentVolumeClaim:
              claimName: symfony-storage
    
    

    enter image description here

    service

    apiVersion: v1
    kind: Service
    metadata:
      name: symfony-service
      namespace: symfony
    spec:
      selector:
        app: symfony
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: LoadBalancer
    

    enter image description here

    enter image description here

    checkout symfony dockerfile and build symfony on k8s

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search