kubernetes啟用PHP+Nginx網(wǎng)頁環(huán)境教程
kubernetes 啟用 PHP + Nginx 網(wǎng)頁環(huán)境
傳統(tǒng)安裝方式進(jìn)行安裝步驟較多,使用kubernetes可以實(shí)現(xiàn)快速啟用環(huán)境,在測試或者線上都可以做到快速 啟用
編寫 yaml 文件
[root@k8s-master01 ~]# vim PHP-Nginx-Deployment-ConfMap-Service.yaml [root@k8s-master01 ~]# cat PHP-Nginx-Deployment-ConfMap-Service.yaml kind: Service # 對象類型 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 # 對象類型 apiVersion: v1 # api 版本 metadata: # 元數(shù)據(jù) name: nginx-config # 對象名稱 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 # 對象類型 apiVersion: apps/v1 # api 版本 metadata: # 元數(shù)據(jù) name: php-fpm-nginx # Deployment 對象名稱 spec: # Deployment 對象規(guī)約 selector: # 選擇器 matchLabels: # 標(biāo)簽匹配 app: php-fpm-nginx replicas: 3 # 副本數(shù)量 template: # 模版 metadata: # Pod 對象的元數(shù)據(jù) labels: # Pod 對象的標(biāo)簽 app: php-fpm-nginx spec: # Pod 對象規(guī)約 containers: # 這里設(shè)置了兩個(gè)容器 - name: php-fpm # 第一個(gè)容器名稱 image: php:7.4.29-fpm # 容器鏡像 imagePullPolicy: IfNotPresent #鏡像拉取策略 livenessProbe: # 存活探測 initialDelaySeconds: 5 # 容器啟動后要等待多少秒后才啟動存活和就緒探測器 periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測 tcpSocket: # 監(jiān)測tcp端口 port: 9000 #監(jiān)測端口 readinessProbe: # 就緒探測 initialDelaySeconds: 5 # 容器啟動后要等待多少秒后才啟動存活和就緒探測器 periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測 tcpSocket: # 監(jiān)測tcp端口 port: 9000 #監(jiān)測端口 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: # 存活探測 initialDelaySeconds: 5 # 容器啟動后要等待多少秒后才啟動存活和就緒探測器 periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測 httpGet: # 以httpGet方式進(jìn)行探測 path: / # 探測路徑 port: 80 # 探測端口 readinessProbe: # 就緒探測 initialDelaySeconds: 5 # 容器啟動后要等待多少秒后才啟動存活和就緒探測器 periodSeconds: 10 # 每多少秒執(zhí)行一次存活探測 httpGet: # 以httpGet方式進(jìn)行探測 path: / # 探測路徑 port: 80 # 探測端口 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)前目錄下 mv build/* . #設(shè)置權(quán)限 chmod 777 -R *
創(chuàng)建資源
kubectl apply -f PHP-Nginx-Deployment-ConfMap-Service.yaml
測試環(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)境的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Nginx在Windows下的安裝及環(huán)境配置(將nginx作為服務(wù)運(yùn)行)
這篇文章主要介紹了Nginx在Windows下的安裝及環(huán)境配置,主要是將nginx作為服務(wù)運(yùn)行,需要的朋友可以參考下2018-11-11nginx代理多次302的解決方法(nginx Follow 302)
這篇文章主要介紹了nginx代理多次302的解決方法(nginx Follow 302),詳細(xì)的介紹了解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Nginx+Tomcat負(fù)載均衡集群的實(shí)現(xiàn)示例
本文主要介紹了Nginx + Tomcat負(fù)載均衡集群的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux)
這篇文章主要介紹了Nginx反向代理及負(fù)載均衡如何實(shí)現(xiàn)(基于linux),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09