kubernetes启用PHP+Nginx网页环境传统的安装方式安装步骤较多。使用kubernetes可以快速启用环境。它可以在测试期间或在线时快速启用。编写yaml文件[root@k8s-master01~]#vimPHP-Nginx-Deployment-ConfMap-Service.yaml[root@k8s-master01~]#catPHP-Nginx-Deployment-ConfMap-Service.yamlkind:Service#对象类型apiVersion:v1#APIversionmetadata:#Metadataname:php-fpm-nginx#Service服务名称spec:type:NodePort#typeisnodeportselector:#labelselectorapp:php-fpm-nginxports:#portinformation-port:80#容器80端口protocol:TCP#tcptypetargetPort:80#服务暴露nginx容器的80端口---kind:ConfigMap#objecttypeapiVersion:v1#apiversionmetadata:#metadataname:nginx-config#objectnamedata:#key-value数据集nginx.conf:|#将nginxconfig配置写入ConfigMap,经典的php-fpm代理设置,这里不再赘述usernginx;worker_processes自动;error_log/var/log/nginx/error.log通知;pid/var/run/nginx.pid;事件{worker_connections1024;}http{包括/etc/nginx/mime.types;默认类型申请离子/八位位组流;log_formatmain'$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.logmain;发送文件;keepalive_timeout65;服务器{听80default_server;听[::]:80默认服务器;根/var/www/html;索引index.php;服务器名称_;if(-f$request_filename/index.html){重写(.*)$1/index.html中断;}if(-f$request_filename/index.php){重写(.*)$1/index.php;}if(!-f$request_filename){重写(.*)/index.php;}location/{try_files$uri$uri/=404;}location~\.php${包括fastcgi_params;fastcgi_param请求UEST_METHOD$request_method;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_pass127.0.0.1:9000;}}包含/etc/nginx/conf.d/*.conf;}---kind:Deployment#对象类型apiVersion:apps/v1#apiversionmetadata:#Metadataname:php-fpm-nginx#Deploymentobjectnamespec:#Deploymentobjectspecificationselector:#SelectormatchLabels:#Labelmatchingapp:php-fpm-nginxreplicas:3#replicastemplate:#templatemetadata:#Podobjectlabels的元数据:#Podobjectlabelapp:php-fpm-nginxspec:#Podobjectspecificationcontainers:#两个容器这里设置了-name:php-fpm#第一个容器镜像的名称:php:7.4.29-fpm#容器镜像imagePullPolicy:IfNotPresent#镜像拉取策略livenessProbe:#存活检测initialDelaySeconds:5#等待多少秒后容器在启动生存和就绪探测之前启动periodSeconds:10#每多少每秒执行一次livenessdetectiontcpSocket:#Monitortcpportport:9000#MonitorportreadinessProbe:#ReadinessdetectioninitialDelaySeconds:5#多少秒容器在开始存储之前等待LivenessandReadinessdetectorperiodSeconds:10#多少秒执行一次livenessdetectiontcpSocket:#监控tcp端口port:9000#监控端口resources:#资源约束请求:#最小限制内存:"64Mi"#内存最新64Mcpu:"250m"#CPU最大使用0.25核限制:#最大限制内存:"128Mi"#内存最新128Mcpu:"500m"#CPU最大使用0.5核ports:-containerPort:9000#php-fpmportvolumeMounts:#mountdataVolume-mountPath:/var/www/html#挂载两个容器共享的volumename:nginx-wwwlifecycle:#LifecyclepostStart:#当容器处于postStart阶段时,执行命令exec:command:["/bin/sh","-c","echostartup..."]#复制/app/index.php到挂载卷preStop:exec:command:-sh-'-c'-sleep5&&kill-SIGQUIT1#Gracefulexit-name:nginx#第二个容器名称image:nginx#容器镜像imagePullPolicy:IfNotPresentlivenessProbe:#livenessprobeinitialDelaySeconds:5#容器启动后等待多少秒才开始livenessandreadinessprobeperiodSeconds:10#everyHowmanysecondstoperformalivenessprobehttpGet:#ProbeinhttpGetpath:/#Probepathport:80#ProbeportreadinessProbe:#ReadinessprobeinitialDelaySeconds:5#容器后多少秒开始存活和就绪探测startsperiodSeconds:10#多少秒进行一次生存检测httpGet:#通过httpGet进行探测path:/#检测路径port:80#检测端口resources:#资源约束requests:#最小限制memory:"64Mi"#最新内存是64Mcpu:"250m"#CPU最大使用0.25核limits:#最大限制内存:"128Mi"#内存最新128Mcpu:"500m"#CPU最大使用0.5核ports:-containerPort:80#nginxportvolumeMounts:#nginxcontainer挂载了两个卷,一个是用php-fpm容器共享的volume,另一个是nginx.conf配置的volume-mountPath:/var/www/html#挂载两个容器共享的volumename:nginx-www-mountPath:/etc/nginx/nginx.conf#挂载配置了nginx.conf的volume子路径:nginx.confname:nginx-configlifecycle:preStop:exec:command:-sh-'-c'-sleep5&&/usr/sbin/nginx-squit#优雅exitvolumes:-name:nginx-www#通过nfs挂载网站文件nfs:path:/html/server:192.168.1.123-name:nginx-configconfigMap:#configMapname:nginx-configdeploywebsite#下载网站代码wgethttps://typecho.org/downloads/1.1-17.10.30-release.tar.gz#解压源码包tarxvf1.1-17.10.30-release.tar.gz#移动到当前目录mvbuild/*。#设置权限chmod777-R*创建资源kubectlapply-fPHP-Nginx-Deployment-ConfMap-Service.yaml测试环境kubectlgetpod-lapp=php-fpm-nginxNAMEREADYSTATUSRESTARTSAGEphp-fpm-nginx-8b4bfb457-24bpd2/2Running1(6m34sago)16mphp-fpm-nginx-8b4bfb457-fvqd62/2Running2(5m39sago)16mphp-fpm-nginx-8b4bfbsc427/2Running1(6分钟34秒前)16mkubectl获取配置映射|grepnginxNAMEDATAAGEnginex-config117mkubectlgetsvc|grepnginxphp-fpm-nginxNodePort10.98.66.104
