Kubernetes php-fpm & nginx 启动慢

问题描述 投票:0回答:1

我正在尝试将 php symfony 项目放在 kubernetes 上! 它可以工作,但我的 php-fpm 启动速度很慢

我将其构建为一个 pod,其中包含一个 php 容器和一个 nginx 容器

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

因此,在这张图表中,当我重新启动或进行滚动更新时,我的第一个容器等待第二个容器准备就绪(因此 php-fpm 在端口 9000 上运行,nginx 在端口 80 上运行)

在我的 nginx 上,我创建了一个 /nginx/health 来检查它何时准备好 我尝试在不运行 php-fpm 的情况下运行我的 pod,并使用命令 php-fpm 手动启动它(它写 php-fpm 已准备好处理连接),并且需要花费大量时间“大约 30 秒”来显示我的页面 我得到的解决方案之一是将 nginx 就绪探针置于 smt 高位,以便 fpm 可以在不终止旧 pod 的情况下启动

但我想知道为什么 php-fpm 告诉我他正在监听请求但尚未完全准备好

谢谢:D!

php kubernetes nginx azure-aks
1个回答
0
投票

PHP-FPM 的延迟就绪可能是由于多种因素造成的,包括就绪探针配置、资源分配或 Symfony 中的缓存/预热问题。 尝试增加initialDelaySeconds 和periodSeconds 值。将initialDelaySeconds 设置为大约 20-30 秒可以让 PHP-FPM 在 Kubernetes 检查准备情况之前有时间完全初始化。

假设您的 PHP-FPM 的 Dockerfile 和 Symfony 的 NGINX 已准备好并推送

enter image description here

相应地配置您的 yaml

PVC

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

enter image description here

部署

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

服务

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

签出symfony dockerfile在k8s上构建symfony

© www.soinside.com 2019 - 2024. All rights reserved.