十八、01-运用haproxy实现nginx、mysql服务负载均衡

一、运用haproxy实现nginx、mysql服务负载均衡

1、环境配置
主机名 用途 ip 需要搭建的服务 类型 备注
node01 负载均衡前端 192.168.85.128 haproxy centos7
node02 负载均衡后端 192.168.85.129 nginx、mysql centos7
node03 负载均衡后端 192.168.85.130 nginx、mysql centos7
2、在node01上面安装haproxy服务。并进行配置,启动haproxy
[root@node01 ~]#  yum install -y haproxy
[root@node01 ~]# cd /etc/haproxy/
[root@node01 ~]# cp haproxy.cfg{,.bak}
[root@node01 ~]# vim haproxy.cfg   (先在配置文件中定义一个状态页)
listen stats
        bind *:9099
        acl allowstats src 192.168.85.1
        acl all src 0.0.0.0/0.0.0.0
        http-request allow if allowstats
        http-request deny if all
        stats enable
        stats uri /myproxy?admin
        stats realm "HAProxy Stats Page"
        stats auth admin:admin
        stats admin if TRUE
[root@node01 ~]# sysatemctl start haproxy
[root@node01 ~]# ss -tnl
3、配置后端(安装nginx、mysql)
[root@node02 ~]# yum install -y epel-release
[root@node02 ~]# yum install -y nginx
[root@node02 ~]# echo "Nginx Server 1" > /var/www/html/index.html
[root@node02 ~]# vim /etc/nginx/nginx.conf
 38     server {
 39         listen       80 default_server;
 40         listen       [::]:80 default_server;
 41         server_name  _;
 42         root         /var/www/html;
修改第四十二行,将网页的目录切换到/var/www/html目录下面
[root@node02 ~]# systemctl start nginx
[root@node02 ~]# yum install -y mariadb-server
[root@node02 ~]# vim /etc/my.cnf
[mysqld]
skip_name_resolve = ON
innodb_file_per_table = ON
[root@node02 ~]# systemctl start mariadb
MariaDB [(none)]> create  database server1; (在node02的mariadb上面创建一个server1的数据库)
[root@node03 ~]# yum install -y epel-release
[root@node03 ~]# yum install -y nginx
[root@node03 ~]# echo "Nginx Server 2" > /var/www/html/index.html
[root@node03 ~]# vim /etc/nginx/nginx.conf
 38     server {
 39         listen       80 default_server;
 40         listen       [::]:80 default_server;
 41         server_name  _;
 42         root         /var/www/html;
修改第四十二行,将网页的目录切换到/var/www/html目录下面
[root@node03 ~]# systemctl start nginx
[root@node03 ~]# yum install -y mariadb-server
[root@node03 ~]# vim /etc/my.cnf
[mysqld]
skip_name_resolve = ON
innodb_file_per_table = ON
[root@node03 ~]# systemctl start mariadb
MariaDB [(none)]> create  database server2; (在node03的mariadb上面创建一个server2的数据库)
3、在haproxy上配置前端代理
[root@node01 ~]# vim /etc/haproxy/haproxy.cfg
frontend nginx *:80
        default_backend  mynginx
backend mynginx
        balance     roundrobin
        server dynsrv1 192.168.85.129:80 check
        server dynsrv2 192.168.85.130:80 check
listen mymysql
        bind *:3306
        mode tcp
        balance    roundrobin
        server staticsrv1 192.168.85.129:3306
        server staticsrv2 192.168.85.130:3306

listen stats
        bind *:9099
        acl allowstats src 192.168.85.1
        acl all src 0.0.0.0/0.0.0.0
        http-request allow if allowstats
        http-request deny if all
        stats enable
        stats uri /myproxy?admin
        stats realm "HAProxy Stats Page"
        stats auth admin:admin
        stats admin if TRUE
 [root@node01 ~]# systemctl restart haproxy
4、测试
[root@node01 ~]# curl http://192.168.85.128
Nginx Server 1
[root@node01 ~]# curl http://192.168.85.128
Nginx Server 2
[root@node01 ~]# mysql -h 192.168.85.128 -u root -pxiaoshi22 -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| server1            |
+--------------------+
[root@node01 ~]# mysql -h 192.168.85.128 -u root -pxiaoshi22 -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| server2            |
+--------------------+

你可能感兴趣的:(十八、01-运用haproxy实现nginx、mysql服务负载均衡)