云其它

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > 云其它 > kubernetes部署nginx+php网站环境

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

作者:ICT董老师

本文详细介绍了如何在Kubernetes中部署Nginx+PHP网站环境,包括环境准备、部署步骤、Service、ConfigMap、Ingress等配置,并提供了具体的YAML文件示例和命令,通过这些步骤,可以实现高可用、可扩展的Web服务

在 Kubernetes 中部署 Nginx + PHP 网站环境,需通过 Deployment 管理应用副本、Service 暴露服务端口,并利用 Ingress 实现 HTTP 路由。

以下是具体步骤及配置示例:

一、环境准备

Kubernetes 集群

Docker 镜像

二、部署步骤

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

关键点

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 端口到集群外部(如 NodePort 或 LoadBalancer)。

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

三、验证部署

检查 Pod 状态:kubectl get pods

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

访问应用

四、关键配置说明

五、扩展优化

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文