目录

web高可用集群项目数据库主从同步文件共享存储nginx动静分离负载均衡高可用

web高可用集群项目(数据库主从同步、文件共享存储、nginx动静分离+负载均衡+高可用)

一、项目环境

https://i-blog.csdnimg.cn/direct/a2501c6036ae42ffb587ddc129f75656.jpeg

二、环境准备

主机名IP地址备注
openEuler-1192.168.121.11主负载调度器
openEuler-2192.168.121.12副负载调度器
openEuler-3192.168.121.13web-1(静态)
openEuler-4192.168.121.14web-2(静态)
openEuler-5192.168.121.15web-3(动态)
openEuler-6192.168.121.16web-4(动态)
openEuler-7192.168.121.17node-1(共享存储)
openEuler-8192.168.121.18node-2(共享存储)
openEuler-9192.168.121.19node-3(共享存储)
openEuler-10192.168.121.20mysql-master
openEuler-11192.168.121.21mysql-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 ~]# mysql

mysql> 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 ~]# mysql

mysql> 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 event
                  Master_Host: 192.168.121.20
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000001
          Read_Master_Log_Pos: 978
               Relay_Log_File: openEuler-11-relay-bin.000002
                Relay_Log_Pos: 1188
        Relay_Master_Log_File: binlog.000001
             Slave_IO_Running: Yes
            Slave_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 '数据库连接成功!';
else
 echo '数据库连接失败!';
$con->close();
?>
[root@openEuler-5 ~]# systemctl restart nginx.service

浏览器访问测试:

https://i-blog.csdnimg.cn/direct/b2983fa1d04d482fb73bb155fcb5777b.png

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/

接着浏览器访问:

https://i-blog.csdnimg.cn/direct/16eb4fb27ec14b8fb1426a06ceb4ae83.png

若弹出此窗口则按照其提示来完成:

按照提示写入文件:

[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: 2

Hostname: 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_vol

Volume 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 ]
then
        systemctl start nginx
        sleep 2
        if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
        then
                systemctl stop keepalived
        fi
fi
[root@openEuler-1 ~]# chmod +x /script/check_ngx.sh

# 另一台负载调度器同理

4、配置keepalived实现高可用

# [root@openEuler-1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id 11
}

vrrp_script check_nginx {
        script "/script/check_ngx.sh"
        interval 1
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }

    track_script {
        check_nginx
    }

    virtual_ipaddress {
        192.168.121.10
    }
}

# 另一台配置,稍作修改
[root@openEuler-2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id 12
}

vrrp_script check_nginx {
        script "/script/check_ngx.sh"
        interval 1
}

vrrp_instance VI_1 {
    state SLAVE
    interface ens160
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_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、测试访问静态页面

https://i-blog.csdnimg.cn/direct/48dc53b9a01844938a947110af82e6b2.png

多访问几次,查看静态web服务器的日志:

https://i-blog.csdnimg.cn/direct/502553471a7946fda102043f44cc2371.png

https://i-blog.csdnimg.cn/direct/c1d275cbb2de4f4cb1b8ff8bacc2a6ce.png

可以看到是轮询的访问两台静态web服务器。

2、测试访问动态页面

https://i-blog.csdnimg.cn/direct/a02c1ba3852d4d51987c5dcf1400782f.png

多访问几次,查看动态web服务器的日志(这里采用动态查看日志,访问前通过空格隔开):

https://i-blog.csdnimg.cn/direct/0a2edb1d9b754684ad7b0f3d7a61126d.png

https://i-blog.csdnimg.cn/direct/9be968327db2412489c5f1304a0a19fe.png

可以看到只访问了其中一台动态web端,因为采用的是哈希算法。

访问wordpress:

https://i-blog.csdnimg.cn/direct/66ad21442f704fd9b2afc45af060c421.png

3、模拟后端web端节点故障

https://i-blog.csdnimg.cn/direct/ff7f9bad37ca44818eef4e1041eaa7e9.png

https://i-blog.csdnimg.cn/direct/36ad493219394b0fbc1d276adf35f1c5.png

用户仍然可以通过另一台服务器进行访问。

4、模拟负载调度器节点故障

nginx故障:

https://i-blog.csdnimg.cn/direct/83e2e80e839542ea8718b35ee5c95aea.png

由于配置了脚本检查,当nginx服务停止后会尝试重启。

测试在配置文件中加入未知字符,使其不能重启时:

https://i-blog.csdnimg.cn/direct/3331b6ef27d142fab3ef7f33b97e75fb.png

https://i-blog.csdnimg.cn/direct/9be6ff1491b044e793ff748f0a6c9f9e.png

可以看到vip进行了漂移,去到了备用的服务器上。

keepalived故障同理。