VMware+centos6.5环境下keepalived+nginx实验

 

准备工作:

下载filezilla 并安装。用来上传文件。

 

下载keepalived-1.2.8.tar.gz

配制2台虚拟机实验,hosts模式,在虚拟网络编辑器页面关闭host模式的dhcp

IP地址规划(同一网络段

192.168.40.155(主均衡节点)

192.168.40.145(从均衡节点) ,

192.168.40.252(VIP)

 

192.168.40.155(主均衡节点)

设置网络环境

 

关闭iptablesSELINUX

#chkconfig iptables off

#service iptables stop

#setenforce 0

#vi /etc/sysconfig/selinux

---------------

SELINUX=disabled

---------------

 

修改文件  vi  /etc/sysconfig/network,配置如下

NETWORKING=yes

HOSTNAME=KPLVS1126

NETWORKING_IPV6=no

 

 

修改文件 vi  /etc/sysconfig/network-scripts/ifcfg-eth0,配置如下

DEVICE="eth0"

BOOTPROTO="static"

IPV6INIT="no"

MTU="1500"

NM_CONTROLLED="no"

ONBOOT="yes"

TYPE="Ethernet"

IPADDR=192.168.40.155

PREFIX=24

ftp上传安装文件到目录 /usr/

 

安装nginx 忽略

 

 

 

安装keepalived

tar zxvfkeepalived-1.2.8.tar.gz

cdkeepalived-1.2.8

./configure--prefix=/usr/local/keepalived

make

makeinstall

 

 

 

 

 

配置文件

mkdir -p/etc/keepalived/

cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf

chmod+x /etc/init.d/keepalived

vi/etc/keepalived/keepalived.conf

 

修改keepalived配置文件:

 

 

vi  /etc/keepalived/keepalived.conf

global_defs {

 

 router_id nginx_master

 

}

 

vrrp_script chk_http_port {

script"/etc/keepalived/check_ng.sh"

interval 2

weight 2

}

 

 

vrrp_instance VI_1 {

   state MASTER

   interface eth0

    virtual_router_id81

    priority 100

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

track_script {

   chk_http_port

    }

   virtual_ipaddress {

       192.168.40.252/24 dev eth0

    }

}

 

cat /etc/keepalived/check_ng.sh

#! /bin/bash

 

N= 'ps -C nginx --no-header |wc -l'

   if[ $N -eq 0 ];then

       /usr/sbin/nginx

       sleep 3

       if ['ps -C nginx --no-header |wc -l' -eq 0 ];then

           killall keepalived

       fi

fi

 

 

192.168.40.145(从均衡节点)

设置网络环境

 

关闭iptablesSELINUX

#chkconfig iptables off

#service iptables stop

#setenforce 0

#vi /etc/sysconfig/selinux

---------------

SELINUX=disabled

---------------

 

修改文件  vi  /etc/sysconfig/network,配置如下

NETWORKING=yes

HOSTNAME=KPLVS1126

NETWORKING_IPV6=no

 

 

修改文件 vi  /etc/sysconfig/network-scripts/ifcfg-eth0,配置如下

DEVICE="eth0"

BOOTPROTO="static"

IPV6INIT="no"

MTU="1500"

NM_CONTROLLED="no"

ONBOOT="yes"

TYPE="Ethernet"

IPADDR=192.168.40.145

PREFIX=24

ftp上传安装文件到目录 /usr/

 

安装nginx 忽略

 

 

 

安装keepalived

tar zxvfkeepalived-1.2.8.tar.gz

cdkeepalived-1.2.8

./configure--prefix=/usr/local/keepalived

make

makeinstall

 

 

 

 

 

配置文件

mkdir -p/etc/keepalived/

cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf

chmod+x /etc/init.d/keepalived

vi/etc/keepalived/keepalived.conf

 

修改keepalived配置文件:

 

 

vi  /etc/keepalived/keepalived.conf

global_defs {

 

 router_id nginx_master

 

}

 

vrrp_script chk_http_port {

script"/etc/keepalived/check_ng.sh"

interval 2

weight 2

}

 

 

vrrp_instance VI_1 {

    state BACKUP

   interface eth0

    virtual_router_id 81

    priority 90

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 1111

    }

track_script {

   chk_http_port

    }

   virtual_ipaddress {

       192.168.40.252/24 dev eth0

    }

}

 

cat /etc/keepalived/check_ng.sh

#! /bin/bash

 

N= 'ps -C nginx --no-header |wc -l'

   if[ $N -eq 0 ];then

       /usr/sbin/nginx

       sleep 3

       if ['ps -C nginx --no-header |wc -l' -eq 0 ];then

           killall keepalived

       fi

fi

 

 

 

验证虚拟ip 

命令:ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue state UNKNOWN

   link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

   inet 127.0.0.1/8 scope host lo

   inet6 ::1/128 scope host

      valid_lft forever preferred_lft forever

2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWNqlen 1000

   link/ether 00:0c:29:f3:71:10 brd ff:ff:ff:ff:ff:ff

   inet 192.168.40.145/24 brd 192.168.40.255 scope global eth0

   inet 192.168.40.252/24 scope global secondary eth0

   inet6 fe80::20c:29ff:fef3:7110/64 scope link

      valid_lft forever preferred_lft forever

vip访问验证

在主机cmd窗口: ping 192.168.40.252

 

C:\Users\bbgds>ping 192.168.40.252

如下图所示,说明vip绑定成功,并能访问

 

正在 Ping 192.168.40.252 具有 32 字节的数据:

来自 192.168.40.252 的回复: 字节=32 时间=1ms TTL=64

来自 192.168.40.252 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.40.252 的回复: 字节=32 时间<1ms TTL=64

来自 192.168.40.252 的回复: 字节=32 时间<1ms TTL=64

 

192.168.40.252 的 Ping 统计信息:

    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

往返行程的估计时间(以毫秒为单位):

    最短 = 0ms,最长 = 1ms,平均 = 0ms

 

 

 

 


Logo

更多推荐