k8s nginx-php 环境部署
环境基于minikube
configmap.yaml nginx配置 kind: ConfigMap # 对象类型 apiVersion: v1 # api 版本 metadata: # 元数据 name: nginx-config # 对象名称 data: # key-value 数据集合 nginx.conf: | events { } http { server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; #php 与 nginx 的目录必须相同 index index.php; server_name _; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include fastcgi_params; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #注意目录 fastcgi_pass 127.0.0.1:9000; } } }
php-fpm.yaml nginx fpm共用一个pod 这样127.0.0.1:9000能访问到 kind: Deployment # 对象类型 apiVersion: apps/v1 # api 版本 metadata: # 元数据 name: php-fpm-nginx # Deployment 对象名称 spec: # Deployment 对象规约 selector: # 选择器 matchLabels: # 标签匹配 app: php-fpm-nginx replicas: 1 # 副本数量 template: # 模版 metadata: # Pod 对象的元数据 labels: # Pod 对象的标签 app: php-fpm-nginx spec: # Pod 对象规约 containers: # 这里设置了两个容器 - name: php-fpm # 第一个容器名称 image: hejinxue/fpm:v1 # 容器镜像 已经把代码打包到镜像中 ports: - containerPort: 9000 # php-fpm 端口
envFrom:
- secretRef:
name: mysecret #把敏感的数据信息放入$_SERVER
- name: nginx # 第二个容器名称 image: hejinxue/nginx:v1 # 容器镜像 创建了默认的index.php空文件 ports: - containerPort: 80 # nginx 端口 volumeMounts: - mountPath: /etc/nginx/nginx.conf # 挂载配置了 nginx.conf 的 volume subPath: nginx.conf name: nginx-config volumes: - name: nginx-config configMap: name: nginx-config
kubectl expose deployment php-fpm-nginx --type=NodePort --port=80 暴露80端口 会创建Service
创建nginx—ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: hello-world.info
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: php-fpm-nginx
port:
number: 80
创建Secret
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
USER_NAME: YWRtaW4=
PASSWORD: MWYyZDFlMmU2N2Rm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | apiVersion: v1 kind: Pod metadata: name: php-pod spec: containers: - name: fpm image: php-fpm ports: - name: web containerPort: 80 lifecycle: preStop: exec : command: - sh - '-c' - sleep 5 && kill -SIGQUIT 1 //通过此种方式 处理pod删除时 未完成的请求 - name: nginx image: nginx ports: - name: http containerPort: 80 lifecycle: preStop: exec : command: - sh - '-c' - sleep 5 && /usr/sbin/nginx -s quit terminationGracePeriodSeconds: 30 |
参考文章
https://learnku.com/articles/42320
标签:
k8s
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?