Keepalived简单配置
Keepalived服务的三个重要功能
- 管理LVS负载均衡
- 对LVS节点做健康检查(基于端口和URL)
- VRRP高可用功能
原理:
- 通过VRRP(虚拟路由冗余协议)实现高可用
- 通过竞选机制实现高可用(手动:配置优先级,优先级大的是主,小的是备)
- 串口线、网线直连、局域网通信
- master首先以多播的方式发信息
- 加密数据
Keepalived高可用服务搭建
规划:
HOSTNAME IP 说明
lb01 10.0.0.5 Keepalived主服务器(Nginx主负载均衡器)
lb02 10.0.0.6 Keepalived备服务器(Nginx辅负载均衡器)
web01 10.0.0.7 web01服务器
web02 10.0.0.8 web02服务器
#部署web01
[root@web01 conf.d]# cat 02_www.etiantian.org.conf
server {
listen 80;
server_name www.etiantian.org;
root /data/www;
location / {
index index.html;
}
}
[root@web01 conf.d]# mkdir -p /data/www
[root@web01 conf.d]# echo web01 >/data/www/index.html
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
[root@web01 conf.d]# grep www /etc/hosts
[root@web01 conf.d]# curl -H "host:www.etiantian.org" 10.0.0.7
web01
#部署web02
[root@web02 conf.d]# cat 02_www.etiantian.org.conf
server {
listen 80;
server_name www.etiantian.org;
root /data/www;
location / {
index index.html;
}
}
[root@web02 conf.d]# mkdir -p /data/www
[root@web02 conf.d]# echo web02 >/data/www/index.html
[root@web02 conf.d]# nginx -t
[root@web02 conf.d]# systemctl restart nginx
[root@web02 conf.d]# curl -H "host:www.etiantian.org" 10.0.0.8
web02
#部署lb
[root@lb02 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
priority=1
yum install yum-plugin-priorities -y
yum install nginx -y
systemctl start nginx
systemctl enable nginx
#从lb01拷贝配置
[root@lb02 ~]# scp -rp 172.16.1.5:/etc/nginx /etc/
[root@lb02 conf.d]# cat 03_www.etiantian.org.conf
upstream www {
server 172.16.1.7 weight=1; #默认80端口
server 172.16.1.8 weight=1; #默认80端口
}
server {
listen 80;
server_name www.etiantian.org;
location / {
proxy_pass http://www;
proxy_set_header Host $http_host;
}
}
nginx -t
systemctl restart nginx
#vip 10.0.0.6 测试负载
[root@lb02 conf.d]# curl -H "host:www.etiantian.org" 10.0.0.6
web01
[root@lb02 conf.d]# curl -H "host:www.etiantian.org" 10.0.0.6
web02
#lb01,lb02分别安装keepalived
yum install keepalived -y
#参考下面修改好配置文件再启动
systemctl start keepalived
版本 keepalived-1.3.5-19.el7.x86_64
#可以网上搜索找最新版的看看怎么安装?
#Keepalived配置文件说明
TOP HIERACHY
GLOBAL CONFIGURATION
VRRPD CONFIGURATION
LVS CONFIGURATION(管理LVS的,nginx此处删掉)
#Master配置 说明不做为配置。
[root@lb01 keepalived]# cat keepalived.conf
global_defs {
router_id lb01 #<==id为lb01,不同的keepalived.conf此ID要唯一。
}
vrrp_instance VI_1 { #<==实例名字为VI_1,相同实例的备节点名字要和这个相同。
state MASTER #<==状态为MASTER,备节点状态需要为BACKUP。
interface eth0 #<==通信接口为eth0,此参数备节点设置和主节点相同。
virtual_router_id 51 #<==实例ID为55,keepalived.conf里唯一。
priority 150 #<==优先级为150,备节点的优先级必须比此数字低。
advert_int 1 #<==通信检查间隔时间1秒。
authentication {
auth_type PASS #<==PASS认证类型,此参数备节点设置和主节点相同。
auth_pass 1111 #<==密码是1111,此参数备节点设置和主节点相同。
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:3 #<==虚拟IP,即VIP,子网掩码为24位,绑定接口为eth0,别名为eth0:3,此参数备节点设置和主节点相同。
}
} #提示:此处设置的虚拟IP为10.0.0.3,即网站域名绑定的IP。
配置 lb(单实例)
#配置 lb01 keepalved.conf
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
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 {
10.0.0.3/24 dev eth0 label eth0:3
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|grep '10.0.0.3'
#配置 lb02 keepalved.conf
[root@lb02 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
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 {
10.0.0.3/24 dev eth0 label eth0:3
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|grep '10.0.0.3'
停止一端服务器。看另一端IP接管情况
配置 lb(多实例)
#配置 lb01 keepalved.conf
[root@lb01 conf.d]$cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
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 {
10.0.0.3/24 dev eth0 label eth0:3
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:4
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|egrep '10.0.0.3|10.0.0.4'
#配置 lb02 keepalved.conf
[root@lb02 conf.d]$cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
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 {
10.0.0.3/24 dev eth0 label eth0:3
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:4
}
}
#启动
systemctl start keepalived
systemctl enable keepalived
ps -ef|grep keepalived
ifconfig|egrep '10.0.0.3|10.0.0.4'
单实例场景与多实例
单实例hosts
lb01:
10.0.0.3 www.etiantian.org blog.etiantian.org
lb02:热备 浪费机器
多实例hosts
两边跑服务;
lb01:
10.0.0.3 www.etiantian.org
lb02:
10.0.0.4 blog.etiantian.org
某一台宕机,总流量能被撑住即可。