redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

redis集群部署

服务架构

redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

所有节点操作

1.关闭防⽕墙、修改主机名、配置本地解析

 

redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

2.修改系统参数

[[email protected] ~]# cat >> /etc/security/limits.conf << EOF

> * soft nofile 102400
> * hard nofile 102400
> EOF
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

3.环境设置:

#设置TCP队列监听队列⼤⼩

[[email protected] ~]# echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf
#OOM相关:vm.overcommit_memory
[[email protected] ~]# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
#查看修改的内容
[[email protected] ~]# sysctl -p
#开启内核的“Transparent Huge Pages (THP)”特性
[[email protected] ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
#临时⽣效
[[email protected] ~]# echo "echo never >
/sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
[[email protected] ~]# chmod +x /etc/rc.local #永久⽣效⽅式
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

4.开始部署redis

安装gcc
[[email protected] ~]# yum -y install gcc glibc glibc-kernheaders glibc-common
glibc-devel make
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊
升级gcc
[[email protected] ~]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++
devtoolset-9-binutils
[[email protected] ~]# yum -y install centos-release-scl
[[email protected] ~]# scl enable devtoolset-9 bash
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

设置为永久升级

[[email protected] ~]# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

redis安装

[[email protected] ~]# cd /usr/local/src
[[email protected] src]# wget http://download.redis.io/releases/redis-
6.0.5.tar.gz
[[email protected] src]# tar -xzf redis-6.0.5.tar.gz
[[email protected] src]# cd redis-6.0.5/
[[email protected] redis-6.0.5]# make
[[email protected] redis-6.0.5]# make install PREFIX=/usr/local/redis-cluster
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

5.创建实例⽬录

[[email protected] ~]# mkdir -p /redis/{6001,6002}/{conf,data,log}
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊
[[email protected] ~]# cd /redis/6001/conf/
[[email protected] 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
EOF

6.配置redis服务6002配置⽂件

[[email protected] conf]# sed 's/6001/6002/g' redis.conf >
/redis/6002/conf/redis.conf
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

7.编辑启动redis脚本

[[email protected] ~]# 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
[[email protected] ~]# chmod +x /usr/local/redis-cluster/start-redis-cluster.sh
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

8.启动redis并查看

[[email protected] ~]# bash /usr/local/redis-cluster/start-redis-cluster.sh
[[email protected] ~]# ss -anput | grep redis
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊
其他节点以此部署
 

haproxy部署

部署主从两台haproxy服务器

安装haproxy

[[email protected] ~]# yum install -y haproxy.x86_64
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

设置haproxy配置⽂件并重启服务

[[email protected] ~]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
[[email protected] ~]# systemctl restart haproxy.service
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊
Haproxy-1配置⽂件内容:
global
log 127.0.0.1 local1
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 50000listen stats
bind *:1314
stats enable
stats hide-version
stats uri /haproxystats
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
frontend web
option httplog
option http-server-close
option forwardfor except 127.0.0.0/8
#option redispatch
mode http
bind *:80
default_backend httpservers
backend httpservers
balance roundrobin
server redis01 172.16.124.155:6001 check maxconn 2000
server redis01 172.16.124.155:6002 check maxconn 2000
server redis02 172.16.124.156:6003 check maxconn 2000
server redis02 172.16.124.156:6004 check maxconn 2000
server redis03 172.16.124.157:6005 check maxconn 2000
server redis03 172.16.124.157:6006 check maxconn 2000

Haproxy02配置⽂件内容:

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 /haproxystats
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
frontend web
option httplog
option http-server-close
option forwardfor except 127.0.0.0/8
#option redispatch
mode http
bind *:80
default_backend httpservers
backend httpservers
balance roundrobin
server redis01 172.16.124.155:6001 check maxconn 2000
server redis01 172.16.124.155:6002 check maxconn 2000
server redis02 172.16.124.156:6003 check maxconn 2000
server redis02 172.16.124.156:6004 check maxconn 2000
server redis03 172.16.124.157:6005 check maxconn 2000
server redis03 172.16.124.157:6006 check maxconn 2000
 

浏览器访问测试登陆查看代理服务信息

redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

登陆查看代理服务信息

redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

keepalived部署

部署主从两台keepalived服务

安装keepalived

[[email protected] ~]# yum -y install keepalived
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

配置keepalived配置⽂件并重启服务

[[email protected] ~]# cp /etc/keepalived/keepalived.conf{,.bak}
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
[[email protected] ~]# systemctl restart keepalived.service
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

#MASTER配置文件内容:

! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_instance VI_1 {
state MASTER
nopreempt
interface eth0
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.124.200
}
}

#BACKUP配置文件内容:

! Configuration File for keepalived

global_defs { router_id director1
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.124.200
}
}

查看IP地址

[[email protected] ~]# ip a
redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊

测试

测试访问vip

redis集群+haproxy代理+keepalived高可用部署-想学吗 我教你啊