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
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