K8S中基于Nginx+PHP+MySQL实现的WordPress的web站点
LNMP案例之基于Nginx+PHP实现WordPress博客站点,要求Nginx+PHP运行在同一个Pod的不同容器,MySQL
运行与default的namespace并可以通过service name增删改查数据库。
1.安装nfs服务器,基于PV作为后端存储
[root@localhost7B ]# cat /etc/exports
/data/k8sdata *(rw,no_root_squash)
#创建项目目录
[root@localhost7B ~]# mkdir /data/k8sdata/magedu/wordpress
#第三步创建的账号,nginx要读取。
[root@localhost7B magedu]# chown 2019:2019 wordpress -R
#测试页
[root@localhost7B wordpress]# cat
<?php
phpinfo();
?>
[root@localhost7B ]# systemctl restart nfs-server.service
2.构造初始化镜像
下载初始镜像,上传镜像到harbor.zzhz.com服务器
[root@localhost7C centos]# docker pull centos:7.6.1810
[root@localhost7C centos]# docker tag f1cb7c7d58b7 harbor.zzhz.com/baseimages/centos:7.6.1810
[root@localhost7C centos]# docker push harbor.zzhz.com/baseimages/centos:7.6.1810
3.对初始镜像进行基础软件安装,此镜像为nginx tomcat jdk的基本镜像
[root@localhost7C centos]# ll
-rw-r--r-- 1 root root 174 4月 7 2020 build-command.sh
-rw-r--r-- 1 root root 503 4月 9 2020 Dockerfile
-rw-r--r-- 1 root root 24694569 4月 7 2020 filebeat-7.6.1-x86_64.rpm
[root@localhost7C centos]# cat Dockerfile
#自定义Centos 基础镜像
from from harbor.zzhz.com/baseimages/centos:7.6.1810
LABEL MAINTAINER="2973707860@qq.com"
ADD filebeat-7.6.1-x86_64.rpm /tmp
RUN yum install -y /tmp/filebeat-7.6.1-x86_64.rpm vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel \
openssl openssl-devel iproute net-tools iotop && rm -rf /etc/localtime /tmp/filebeat-7.6.1-x86_64.rpm \
&& ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && useradd nginx -u 2019 && useradd www -u 2020
#harbor先创建/baseimages项目
[root@localhost7C centos]# cat build-command.sh
#!/bin/bash
docker build -t harbor.zzhz.com/baseimages/magedu-centos-base:7.6.1810 .
docker push harbor.zzhz.com/baseimages/magedu-centos-base:7.6.1810
#执行构造
[root@localhost7C centos]# chmod +x build-command.sh
[root@localhost7C centos]# ./build-command.sh
4.创建nginx软件公用镜像
[root@localhost7C pub-images]# ll nginx-base-wordpress/
总用量 1000
-rw-r--r-- 1 root root 164 4月 11 17:21 build-command.sh
-rw-r--r-- 1 root root 496 4月 11 17:17 Dockerfile
-rw-r--r-- 1 root root 1015384 4月 7 2020 nginx-1.14.2.tar.gz
[root@localhost7C pub-images]# cd nginx-base-wordpress/
[root@localhost7C nginx-base-wordpress]# cat Dockerfile
#Nginx Base Image
FROM harbor.zzhz.com/baseimages/magedu-centos-base:7.6.1810
MAINTAINER zhangshijie@magedu.net
RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
ADD nginx-1.14.2.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.14.2 && ./configure --prefix=/usr/local/nginx && make && make install && ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx &&rm -rf /usr/local/src/nginx-1.14.2.tar.gz
[root@localhost7C nginx-base-wordpress]# cat build-command.sh
#!/bin/bash
docker build -t harbor.zzhz.com/pub-images/nginx-base-wordpress:v1.14.2 .
sleep 1
docker push harbor.zzhz.com/pub-images/nginx-base-wordpress:v1.14.2
[root@localhost7C nginx-base-wordpress]# chmod +x build-command.sh
[root@localhost7C nginx-base-wordpress]# ./build-command.sh
4.创建nginx项目镜像。业务使用。
[root@localhost7C linux39]# tree wordpress/
wordpress/
├── nginx
│ ├── build-command.sh
│ ├── Dockerfile
│ ├── index.html
│ ├── nginx.conf
│ └── run_nginx.sh
└── php
├── build-command.sh
├── Dockerfile
├── run_php.sh
└── www.conf
[root@localhost7C nginx]# cat nginx.conf
user nginx nginx;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#daemon off;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 10M;
client_body_buffer_size 16k;
client_body_temp_path /apps/nginx/tmp 1 2 2;
gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/nginx/wordpress;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /home/nginx/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
执行脚本
[root@localhost7C nginx]# cat run_nginx.sh
#!/bin/bash
#echo "nameserver 10.20.254.254" > /etc/resolv.conf
/usr/local/nginx/sbin/nginx
tail -f /etc/hosts
[root@localhost7C nginx]# cat index.html
nginx web1
[root@localhost7C linux39]# cd wordpress/nginx/
[root@localhost7C nginx]# cat Dockerfile
FROM harbor.zzhz.com/pub-images/nginx-base-wordpress:v1.14.2
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD run_nginx.sh /usr/local/nginx/sbin/run_nginx.sh
RUN mkdir -pv /home/nginx/wordpress
ADD index.html /home/nginx/wordpress/
RUN chown nginx.nginx /home/nginx/wordpress/ -R
EXPOSE 80 443
CMD ["/usr/local/nginx/sbin/run_nginx.sh"]
[root@localhost7C nginx]# cat build-command.sh
#!/bin/bash
TAG=$1
docker build -t harbor.zzhz.com/linux39/wordpress-nginx:${TAG} .
echo "镜像制作完成,即将上传至Harbor服务器"
sleep 1
docker push harbor.zzhz.com/linux39/wordpress-nginx:${TAG}
echo "镜像上传完成"
[root@localhost7C nginx]# chmod +x *.sh
[root@localhost7C nginx]# build-command.sh 2023-04-12-v2
测试:
[root@localhost7C nginx-base-wordpress]# docker run -it --rm -p 80:80 harbor.zzhz.com/linux39/wordpress-nginx:2023-04-12-v2 bash
5.创建php镜像。
[root@localhost7C php]# grep "^[a-Z]" www.conf
user = nginx
group = nginx
listen = 0.0.0.0:9000
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /opt/remi/php56/root/var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /opt/remi/php56/root/var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /opt/remi/php56/root/var/lib/php/session
php_value[soap.wsdl_cache_dir] = /opt/remi/php56/root/var/lib/php/wsdlcache
[root@localhost7C php]# cat run_php.sh
#!/bin/bash
#echo "nameserver 10.20.254.254" > /etc/resolv.conf
/opt/remi/php56/root/usr/sbin/php-fpm
#/opt/remi/php56/root/usr/sbin/php-fpm --nodaemonize
tail -f /etc/hosts
[root@localhost7C php]# cat Dockerfile
#PHP Base Image
FROM harbor.zzhz.com/baseimages/magedu-centos-base:7.6.1810
MAINTAINER zhangshijie@magedu.net
RUN yum install -y https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm && yum install php56-php-fpm php56-php-mysql -y
ADD www.conf /opt/remi/php56/root/etc/php-fpm.d/www.conf
#RUN useradd nginx -u 2019
ADD run_php.sh /usr/local/bin/run_php.sh
EXPOSE 9000
CMD ["/usr/local/bin/run_php.sh"]
[root@localhost7C php]# cat build-command.sh
#!/bin/bash
TAG=$1
docker build -t harbor.zzhz.com/linux39/wordpress-php-5.6:${TAG} .
echo "镜像制作完成,即将上传至Harbor服务器"
sleep 1
docker push harbor.zzhz.com/linux39/wordpress-php-5.6:${TAG}
echo "镜像上传完成"
6.yaml文件编写
[root@localhost7C wordpress]# cat wordpress.yaml
kind: Deployment
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
metadata:
labels:
app: wordpress-app
name: wordpress-app-deployment
namespace: magedu
spec:
replicas: 1
selector:
matchLabels:
app: wordpress-app
template:
metadata:
labels:
app: wordpress-app
spec:
containers:
- name: wordpress-app-nginx
image: harbor.zzhz.com/linux39/wordpress-nginx:2023-04-12-v2
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
- containerPort: 443
protocol: TCP
name: https
volumeMounts:
- name: wordpress
mountPath: /home/nginx/wordpress
readOnly: false
- name: wordpress-app-php
image: harbor.zzhz.com/linux39/wordpress-php-5.6:2023-04-12
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 9000
protocol: TCP
name: http
volumeMounts:
- name: wordpress
mountPath: /home/nginx/wordpress
readOnly: false
volumes:
- name: wordpress
nfs:
server: 192.168.80.110
path: /data/k8sdata/magedu/wordpress
---
kind: Service
apiVersion: v1
metadata:
labels:
app: wordpress-app
name: wordpress-app-spec
namespace: magedu
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30031
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30033
selector:
app: wordpress-app
[root@localhost7C wordpress]# kubectl apply -f wordpress.yaml
测试访问 http://192.168.80.160:30031/test.php
8.数据库创建参考
实例:基于PVC+StatefulSet实现的MySQL主从架构 https://www.cnblogs.com/Yuanbangchen/p/17309336.html
创建账号和数据库
[root@localhost7C mysql]# kubectl exec -it mysql-0 bash
root@mysql-0:/# mysql
mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wordpress"@"%" IDENTIFIED BY "wordpress";
mysql> select user,host,authentication_string from user;
+-----------+------+-------------------------------------------+
| user | host | authentication_string |
+-----------+------+-------------------------------------------+
| root | % | |
| wordpress | % | *C260A4F79FA905AF65142FFE0B9A14FE0E1519CC |
+-----------+------+-------------------------------------------+
2 rows in set (0.00 sec)
9初始化wordpress 项目。
#http://192.168.80.160:30031/index.html
![]()
FQDN:$(podname).(headless server name).namespace.svc.zzhz.local
本例地址:mysql-0.mysql.default.svc.zzhz.local
![]()
设置账号和密码:2ThVNUcUJZ#HGN&alQ