Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Centos 7 Building High availability Web Cluster based on Haproxy

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Haproxy is a popular cluster scheduling tool at present. There are many similar cluster scheduling tools, such as LVS and Nginx. Comparatively speaking, LVS has the best performance, but the construction is relatively complex; the upstream module of Nginx supports the cluster function, but the health check function of the cluster node is not strong, and the performance is not as good as Haproxy, so it is more used in the enterprise intranet environment.

To build a LVS cluster, please refer to the blog article: Centos 7 to build a LVS+Keepalived highly available Web service cluster

Nginx cluster can refer to blog post: install Tomcat service and its load balancing based on centos 7

Overview of the principle of building Web clusters by Haproxy reference blog: Overview of building Web clusters by Haproxy

Deploy and build a high-availability Nginx cluster based on Haproxy

The deployment environment is as follows:

Preparatory work

1) connect the network, and the firewall releases the relevant traffic (I turn off the firewall directly here)

2) prepare the system image and configure the local yum (self-configuration)

3) download the haproxy source code package, which can be downloaded from the network disk link provided by me and use: https://pan.baidu.com/s/1I8JCUhejz0VSe8Q4lhzUpQ

Extraction code: th9x

4) web websites can be built using apache, Nginx or Tomcat, as long as they can be accessed. You can refer to the deployment of two Nginx and one Apache,web website here:

APache Web Services configure access control and build virtual hosts

Centos 7 deploys Nginx Web Services

Install Tomcat service and its load balancing based on centos 7

1. Deploy the first Nginx website

For more information about Nginx configuration and instructions, please refer to: Centos 7 deployment of Nginx website Services

[root@centos01 ~] # yum-y install prce-devel zlib-devel [root@centos01 ~] # useradd-M-s / sbin/nologin nginx [root@centos01 ~] # umount / mnt/ [root@centos01 ~] # mount / dev/cdrom / mnt/ mount: / dev/sr0 write protection [root@centos01 ~] # scp / mnt/nginx-1.6.0.tar.gz root@192.168.100.20:/root The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established.ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f will be mounted as read-only: 58:51:51:4b:3b.Are you sure you want to continue connecting (yes/no)? Yes Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.root@192.168.100.20's password: nginx-1.6.0.tar.gz 100% 784KB 68.2MB/s 00:00 [root@centos01 ~] # scp / mnt/haproxy-1.4.24.tar.gz root@192.168.100.30:/root The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established.ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.Are you sure you want to continue connecting (yes/no)? Yes Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts.root@192.168.100.30's password: haproxy-1.4.24.tar.gz 100% 817KB 31.1MB/s 00:00 00:00 [root@centos01 ~] # tar zxvf / mnt/ Nginx-1.6.0.tar.gz-C / usr/src/ [root@centos01 ~] # cd / usr/src/nginx-1.6.0/ [root@centos01 nginx-1.6.0] #. / configure-- prefix=/usr/local/nginx-- user=nginx-- group=nginx-- with-http_stub_status_module [root@centos01 nginx-1.6.0] # make & & make install [root@centos01] # ln-s / usr/ Local/nginx/sbin/* / usr/local/sbin/ [root@centos01 ~] # echo "192.168.100.10:nginx" > / usr/local/nginx/html/index.html [root@centos01 ~] # nginx [root@centos01 ~] # netstat-anptu | grep nginx tcp 00 0.0.0.0 echo 80 0.0.0.0: * LISTEN 3685/ngin: master 2, Deploy the second Nginx website [root@centos02 ~] # yum-y install pcre-devel zlib-devel [root@centos02 ~] # ls anaconda-ks.cfg initial-setup-ks.cfg nginx-1.6.0.tar.gz [root@centos02 ~] # tar zxvf nginx-1.6.0.tar.gz-C / usr/src/ [root@centos02 ~] # useradd-M-s / sbin/nologin nginx [root@centos02 ~] # cd / usr / src/nginx-1.6.0/ [root@centos02 nginx-1.6.0] #. / configure-- prefix=/usr/local/nginx-- user=nginx-- group=nginx-- with-http_stub_status_module [root@centos02 nginx-1.6.0] # make & & make install [root@centos02] # ln-s / usr/local/nginx/sbin/* / usr/local/sbin/ [root@centos02] # echo " 192.168.100.20:nginx "> / usr/local/nginx/html/index.html [root@centos02 ~] # nginx [root@centos02 ~] # netstat-anptu | grep nginx tcp 00 0.0.0.0 root@centos02 80 0.0.0.0 * LISTEN 6059/ngin: master 3, Configure client 1) add VM1 Nic to client Configure the IP address of the same network segment as the server

2) access the first nginx server

3) access the second nginx server

4 、 Deploy Haproxy server [root@centos03 ~] # yum-y install pcre-devel bzip2-devel [root@centos03 ~] # lsanaconda-ks.cfg haproxy-1.4.24.tar.gz initial-setup-ks.cfg [root@centos03 ~] # tar zxvf haproxy-1.4.24.tar.gz-C / usr/src/ [root@centos03 ~] # cd / usr/src/haproxy-1.4.24/ [root@centos03 haproxy-1.4.24] # make install [root@centos03 ~] # mkdir / etc/haproxy [root@centos03 ~] # cp / usr/src/haproxy-1.4.24/examples/haproxy.cfg / etc/haproxy/ [root@centos03 ~] # cp / usr/src/haproxy-1.4.24/examples/haproxy.init / etc/init.d/haproxy [root@centos03 ~] # chmod + x / etc / init.d/haproxy [root@centos03 ~] # chkconfig-- add haproxy [root@centos03 ~] # chkconfig-- level 35 haproxy on [root@centos03 ~] # cp / usr/src/haproxy-1.4.24/haproxy / usr/sbin/ [root@centos03 ~] # mkdir-p / usr/share/haproxy [root@centos03 ~] # vim / etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy -1.2.1 gid 99 daemon # debug # quietdefaults log global mode http option httplog option dontlognull retries 3 redispatch maxconn 2000 contimeout 10 clitimeout 10 Srvtimeout 10 listen nginx 192.168.100.30 balance roundrobin server web01 80 check inter 2000 fall 3 server web02 192.168.100.10 via systemctl 80 check inter 2000 fall 3 [root@centos03] # / etc/init.d/haproxy start Starting haproxy (via systemctl): [OK]

At this point, the Haproxy cluster deployment is complete, and you can now use the client to access the IP address of the Haproxy server. The first page visited is the first Nginx server, and the second page visited is the second Nginx server.

5. Deploy DNS

I have directly configured it here. For more information on the detailed configuration of DNS and the explanation of configuration items and an overview of the working principle of DNS, please refer to the blog post: CentOS7 simple to build DNS service

[root@centos03 ~] # yum-y install bind bind-chroot bind-utils [root@centos03 ~] # echo "" > / etc/named.conf [root@centos03 ~] # vim / etc/named.conf options {listen-on port 53 {192.168.100.0 24;}; directory "/ var/named";}; zone bdqn.com IN {type master File "bdqn.com.zone";}; [root@centos03 ~] # named-checkconf-z / etc/named.conf [root@centos03 ~] # vim / var/named/bdqn.com.zone$TTL 86400 @ SOA bdqn.com. Root.bdqn.com. (2019112201 1H 15M 1W 1D) @ NS centos03.bdqn.com.centos03 A 192.168.100.30www A 192.168.100.30 [root@centos03 ~] # named-checkzone bdqn.com/ var/named/bdqn.com.zone zone bdqn.com/IN: loaded serial 2019112201OK [root@centos03 ~] # chmod + x / Var/named/bdqn.com.zone [root@centos03 ~] # chown named:named / var/named/bdqn.com.zone [root@centos03 ~] # systemctl start named [root@centos03 ~] # systemctl enable named

Now that the DNS has been configured, the client can add a DNS address and open a browser to access it using the domain name.

2. Use Haproxy to build Apache website cluster 1. Deploy apache website

An overview of the detailed configuration and working principle of Apache website reference blog article: CentOS 7.4 build Apache website service

[root@centos04] # tar zxvf / mnt/httpd-2.2.17.tar.gz-C / usr/src/ [root@centos04] # cd / usr/src/httpd-2.2.17/ [root@centos04 httpd-2.2.17] #. / configure-- prefix=/usr/local/httpd-- enable-so-enable-rewrite-- enable-charset-lite-- enable-cgi [root@centos04 httpd-2.2. 17] # make & & make install [root@centos04 ~] # ln-s / usr/local/httpd/bin/* / usr/local/bin/ [root@centos04 ~] # cp / usr/local/httpd/bin/apachectl / etc/init.d/httpd [root@centos04 ~] # chmod + x / etc/init.d/httpd [root@centos04 ~] # vim / etc/init.d/httpd #! / bin/sh#chkconfig 35 80 20#description:apache server [root @ centos04 ~] # chkconfig-- add httpd [root@centos05 ~] # echo "192.168.100.40:apache" > / usr/local/httpd/htdocs/index.html [root@centos04 ~] # apachectl-t [root@centos04 ~] # systemctl start httpd [root@centos04 ~] # systemctl enable httpd [root@centos04 ~] # netstat-anptu | grep httpd tcp6 00: 80 :: * LISTEN 53801/httpd [root@centos04 ~] # systemctl is-enabled httpd.service enabled2, Deploy Haproxy service listen nginx 192.168.100.30 weight 80 balance roundrobin server web01 192.168.100.10 check inter 2000 fall 3 server web02 192.168.100.20 fall 80 check inter 2000 fall 3 server apache01 192.168.100.40 weight 80 check inter 2000 fall 3 weight 1 [root@centos03 ~] # / etc/init .d / haproxy restart Restarting haproxy (via systemctl): [OK]

At this point, the cluster based on the Apache website has been deployed and can now be accessed using the client

[root@centos03] # tail-f / var/log/haproxy/haproxy-info.log

-this is the end of this article. Thank you for reading-

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report