荐 KeepAlived+Haproxy+Redis 项目实验
程序员文章站
2022-06-27 21:05:28
KeepAlived+Haproxy+Redis 实现主从热备、负载均衡、高可用;实验环境HOSTSIPOS_VERSIONPROJECTkeep-hapro01192.168.5.160CentOS 7.4HA+Haproxykeep-hapro02192.168.5.166CentOS 7.4HA+Haproxyredis-01192.168.5.68CentOS 7.4Redis-Master,Slaveredis-02192.168....
KeepAlived+Haproxy+Redis 实现主从热备、负载均衡、高可用;
实验环境
HOSTS | IP | OS_VERSION | PROJECT |
---|---|---|---|
keep-hapro01 | 192.168.5.160 | CentOS 7.4 | HA+Haproxy |
keep-hapro02 | 192.168.5.166 | CentOS 7.4 | HA+Haproxy |
redis-01 | 192.168.5.68 | CentOS 7.4 | Redis-Master,Slave |
redis-02 | 192.168.5.77 | CentOS 7.4 | Redis-Master,Slave |
redis-03 | 192.168.5.88 | CentOS 7.4 | Redis-Master,Slave |
一、Redis-Cluster 部署
1.部署环境
HOSTS | IP | PORT | CONF_FILE |
---|---|---|---|
redis-01 | 192.168.5.68 | 6001 | /redis/6001/conf/redis.conf |
redis-01 | 192.168.5.68 | 6002 | /redis/6002/conf/redis.conf |
redis-02 | 192.168.5.77 | 6001 | /redis/6001/conf/redis.conf |
redis-02 | 192.168.5.77 | 6002 | /redis/6002/conf/redis.conf |
redis-03 | 192.168.5.88 | 6001 | /redis/6001/conf/redis.conf |
redis-03 | 192.168.5.88 | 6002 | /redis/6002/conf/redis.conf |
2.修改集群主机名并配置域名解析(所有节点)
[root@redis-01 ~]# cat >> /etc/hosts <<-EOF
192.168.5.68 redis-01
192.168.5.77 redis-02
192.168.5.88 redis-03
EOF
3.修改系统优化参数(所有节点)
1.修改最大可打开文件数
[root@redis-01 ~]# cat >> /etc/security/limits.conf << EOF
> * soft nofile 102400
> * hard nofile 102400
> EOF
2.TCP 监听队列大小
[root@redis-01 ~]# echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf
[root@redis-01 ~]# sysctl -p
net.core.somaxconn = 32767
3.OOM 相关:vm.overcommit_memory
[root@redis-01 ~]# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
[root@redis-01 ~]# sysctl -p
net.core.somaxconn = 32767
vm.overcommit_memory = 1
4.开启内核的" Transparent Huge Pages (THP) "特性
[root@redis-01 ~]# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
[root@redis-01 ~]# chmod +x /etc/rc.local
5、安装 Redis 并配置 Redis-Cluster(所有节点)
1.安装 GCC 环境
[root@redis-01 ~]# yum -y install gcc glibc glibc-kernheaders glibc-common glibc-devel make
[root@redis-01 ~]# yum -y install centos-release-scl
[root@redis-01 ~]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@redis-01 ~]# scl enable devtoolset-9 bash
[root@redis-01 ~]# echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
2.编译安装 Redis
[root@redis-01 ~]# cd /usr/local/src
[root@redis-01 src]# wget http://download.redis.io/releases/redis-6.0.5.tar.gz
[root@redis-01 src]## tar -xzvf redis-6.0.5.tar.gz
[root@redis-01 src]## cd redis-6.0.5/
[root@redis-01 redis-6.0.5]# make
[root@redis-01 redis-6.0.5]# make install PREFIX=/usr/local/redis-cluster
3.配置 Redis 多实例
[root@redis01 ~]# mkdir -p /redis/{6001,6002}/{conf,data,log}
[root@redis-01 conf]# cat >> redis.conf << EOF
> bind 0.0.0.0
> protected-mode no
> port 6001
> daemonize no
> dir /redis/6001/data
> cluster-enabled yes
> cluster-config-file /redis/6001/conf/nodes.conf
> cluster-node-timeout 5000
> appendonly yes
> daemonize yes
> pidfile /redis/6001/redis.pid
> logfile /redis/6001/log/redis.log
> requirepass redis
> masterauth redis
> EOF
[root@redis-01 conf]# sed 's/6001/6002/g' redis.conf > /redis/6002/conf/redis.conf
[root@redis-01 redis-6.0.5]# cat >/usr/local/redis-cluster/start-redis-cluster.sh<<-EOF
> #!/bin/bash
> REDIS_HOME=/usr/local/redis-cluster
> REDIS_CONF=/redis
> \$REDIS_HOME/bin/redis-server \$REDIS_CONF/6001/conf/redis.conf
> \$REDIS_HOME/bin/redis-server \$REDIS_CONF/6002/conf/redis.conf
> EOF
[root@redis-01 redis-6.0.5]# chmod +x /usr/local/redis-cluster/start-redis-cluster.sh
[root@redis-01 redis-6.0.5]# bash /usr/local/redis-cluster/start-redis-cluster.sh
[root@redis-01 redis-6.0.5]# ss -anput | grep redis
tcp LISTEN 0 511 *:6001 *:* users:(("redis-server",pid=36870,fd=6))
tcp LISTEN 0 511 *:6002 *:* users:(("redis-server",pid=36872,fd=6))
tcp LISTEN 0 511 *:16001 *:* users:(("redis-server",pid=36870,fd=9))
tcp LISTEN 0 511 *:16002 *:* users:(("redis-server",pid=36872,fd=9))
4.创建 Redis-Cluster
[root@redis-01 redis-6.0.5]# cd /usr/local/redis-cluster/bin/
[root@redis-01 bin]# ./redis-cli --cluster create 192.168.5.68:6001 192.168.5.68:6002 192.168.5.77:6001 192.168.5.77:6002 192.168.5.88:6001 192.168.5.88:6002 --cluster-replicas 1 -a redis
[root@redis-01 bin]# ps -ef | grep redis
root 19824 1 0 15:58 ? 00:00:00 /usr/local/redis-cluster/bin/redis-server 0.0.0.0:6001 [cluster]
root 19826 1 0 15:58 ? 00:00:00 /usr/local/redis-cluster/bin/redis-server 0.0.0.0:6002 [cluster]
root 19883 19853 0 16:05 pts/0 00:00:00 grep --color=auto redis
5.测试集群状态
[root@redis-01 bin]# ./redis-cli -c -h 192.168.5.68 -p 6001 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.5.68:6001> cluster nodes
18a2bc805852d9d1360471c52c3e65554eac8b90 192.168.5.68:6002@16002 slave 577cc46bd375f693c5340bdec530fd96452c265e 0 1593850355000 5 connected
577cc46bd375f693c5340bdec530fd96452c265e 192.168.5.88:6001@16001 master - 0 1593850355506 5 connected 10923-16383
87d42183008f375ab42b409a80f7a06b65e7670f 192.168.5.77:6002@16002 slave ae4ac8a447bfdb789e3a3e38edebd36478462851 0 1593850355506 4 connected
094b4feeecd1f03aa81a9bb285f5b8790025fe1a 192.168.5.88:6002@16002 slave 27a82314cb79958393140d70cb9b47bfc6a86b8f 0 1593850354596 6 connected
ae4ac8a447bfdb789e3a3e38edebd36478462851 192.168.5.68:6001@16001 myself,master - 0 1593850354000 1 connected 0-5460
27a82314cb79958393140d70cb9b47bfc6a86b8f 192.168.5.77:6001@16001 master - 0 1593850356009 3 connected 5461-10922
[root@redis-01 bin]# ./redis-cli -c -h 192.168.5.68 -p 6001 -a redis # 进入集群模式进行测试,-c 集群模式
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.5.68:6001> set name huihui
-> Redirected to slot [5798] located at 192.168.5.77:6001
OK
[root@redis-01 bin]# ./redis-cli -c -h 192.168.5.68 -p 6002 -a redis
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.5.68:6002> get name
-> Redirected to slot [5798] located at 192.168.5.77:6001
"huihui"
二、Haproxy 部署
1.安装 Haproxy
[root@keep-hapro01 ~]# yum -y install haproxy
[root@keep-hapro02 ~]# yum -y install haproxy
2.配置 Haproxy(主备一致)
[root@keep-hapro01 ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
retries 3
maxconn 3000
contimeout 50000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:1314
stats enable
stats hide-version
stats uri /haproxystatus
stats auth admin:123.com
stats admin if TRUE
listen redis
bind *:6379
mode tcp
balance roundrobin
server redis01 192.168.5.68:6001 weight 1 check inter 1s rise 2 fall 2
server redis02 192.168.5.68:6002 weight 1 check inter 1s rise 2 fall 2
server redis03 192.168.5.77:6001 weight 1 check inter 1s rise 2 fall 2
server redis04 192.168.5.77:6002 weight 1 check inter 1s rise 2 fall 2
server redis05 192.168.5.88:6001 weight 1 check inter 1s rise 2 fall 2
server redis06 192.168.5.88:6002 weight 1 check inter 1s rise 2 fall 2
3.启动 Haproxy
[root@keep-hapro01 ~]# systemctl restart haproxy
[root@keep-hapro02 ~]# systemctl restart haproxy
三、KeepAlived 部署
1.安装 KeepAlived
[root@keep-hapro01 ~]# yum -y install keepalived
[root@keep-hapro02 ~]# yum -y install keepalived
2.配置 KeepAlived
HA-MASTER
[root@keep-hapro01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id hapro-master
}
vrrp_script check_hapro {
script "/etc/keepalived/check_haproxy.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 89
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.100/24
}
[root@keep-hapro01 ~]# vim /etc/keepalived/check_haproxy.sh
#!/usr/bin/bash
/usr/bin/systemctl status haproxy
if [[ $? -ne 0 ]]; then
/usr/bin/killall keepalived
if [[ $? -ne 0 ]]; then
/usr/bin/yum -y install psmisc
/usr/bin/killall keepalived
fi
fi
[root@keep-hapro01 ~]# chmod +x /etc/keepalived/check_haproxy.sh
HA-BACKUP
[root@keep-hapro02 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id hapro-backup
}
vrrp_script check_hapro {
script "/etc/keepalived/check_haproxy.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface ens33
virtual_router_id 89
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.100/24
}
track_script {
check_hapro
}
}
[root@keep-hapro01 ~]# vim /etc/keepalived/check_haproxy.sh
#!/usr/bin/bash
/usr/bin/systemctl status haproxy
if [[ $? -ne 0 ]]; then
/usr/bin/killall keepalived
if [[ $? -ne 0 ]]; then
/usr/bin/yum -y install psmisc
/usr/bin/killall keepalived
fi
fi
[root@keep-hapro01 ~]# chmod +x /etc/keepalived/check_haproxy.sh
3.启动 KeepAlived
[root@keep-hapro01 ~]# systemctl restart keepalived
[root@keep-hapro02 ~]# systemctl restart keepalived
[root@keep-hapro01 ~]# ip a #查看是否生成vip
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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 qlen 1000
link/ether 00:0c:29:5b:2e:fa brd ff:ff:ff:ff:ff:ff
inet 192.168.5.160/24 brd 192.168.5.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.5.100/24 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::452c:d258:f888:12a9/64 scope link
valid_lft forever preferred_lft forever
四、监测集群状态
浏览器登录 http://192.168.5.100:1314/haproxystatus
输入事先定义好的用户名与密码;
本文地址:https://blog.csdn.net/m0_46395355/article/details/107138212
上一篇: Flume-概述,基础架构,组件介绍
推荐阅读
-
英特尔实验单芯片云计算项目手机或变超级计算机
-
荐 使用IDEA搭建一个简单的JavaWeb图书管理项目(详细步骤指导、提供源码)
-
荐 操作系统实验:单处理器系统的进程调度(学习笔记)
-
荐 Android学习中一些小项目(连载中)
-
荐 Linux网络系列--YUM仓库部署与NFS服务(YUM仓库部署、YUM命令使用、NFS共享存储服务讲解及实验)
-
荐 我把Github上最牛b的Java教程和实战项目整合成了一个PDF文档
-
荐 gradle构建多模块(父子项目)项目
-
荐 Python全栈(八)Flask项目实战之5.CMS后台权限验证
-
荐 搭建简单的mybatis项目[maven]
-
荐 四十一、Vue项目上手 | 用户管理系统 实现用户修改和删除功能(完成篇)