首页 > 代码库 > Ubuntu上ipvsadm+keepalived
Ubuntu上ipvsadm+keepalived
Ubuntu上ipvsadm+keepalived
http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
MASTER服务器
keepalived部分:
$ sudo apt-get install libpopt-dev
$ sudo apt-get install daemon
$ sudo tar zxfv keepalived-1.1.19.tar.gz
$ cd keepalived-1.1.19
$ ./configure --prefix=/usr/local/keepalived
$ make
$ sudo make install
$ sudo mkdir /etc/sysconfig
$ sudo cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
$ sudo cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
$ sudo cp /usr/local/keepalived/sbin/keepalived /sbin/
$ sudo mkdir /etc/keepalived
$ sudo cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
$ sudo vi /etc/keepalived/keepalived.conf
按a或i进入编辑模式
! Configuration File for keepalived
global_defs {
notification_email {
yang@126.com (当IP发生迁移等事件的时候发送邮件到指定邮箱)
}
notification_email_from root@localhost (邮件发送方)
smtp_server 127.0.0.1 (邮件外发服务器地址)
smtp_connect_timeout 30 (邮件超时时间为30秒)
router_id LVS_DEVEL (一个实例的标识)
}
vrrp_instance VI_1 {
State MASTER (设置服务器模式,注要大写)
interface eth0 (实例网卡,也就是提供服务的网卡)
virtual_router_id 51
priority 100 (设定权值为100 主备之间的权值差最好大于50 权值范围为0~255)
advert_int 1 (检查时间为1秒)
authentication { (认证方式为密码认证)
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.1.10 (10.0.1.10并不存在)
}
}
后面的内容不用改
按Esc键退出编辑模式
:wq
$ sudo mkdir -p /etc/rc.d/init.d
$ sudo cp /usr/share/initramfs-tools/scripts/functions /etc/rc.d/init.d/
$ sudo vi /etc/init.d/keepalived +23
按a或i进入编辑模式
daemon keepalived start (默认为daemon keepalived ${KEEPALIVED_OPTIONS})
按Esc键退出编辑模式
:wq (保存并退出)
$ sudo mkdir /var/lock/subsys (注:这个目录重启服务器后会消失,需要将其写道开机启动项中,如写到/etc/rc.local里)
$ service keepalived start
开机自启动
$ sudo vi /etc/rc.local (在exit 0前)
按a或i进入编辑模式
mkdir /var/lock/subsys
service keepalived start
按Esc键退出编辑模式
:wq (保存并退出)
$ sudo ip a (可以看到eth0多了个10.0.1.10的ip)
ipvsadm部分:
$ sudo apt-get install ipvsadm
$ sudo ipvsadm -A -t 10.0.1.10:22 -s rr (注rr为轮询模式)
$ sudo ipvsadm -a -t 10.0.1.10:22 -r 10.0.1.3:22 -g
$ sudo ipvsadm -a -t 10.0.1.10:22 -r 10.0.1.4:22 -g
$ sudo ipvsadm --save (保存ipvsadm设置)
$ sudo ipvsadm -l (查看ipvsadm设置)
$ sudo ipvsadm -C (清除ipvsadm设置)
$ sudo ipvsadm -a (增加real服务器,如:$ sudo ipvsadm -a -t 10.0.1.10:22 -r 10.0.1.3:22 -g)
$ sudo ipvsadm -d (删除real服务器,如:$ sudo ipvsadm -d -t 10.0.1.10:22 -r 10.0.1.3:22)
设置开机自启动:
$ sudo vi /etc/rc.local (在exit 0上面)
按a或i进入编辑模式
ipvsadm -A -t 10.0.1.10:22 -s rr (注rr为轮询模式)
ipvsadm -a -t 10.0.1.10:22 -r 10.0.1.3:22 -g
ipvsadm -a -t 10.0.1.10:22 -r 10.0.1.4:22 -g
ipvsadm --save
按Esc键退出编辑模式
:wq (保存并退出)
BACKUP服务器步骤同上,只需将/etc/keepalived/keepalived.conf里的MASTER改为BACKUP,priority改为50即可
BACKUP服务器ip a显示eth0没有多出10.0.1.10这个ip,等MASTER服务器宕后就会显示出来,当MASTER服务器正常后会自动消失
ipvsadm客户端(10.0.1.3)
$ sudo ifconfig lo:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10
$ sudo ip a (查看设置是否生效)
$ sudo route add -host 10.0.1.10 dev lo:0
$ sudo route -n (查看设置是否生效)
$ sudo echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
$ sudo echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
$ sudo echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
$ sudo echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
设置开机自启动:
$ sudo vi /etc/rc.local (在exit 0上面)
按a或i进入编辑模式
ifconfig lo:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10
按Esc键退出编辑模式
:wq (保存并退出)
ipvsadm客户端(10.0.1.4)
$ sudo ifconfig lo:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10
$ sudo ip a (查看设置是否生效)
$ sudo route add -host 10.0.1.10 dev lo:0
$ sudo route -n (查看设置是否生效)
$ sudo echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
$ sudo echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
$ sudo echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
$ sudo echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
设置开机自启动:
$ sudo vi /etc/rc.local (在exit 0上面)
按a或i进入编辑模式
ifconfig lo:0 10.0.1.10 netmask 255.255.255.255 broadcast 10.0.1.10
按Esc键退出编辑模式
:wq (保存并退出)
验证:
putty或ssh 10.0.1.10 (第一次登录的是10.0.1.3,第二次登录的是10.0.1.4)
本文出自 “linux” 博客,谢绝转载!
Ubuntu上ipvsadm+keepalived