1. 程式人生 > >openstack 例項配置keepalived

openstack 例項配置keepalived

#!/bin/bash
nginx1:192.168.97.101
nginx2:192.168.97.102
vip: 192.168.97.241

#安裝軟體

yum install nginx wget

echo -e "192.168.97.101 nginx-master " > /usr/share/nginx/html/index.html
echo -e "192.168.97.102 nginx-backup " > /usr/share/nginx/html/index.html

yum install keepalived -y


##配置檢查指令碼,兩臺節點都配置
 

cat <<END> /etc/nginx/check_nginx_alive.sh
#!/bin/sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ]
 then echo 'nginx server is died'
  systemctl stop keepalived
fi
END

chmod a+x /etc/nginx/check_nginx_alive.sh


##配置keepalived.conf
###主節點配置
cp /etc/keepalived/keepalived.conf{,.bk}

egrep -v '#|^$' /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_nginx_alive {
    script "/etc/nginx/check_nginx_alive.sh"
    interval 3
    weight -10
}
global_defs {
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.97.241
    }
    track_script {
    check_nginx_alive
    }
}
virtual_server 192.168.97.241 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    real_server 192.168.97.101 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

###備節點配置

egrep -v '#|^$' /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_nginx_alive {
    script "/etc/nginx/check_nginx_alive.sh"
    interval 3
    weight -10
}
global_defs {
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.97.241
    }
   track_script {
    check_nginx_alive
    }
}
virtual_server 192.168.97.241 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP
    real_server 192.168.97.102 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

##啟動服務
systemctl start keepalived && systemctl enable keepalived
##修改openstack中例項埠屬性

在OpenStack中預設由於安全組策略限制,雲主機只響應自己的iP地址請求,如果需要做HA,可以用以下兩種方式實現
這裡使用的是第一種,第二種忽略

1、 增加allow_address_pairs屬性

neutron port-list |grep 192.168.97.101
neutron port-update  958ea025-d13c-4c8b-af8a-207339fe5299 --allowed_address_pairs list=true type=dict ip_address=192.168.97.241
neutron port-list |grep 192.168.97.102
neutron port-update 184cee31-9eee-4282-9da9-dce542c1056a --allowed_address_pairs list=true type=dict ip_address=192.168.97.241

2、 關閉neutron port的安全組特性 ,這種方法忽略

neutron port-update --no-security-groups $port_id
neutron port-update $port_id --port-security-enabled=False

##測試
web 輸入 192.168.97.241  顯示ok,
手動關閉主節點keepalived ,備節點顯示ok