keepalived安装---VIP
<p>keepalived的切换可以是自动的,但是却做不到毫秒级别,他怎么都需要几秒钟的时间进行切换。
这就有一个问题,虽然在主节点出现问题我们转向备份节点时,这个延时无可避免,但是在我们修复主节点后,实际上并没有必要再马上做一次切换,所以Keepalived提供了一种非抢占模式,来满足这个要求。所以,生产环境中建议采用<code>非抢占模式</code>。</p>
<h1>一、Keepalived 抢占模式</h1>
<h2>1.keepalived安装</h2>
<p>在所有worker节点上安装</p>
<h3>1.1 安装keepalived</h3>
<pre><code class="language-bash">yum -y install keepalived</code></pre>
<h3>1.2 keepalived配置</h3>
<h4>1.2.1 worker-01 的keepalived配置文件</h4>
<pre><code class="language-bash">mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf,输入如下内容:
! Configuration File for keepalived
global_defs {
router_id worker-01.techzsun.com
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 50
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.7.207
}
}</code></pre>
<h4>1.2.2 worker-02 的keepalived配置文件</h4>
<pre><code class="language-bash">mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf,输入如下内容:
! Configuration File for keepalived
global_defs {
router_id worker-02.techzsun.com
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.7.207
}
}</code></pre>
<h4>1.2.3 worker-03 的keepalived配置文件</h4>
<pre><code class="language-bash">mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf,输入如下内容:
! Configuration File for keepalived
global_defs {
router_id worker-03.techzsun.com
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.7.207
}
}</code></pre>
<h3>1.3 测试VIP</h3>
<h4>1.3.1 查看VIP</h4>
<p>worker-01 节点上查看</p>
<pre><code class="language-bash">[root@worker-01 ~]# ip a |grep ens192
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 172.16.7.204/24 brd 172.16.7.255 scope global noprefixroute ens192
inet 172.16.7.207/32 scope global ens192</code></pre>
<p>worker-02 节点上查看</p>
<pre><code class="language-bash">[root@worker-02 ~]# ip a | grep ens192
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 172.16.7.205/24 brd 172.16.7.255 scope global noprefixroute ens192</code></pre>
<p>worker-03 节点上查看</p>
<pre><code class="language-bash">[root@worker-03 ~]# ip a |grep ens192
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 172.16.7.206/24 brd 172.16.7.255 scope global noprefixroute ens192</code></pre>
<p>可以看出,现在的VIP(172.16.7.204)上。
测试VIP是否会飘:
只要把VIP所在主机172.16.7.204关机,VIP就会飘到另外的主机上。</p>
<h4>1.3.2 测试VIP</h4>
<pre><code class="language-bash">[root@worker-01 ~]# ping 172.16.7.207 -c 4
PING 172.16.7.207 (172.16.7.207) 56(84) bytes of data.
64 bytes from 172.16.7.207: icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from 172.16.7.207: icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from 172.16.7.207: icmp_seq=3 ttl=64 time=0.040 ms</code></pre>
<h1>二、Keepalived 非抢占模式</h1>
<p>下面我们就来介绍一下Keepalived的非抢占模式的配置(无MASTER节点,全部依据优先级确定哪个节点进行工作):
此模式下keepalived 的配置</p>
<pre><code class="language-bash">! Configuration File for keepalived
global_defs {
# 每个节点主机名不一样
router_id worker-01.techzsun.com
}
vrrp_instance VI_1 {
# 所有节点都是BACKUP
state BACKUP
# 每个节点对应的网卡名称
interface ens192
# 同一个keepalived组,节点的设置必须一样,这样才会被识别
virtual_router_id 50
# 这个关键配置项,设置为“非抢占”模式
nopreempt
# 每个节点的优先级一定要不一样
priority 120
# 组播信息发送间隔,每个节点设置必须一样
advert_int 1
# 验证信息,只有验证信息相同,才能被加入到一个组中。
authentication {
auth_type PASS
auth_pass 310012
}
# 虚拟地址和绑定的端口,如果有多个,就绑定多个
# dev 是指定浮动IP要绑定的网卡设备号
virtual_ipaddress {
172.16.7.207
}
# keepalived 在某些环境(如:OpenStack)下一直脑裂无法选举。
# 在不支持组播的场景下,可以使用单播模式
unicast_peer {
172.16.7.204
172.16.7.205
172.16.7.206
}
}</code></pre>
<p>参考资料:</p>
<ol>
<li><a href="https://www.cnblogs.com/shangsharon/p/11557587.html">https://www.cnblogs.com/shangsharon/p/11557587.html</a></li>
<li><a href="https://www.cnblogs.com/zhaoya2019/p/13032218.html">https://www.cnblogs.com/zhaoya2019/p/13032218.html</a></li>
<li><a href="https://blog.csdn.net/yinwenjie/article/details/47130609">https://blog.csdn.net/yinwenjie/article/details/47130609</a> 详解</li>
<li>keepalived 的 unicast 单播模式+结合秒云
最近遇到一个问题,keepalived 在 OpenStack 环境下一直脑裂无法选举。在不支持组播的场景下,可以使用单播模式。
<a href="https://www.cnblogs.com/keithtt/p/12758731.html">https://www.cnblogs.com/keithtt/p/12758731.html</a>
5.keepalived脑裂及解决办法
<a href="https://blog.csdn.net/weixin_43557605/article/details/104006699">https://blog.csdn.net/weixin_43557605/article/details/104006699</a></li>
</ol>