一、運(yùn)用haproxy實(shí)現(xiàn)nginx颓芭、mysql服務(wù)負(fù)載均衡
1顷锰、環(huán)境配置
主機(jī)名 |
用途 |
ip |
需要搭建的服務(wù) |
類型 |
備注 |
node01 |
負(fù)載均衡前端 |
192.168.85.128 |
haproxy |
centos7 |
|
node02 |
負(fù)載均衡后端 |
192.168.85.129 |
nginx、mysql |
centos7 |
|
node03 |
負(fù)載均衡后端 |
192.168.85.130 |
nginx亡问、mysql |
centos7 |
|
2官紫、在node01上面安裝haproxy服務(wù)。并進(jìn)行配置州藕,啟動(dòng)haproxy
[root@node01 ~]# yum install -y haproxy
[root@node01 ~]# cd /etc/haproxy/
[root@node01 ~]# cp haproxy.cfg{,.bak}
[root@node01 ~]# vim haproxy.cfg (先在配置文件中定義一個(gè)狀態(tài)頁)
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;
修改第四十二行慎框,將網(wǎng)頁的目錄切換到/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上面創(chuàng)建一個(gè)server1的數(shù)據(jù)庫)
[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;
修改第四十二行,將網(wǎng)頁的目錄切換到/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上面創(chuàng)建一個(gè)server2的數(shù)據(jù)庫)
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 |
+--------------------+