www.colben.cn/content/post/keepalived.md
2024-12-04 22:33:57 +08:00

259 lines
6.7 KiB
Markdown

---
title: "Keepalived 笔记"
date: 2019-10-30T11:22:03+08:00
lastmod: 2019-10-30T11:22:03+08:00
tags: ["keepalived", "高可用", "负载均衡"]
categories: ["ha/lb"]
---
## keepalive 服务器
- 环境
- 应用服务器
- ka101: 10.1.1.101
- ka102: 10.1.1.102
- 虚拟地址
- ip: 10.1.1.100
- 安装 keepalived
```bash
yum install keepalived
```
- 配置虚拟 ip
```bash
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
ip addr add 10.1.1.100/32 brd 10.1.1.100 dev lo
ip route add 10.1.1.100 dev lo
```
## 两个 haproxy 不抢占
- MASTER/BACKUP 完整配置
```
global_defs {
router_id ka101 #BACKUP 这里是 ka102
script_user root
enable_script_security
}
vrrp_script chk_haproxy {
script "/usr/bin/systemctl status haproxy"
interval 2
weight 0
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP #MASTER 和 BACKUP 这里都是 BACKUP
virtual_router_id 1
priority 150 #BACKUP 这里是 100
advert_int 2
nopreempt #BACKUP 优先级低,需注释此行
interface eth0
track_script {
chk_haproxy
}
authentication {
auth_type PASS
auth_pass 1011100
}
virtual_ipaddress {
10.1.1.100/24 dev eth0
}
}
```
## 两个 LVS-DR 调度器不抢占均衡后端 MySQL 和 Ceph 负载
- 环境
- mysqld 服务器
- mysql103: 10.1.1.103
- mysql104: 10.1.1.104
- mysql105: 10.1.1.105
- ceph-radosgw 服务器
- ceph106: 10.1.1.106
- ceph107: 10.1.1.107
- ceph108: 10.1.1.108
- MASTER/BACKUP 完整配置
- 全局和实例配置
```
global_defs {
router_id ka101 #BACKUP 配置 ka102
}
vrrp_instance V1_1 {
state BACKUP #BACKUP 也配置 BACKUP
interface eth0
virtual_router_id 1
priority 150 #BACKUP 配置 100
advert_int 1
nopreempt #BACKUP 优先级低,需注释此行
authentication {
auth_type PASS
auth_pass 1011100
}
virtual_ipaddress {
10.1.1.100/24 dev eth0
}
}
```
- LVS 均衡 mysql galera cluser 负载
```
virtual_server 10.1.1.100 3306 {
delay_loop 16
lb_algo sh
lb_kind DR
protocol TCP
real_server 10.1.1.103 3306 {
weight 1
TCP_CHECK {
connect_timeout 4
delay_before_retry 2
connect_port 3306
}
}
real_server 10.1.1.104 3306 {
weight 1
TCP_CHECK {
connect_timeout 4
delay_before_retry 2
connect_port 3306
}
}
real_server 10.1.1.105 3306 {
weight 1
TCP_CHECK {
connect_timeout 4
delay_before_retry 2
connect_port 3306
}
}
}
```
- LVS 均衡 ceph radosgw 负载
```
virtual_server 10.1.1.100 7480 {
delay_loop 16
lb_algo sh
lb_kind DR
protocol TCP
real_server 10.1.1.106 7480 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 8
nb_get_retry 2
delay_before_retry 2
connect_port 7480
}
}
real_server 10.1.1.107 7480 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 8
nb_get_retry 2
delay_before_retry 2
connect_port 7480
}
}
real_server 10.1.1.108 7480 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 8
nb_get_retry 2
delay_before_retry 2
connect_port 7480
}
}
}
```
## 不抢占自动切换两台 Redis 主从状态
- MASTER/BACKUP 完整配置
```
global_defs {
router_id ka101 #BACKUP 这里是 ka102
script_user root
enable_script_security
}
vrrp_script chk_redis {
script "/usr/bin/systemctl status redis"
interval 2
weight 0
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP #MASTER 和 BACKUP 这里都是 BACKUP
virtual_router_id 51
priority 150 #BACKUP 这里是 100
advert_int 1
nopreempt #BACKUP 优先级低,需注释此行
interface eth0
notify_master /etc/keepalived/scripts/master.sh
notify_backup /etc/keepalived/scripts/backup.sh
track_script {
chk_redis
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
10.1.1.100/24 dev eth0
}
}
```
- 在全部 redis 服务器上创建 /etc/keepalived/scripts/master.sh 脚本,内容如下
```bash
#!/bin/bash
#
/usr/bin/sed -i '/^slaveof/d' /etc/redis.conf
/usr/bin/systemctl restart redis
```
- 在 ka101 上创建 /etc/keepalived/scripts/backup.sh 脚本,内容如下
```bash
#!/bin/bash
#
/usr/bin/sed -i '/^slaveof/d' /etc/redis.conf
echo 'slaveof 10.1.1.102 6379' >> /etc/redis.conf
/usr/bin/systemctl restart redis
```
- 在 ka102 上创建 /etc/keepalived/scripts/backup.sh 脚本,内容如下
```bash
#!/bin/bash
#
/usr/bin/sed -i '/^slaveof/d' /etc/redis.conf
echo 'slaveof 10.1.1.101 6379' >> /etc/redis.conf
/usr/bin/systemctl restart redis
```
- 在全部 redis 服务器上赋予脚本可执行权限
```bash
chmod 0755 /etc/keepalived/scripts/*.sh
```
- 修改 redis 配置
```
requirepass redis_password
maxclients 1000
maxmemory 4294967296
maxmemory-policy volatile-lru
```