一、项目环境
二、环境准备
主机名 | IP地址 | 备注 |
openEuler-1 | 192.168.121.11 | 主负载调度器 |
openEuler-2 | 192.168.121.12 | 副负载调度器 |
openEuler-3 | 192.168.121.13 | web-1(静态) |
openEuler-4 | 192.168.121.14 | web-2(静态) |
openEuler-5 | 192.168.121.15 | web-3(动态) |
openEuler-6 | 192.168.121.16 | web-4(动态) |
openEuler-7 | 192.168.121.17 | node-1(共享存储) |
openEuler-8 | 192.168.121.18 | node-2(共享存储) |
openEuler-9 | 192.168.121.19 | node-3(共享存储) |
openEuler-10 | 192.168.121.20 | mysql-master |
openEuler-11 | 192.168.121.21 | mysql-slave |
注意:该实验环境默认关闭防火墙和SELinux
三、数据库配置
1、安装环境
[root@openEuler-10 ~]# yum install mysql-server -y
[root@openEuler-10 ~]# systemctl enable --now mysqld# 另外两台服务器同理
2、配置主从同步
主库配置:
[root@openEuler-10 ~]# vim /etc/my.cnf
[root@openEuler-10 ~]# tail -n3 /etc/my.cnf
server_id=20
gtid_mode=ON
enforce-gtid-consistency=ON
[root@openEuler-10 ~]# systemctl restart mysqld
[root@openEuler-10 ~]# mysqlmysql> create user rep@'192.168.121.%' identified with mysql_native_password by '123456';
mysql> grant replication slave on *.* to rep@'192.168.121.%';
mysql> flush privileges;
从库配置:
[root@openEuler-11 ~]# vim /etc/my.cnf
[root@openEuler-11 ~]# tail -n3 /etc/my.cnf
server_id=21
gtid_mode=ON
enforce-gtid-consistency=ON
[root@openEuler-11 ~]# systemctl restart mysqld
[root@openEuler-11 ~]# mysqlmysql> change replication source to-> source_host='192.168.121.20',-> source_user='rep',-> source_password='123456',-> source_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.06 sec)mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)mysql> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting for source to send eventMaster_Host: 192.168.121.20Master_User: repMaster_Port: 3306Connect_Retry: 60Master_Log_File: binlog.000001Read_Master_Log_Pos: 978Relay_Log_File: openEuler-11-relay-bin.000002Relay_Log_Pos: 1188Relay_Master_Log_File: binlog.000001Slave_IO_Running: YesSlave_SQL_Running: Yes...
3、创建库以及授权用户
mysql> create database wordpress;
mysql> create user wp@'%' identified by 'wp123456';
mysql> grant all privileges on wordpress.* to wp@'%';
mysql> flush privileges;# 检查从库中是否同步
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
四、web服务端配置(动态)
1、安装环境
[root@openEuler-5 ~]# yum install nginx mysql-server php php-mysqlnd -y
[root@openEuler-5 ~]# echo "dynamic test page!" > /usr/share/nginx/html/index.html
[root@openEuler-5 ~]# systemctl enable --now nginx.service
2、PHP和MySQL对接测试
[root@openEuler-5 ~]# vim /usr/share/nginx/html/index.php
<?php
$con = mysqli_connect("192.168.121.20","wp","wp123456");
if ($con)echo '数据库连接成功!';
elseecho '数据库连接失败!';
$con->close();
?>
[root@openEuler-5 ~]# systemctl restart nginx.service
浏览器访问测试:192.168.121.15/mysql_connect.php
3、安装部署wordpress
[root@openEuler-5 ~]# wget -c http://wordpress.org/latest.tar.gz
[root@openEuler-5 ~]# tar -xzf latest.tar.gz -C /usr/share/nginx/html/
接着浏览器访问:http://192.168.121.15/wordpress
若弹出此窗口则按照其提示来完成:
按照提示写入文件:
[root@openEuler-5 ~]# vim /usr/share/nginx/html/wp-config.php
最后创建站点并登录:
4、配置另一个web端
重复步骤1-4。
五、共享存储配置
1、安装环境
[root@openEuler-7 ~]# yum install glusterfs-server -y
[root@openEuler-7 ~]# systemctl enable --now glusterd# 另外两台服务器同理
2、创建存储目录
[root@openEuler-7 ~]# mkdir -p /gluster/brick1# 另外两台服务器同理
3、配置hosts解析
[root@openEuler-7 ~]# vim /etc/hosts
[root@openEuler-7 ~]# tail -n3 /etc/hosts
192.168.121.17 openEuler-7
192.168.121.18 openEuler-8
192.168.121.19 openEuler-9# 另外两台服务器同理
4、配置GlusterFS存储信任池
在其中一个节点上配置:
[root@openEuler-7 ~]# gluster peer probe openEuler-8
peer probe: success
[root@openEuler-7 ~]# gluster peer probe openEuler-9
peer probe: success# 查看信任池状态
[root@openEuler-7 ~]# gluster peer status
Number of Peers: 2Hostname: openEuler-8
Uuid: 6c17b306-413f-4f1c-a5f5-90f43104eda6
State: Peer in Cluster (Connected)Hostname: openEuler-9
Uuid: b6c8b385-daf8-476b-8207-fafe8b9119eb
State: Peer in Cluster (Connected)# 查看信任池列表
[root@openEuler-7 ~]# gluster pool list
UUID Hostname State
6c17b306-413f-4f1c-a5f5-90f43104eda6 openEuler-8 Connected
b6c8b385-daf8-476b-8207-fafe8b9119eb openEuler-9 Connected
20d6bdce-a81f-4f69-82a6-9a5b224dba4e localhost Connected
5、创建web卷
[root@openEuler-7 ~]# gluster volume create web_vol replica 2 arbiter 1 \
openEuler-7:/gluster/brick1 \
openEuler-8:/gluster/brick1 \
openEuler-9:/gluster/brick1 \
force[root@openEuler-7 ~]# gluster volume start web_vol
volume start: web_vol: success
[root@openEuler-7 ~]# gluster volume info web_volVolume Name: web_vol
Type: Replicate
Volume ID: 3db1b974-bc4c-45c4-84bb-381107dd612b
Status: Started
Snapshot Count: 0
Number of Bricks: 1 x (2 + 1) = 3
Transport-type: tcp
Bricks:
Brick1: openEuler-7:/gluster/brick1
Brick2: openEuler-8:/gluster/brick1
Brick3: openEuler-9:/gluster/brick1 (arbiter)
Options Reconfigured:
cluster.granular-entry-heal: on
storage.fips-mode-rchecksum: on
transport.address-family: inet
nfs.disable: on
performance.client-io-threads: off
六、web服务端配置(静态)
1、安装环境
[root@openEuler-3 ~]# yum install glusterfs-fuse nginx -y
[root@openEuler-3 ~]# echo "static test page!" > /usr/share/nginx/html/index.html
[root@openEuler-3 ~]# systemctl enable --now nginx.service# 另一台服务器同理
2、挂载目录
# 备份原来的根目录数据(后续另一个web服务配置时不需要备份)
[root@openEuler-3 ~]# cd /usr/share/nginx/
[root@openEuler-3 nginx]# ls
html modules
[root@openEuler-3 nginx]# cp -r html{,.bak}# 配置hosts解析
[root@openEuler-3 nginx]# vim /etc/hosts
[root@openEuler-3 nginx]# tail -n3 /etc/hosts
192.168.121.17 openEuler-7
192.168.121.18 openEuler-8
192.168.121.19 openEuler-9# 高可用挂载(节点故障自动转移)
[root@openEuler-3 nginx]# mount -t glusterfs openEuler-7,openEuler-8,openEuler-9:/web_vol ./html/# 还原数据
[root@openEuler-3 nginx]# mv html.bak/* html/
[root@openEuler-3 nginx]# tree html
html
├── 404.html
├── 50x.html
├── index.html
└── nginx-logo.png
3、配置另一个web端
重复步骤1-3。
4、编写静态网页测试
# 在其中一台web服务器编写
[root@openEuler-3 nginx]# cd html/
[root@openEuler-3 html]# echo "web test1 page" > test1.html
[root@openEuler-3 html]# echo "web test2 page" > test2.html
[root@openEuler-3 html]# echo "web test3 page" > test3.html
[root@openEuler-3 html]# echo "web test4 page" > test4.html
[root@openEuler-3 html]# echo "web test5 page" > test5.html# 查看另一个web
[root@openEuler-4 html]# ls
404.html index.html test1.html test3.html test5.html
50x.html nginx-logo.png test2.html test4.html
七、负载均衡器配置
1、安装环境
[root@openEuler-1 ~]# yum install nginx keepalived -y# 设置开机自启
[root@openEuler-1 ~]# systemctl enable --now nginx.service
[root@openEuler-1 ~]# systemctl enable --now keepalived.service
2、配置负载均衡(虚拟主机+动静分离)
[root@openEuler-1 ~]# vim /etc/nginx/conf.d/lb.conf
upstream static_webs {server 192.168.121.13:80;server 192.168.121.14:80;}upstream dynamic_webs {ip_hash;server 192.168.121.15:80;server 192.168.121.16:80;
}server {listen 80;server_name static.yunjisuan.com;location / {proxy_pass http://static_webs;# proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;}
}server {listen 80;server_name dynamic.yunjisuan.com;location / {proxy_pass http://dynamic_webs;# proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;}
}# 另一台负载调度器同理
3、创建web服务检查脚本
[root@openEuler-1 ~]# mkdir /script/
[root@openEuler-1 ~]# vim /script/check_ngx.sh
#!/bin/bash
if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
thensystemctl start nginxsleep 2if [ `ps -C nginx --no-header |wc -l` -eq 0 ]thensystemctl stop keepalivedfi
fi
[root@openEuler-1 ~]# chmod +x /script/check_ngx.sh# 另一台负载调度器同理
4、配置keepalived实现高可用
# [root@openEuler-1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id 11
}vrrp_script check_nginx {script "/script/check_ngx.sh"interval 1
}vrrp_instance VI_1 {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check_nginx}virtual_ipaddress {192.168.121.10}
}# 另一台配置,稍作修改
[root@openEuler-2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id 12
}vrrp_script check_nginx {script "/script/check_ngx.sh"interval 1
}vrrp_instance VI_1 {state SLAVEinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check_nginx}virtual_ipaddress {192.168.121.10}
}# 重启服务
[root@openEuler-1 ~]# systemctl restart keepalived.service
[root@openEuler-2 ~]# systemctl restart keepalived.service
八、测试
测试主机上配置hosts解析:
192.168.121.10 static.yunjisuan.com dynamic.yunjisuan.com
1、测试访问静态页面
多访问几次,查看静态web服务器的日志:
可以看到是轮询的访问两台静态web服务器。
2、测试访问动态页面
多访问几次,查看动态web服务器的日志(这里采用动态查看日志,访问前通过空格隔开):
可以看到只访问了其中一台动态web端,因为采用的是哈希算法。
访问wordpress:
3、模拟后端web端节点故障
用户仍然可以通过另一台服务器进行访问。
4、模拟负载调度器节点故障
nginx故障:
由于配置了脚本检查,当nginx服务停止后会尝试重启。
测试在配置文件中加入未知字符,使其不能重启时:
可以看到vip进行了漂移,去到了备用的服务器上。
keepalived故障同理。