keepalived+nginx实现双机主备

192.168.25.100 vip 虚拟ip

192.168.25.134 (nginx+keepalived)主

192.168.25.135(nginx+keepalived)备

134、135两台服务器nginx配置

server {
        listen      80;
        server_name  localhost;

        location /{

                root html;
                index index.html index.htm;

        }
    }

分别为134和135两台服务器安装keepalived,安装步骤见 Keepalived安装部署

配置192.168.25.134(主) Keepalived

进入keepalived核心配置文件所在位置

cd /etc/keepalived
vim keepalived.conf
global_defs {
   router_id keep_134
}

vrrp_instance VI_1 {
	# 表示状态是MASTER主机还是备用机BACKUP
	state MASTER
	# 该实例绑定的网卡
	interface ens33
	# 保证主备节点一致即可
	virtual_router_id 51
	# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
	priority 100
	# 主备之间同步检查时间间隔,单位秒
	advert_int 2
	# 认证权限密码,防止非法节点进入
	authentication {
		auth_type PASS
		auth_pass 1111
	}
	# 虚拟出来的ip,可以有多个(vip)
	virtual_ipaddress {
		192.168.25.100
	}
}

配置interface绑定网卡时可以通过ip addr 查看网卡名称

[root@localhost sbin]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:34:0a:45 brd ff:ff:ff:ff:ff:ff
    inet 192.168.25.134/24 brd 192.168.25.255 scope global noprefixroute dynamic ens33
       valid_lft 1572sec preferred_lft 1572sec
    inet6 fe80::8693:c0a4:8360:c07f/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

启动keepalived

cd /usr/local/keepalived/sbin
./keepalived

查看vip 在网卡ens33下,多了一个192.168.25.100,这个就是虚拟ip

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:34:0a:45 brd ff:ff:ff:ff:ff:ff
    inet 192.168.25.134/24 brd 192.168.25.255 scope global noprefixroute dynamic ens33
       valid_lft 1644sec preferred_lft 1644sec
    inet 192.168.25.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::8693:c0a4:8360:c07f/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

访问虚拟ip192.168.25.100,会跳到134这台nginx上
在这里插入图片描述
配置192.168.25.135(备

global_defs {
   router_id keep_135
}

vrrp_instance VI_1 {
    # 备用机设置为BACKUP
    state BACKUP
    interface ens33
    virtual_router_id 51
    # 权重低于MASTER
    priority 80
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        # 注意:主备两台的vip都是一样的,绑定到同一个vip
        192.168.25.100
    }
}

查看135的ip addr 发现是正常的 ens下只有192.168.25.135这一个IP

[root@localhost sbin]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:74:a0:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.25.135/24 brd 192.168.25.255 scope global noprefixroute dynamic ens33
       valid_lft 1424sec preferred_lft 1424sec
    inet6 fe80::b4c1:b3db:5c81:2e92/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:71:96:f7:b8 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:71ff:fe96:f7b8/64 scope link 
       valid_lft forever preferred_lft forever
5: veth49aea05@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether f6:14:c1:e5:c5:d2 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::f414:c1ff:fee5:c5d2/64 scope link 
       valid_lft forever preferred_lft forever

现在需要模拟134这台机器的keepalive宕机,ps -ef | grep keepalived 查看进程号,再kill -9 进程号 这时再访问192.168.25.100时,访问会落到192.168.25.135备用机上
在这里插入图片描述
这时再查看135上的ip addr,ens33下包含192.168.25.135和虚拟ip 192.168.25.100

[root@localhost etc]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:74:a0:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.25.135/24 brd 192.168.25.255 scope global noprefixroute dynamic ens33
       valid_lft 1375sec preferred_lft 1375sec
    inet 192.168.25.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::b4c1:b3db:5c81:2e92/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:71:96:f7:b8 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:71ff:fe96:f7b8/64 scope link 
       valid_lft forever preferred_lft forever
5: veth49aea05@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether f6:14:c1:e5:c5:d2 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::f414:c1ff:fee5:c5d2/64 scope link 
       valid_lft forever preferred_lft forever

这样就实现了双机主备,但实际开发中,很少使用这种方式来搭建集群,虚拟ip也需要购买,可以构建nginx集群外加负载均衡器即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值