kubernetes啟用PHP+Nginx網(wǎng)頁環(huán)境教程
kubernetes 啟用 PHP + Nginx 網(wǎng)頁環(huán)境
傳統(tǒng)安裝方式進(jìn)行安裝步驟較多,使用kubernetes可以實(shí)現(xiàn)快速啟用環(huán)境,在測(cè)試或者線上都可以做到快速 啟用
編寫 yaml 文件
[root@k8s-master01 ~]# vim PHP-Nginx-Deployment-ConfMap-Service.yaml
[root@k8s-master01 ~]# cat PHP-Nginx-Deployment-ConfMap-Service.yaml
kind: Service # 對(duì)象類型
apiVersion: v1 # api 版本
metadata: # 元數(shù)據(jù)
name: php-fpm-nginx #Service 服務(wù)名
spec:
type: NodePort # 類型為nodeport
selector: #標(biāo)簽選擇器
app: php-fpm-nginx
ports: #端口信息
- port: 80 # 容器端口80
protocol: TCP #tcp類型
targetPort: 80 # Service 將 nginx 容器的 80 端口暴露出來
---
kind: ConfigMap # 對(duì)象類型
apiVersion: v1 # api 版本
metadata: # 元數(shù)據(jù)
name: nginx-config # 對(duì)象名稱
data: # key-value 數(shù)據(jù)集合
nginx.conf: | # 將 nginx config 配置寫入 ConfigMap 中,經(jīng)典的 php-fpm 代理設(shè)置,這里就不再多說了
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php;
server_name _;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php) {
rewrite (.*) $1/index.php;
}
if (!-f $request_filename) {
rewrite (.*) /index.php;
}
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;
}
}
include /etc/nginx/conf.d/*.conf;
}
---
kind: Deployment # 對(duì)象類型
apiVersion: apps/v1 # api 版本
metadata: # 元數(shù)據(jù)
name: php-fpm-nginx # Deployment 對(duì)象名稱
spec: # Deployment 對(duì)象規(guī)約
selector: # 選擇器
matchLabels: # 標(biāo)簽匹配
app: php-fpm-nginx
replicas: 3 # 副本數(shù)量
template: # 模版
metadata: # Pod 對(duì)象的元數(shù)據(jù)
labels: # Pod 對(duì)象的標(biāo)簽
app: php-fpm-nginx
spec: # Pod 對(duì)象規(guī)約
containers: # 這里設(shè)置了兩個(gè)容器
- name: php-fpm # 第一個(gè)容器名稱
image: php:7.4.29-fpm # 容器鏡像
imagePullPolicy: IfNotPresent #鏡像拉取策略
livenessProbe: # 存活探測(cè)
initialDelaySeconds: 5 # 容器啟動(dòng)后要等待多少秒后才啟動(dòng)存活和就緒探測(cè)器
periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測(cè)
tcpSocket: # 監(jiān)測(cè)tcp端口
port: 9000 #監(jiān)測(cè)端口
readinessProbe: # 就緒探測(cè)
initialDelaySeconds: 5 # 容器啟動(dòng)后要等待多少秒后才啟動(dòng)存活和就緒探測(cè)器
periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測(cè)
tcpSocket: # 監(jiān)測(cè)tcp端口
port: 9000 #監(jiān)測(cè)端口
resources: # 資源約束
requests: # 最小限制
memory: "64Mi" # 內(nèi)存最新64M
cpu: "250m" # CPU最大使用0.25核
limits: # 最大限制
memory: "128Mi" # 內(nèi)存最新128M
cpu: "500m" # CPU最大使用0.5核
ports:
- containerPort: 9000 # php-fpm 端口
volumeMounts: # 掛載數(shù)據(jù)卷
- mountPath: /var/www/html # 掛載兩個(gè)容器共享的 volume
name: nginx-www
lifecycle: # 生命周期
postStart: # 當(dāng)容器處于 postStart 階段時(shí),執(zhí)行一下命令
exec:
command: ["/bin/sh", "-c", "echo startup..."] # 將 /app/index.php 復(fù)制到掛載的 volume
preStop:
exec:
command:
- sh
- '-c'
- sleep 5 && kill -SIGQUIT 1 # 優(yōu)雅退出
- name: nginx # 第二個(gè)容器名稱
image: nginx # 容器鏡像
imagePullPolicy: IfNotPresent
livenessProbe: # 存活探測(cè)
initialDelaySeconds: 5 # 容器啟動(dòng)后要等待多少秒后才啟動(dòng)存活和就緒探測(cè)器
periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測(cè)
httpGet: # 以httpGet方式進(jìn)行探測(cè)
path: / # 探測(cè)路徑
port: 80 # 探測(cè)端口
readinessProbe: # 就緒探測(cè)
initialDelaySeconds: 5 # 容器啟動(dòng)后要等待多少秒后才啟動(dòng)存活和就緒探測(cè)器
periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測(cè)
httpGet: # 以httpGet方式進(jìn)行探測(cè)
path: / # 探測(cè)路徑
port: 80 # 探測(cè)端口
resources: # 資源約束
requests: # 最小限制
memory: "64Mi" # 內(nèi)存最新64M
cpu: "250m" # CPU最大使用0.25核
limits: # 最大限制
memory: "128Mi" # 內(nèi)存最新128M
cpu: "500m" # CPU最大使用0.5核
ports:
- containerPort: 80 # nginx 端口
volumeMounts: # nginx 容器掛載了兩個(gè) volume,一個(gè)是與 php-fpm 容器共享的 volume,另外一個(gè)是配置了 nginx.conf 的 volume
- mountPath: /var/www/html # 掛載兩個(gè)容器共享的 volume
name: nginx-www
- mountPath: /etc/nginx/nginx.conf # 掛載配置了 nginx.conf 的 volume
subPath: nginx.conf
name: nginx-config
lifecycle:
preStop:
exec:
command:
- sh
- '-c'
- sleep 5 && /usr/sbin/nginx -s quit # 優(yōu)雅退出
volumes:
- name: nginx-www # 網(wǎng)站文件通過nfs掛載
nfs:
path: /html/
server: 192.168.1.123
- name: nginx-config
configMap: # configMap
name: nginx-config部署網(wǎng)站
# 下載網(wǎng)站代碼 wget https://typecho.org/downloads/1.1-17.10.30-release.tar.gz # 解壓源碼包 tar xvf 1.1-17.10.30-release.tar.gz #移動(dòng)到當(dāng)前目錄下 mv build/* . #設(shè)置權(quán)限 chmod 777 -R *
創(chuàng)建資源
kubectl apply -f PHP-Nginx-Deployment-ConfMap-Service.yaml
測(cè)試環(huán)境
kubectl get pod -l app=php-fpm-nginx NAME READY STATUS RESTARTS AGE php-fpm-nginx-8b4bfb457-24bpd 2/2 Running 1 (6m34s ago) 16m php-fpm-nginx-8b4bfb457-fvqd6 2/2 Running 2 (5m39s ago) 16m php-fpm-nginx-8b4bfb457-kmzsc 2/2 Running 1 (6m34s ago) 16m kubectl get configmaps | grep nginx NAME DATA AGE nginx-config 1 17m kubectl get svc | grep nginx php-fpm-nginx NodePort 10.98.66.104 <none> 80:31937/TCP 16m

以上就是kubernetes啟用PHP+Nginx網(wǎng)頁環(huán)境教程的詳細(xì)內(nèi)容,更多關(guān)于kubernetes啟用PHP Nginx網(wǎng)頁環(huán)境的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx配置ssl實(shí)現(xiàn)https的全過程記錄
這篇文章主要給大家介紹了關(guān)于Nginx配置ssl實(shí)現(xiàn)https的相關(guān)資料,文章通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
Nginx與后臺(tái)應(yīng)用端口沖突的解決方案
在部署Web應(yīng)用時(shí),Nginx和后臺(tái)應(yīng)用(如Node.js、Python Flask、Java Spring Boot等)常常需要同時(shí)運(yùn)行在一臺(tái)服務(wù)器上,然而,當(dāng)它們需要監(jiān)聽同一個(gè)端口(如8000)時(shí),就會(huì)出現(xiàn)端口沖突的問題,本文將詳細(xì)介紹幾種解決Nginx與后臺(tái)應(yīng)用端口沖突的方法2025-02-02
Nginx 配置反向代理使用 Google fonts 字體并開啟 HTTP2/SSL 支持
nginx作為web服務(wù)器一個(gè)重要的功能就是反向代理。當(dāng)然你也可以使用nginx配置正向代理,本是介紹如何配置nginx的反向代理。nginx反向代理的指令不需要新增額外的模塊,默認(rèn)自帶proxy_pass指令,只需要修改配置文件就可以實(shí)現(xiàn)反向代理。2017-04-04
nginx中配置https的詳細(xì)步驟及配置SSL/TLS證書的完整流程
本文將詳細(xì)介紹如何在 Nginx 服務(wù)器上配置 SSL/TLS 證書,包括獲取證書、配置 Nginx、強(qiáng)化安全性等完整流程,無論您是使用 Let’s Encrypt 的免費(fèi)證書還是自簽名證書,都能在本指南中找到對(duì)應(yīng)的配置方法,感興趣的朋友跟隨小編一起看看吧2025-10-10
nginx部署https網(wǎng)站的實(shí)現(xiàn)步驟(親測(cè))
本文詳細(xì)介紹了使用Nginx在保持與http服務(wù)兼容的情況下部署HTTPS,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02

