Ubantu19.04部署haproxy和keepalived服务

          Ubantu19.04部署haproxy和keepalived服务

                                                                                           作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.安装keepalived和haproxy软件服务

1>.安装ubantu系统

  博主推荐阅读:
    https://www.cnblogs.com/yinzhengjie/p/12180899.html

2>.部署架构

  haproxy101.yinzhengjie.org.cn
    keepalived 

  haproxy201.yinzhengjie.org.cn
    keepalived

3>.安装haproxy和keepalived软件

root@haproxy101:~# apt-get -y  install haproxy keepalived
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ipvsadm libgdbm5 liblua5.3-0 libperl5.26 libsensors4 libsnmp-base libsnmp30 perl-modules-5.26
Suggested packages:
  vim-haproxy haproxy-doc heartbeat ldirectord gdbm-l10n lm-sensors snmp-mibs-downloader
The following NEW packages will be installed:
  haproxy ipvsadm keepalived libgdbm5 liblua5.3-0 libperl5.26 libsensors4 libsnmp-base libsnmp30 perl-modules-5.26
0 upgraded, 10 newly installed, 0 to remove and 19 not upgraded.
Need to get 9,008 kB of archives.
After this operation, 49.1 MB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 liblua5.3-0 amd64 5.3.3-1ubuntu0.18.04.1 [115 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 haproxy amd64 1.8.8-1ubuntu0.9 [1,117 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 perl-modules-5.26 all 5.26.1-6ubuntu0.3 [2,763 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libgdbm5 amd64 1.14.1-6 [26.0 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 libperl5.26 amd64 5.26.1-6ubuntu0.3 [3,527 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libsensors4 amd64 1:3.4.0-4 [28.8 kB]
Get:7 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 libsnmp-base all 5.7.3+dfsg-1.8ubuntu3.3 [224 kB]
Get:8 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 libsnmp30 amd64 5.7.3+dfsg-1.8ubuntu3.3 [929 kB]
Get:9 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 keepalived amd64 1:1.3.9-1ubuntu0.18.04.2 [244 kB]
Get:10 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 ipvsadm amd64 1:1.28-3ubuntu0.18.04.1 [35.5 kB]
Fetched 9,008 kB in 3s (3,181 kB/s) 
Selecting previously unselected package liblua5.3-0:amd64.
(Reading database ... 67311 files and directories currently installed.)
Preparing to unpack .../0-liblua5.3-0_5.3.3-1ubuntu0.18.04.1_amd64.deb ...
Unpacking liblua5.3-0:amd64 (5.3.3-1ubuntu0.18.04.1) ...
Selecting previously unselected package haproxy.
Preparing to unpack .../1-haproxy_1.8.8-1ubuntu0.9_amd64.deb ...
Unpacking haproxy (1.8.8-1ubuntu0.9) ...
Selecting previously unselected package perl-modules-5.26.
Preparing to unpack .../2-perl-modules-5.26_5.26.1-6ubuntu0.3_all.deb ...
Unpacking perl-modules-5.26 (5.26.1-6ubuntu0.3) ...
Selecting previously unselected package libgdbm5:amd64.
Preparing to unpack .../3-libgdbm5_1.14.1-6_amd64.deb ...
Unpacking libgdbm5:amd64 (1.14.1-6) ...
Selecting previously unselected package libperl5.26:amd64.
Preparing to unpack .../4-libperl5.26_5.26.1-6ubuntu0.3_amd64.deb ...
Unpacking libperl5.26:amd64 (5.26.1-6ubuntu0.3) ...
Selecting previously unselected package libsensors4:amd64.
Preparing to unpack .../5-libsensors4_1%3a3.4.0-4_amd64.deb ...
Unpacking libsensors4:amd64 (1:3.4.0-4) ...
Selecting previously unselected package libsnmp-base.
Preparing to unpack .../6-libsnmp-base_5.7.3+dfsg-1.8ubuntu3.3_all.deb ...
Unpacking libsnmp-base (5.7.3+dfsg-1.8ubuntu3.3) ...
Selecting previously unselected package libsnmp30:amd64.
Preparing to unpack .../7-libsnmp30_5.7.3+dfsg-1.8ubuntu3.3_amd64.deb ...
Unpacking libsnmp30:amd64 (5.7.3+dfsg-1.8ubuntu3.3) ...
Selecting previously unselected package keepalived.
Preparing to unpack .../8-keepalived_1%3a1.3.9-1ubuntu0.18.04.2_amd64.deb ...
Unpacking keepalived (1:1.3.9-1ubuntu0.18.04.2) ...
Selecting previously unselected package ipvsadm.
Preparing to unpack .../9-ipvsadm_1%3a1.28-3ubuntu0.18.04.1_amd64.deb ...
Unpacking ipvsadm (1:1.28-3ubuntu0.18.04.1) ...
Setting up ipvsadm (1:1.28-3ubuntu0.18.04.1) ...
Setting up perl-modules-5.26 (5.26.1-6ubuntu0.3) ...
Setting up libgdbm5:amd64 (1.14.1-6) ...
Setting up libsnmp-base (5.7.3+dfsg-1.8ubuntu3.3) ...
Setting up libperl5.26:amd64 (5.26.1-6ubuntu0.3) ...
Setting up libsensors4:amd64 (1:3.4.0-4) ...
Setting up liblua5.3-0:amd64 (5.3.3-1ubuntu0.18.04.1) ...
Setting up haproxy (1.8.8-1ubuntu0.9) ...
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /lib/systemd/system/haproxy.service.
Setting up libsnmp30:amd64 (5.7.3+dfsg-1.8ubuntu3.3) ...
Setting up keepalived (1:1.3.9-1ubuntu0.18.04.2) ...
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /lib/systemd/system/keepalived.service.
Processing triggers for rsyslog (8.32.0-1ubuntu7) ...
Processing triggers for systemd (240-6ubuntu5) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for dbus (1.12.12-1ubuntu1) ...
Processing triggers for libc-bin (2.29-0ubuntu2) ...
root@haproxy101:~# 
root@haproxy101:~# apt-get -y install haproxy keepalived
root@haproxy201:~# apt-get -y  install haproxy keepalived
Reading package lists... Done
Building dependency tree       
Reading state information... Done
haproxy is already the newest version (1.8.8-1ubuntu0.9).
The following additional packages will be installed:
  ipvsadm libgdbm5 libperl5.26 libsensors4 libsnmp-base libsnmp30 perl-modules-5.26
Suggested packages:
  heartbeat ldirectord gdbm-l10n lm-sensors snmp-mibs-downloader
The following NEW packages will be installed:
  ipvsadm keepalived libgdbm5 libperl5.26 libsensors4 libsnmp-base libsnmp30 perl-modules-5.26
upgraded, 8 newly installed, 0 to remove and 19 not upgraded.
Need to get 7,777 kB of archives.
After this operation, 46.2 MB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 perl-modules-5.26 all 5.26.1-6ubuntu0.3 [2,763 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libgdbm5 amd64 1.14.1-6 [26.0 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 libperl5.26 amd64 5.26.1-6ubuntu0.3 [3,527 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libsensors4 amd64 1:3.4.0-4 [28.8 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 libsnmp-base all 5.7.3+dfsg-1.8ubuntu3.3 [224 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 libsnmp30 amd64 5.7.3+dfsg-1.8ubuntu3.3 [929 kB]
Get:7 http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 keepalived amd64 1:1.3.9-1ubuntu0.18.04.2 [244 kB]
Get:8 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 ipvsadm amd64 1:1.28-3ubuntu0.18.04.1 [35.5 kB]
Fetched 7,777 kB in 1s (6,896 kB/s)
Selecting previously unselected package perl-modules-5.26.
(Reading database ... 67437 files and directories currently installed.)
Preparing to unpack .../0-perl-modules-5.26_5.26.1-6ubuntu0.3_all.deb ...
Unpacking perl-modules-5.26 (5.26.1-6ubuntu0.3) ...
Selecting previously unselected package libgdbm5:amd64.
Preparing to unpack .../1-libgdbm5_1.14.1-6_amd64.deb ...
Unpacking libgdbm5:amd64 (1.14.1-6) ...
Selecting previously unselected package libperl5.26:amd64.
Preparing to unpack .../2-libperl5.26_5.26.1-6ubuntu0.3_amd64.deb ...
Unpacking libperl5.26:amd64 (5.26.1-6ubuntu0.3) ...
Selecting previously unselected package libsensors4:amd64.
Preparing to unpack .../3-libsensors4_1%3a3.4.0-4_amd64.deb ...
Unpacking libsensors4:amd64 (1:3.4.0-4) ...
Selecting previously unselected package libsnmp-base.
Preparing to unpack .../4-libsnmp-base_5.7.3+dfsg-1.8ubuntu3.3_all.deb ...
Unpacking libsnmp-base (5.7.3+dfsg-1.8ubuntu3.3) ...
Selecting previously unselected package libsnmp30:amd64.
Preparing to unpack .../5-libsnmp30_5.7.3+dfsg-1.8ubuntu3.3_amd64.deb ...
Unpacking libsnmp30:amd64 (5.7.3+dfsg-1.8ubuntu3.3) ...
Selecting previously unselected package keepalived.
Preparing to unpack .../6-keepalived_1%3a1.3.9-1ubuntu0.18.04.2_amd64.deb ...
Unpacking keepalived (1:1.3.9-1ubuntu0.18.04.2) ...
Selecting previously unselected package ipvsadm.
Preparing to unpack .../7-ipvsadm_1%3a1.28-3ubuntu0.18.04.1_amd64.deb ...
Unpacking ipvsadm (1:1.28-3ubuntu0.18.04.1) ...
Setting up ipvsadm (1:1.28-3ubuntu0.18.04.1) ...
Setting up perl-modules-5.26 (5.26.1-6ubuntu0.3) ...
Setting up libgdbm5:amd64 (1.14.1-6) ...
Setting up libsnmp-base (5.7.3+dfsg-1.8ubuntu3.3) ...
Setting up libperl5.26:amd64 (5.26.1-6ubuntu0.3) ...
Setting up libsensors4:amd64 (1:3.4.0-4) ...
Setting up libsnmp30:amd64 (5.7.3+dfsg-1.8ubuntu3.3) ...
Setting up keepalived (1:1.3.9-1ubuntu0.18.04.2) ...
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.service → /lib/systemd/system/keepalived.service.
Processing triggers for systemd (240-6ubuntu5) ...
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for dbus (1.12.12-1ubuntu1) ...
Processing triggers for libc-bin (2.29-0ubuntu2) ...
root@haproxy201:~# 
 
root@haproxy201:~# apt-get -y install haproxy keepalived

二.配置keepalived服务

1>.编辑keepalived的配置文件

root@haproxy101:~# cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf
root@haproxy101:~#
root@haproxy101:~# vim /etc/keepalived/keepalived.conf 
root@haproxy101:~# 
root@haproxy101:~# cat  /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    interface ens33
    virtual_router_id 50
    nopreempt
    priority 100
    advert_int 1
    virtual_ipaddress {
        192.168.7.253 dev ens33 label ens33:0
    }
}
root@haproxy101:~# 

2>.启动keepalived服务并设置开机自启动

root@haproxy101:~# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.101  netmask 255.255.248.0  broadcast 192.168.7.255
        inet6 fe80::20c:29ff:fe96:713b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:96:71:3b  txqueuelen 1000  (Ethernet)
        RX packets 13220  bytes 16776100 (16.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3920  bytes 386861 (386.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 176  bytes 15256 (15.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 176  bytes 15256 (15.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@haproxy101:~# 
root@haproxy101:~# systemctl start keepalived
root@haproxy101:~# 
root@haproxy101:~# systemctl enable keepalived
Synchronizing state of keepalived.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable keepalived
root@haproxy101:~# 
root@haproxy101:~# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.101  netmask 255.255.248.0  broadcast 192.168.7.255
        inet6 fe80::20c:29ff:fe96:713b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:96:71:3b  txqueuelen 1000  (Ethernet)
        RX packets 13306  bytes 16783333 (16.7 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4016  bytes 401026 (401.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.253  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:96:71:3b  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 184  bytes 16032 (16.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 184  bytes 16032 (16.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@haproxy101:~# 

3>.将"haproxy101.yinzhengjie.org.cn"节点的keepalived的配置文件拷贝到"haproxy201.yinzhengjie.org.cn"节点上。

root@haproxy101:~# uname -a
Linux haproxy101.yinzhengjie.org.cn 5.0.0-13-generic #14-Ubuntu SMP Mon Apr 15 14:59:14 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
root@haproxy101:~#  
root@haproxy101:~# scp /etc/keepalived/keepalived.conf root@192.168.7.201:/etc/keepalived/keepalived.conf
The authenticity of host '192.168.7.201 (192.168.7.201)' can't be established.
ECDSA key fingerprint is SHA256:8KwMmYtM6BGEuhMIy8q7zK9WyARxjqRa7NDuQba8ZH8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.7.201' (ECDSA) to the list of known hosts.
root@192.168.7.201's password: 
keepalived.conf                                                                                                                                                                                                                             100%  424   188.4KB/s   00:00    
root@haproxy101:~# 

4>.修改"node201.yinzhengjie.org.cn"节点的优先级即可。

root@haproxy201:~# ll /etc/keepalived/
total 12
drwxr-xr-x  2 root root 4096 Jan 11 22:07 ./
drwxr-xr-x 89 root root 4096 Jan 11 21:41 ../
-rw-r--r--  1 root root  424 Jan 11 22:07 keepalived.conf
root@haproxy201:~# 
root@haproxy201:~# vim /etc/keepalived/keepalived.conf 
root@haproxy201:~# 
root@haproxy201:~# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    interface ens33
    virtual_router_id 50
    nopreempt
    priority 50
    advert_int 1
    virtual_ipaddress {
        192.168.7.253 dev ens33 label ens33:0
    }
}
root@haproxy201:~# 

5>.在"node201.yinzhengjie.org.cn"节点启动keepalived服务并设置为开启启动

root@haproxy201:~# systemctl start keepalived
root@haproxy201:~# 
root@haproxy201:~# systemctl enable keepalived
Synchronizing state of keepalived.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable keepalived
root@haproxy201:~# 
root@haproxy201:~# ps -ef | grep keepalived
root       1358      1  0 22:11 ?        00:00:00 /usr/sbin/keepalived
root       1359   1358  0 22:11 ?        00:00:00 /usr/sbin/keepalived
root       1360   1358  0 22:11 ?        00:00:00 /usr/sbin/keepalived
root       1454   1062  0 22:12 pts/0    00:00:00 grep --color=auto keepalived
root@haproxy201:~# 
root@haproxy201:~# 
root@haproxy201:~# systemctl status keepalived
● keepalived.service - Keepalive Daemon (LVS and VRRP)
   Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2020-01-11 22:11:43 CST; 1min 22s ago
 Main PID: 1358 (keepalived)
    Tasks: 3 (limit: 4619)
   Memory: 5.8M
   CGroup: /system.slice/keepalived.service
           ├─1358 /usr/sbin/keepalived
           ├─1359 /usr/sbin/keepalived
           └─1360 /usr/sbin/keepalived

Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived[1358]: Starting Healthcheck child process, pid=1359
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived_healthcheckers[1359]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived[1358]: Starting VRRP child process, pid=1360
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived_vrrp[1360]: Registering Kernel netlink reflector
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived_vrrp[1360]: Registering Kernel netlink command channel
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived_vrrp[1360]: Registering gratuitous ARP shared channel
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived_vrrp[1360]: Opening file '/etc/keepalived/keepalived.conf'.
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn systemd[1]: Started Keepalive Daemon (LVS and VRRP).
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived_vrrp[1360]: Using LinkWatch kernel netlink reflector...
Jan 11 22:11:43 haproxy201.yinzhengjie.org.cn Keepalived_vrrp[1360]: VRRP_Instance(VI_1) Entering BACKUP STATE
root@haproxy201:~# 
root@haproxy201:~# 

6>.停止"node101.yinzhengjie.org.cn"节点的keepalived服务

root@haproxy101:~# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.101  netmask 255.255.248.0  broadcast 192.168.7.255
        inet6 fe80::20c:29ff:fe96:713b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:96:71:3b  txqueuelen 1000  (Ethernet)
        RX packets 14007  bytes 16851015 (16.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5465  bytes 524770 (524.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.253  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:96:71:3b  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 184  bytes 16032 (16.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 184  bytes 16032 (16.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@haproxy101:~# 
root@haproxy101:~# systemctl stop keepalived
root@haproxy101:~# 
root@haproxy101:~# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.101  netmask 255.255.248.0  broadcast 192.168.7.255
        inet6 fe80::20c:29ff:fe96:713b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:96:71:3b  txqueuelen 1000  (Ethernet)
        RX packets 14059  bytes 16855377 (16.8 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5510  bytes 530628 (530.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 184  bytes 16032 (16.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 184  bytes 16032 (16.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@haproxy101:~# 

7>.查看"node201.yinzhengjie.org.cn"节点是否接管了VIP

root@haproxy201:~# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.201  netmask 255.255.248.0  broadcast 192.168.7.255
        inet6 fe80::20c:29ff:fea6:ed5a  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a6:ed:5a  txqueuelen 1000  (Ethernet)
        RX packets 1454  bytes 122754 (122.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 878  bytes 148988 (148.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.7.253  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:a6:ed:5a  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 12  bytes 1180 (1.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1180 (1.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@haproxy201:~# 

三.配置haproxy服务

1>.编辑haproxy的配置文件

root@haproxy101:~# cat /etc/haproxy/haproxy.cfg 
global
    maxconn 100000
    user haproxy
    group haproxy
    daemon
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    nbthread 2
    log 127.0.0.1 local5 info

defaults
    option http-keep-alive
    option  forwardfor
    option redispatch
    option abortonclose
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms

listen status_page
    bind 192.168.7.253:8888
    stats enable
    stats uri /haproxy-status
    stats auth    admin:yinzhengjie
    stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"

listen WEB_PORT_80
    bind 192.168.7.253:80
    server web01 www.cnblogs.com:80/yinzhengjie/  check inter 3000 fall 3 rise 5
root@haproxy101:~# 
root@haproxy101:~# scp /etc/haproxy/haproxy.cfg root@192.168.7.201:/etc/haproxy/haproxy.cfg
root@192.168.7.201's password: 
haproxy.cfg                                                                                                                                                                                                                                 100%  818   804.7KB/s   00:00    
root@haproxy101:~# 

2>.启动haproxy服务器

root@haproxy101:~# 
root@haproxy101:~# ss -ntl
State                               Recv-Q                               Send-Q                                                              Local Address:Port                                                               Peer Address:Port                               
LISTEN                              0                                    128                                                                 127.0.0.53%lo:53                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                       0.0.0.0:22                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                          [::]:22                                                                         [::]:*                                  
root@haproxy101:~# 
root@haproxy101:~# systemctl restart haproxy
root@haproxy101:~# 
root@haproxy101:~# ss -ntl
State                               Recv-Q                               Send-Q                                                              Local Address:Port                                                               Peer Address:Port                               
LISTEN                              0                                    20480                                                               192.168.7.253:80                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                 127.0.0.53%lo:53                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                       0.0.0.0:22                                                                      0.0.0.0:*                                  
LISTEN                              0                                    20480                                                               192.168.7.253:8888                                                                    0.0.0.0:*                                  
LISTEN                              0                                    128                                                                          [::]:22                                                                         [::]:*                                  
root@haproxy101:~# 

3>.

4>.

5>.

原文地址:https://www.cnblogs.com/yinzhengjie/p/12174356.html