news 2026/5/8 16:52:09

通过kubernetes部署nginx + php网站环境

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
通过kubernetes部署nginx + php网站环境

在 Kubernetes 中部署 Nginx + PHP 网站环境,需通过 Deployment 管理应用副本、Service 暴露服务端口,并利用 Ingress 实现 HTTP 路由。以下是具体步骤及配置示例:

一、环境准备

  1. Kubernetes 集群
  2. Docker 镜像
    • PHP 应用镜像:基于官方 PHP 镜像(如php:7.4-fpm-alpine)构建,包含应用代码。
    • Nginx 镜像:使用官方 Nginx 镜像(如nginx:1.16.1),需配置反向代理指向 PHP。

二、部署步骤

1. 创建 PHP Deployment

创建Dockerfile
创建一个包含以下内容的Dockerfile

官方 PHP 镜像提供了docker-php-ext-install脚本,专门用于安装核心扩展(包括mysqli)。

dockerfile:

FROM php:7.4-fpm-alpine # 使用官方脚本安装 mysqli 扩展 RUN docker-php-ext-install mysqli

构建并推送镜像
使用自己的镜像仓库kubernetes-register.dong.com

# 构建镜像 docker build -t kubernetes-register.dong.com/php-with-mysqli:7.4-fpm-alpine . docker tag kubernetes-register.dong.com/php-with-mysqli:7.4-fpm-alpine kubernetes-register.dong.com/mysql/php-with-mysqli:7.4-fpm-alpine # 推送镜像到你的私有仓库 docker push kubernetes-register.dong.com/mysql/php-with-mysqli:7.4-fpm-alpine

定义 PHP 应用的副本数、容器镜像及端口(事先配置好PV动态供给)

php-deployment.yaml

# php-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: php-app spec: replicas: 2 selector: matchLabels: app: php template: metadata: labels: app: php spec: containers: - name: php image: kubernetes-register.dong.com/mysql/php-with-mysqli:7.4-fpm-alpine ports: - containerPort: 9000 volumeMounts: - name: html mountPath: /var/www/html volumes: - name: html persistentVolumeClaim: claimName: nginx-pvc

命令:

kubectl apply -f php-deployment.yaml
2. 创建 PHP Service

暴露 PHP 应用的端口,供 Nginx 内部访问。

php-service.yaml

# php-service.yaml apiVersion: v1 kind: Service metadata: name: php-service spec: selector: app: php ports: - protocol: TCP port: 9000 targetPort: 9000

命令:

kubectl apply -f php-service.yaml
3. 创建 Nginx Deployment

配置 Nginx 作为反向代理,指向 PHP Service。

nginx-deployment.yaml

# nginx-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-app spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.16.1 ports: - containerPort: 80 volumeMounts: - name: html mountPath: /var/www/html - name: nginx-config mountPath: /etc/nginx/conf.d volumes: - name: html persistentVolumeClaim: claimName: nginx-pvc - name: nginx-config configMap: name: nginx-config

关键点

  • 通过ConfigMap挂载 Nginx 配置文件(见下一步)。
  • 通过PVC挂载网站文件。
  • 容器端口为80(HTTP 默认端口)。
4. 创建 Nginx ConfigMap

定义 Nginx 反向代理配置,指向 PHP Service。

nginx-configmap.yaml

# nginx-configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: nginx-config data: default.conf: | server { listen 80; server_name student.dong.com; location / { root /var/www/html; index index.php index.html index.htm; } location ~ \.php$ { fastcgi_pass php-service:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; include fastcgi_params; } }

命令:

kubectl apply -f nginx-configmap.yaml kubectl apply -f nginx-deployment.yaml
5. 创建 Nginx Service

暴露 Nginx 端口到集群外部(如NodePortLoadBalancer)。

nginx-service.yaml

# nginx-service.yaml apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 type: NodePort

命令:

kubectl apply -f nginx-service.yaml
6. 部署 Ingress(可选)

若需通过域名访问,可部署 Ingress Controller(如 Nginx Ingress)并配置路由规则。

ingress.yaml

# ingress.yaml apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: web-ingress # annotations: # nginx.ingress.kubernetes.io/rewrite-target: /$1 spec: ingressClassName: nginx rules: - host: student.dong.com http: paths: - path: / pathType: Prefix backend: service: name: nginx-service port: number: 80

命令:

kubectl apply -f ingress.yaml

三、验证部署

  1. 检查 Pod 状态:kubectl get pods

  2. 获取 Service 外部 IP:kubectl get svc nginx-service

    若类型为LoadBalancer,会显示外部 IP;若为NodePort,需通过节点 IP + 端口访问。

  3. 访问应用

    • 直接访问外部 IP 或域名。
    • 检查 PHP 是否正常解析(如创建info.php文件输出phpinfo())。

四、关键配置说明

  • PHP-FPM 端口:默认9000,需与 Nginx 配置一致。
  • Nginx 反向代理:通过fastcgi_pass指向 PHP Service 的 ClusterIP。
  • 持久化存储:若需持久化数据(如上传文件),需添加PersistentVolumeClaim(PVC)。

五、扩展优化

  • 自动扩缩容:为 Deployment 配置Horizontal Pod Autoscaler(HPA)。
  • 健康检查:添加livenessProbereadinessProbe确保服务可用性。
  • 日志收集:集成 Fluentd 或 Loki 收集容器日志。

通过以上步骤,即可在 Kubernetes 中高效部署 Nginx + PHP 网站环境,实现高可用、可扩展的 Web 服务。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/30 12:46:18

BongoCat终极体验:我的桌面伴侣完整攻略

BongoCat终极体验:我的桌面伴侣完整攻略 【免费下载链接】BongoCat 让呆萌可爱的 Bongo Cat 陪伴你的键盘敲击与鼠标操作,每一次输入都充满趣味与活力! 项目地址: https://gitcode.com/gh_mirrors/bong/BongoCat 作为一名长期与电脑为…

作者头像 李华
网站建设 2026/5/1 10:22:53

大数据领域 OLAP 的多维度数据探索方法

大数据领域 OLAP 的多维度数据探索方法 关键词:OLAP、多维度分析、数据立方体、切片切块、下钻上卷、ROLAP、MOLAP 摘要:本文系统解析大数据环境下OLAP(在线分析处理)的多维度数据探索核心技术。从基础概念体系出发,深入剖析星型模型、雪花模型等数据建模方法,详细阐述切…

作者头像 李华
网站建设 2026/5/3 23:53:08

PicSharp:重新定义图片压缩体验的高性能跨平台解决方案

PicSharp:重新定义图片压缩体验的高性能跨平台解决方案 【免费下载链接】PicSharp A simple, efficient and flexible cross-platform desktop image compression application. 项目地址: https://gitcode.com/gh_mirrors/pi/PicSharp 作为一名经常需要处理大…

作者头像 李华
网站建设 2026/5/2 17:36:39

ZyPlayer终极体验:5个隐藏技巧让免费观影更精彩

ZyPlayer终极体验:5个隐藏技巧让免费观影更精彩 【免费下载链接】ZyPlayer 跨平台桌面端视频资源播放器,免费高颜值. 项目地址: https://gitcode.com/gh_mirrors/zy/ZyPlayer 在众多跨平台视频播放器中,ZyPlayer以其免费高颜值的特性脱颖而出&…

作者头像 李华