k8s部署nginx+php+mysql
创始人
2024-05-29 22:04:52
0

mysql部署参考我之前文档
http://t.csdn.cn/OKfTH

一.hostPath创建项目

1.编辑dockerfile

vi dockerfile
i
FROM docker.io/openshift/base-centos7:latest#MAINTAINER feiyu "xxx@126.com"RUN yum makecacheRUN yum -y install php-fpm php php-gd php-mysql php-mbstring php-xml php-mcrypt  php-imap php-odbc php-pear php-xmlrpcRUN sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /etc/php-fpm.d/www.confRUN sed -i 's/listen.allowed_clients = 127.0.0.1/;listen.allowed_clients = 127.0.0.1/' /etc/php-fpm.d/www.confEXPOSE 9000CMD ["/sbin/php-fpm"]

#编译

#编译包
docker build -t php:0.1 .#打包镜像
docker save php:0.1 > php-0.1.tar#加载镜像
docker load -i php-0.1.tar

2.部署php

编辑php-deloy-ser.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:name: php-serverlabels:name: php-server
spec:replicas: 1selector:matchLabels:app: php-servertemplate:metadata:labels:app: php-serverspec:containers:- name: php-serverimage: php:0.1imagePullPolicy: IfNotPresentvolumeMounts:- mountPath: /var/www/html/name: nginx-dataports:- containerPort: 9000volumes:- name: nginx-datahostPath:path: /root/k8s/html
---
apiVersion: v1
kind: Service
metadata:name: php
spec:ports:- name: phpport: 9000protocol: TCPtargetPort: 9000selector:app: php-server

#安装

#部署
kubectl apply -f php-deloy-ser.yaml#检查
kubectl get pod,deploy,svc |grep php

3.部署nginx

apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-php
spec:selector:matchLabels:app: nginx-phpreplicas: 1template:metadata:labels:app: nginx-phpspec:containers:- name: nginx-phpimage: nginx:1.23.1imagePullPolicy: IfNotPresentports:- containerPort: 80volumeMounts:- name: nginx-datamountPath: /usr/share/nginx/html- name: nginx-confmountPath: /etc/nginx/conf.d/volumes:- name: nginx-datahostPath:path: /root/k8s/html- name: nginx-confhostPath:path: /root/k8s/conf
---
apiVersion: v1
kind: Service
metadata:name: nginx-php
spec:type: NodePortports:- name: nginxport: 80protocol: TCPtargetPort: 80nodePort: 30004selector:app: nginx-php

#安装

#部署
kubectl apply -f nginx-deploy-ser.yaml#检查
kubectl get pod,deploy,svc |grep nginx

新增nginx配置

cat > /root/k8s/conf/default.conf <

新建index.php测试页面

cat > /root/k8s/html/index.php <
EOF

4.访问测试页面,本机ip加nginx外网端口

192.168.1.xx:30004

二.StorageClass存储创建项目

1.创建pv

vi 01-pvc-nginx-php.yaml
i
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: nginx-php-pvc
spec:accessModes:- ReadWriteManyresources:requests:storage: 2Gi# 存储类,具有相同存储类名称的pv和pvc才能进行绑定storageClassName: nfs-boge# 定义mysql的持久卷声明信息
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: nginx-php-pvc
spec:accessModes:- ReadWriteManyresources:requests:storage: 2Gi# 存储类,具有相同存储类名称的pv和pvc才能进行绑定storageClassName: nfs-boge#创建pv
kubectl apply -f 01-pvc-nginx-php.yaml

2.nginx和php配置文件

vi 02-configMap-nginx-php.yaml
i
apiVersion: v1
kind: ConfigMap
metadata:name: nginx-config
data:default.conf: |server {listen 80;server_name localhost;location / {root /var/www/html;index index.html index.htm index.php;}location ~ \.php$ {root /var/www/html;fastcgi_pass php:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}---
apiVersion: v1
kind: ConfigMap
metadata:name: php-config
data:php.ini: |memory_limit = 256Mdisplay_errors = Offerror_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICTpost_max_size = 32Mupload_max_filesize = 32Mmax_execution_time = 60max_input_time = 60date.timezone = "Asia/Shanghai"[opcache]opcache.enable = 1opcache.memory_consumption = 256opcache.interned_strings_buffer = 16opcache.max_accelerated_files = 10000opcache.validate_timestamps = 1opcache.revalidate_freq = 60#memory_limit:指定 PHP 进程可以使用的内存上限,单位为兆字节。
#display_errors:指定是否在浏览器中显示 PHP 错误信息。设置为 Off 则不会显示错误信息。
#error_reporting:指定 PHP 报告的错误级别。E_ALL 表示报告所有错误,~E_DEPRECATED 和 ~E_STRICT 表示不报告过时和严格模式的错误。
#post_max_size:指定允许上传的 POST 数据的最大大小,单位为兆字节。
#upload_max_filesize:指定允许上传的单个文件的最大大小,单位为兆字节。
#max_execution_time:指定 PHP 进程的最长执行时间,单位为秒。如果 PHP 脚本执行时间超过此值,则 PHP 进程将被终止。
#max_input_time:指定 PHP 处理输入数据(例如 POST 数据和上传文件)的最长时间,单位为秒。
#date.timezone:指定时区,这里设置为亚洲/上海。
#opcache.enable:指定是否启用 PHP OPCache,OPCache 是 PHP 5.5 以上版本的一个内置的缓存模块,可以提高 PHP 的性能。
#opcache.memory_consumption:指定 OPCache 可以使用的最大内存量,单位为兆字节。
#opcache.interned_strings_buffer:指定用于缓存字符串的缓冲区大小。
#opcache.max_accelerated_files:指定可以缓存的 PHP 文件的最大数量。
#opcache.validate_timestamps:指定是否检查文件的修改时间以确定缓存是否过期。设置为 1 则表示开启此功能。
#opcache.revalidate_freq:指定多长时间重新验证一次缓存中的文件,单位为秒。#创建configMap
kubectl apply -f 02-configMap-nginx-php.yaml

3.创建php

vi 03-php-deloy-svc.yaml
i
apiVersion: apps/v1
kind: Deployment
metadata:name: php-serverlabels:name: php-server
spec:replicas: 2selector:matchLabels:app: php-servertemplate:metadata:labels:app: php-serverspec:containers:- name: php-serverimage: php:0.1imagePullPolicy: IfNotPresentenv:- name: TZvalue: Asia/ShanghaivolumeMounts:- mountPath: /var/www/html/name: nginx-data- mountPath: /usr/local/etc/php/conf.dname: php-configports:- containerPort: 9000volumes:- name: nginx-datapersistentVolumeClaim:claimName: nginx-php-pvc- name: php-configconfigMap:name: php-config
---
apiVersion: v1
kind: Service
metadata:name: php
spec:ports:- name: phpport: 9000protocol: TCPtargetPort: 9000selector:app: php-serverkubectl apply -f 03-php-deloy-svc.yaml

4.创建nginx

vi 04-nginx-deploy-svc.yaml
i
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-php
spec:selector:matchLabels:app: nginx-phpreplicas: 1template:metadata:labels:app: nginx-phpspec:containers:- name: nginx-phpimage: nginx:1.23.1imagePullPolicy: IfNotPresentenv:- name: TZvalue: Asia/Shanghaiports:- containerPort: 80volumeMounts:- name: nginx-datamountPath: /usr/share/nginx/html- name: nginx-configmountPath: /etc/nginx/conf.d/volumes:- name: nginx-datapersistentVolumeClaim:claimName: nginx-php-pvc- name: nginx-configconfigMap:name: nginx-config
---
apiVersion: v1
kind: Service
metadata:name: nginx-php
spec:type: NodePortports:- name: nginxport: 80protocol: TCPtargetPort: 80nodePort: 30004selector:app: nginx-phpkubectl apply -f 04-nginx-deploy-svc.yaml

5.测试Nginx访问php

cd /nfs_dir/default-nginx-php-pvc-pvc-7adf8df9-a01a-4584-9ae8-bcbec6929796cat > index.php <
EOF#测试url
http://ingress-http-test2.com:30004/

6.测试php连接mysql数据库


vi db.php
i
connect_error) {die("连接失败:" . $conn->connect_error);
}// 执行查询语句
$sql = "SELECT * FROM user";
$result = $conn->query($sql);// 检查查询结果是否为空
if ($result->num_rows > 0) {// 查询成功,返回yesecho "YES\n";
} else {// 查询失败,返回NOecho "NO\n";
}// 关闭数据库连接
$conn->close();
?>#测试url
http://ingress-http-test2.com:30004/db.php

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...