Entorno de combate real (CentOS7)
VIP 10.211.55.180 (IP virtual) LVS1 10.211.55.151 (LVS principal) LVS2 10.211.55.152 (LVS en espera) RS1 10.211.55.171 (servidor real 1) RS2 10.211.55.172 (servidor real 2)
Inicializar todos los nodos del entorno
iptalbes -F # Vaciar el firewall
systemctl stop firewalld #Cierre el firewall
systemctl deshabilitar firewalld #Detener que el firewall se inicie en el arranque
setenforce 0 # cerrar temporalmente selinux
sed -i 's / ^ SELINUX =. * $ / SELINUX = desactivado / g' / etc / selinux / config # 关闭 selinux
yum -y install epel-release #Install epel source
Operación LVS1,2
1. Instale keepalived ipvsadm
yum -y instalar keepalived ipvsadm
[root @ localhost ~] # lsmod | grep ip_vs #Compruebe el módulo ipvs
ip_vs_rr 12600 1
ip_vs 145497 3 ip_vs_rr
nf_conntrack 139264 9 ip_vs, nf_nat, nf_nat_ipv4, nf_nat_ipv6, xt_conntrack, nf_nat_masquerade_ipv4, nf_conntrack_netlink, nf_conntrack_ipv4, nf_conntrack_ipv6
libcrc32c 12644 4 xfs, ip_vs, nf_nat, nf_conntrack
2. Configurar keepalived (LVS1, 2)
2.1 Modificar el archivo de configuración de keepalived
[root @ localhost ~] # cd / etc / keepalived /
[root @ localhost keepalived] # gato keepalived.conf
! Keepalived el archivo de configuración para global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 del ROUTER_ID del LVS_DEVEL vrrp_skip_check_adv_addr # # vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_instance VI_1 { Estado LVS1 aquí es el maestro # MAESTRO, Entonces LVS2 se cambia a la interfaz BACKUP eth0 # CENTOS7 ens33 virtual_router_id 51 #Si el principal y el standby tienen una prioridad constante 100 # LVS1 es 100, entonces LVS2 es menor que 100, como 90, cuanto mayor es el peso, el VIP flota en qué lado 99 89 advert_int 1 autenticación { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.211.55.180 #VIP } } virtual_server 10.211.55.180 80 {#VIP delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocolo TCP real_server 10.211.55.171 80 {#后端RS1检测 peso 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 10.211.55.172 80 {# 后端 RS2 检测 peso 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
2.2 Configurar el arranque (LVS1, 2)
systemctl enable keepalived
systemctl start keepalived
3. Configure LVS, ejecute el siguiente script (LVS1, 2) y configure el script para que arranque.
vi /opt/lvs_dr.sh
#! / bin / sh # descripción: Iniciar LVS del servidor Director VIP = 10.211.55.180 RIP1 = 10.211.55.171 RIP2 = 10.211.55.172 caso "$ 1" en inicio) echo "iniciar LVS del servidor Director" # establecer la dirección IP virtual y parámetro sysctl # / sbin / ifconfig eth0: 0 $ VIP broadcast $ VIP netmask 255.255.255.255 up # route add -host $ VIP dev eth0: 0 echo "1"> / proc / sys / net / ipv4 / ip_forward #Clear IPVS table / sbin / ipvsadm -C #set LVS / sbin / ipvsadm -A -t $ VIP: 80 -s rr # -p 600 # 需要 会话 持久 化 则把 此处 的 -p 600 取消 注释 / sbin / ipvsadm -a -t $ VIP: 80 -r $ RIP1: 80 -g / sbin / ipvsadm -a -t $ VIP: 80 -r $ RIP2: 80 -g #Run LVS / sbin / ipvsadm ;; stop) echo "cerrar LVS Directorserver" echo "0"> / proc / sys / net / ipv4 / ip_forward / sbin / ipvsadm -C # / sbin / ifconfig eth0: 0 down ;; *) echo "Uso: $ 0 {iniciar | detener}" salir 1 esac
chmod + x /opt/lvs_dr.sh #Añadir autoridad de ejecución
echo "/opt/lvs_dr.sh start" >> / etc / profile #Inicio automático
4. Configure la operación RS y realice las siguientes operaciones (RS1, 2)
Instalar nginx
yum -y install nginx # RS1, 2 están instalados
echo 10.211.55.171> /usr/share/nginx/html/index.html # RS1 en funcionamiento
echo 10.211.55.172> /usr/share/nginx/html/index.html # RS2 en funcionamiento
systemctl start nginx # RS1, 2 están todos operativos
Configure el script RS y configure el arranque para que se inicie automáticamente
vi /opt/lvs_rs.sh
#! / bin / bash VIP = 10.211.55.180 / sbin / ifconfig lo: 0 $ VIP broadcast $ VIP netmask 255.255.255.255 up / sbin / route add -host $ VIP dev lo: 0 echo “1 ″> / proc / sys / net / ipv4 / conf / lo / arp_ignore echo "2 ″> / proc / sys / net / ipv4 / conf / lo / arp_announce echo" 1 ″> / proc / sys / net / ipv4 / conf / all / arp_ignore echo " 2 ″> / proc / sys / net / ipv4 / conf / all / arp_announce sysctl -p
Esta operación consiste en vincular una dirección IP virtual en el dispositivo de bucle invertido y establecer su máscara de subred en 255.255.255.255 para mantener la intercomunicación con la IP virtual en el servidor Director, y luego prohibir la solicitud ARP local.
Dado que la dirección IP virtual, que es la dirección VIP anterior, es compartida por Director Server y todos los servidores Real, si hay una solicitud ARP para la dirección VIP, si el Director Server responde a todos los servidores Real, habrá un problema. Por lo tanto, debe prohibirse. El servidor real responde a la solicitud de ARP. La función del script lvsrs es hacer que Real Server no responda a las solicitudes de arp.
chmod + /opt/lvs_rs.sh #Añadir autoridad de ejecución
echo "/opt/lvs_rs.sh" #Inicio automático
5. Prueba
Encuentra un cliente y ejecuta el siguiente comando
-> $ para i en `seq 20`; haz curl 10.211.55.180; hecho
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172
10.211.55.171
10.211.55.172