在 Kubernetes 中部署 Nginx + PHP 网站环境,需通过 Deployment 管理应用副本、Service 暴露服务端口,并利用 Ingress 实现 HTTP 路由。以下是具体步骤及配置示例:
一、环境准备
- Kubernetes 集群
- Docker 镜像
- PHP 应用镜像 :基于官方 PHP 镜像(如
php:7.4-fpm-alpine)构建,包含应用代码。 - Nginx 镜像 :使用官方 Nginx 镜像(如
nginx:1.16.1),需配置反向代理指向 PHP。
- PHP 应用镜像 :基于官方 PHP 镜像(如
二、部署步骤
1. 创建 PHP Deployment
创建 Dockerfile :
创建一个包含以下内容的 Dockerfile:
官方 PHP 镜像提供了 docker-php-ext-install 脚本,专门用于安装核心扩展(包括 mysqli)。
dockerfile:
bash
FROM php:7.4-fpm-alpine
# 使用官方脚本安装 mysqli 扩展
RUN docker-php-ext-install mysqli
构建并推送镜像 :
使用自己的镜像仓库 kubernetes-register.dong.com:
bash
# 构建镜像
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
bash
# 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
命令:
bash
kubectl apply -f php-deployment.yaml
2. 创建 PHP Service
暴露 PHP 应用的端口,供 Nginx 内部访问。
php-service.yaml
bash
# php-service.yaml
apiVersion: v1
kind: Service
metadata:
name: php-service
spec:
selector:
app: php
ports:
- protocol: TCP
port: 9000
targetPort: 9000
命令:
bash
kubectl apply -f php-service.yaml
3. 创建 Nginx Deployment
配置 Nginx 作为反向代理,指向 PHP Service。
nginx-deployment.yaml
bash
# 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
bash
# 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;
}
}
命令:
bash
kubectl apply -f nginx-configmap.yaml
kubectl apply -f nginx-deployment.yaml
5. 创建 Nginx Service
暴露 Nginx 端口到集群外部(如 NodePort 或 LoadBalancer)。
nginx-service.yaml
bash
# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePort
命令:
bash
kubectl apply -f nginx-service.yaml
6. 部署 Ingress(可选)
若需通过域名访问,可部署 Ingress Controller(如 Nginx Ingress)并配置路由规则。
ingress.yaml
bash
# 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
命令:
bash
kubectl apply -f ingress.yaml
三、验证部署
-
检查 Pod 状态:kubectl get pods
-
获取 Service 外部 IP:kubectl get svc nginx-service
若类型为
LoadBalancer,会显示外部 IP;若为NodePort,需通过节点 IP + 端口访问。 -
访问应用:
- 直接访问外部 IP 或域名。
- 检查 PHP 是否正常解析(如创建
info.php文件输出phpinfo())。
四、关键配置说明
- PHP-FPM 端口 :默认
9000,需与 Nginx 配置一致。 - Nginx 反向代理 :通过
fastcgi_pass指向 PHP Service 的 ClusterIP。 - 持久化存储 :若需持久化数据(如上传文件),需添加
PersistentVolumeClaim(PVC)。
五、扩展优化
- 自动扩缩容 :为 Deployment 配置
Horizontal Pod Autoscaler(HPA)。 - 健康检查 :添加
livenessProbe和readinessProbe确保服务可用性。 - 日志收集:集成 Fluentd 或 Loki 收集容器日志。
通过以上步骤,即可在 Kubernetes 中高效部署 Nginx + PHP 网站环境,实现高可用、可扩展的 Web 服务。