PHP数据容器化与Kubernetes部署

发布时间:2026/6/2 23:45:19

PHP数据容器化与Kubernetes部署 PHP数据容器化与Kubernetes部署容器化和Kubernetes是现代应用部署的标准方式。PHP应用容器化后可以在K8s上弹性运行。今天说说PHP应用的容器化和K8s部署最佳实践。PHP的Docker镜像构建需要注意几个要点。使用多阶段构建减小镜像体积配置OPcache提升性能调整PHP-FPM参数。dockerfile# 多阶段构建FROM composer:latest AS composerCOPY composer.json composer.lock /app/RUN composer install --no-dev --optimize-autoloader --no-interactionFROM php:8.2-fpm-alpine AS base# 安装扩展RUN docker-php-ext-install pdo_mysql mbstring opcache# 配置OPcacheRUN docker-php-ext-configure opcache --enable-opcacheCOPY opcache.ini /usr/local/etc/php/conf.d/opcache.ini# 复制应用代码COPY --fromcomposer /app/vendor /var/www/html/vendorCOPY . /var/www/htmlWORKDIR /var/www/htmlRUN chown -R www-data:www-data storage bootstrap/cacheFROM base AS productionCOPY .env.production .envRUN php artisan optimizeEXPOSE 9000PHP应用的健康检查和就绪探针php// healthz.php - 存活检查header(Content-Type: application/json);$healthy true;$checks [];// 检查PHP-FPM$checks[php] true;// 检查数据库try {$pdo new PDO(mysql:hostlocalhost;dbnametest, root, );$pdo-query(SELECT 1);$checks[database] true;} catch (\Exception $e) {$checks[database] false;$healthy false;}// 检查Redistry {$redis new Redis();$redis-connect(redis-service, 6379);$checks[redis] true;} catch (\Exception $e) {$checks[redis] false;}$status $healthy ? 200 : 503;http_response_code($status);echo json_encode([status $healthy ? healthy : unhealthy,checks $checks,timestamp date(c),]);?// readyz.php - 就绪检查检查服务是否准备好接收流量header(Content-Type: application/json);$ready true;$checks [];try {$pdo new PDO(mysql:hostlocalhost;dbnametest, root, );$stmt $pdo-query(SELECT COUNT(*) FROM migration);$checks[migrations] true;} catch (\Exception $e) {$checks[migrations] false;$ready false;}// 检查缓存预热$cacheWarmed file_exists(/tmp/cache_warmed);$checks[cache] $cacheWarmed;if (!$cacheWarmed) {$ready false;}$status $ready ? 200 : 503;http_response_code($status);echo json_encode([ready $ready,checks $checks,timestamp date(c),]);?Kubernetes部署配置yamlapiVersion: apps/v1kind: Deploymentmetadata:name: php-applabels:app: php-appspec:replicas: 3selector:matchLabels:app: php-apptemplate:metadata:labels:app: php-appspec:containers:- name: php-fpmimage: registry.example.com/php-app:latestports:- containerPort: 9000env:- name: APP_ENVvalue: production- name: DB_HOSTvalueFrom:configMapKeyRef:name: app-configkey: db_host- name: DB_PASSWORDvalueFrom:secretKeyRef:name: app-secretkey: db_passwordresources:requests:memory: 128Micpu: 100mlimits:memory: 256Micpu: 500mlivenessProbe:httpGet:path: /healthzport: 8080initialDelaySeconds: 10periodSeconds: 10readinessProbe:httpGet:path: /readyzport: 8080initialDelaySeconds: 5periodSeconds: 5volumeMounts:- name: storagemountPath: /var/www/html/storagevolumes:- name: storagepersistentVolumeClaim:claimName: php-storage---apiVersion: v1kind: Servicemetadata:name: php-appspec:selector:app: php-appports:- port: 80targetPort: 9000type: ClusterIP---apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata:name: php-app-hpaspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: php-appminReplicas: 2maxReplicas: 10metrics:- type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: 70- type: Resourceresource:name: memorytarget:type: UtilizationaverageUtilization: 80容器化部署的PHP应用需要考虑几个问题。Session不能在本地文件存储要使用Redis外置存储。日志要输出到stdout由容器运行时收集。文件上传要使用对象存储不能存储在容器本地。环境配置通过环境变量注入不同环境使用不同的配置。Kubernetes为PHP应用提供了弹性伸缩、服务发现、配置管理等基础设施。PHP-FPM进程模型与Kubernetes的Pod管理方式配合良好每个Pod运行一个PHP-FPM进程通过Service暴露服务。HPA根据CPU和内存使用率自动调整Pod数量。

相关新闻