Quantcast
Channel: 容器云计算,Devops,DBA,网络安全。
Viewing all articles
Browse latest Browse all 780

keepalived+LVS NAT模式集群配置

$
0
0

keepalived+LVS NAT模式集群配置

 

一,环境介绍

下面的示例在 NAT 模式下使用 Keepalived 在两台服务器上实现简单的故障转移和负载平衡配置。一台服务器充当主服务器,另一台服务器充当备份。
主服务器的优先级高于备份服务器。每个服务器都有两个网络接口,其中一个接口连接到面向外部网络的一侧 (192.168.1.0/24),另一个接口连接到可访问两个 Web 服务器的内部网络 ( 10.0.0.0/24)。

下图显示了 Keepalived master。Keepalived backup服务器 Web 服务器 的IP地址信息 。
LB01:192.168.1.10 192.168.1.1 10.0.0.10 10.0.0.100
LB02:192.168.1.11 10.0.0.11

websvr1 10.0.0.71websvr2 10.0.0.72

 

二,网络拓扑

图 NAT 模式下负载平衡的保持启用配置示例

下图显示 Keepalived 主服务器的网络地址为 192.168.1.10、192.168.1.1(虚拟)、10.0.0.10 和 10.0.0.100(虚拟)。Keepalived 备份服务器的网络地址为 192.168.1.11 和 10.0.0.11。Web 服务器 websvr1 和 websvr2 的网络地址分别为 10.0.0.71 和 10.0.0.72。

 

三,keepalived配置

您可以在 主服务器上使用以下配置:/etc/keepalived/keepalived.conf

global_defs {
   notification_email {
     root@mydomain.com
   }
   notification_email_from svr1@mydomain.com
   smtp_server localhost
   smtp_connect_timeout 30
}

vrrp_sync_group VRRP1 {
#   Group the external and internal VRRP instances so they fail over together
    group {
        external
        internal
        }
}

vrrp_instance external {
    state MASTER
    interface enp0s8
    virtual_router_id 91
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1215
    }
#   Define the virtual IP address for the external network interface
    virtual_ipaddress {
        192.168.1.1/24
    }
}

vrrp_instance internal {
    state MASTER
    interface enp0s9
    virtual_router_id 92
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1215
    }
#   Define the virtual IP address for the internal network interface
    virtual_ipaddress {
        10.0.0.100/24
    }
}

# Define a virtual HTTP server on the virtual IP address 192.168.1.1
virtual_server 192.168.1.1 80 {
    delay_loop 10
    protocol TCP
#   Use round-robin scheduling in this example
    lb_algo rr
#   Use NAT to hide the back-end servers
    lb_kind NAT
#   Persistence of client sessions times out after 2 hours
    persistence_timeout 7200

    real_server 10.0.0.71 80 {
        weight 1
        TCP_CHECK {
          connect_timeout 5
          connect_port 80
        }
    }

    real_server 10.0.0.72 80 {
        weight 1
        TCP_CHECK {
          connect_timeout 5
          connect_port 80
        }
    }
}

这里采用 NAT 模式,这意味着 Keepalived 服务器代表后端服务器处理客户端的入站和出站网络流量

备份服务器的配置是相同的,只需要修改个别配置。

global_defs {
   notification_email {
     root@mydomain.com
   }
   notification_email_from svr2@mydomain.com
   smtp_server localhost
   smtp_connect_timeout 30
}

vrrp_sync_group VRRP1 {
#   Group the external and internal VRRP instances so they fail over together
    group {
        external
        internal
        }
}

vrrp_instance external {
    state BACKUP
    interface enp0s8
    virtual_router_id 91
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1215
    }
#   Define the virtual IP address for the external network interface
    virtual_ipaddress {
        192.168.1.1/24
    }
}

vrrp_instance internal {
    state BACKUP
    interface enp0s9
    virtual_router_id 92
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1215
    }
#   Define the virtual IP address for the internal network interface
    virtual_ipaddress {
        10.0.0.100/24
    }
}

# Define a virtual HTTP server on the virtual IP address 192.168.1.1
virtual_server 192.168.1.1 80 {
    delay_loop 10
    protocol TCP
#   Use round-robin scheduling in this example
    lb_algo rr
#   Use NAT to hide the back-end servers
    lb_kind NAT
#   Persistence of client sessions times out after 2 hours
    persistence_timeout 7200

    real_server 10.0.0.71 80 {
        weight 1
        TCP_CHECK {
          connect_timeout 5
          connect_port 80
        }
    }

    real_server 10.0.0.72 80 {
        weight 1
        TCP_CHECK {
          connect_timeout 5
          connect_port 80
        }
    }
}

还需要另外两个配置更改:

  • 如第 8.7.1 节 "为保持潜行 NAT 模式负载平衡配置防火墙规则"中所述,在配置为负载平衡器的每台 Keepalived 服务器(主服务器和备份)上配置防火墙规则。
  • 如第 8.7.2 节 "为 Keepalived NAT 模式负载平衡配置后端服务器路由"中所述,在打算与 Keepalived 负载平衡器一起使用的每台后端服务器上,为负载平衡器的内部网络接口的虚拟 IP 地址配置默认路由。

有关如何安装和配置 Keepalived 的详细信息,请参见第 8.5 节 "安装和配置 Keepalived"。

 

四,配置firewall防火墙与路由规则

8.7.1 为保持启用 NAT 模式负载平衡配置防火墙规则

如果将 Keepalived 配置为使用 NAT 模式对内部网络上的服务器进行负载平衡,则 Keepalived 服务器将处理所有入站和出站网络流量,并通过使用外部网络接口的虚拟 IP 地址在传出数据包中重写真实后端服务器的源 IP 地址来隐藏后端服务器的存在。

要将 Keepalived 服务器配置为使用 NAT 模式进行负载平衡,请执行以下操作:

  1. 配置防火墙,以便外部网络端的接口与内部网络端的接口位于不同的区域中。

    下面的示例演示如何在接口保留在区域中的情况下将接口移动到区域:

    enp0s9 是internal区域, enp0s8 是public 区域

     

    # firewall-cmd --get-active-zones
    public
      interfaces: enp0s8 enp0s9
    # firewall-cmd --zone=public --remove-interface=enp0s9
    success
    # firewall-cmd --zone=internal --add-interface=enp0s9
    success
    # firewall-cmd --permanent --zone=public --remove-interface=enp0s9
    success
    # firewall-cmd --permanent --zone=internal --add-interface=enp0s9
    success
    # firewall-cmd --get-active-zones
    internal
      interfaces: enp0s9
    public
      interfaces: enp0s8
    
  2. 在外部网络接口上配置 NAT 模式(伪装),例如:

    # firewall-cmd --zone=public --add-masquerade
    success
    # firewall-cmd --permanent --zone=public --add-masquerade
    success
    # firewall-cmd --zone=public --query-masquerade
    yes
    # firewall-cmd --zone=internal --query-masquerade
    no
    
  3. 如果尚未为防火墙启用,请在外部和内部网络接口之间配置转发规则,例如:

    # firewall-cmd --direct --permanent --add-rule ipv4 filter FORWARD 0 \
      -i enp0s8 -o enp0s9 -m state --state RELATED,ESTABLISHED -j ACCEPT
    success
    # firewall-cmd --direct --permanent --add-rule ipv4 filter FORWARD 0 \
      -i enp0s9 -o enp0s8 -j ACCEPT
    success
    # firewall-cmd --direct --permanent --add-rule ipv4 filter FORWARD 0 \
      -j REJECT --reject-with icmp-host-prohibited
    success
    # firewall-cmd --reload
    
  4. 启用对希望 Keepalived 处理的服务或端口的访问。

    例如,要启用对 HTTP 的访问并使此规则在重新启动后保持不变,请输入以下命令:

    # firewall-cmd --zone=public --add-service=http
    success
    # firewall-cmd --permanent --zone=public --add-service=http
    success
    

8.7.2 为保持启用 NAT 模式负载平衡配置后端服务器路由

在要与 Keepalived 负载平衡器一起使用的每个后端真实服务器上,请确保路由表包含负载平衡器内部网络接口的虚拟 IP 地址的默认路由。

例如,如果虚拟 IP 地址为 ,则可以使用 ip 命令检查路由表并设置默认路由:10.0.0.100

# ip route show
10.0.0.0/24 dev enp0s8  proto kernel  scope link  src 10.0.0.71 
# ip route add default via 10.0.0.100 dev enp0s8
# ip route show
default via 10.0.0.100 dev enp0s8 
10.0.0.0/24 dev enp0s8  proto kernel  scope link  src 10.0.0.71 

要使默认路由在重新启动后持续存在,请创建文件: enp0s8 /etc/sysconfig/network-scripts/route-enp0s8

# echo "default via 10.0.0.100 dev enp0s8" > /etc/sysconfig/network-scripts/route-enp0s8

Viewing all articles
Browse latest Browse all 780

Trending Articles


Vimeo Create - Video Maker & Editor 1.5.2 by Vimeo Inc


FORTUITOUS EVENT


KASAMBAHAY BILL IN THE HOUSE


Girasoles para colorear


Inspirational Quotes For you and Motivates you


FORECLOSURE OF REAL ESTATE MORTGAGE


Re:Mutton Pies (lleechef)


Ka longiing longsem kaba skhem bad kaba khlain ka pynlong kein ia ka...


Vimeo 10.7.0 by Vimeo.com, Inc.


BARKADA TAGALOG QUOTES


Vimeo 11.8.1 by Vimeo.com, Inc.


From Male to Female


Pokemon para colorear


Sapos para colorear


Tropa Quotes


Knock knock jokes


RE: Mutton Pies (frankie241)


Hato lada ym dei namar ka jingpyrshah jong U JJM Nichols Roy (Bah Joy) ngin...


Vimeo 10.7.1 by Vimeo.com, Inc.


UPDATE SC IDOL: TWO BECOME ONE