配置keepalived+haproxy+nginx架构

服务架构介绍

---
主机名IP地址安装服务(角色)
porxy192.168.0.10客户端测试访问
web1192.168.0.20nginx
web2192.168.0.21nginx
haproxy-0001192.168.0.22haproxy,keepalived
haproxy-0002192.168.0.23haproxy,keepalived
vip192.168.0.100keepalived提供的vip
vip
客户端
keepalived
haproxy
nginx

haproxy介绍

  • haproxy

    • HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP(四层)和HTTP七层)的应用程序代理。
    • HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。能够补充Nginx的一些缺点比如Session的保持,Cookie的引导;能够支持url检测后端的服务器。单纯从效率上来讲HAProxy比Nginx有更出色的负载均衡速度。
    • HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进当前的架构中,同时可以保护web服务器不被暴露到网络上。
    • HAProxy实现了一种事件驱动,单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。但可以通过优化每个CPU时间片(Cycle)提高性能。
    • HAProxy支持多种负载均衡算法:Round-robin(轮循)、Weight-round-robin(带权轮循)、source(原地址保持)、RI(请求URL)、rdp-cookie(根据cookie)
    • HAProxy可以对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡。
    • HAProxy不能做Web服务器即Cache。

keepalived介绍

  • keepalived

    • Keepalived顾名思义,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生。
    • Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考模型的第三、第四层、第五层交换机制检测每个服务节点的状态,如果某个服务器节点出现异常,或者工作出现故障,Keepalived将检测到,并将出现的故障的服务器节点从集群系统中剔除,这些工作全部是自动完成的,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。
    • 后来Keepalived又加入了VRRP的功能,VRRP(Vritrual Router Redundancy Protocol,虚拟路由冗余协议)出现的目的是解决静态路由出现的单点故障问题,通过VRRP可以实现网络不间断稳定运行,因此Keepalvied 一方面具有服务器状态检测和故障隔离功能,另外一方面也有HA cluster功能。
    • 虚拟路由器由多个VRRP路由器组成,每个VRRP路由器都有各自的IP和共同的VRID(0-255),其中一个VRRP路由器通过竞选成为MASTER,占有VIP,对外提供路由服务,其他成为BACKUP,MASTER以IP组播(组播地址:224.0.0.18)形式发送VRRP协议包,与BACKUP保持心跳连接,若MASTER不可用(或BACKUP接收不到VRRP协议包),则BACKUP通过竞选产生新的MASTER并继续对外提供路由服务,从而实现高可用。
    • Keepalived检查主备节点的健康状态,通过IP漂移,实现主备冗余的服务高可用,服务器集群共享一个虚拟IP,同一时间只有一个服务器占有虚拟IP并对外提供服务,若该服务器不可用,则虚拟IP漂移至另一台服务器并对外提供服务,解决单点故障问题。

配置keepalived+haproxy+nginx架构

安装nginx,配置网站服务

配置web1

安装nginx
[root@web1 ~]# yum -y install gcc make
[root@web1 ~]# yum -y install pcre-devel
[root@web1 ~]# yum -y install  openssl openssl-devel
[root@web1 ~]# tar xf nginx-1.17.6.tar.gz
[root@web1 ~]# cd nginx-1.17.6
[root@web1 nginx-1.17.6]# ./configure --with-http_ssl_module
[root@web1 nginx-1.17.6]# make && make install
[root@web1 nginx-1.17.6]# ls /usr/local/nginx/
conf  html  logs  sbin

[root@web1 nginx-1.17.6]# cd /usr/local/nginx/
#nginx默认的配置,打开解析php文件,实现动静分离,注意此处修改为include        fastcgi.conf;
[root@web1 nginx]# vim  conf/nginx.conf
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
 71         }

配置网站
#准备静态页面
[root@web1 nginx]# vim html/index.html
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~</font>
  </marquee>
</html>

#准备动态页面
[root@web1 nginx]# vim html/test.php
<pre>
<?PHP
//phpinfo();
//$arr = array("id" => random_int(0000,9999));
foreach (array("REMOTE_ADDR", "REQUEST_METHOD", "HTTP_USER_AGENT", "REQUEST_URI") as $i) {
  $arr[$i] = $_SERVER[$i];
}
if($_SERVER['REQUEST_METHOD']=="POST"){
  $arr += $_POST;
}else{
  $arr += $_GET;
}
print_R($arr);
print_R("php_host: \t".gethostname()."\n");
$n = 0;
$start = 1;
$end = isset($_GET["id"])? $_GET["id"] : 10000 ;
for($num = $start; $num <= $end; $num++) {
    if ( $num == 1 ) continue;
    for ($i = 2; $i <= sqrt($num); $i++) {
        if ($num % $i == 0) continue 2;
    }
    $n++;
}
print_R($n."\n");
?>
安装php
yum -y install php
yum -y install php-fpm
启动服务
[root@web1 nginx]# systemctl enable php-fpm --now
[root@web1 nginx]# sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web1 nginx]# sbin/nginx
[root@web1 nginx]# ss -utnlp | grep 80
tcp    LISTEN     0      511       *:80                    *:*                   users:(("nginx",pid=16056,fd=6),("nginx",pid=16055,fd=6))
[root@web1 nginx]# ss -utnlp | grep 9000
tcp    LISTEN     0      128    127.0.0.1:9000                  *:*                   users:(("php-fpm",pid=16052,fd=0),("php-fpm",pid=16051,fd=0),("php-fpm",pid=16050,fd=0),("php-fpm",pid=16049,fd=0),("php-fpm",pid=16048,fd=0),("php-fpm",pid=16047,fd=6))

#关闭nginx服务
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s sotp
配置nginx开机自启
把nginx加入系统服务,使用systemctl控制(笔者采用此种方法)
#编写Unit文件,使systemctl命令控制nginx

[root@web1 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
#nginx是多进程类型程序,要设置为forking
Type=forking
#当执行了systemctl start nginx之后执行的命令
ExecStart=/usr/local/nginx/sbin/nginx
#当执行了systemctl reload nginx之后执行的命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#当执行了systemctl stop nginx之后执行的命令,这里是用kill命令发送退出信号给nginx的进程号,相当于停止nginx服务,-s QUIT是发送退出信号,$MAINPID是变量,里面存了nginx的进程号
ExecStop=/bin/kill -s QUIT ${MAINPID}

[Install]
#支持开机自启
WantedBy=multi-user.target

#激活刚才的test.service文件,但有时可能不好使,可以重启系统然后重启服务之后可以用systemctl等命令控制nginx
[root@web1 ~]# systemctl daemon-reload
#启动服务并设置开机自启
[root@web1 ~]# systemctl enable nginx --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@web1 ~]# ss -utnlp | grep nginx
tcp    LISTEN     0      511       *:80                    *:*                   users:(("nginx",pid=1748,fd=6),("nginx",pid=1747,fd=6))

把nginx启动命令写入开机启动脚本
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
本机访问查看

在这里插入图片描述


[root@web1 nginx]# curl localhost
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~</font>
  </marquee>
</html>

[root@web1 nginx]# curl localhost/test.php
<pre>
Array
(
    [REMOTE_ADDR] => 127.0.0.1
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web1
1229


配置web2

安装nginx同web1
[root@web2 ~]# yum -y install gcc make
[root@web2 ~]# yum -y install pcre-devel
[root@web2 ~]# yum -y install  openssl openssl-devel
[root@web2 ~]# tar xf nginx-1.17.6.tar.gz
[root@web2 ~]# cd nginx-1.17.6/
[root@web2 nginx-1.17.6]# ./configure --with-http_ssl_module
[root@web2 nginx-1.17.6]# make && make install
[root@web2 nginx-1.17.6]# cd /usr/local/nginx/
[root@web2 nginx]# vim conf/nginx.conf
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
 71         }

配置网站
[root@web2 nginx]# vim html/index.html
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>

#准备动态页面
[root@web2 nginx]# vim html/test.php
<pre>
<?PHP
//phpinfo();
//$arr = array("id" => random_int(0000,9999));
foreach (array("REMOTE_ADDR", "REQUEST_METHOD", "HTTP_USER_AGENT", "REQUEST_URI") as $i) {
  $arr[$i] = $_SERVER[$i];
}
if($_SERVER['REQUEST_METHOD']=="POST"){
  $arr += $_POST;
}else{
  $arr += $_GET;
}
print_R($arr);
print_R("php_host: \t".gethostname()."\n");
$n = 0;
$start = 1;
$end = isset($_GET["id"])? $_GET["id"] : 10000 ;
for($num = $start; $num <= $end; $num++) {
    if ( $num == 1 ) continue;
    for ($i = 2; $i <= sqrt($num); $i++) {
        if ($num % $i == 0) continue 2;
    }
    $n++;
}
print_R($n."\n");
?>
安装php
yum -y install php
yum -y install php-fpm
启动服务
[root@web2 nginx]# systemctl enable php-fpm --now
[root@web2 nginx]# sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web2 nginx]# sbin/nginx
[root@web2 nginx]# ss -utnlp | grep 80
udp    UNCONN     0      0         *:68                    *:*                   users:(("dhclient",pid=580,fd=6))
tcp    LISTEN     0      511       *:80                    *:*                   users:(("nginx",pid=13691,fd=6),("nginx",pid=13690,fd=6))
[root@web2 nginx]# ss -utnlp | grep 9000
tcp    LISTEN     0      128    127.0.0.1:9000                  *:*                   users:(("php-fpm",pid=13687,fd=0),("php-fpm",pid=13686,fd=0),("php-fpm",pid=13685,fd=0),("php-fpm",pid=13684,fd=0),("php-fpm",pid=13683,fd=0),("php-fpm",pid=13682,fd=6))

#关闭nginx服务
[root@web2 ~]# /usr/local/nginx/sbin/nginx -s sotp
配置nginx开机自启
把nginx加入系统服务,使用systemctl控制(笔者采用此种方法)
#编写Unit文件,使systemctl命令控制nginx

[root@web2 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
#nginx是多进程类型程序,要设置为forking
Type=forking
#当执行了systemctl start nginx之后执行的命令
ExecStart=/usr/local/nginx/sbin/nginx
#当执行了systemctl reload nginx之后执行的命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#当执行了systemctl stop nginx之后执行的命令,这里是用kill命令发送退出信号给nginx的进程号,相当于停止nginx服务,-s QUIT是发送退出信号,$MAINPID是变量,里面存了nginx的进程号
ExecStop=/bin/kill -s QUIT ${MAINPID}

[Install]
#支持开机自启
WantedBy=multi-user.target

#激活刚才的test.service文件,但有时可能不好使,可以重启系统然后重启服务之后可以用systemctl等命令控制nginx
[root@web2 ~]# systemctl daemon-reload
#启动服务并设置开机自启
[root@web2 ~]# systemctl enable nginx --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@webw ~]# ss -utnlp | grep nginx
tcp    LISTEN     0      511       *:80                    *:*                   users:(("nginx",pid=1748,fd=6),("nginx",pid=1747,fd=6))

把nginx启动命令写入开机启动脚本
[root@web2 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
本机访问查看
[root@web2 nginx]# curl localhost
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>
[root@web2 nginx]# curl localhost/test.php
<pre>
Array
(
    [REMOTE_ADDR] => 127.0.0.1
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229

安装haproxy,配置haproxy代理nginx服务

haproxy-0001 haproxy-0002配置相同

安装haproxy
  • 一款实现负载均衡的调度器
  • 适用于负载特别大的web站点
  • HAProxy的工作模式:
    • mode http:只适用于web服务
    • mode tcp:适用于各种服务
    • mode health:仅做健康检查,很少使用
[root@haproxy-0001 ~]# yum -y install haproxy
配置haproxy
vim /etc/haproxy/haproxy.cfg
# 配置文件中,global是全局配置;default是缺省配置,如果后续有和default相同的配置,default配置将会被覆盖。
# 配置文件中,frontend描述haproxy怎么和用户交互;backend描述haproxy怎么和后台应用服务器交互。这两个选项,一般不单独使用,而是合并到一起,名为listen。
# 将61行之后全部删除,写入以下内容
 61 listen myweb 0.0.0.0:80   # 定义本机监听地址
 62     balance roundrobin    # 调度算法为轮询
 # 对web服务器做健康检查,2秒检查一次,如果连续2次检查成功,认为服务器是健康的,如果连续5次检查失败,认为服务器坏了
 63     server web1 192.168.0.20 check inter 2000 rise 2 fall 5
 64     server web2 192.168.0.21 check inter 2000 rise 2 fall 5
启动服务
systemctl enable haproxy.service --now
ss -utnlp  | grep 80
tcp    LISTEN     0      1024      *:80                    *:*                   users:(("haproxy",pid=10946,fd=5))

访问测试haproxy-0001
#访问静态页面
[root@haproxy-0001 ~]# for i in {1..6}; do curl http://192.168.0.22/; done
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>

#访问动态页面
[root@haproxy-0001 ~]# for i in {1..6}; do curl http://192.168.0.22/test.php; done
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.22
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web1
1229

<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.22
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.22
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web1
1229

<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.22
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.22
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web1
1229

<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.22
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229

访问测试 haproxy-0002
[root@haproxy-0002 ~]# curl http://192.168.0.23
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

[root@haproxy-0002 ~]# for i in {1..6}; do curl http://192.168.0.23/; done
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web2~~~~</font>
  </marquee>
</html>
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

[root@haproxy-0002 ~]# for i in {1..6}; do curl http://192.168.0.23/test.php; done
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.23
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.23
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web1
1229

<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.23
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.23
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web1
1229

<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.23
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.23
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web1
1229


安装keepalived,配置vip,启动keepalived

配置haproxy-0001

[root@haproxy-0001 ~]# yum -y install keepalived.x86_64
[root@haproxy-0001 ~]# vim /etc/keepalived/keepalived.conf
 12    router_id haproxy-0001	# 设置本机在集群中的唯一识别符
 13    vrrp_iptables	## 自动配置iptables放行规则
 14    vrrp_skip_check_adv_addr	
 15    vrrp_strict
 16    vrrp_garp_interval 0
 17    vrrp_gna_interval 0
 18 }
 19
 20 vrrp_instance VI_1 {
 21     state MASTER	# 状态,主为MASTER,备为BACKUP
 22     interface eth0	# haproxy服务使用的网卡
 23     virtual_router_id 51	# 虚拟路由器地址
 24     priority 100	# 优先级,数字越大越优先
 25     advert_int 1	# 发送心跳消息的间隔
 26     authentication {	
 27         auth_type PASS	# 认证类型为共享密码
 28         auth_pass 1111	# 集群中的机器密码相同,才能成为集群
 29     }
 30     virtual_ipaddress {	
 31         192.168.0.100/24	# VIP地址,可配置多个
 32     }
 33 }
# 删除下面所有行

启动 keepalived服务
#启动keepalived,并设置开机自启
[root@haproxy-0001 ~]# systemctl enable keepalived.service --now
 #能看到vip 192.168.0.100/24
 [root@haproxy-0001 ~]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:e0:38:4e brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.22/24 brd 192.168.0.255 scope global noprefixroute dynamic eth0
       valid_lft 84683sec preferred_lft 84683sec
    inet 192.168.0.100/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fee0:384e/64 scope link
       valid_lft forever preferred_lft forever

配置haproxy-0002

[root@haproxy-0002 ~]# yum -y install keepalived.x86_64
[root@haproxy-0002 ~]# vim /etc/keepalived/keepalived.conf
 12    router_id haproxy-0002	# 设本机在集群中的唯一识别符
 13    vrrp_iptables	## 自动配置iptables放行规则
 14    vrrp_skip_check_adv_addr	
 15    vrrp_strict
 16    vrrp_garp_interval 0
 17    vrrp_gna_interval 0
 18 }
 19
 20 vrrp_instance VI_1 {
 21     state BACKUP	# 状态,主为MASTER,备为BACKUP
 22     interface eth0	# haproxy服务使用的网卡
 23     virtual_router_id 51	# 虚拟路由器地址
 24     priority 80	# 优先级,数字越大越优先
 25     advert_int 1	# 发送心跳消息的间隔
 26     authentication {	
 27         auth_type PASS	# 认证类型为共享密码
 28         auth_pass 1111	# 集群中的机器密码相同,才能成为集群
 29     }
 30     virtual_ipaddress {	
 31         192.168.0.100/24	# VIP地址,可配置多个
 32     }
 33 }
# 删除下面所有行

启动 keepalived服务
#启动keepalived,并设置开机自启
[root@haproxy-0002 ~]# systemctl enable keepalived.service --now

访问测试

云主机需要关闭服务器的源目的地址检查,haproxy-0001,haproxy-0002都需要关闭在这里插入图片描述

[root@proxy ~]# curl 192.168.0.100
<html>
  <marquee  behavior="alternate">
      <font size="12px" color=#00ff00>Hello World web1~~~~ </font>
  </marquee>
</html>

[root@proxy ~]# curl 192.168.0.100/test.php
<pre>
Array
(
    [REMOTE_ADDR] => 192.168.0.22
    [REQUEST_METHOD] => GET
    [HTTP_USER_AGENT] => curl/7.29.0
    [REQUEST_URI] => /test.php
)
php_host:       web2
1229



为keepalived的vip绑定弹性公网ip

详细步骤请参考https://support.huaweicloud.com/usermanual-vpc/zh-cn_topic_0067802474.html

登录管理控制台。

在管理控制台左上角单击,选择区域和项目。
在系统首页,选择“网络>虚拟私有云”。
进入“我的VPC”页面。
在这里插入图片描述

在左侧导航栏,选择“虚拟私有云 > 子网”。

在这里插入图片描述

在子网列表中,单击虚拟IP地址所属子网名称,进入子网。

选择“IP地址管理”页签,先申请虚拟IP地址。

在这里插入图片描述

申请虚拟IP地址

在这里插入图片描述

虚拟IP申请成功后,为虚拟IP绑定实例,可绑定多个实例。

笔者这里申请的为keepalievd设置的虚拟IP192.168.0.100,把虚拟IP绑定给实例haproxy-0001和proxy-0002
在这里插入图片描述

给虚拟ip绑定弹性公网IP

选择需要绑定的弹性公网IP地址或弹性云服务器(及网卡)。
笔者把新购买的弹性公网IP绑定到虚拟IP192.168.0.100
在这里插入图片描述

绑定完成

在这里插入图片描述

说明:
弹性云服务器多网卡时,建议绑定主网卡。
一个弹性云服务器的网卡可以绑定多个虚拟IP。
IPv6的虚拟IP仅支持绑定一个网卡(双栈网卡),如需进行服务器的主备切换,请通过调用API方式。具体请参考配置云服务器高可用的IPv6虚拟IP功能。

为已绑定虚拟IP的弹性云服务器手工配置虚拟IP地址。

访问虚拟IP对应的的弹性公网IP测试

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值