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

redis集群部署

服务架构

所有节点操作

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

 

2.修改系统参数

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

> * soft nofile 102400
> * hard nofile 102400
> EOF

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 # 永久⽣效⽅式

4.开始部署redis

安装 gcc
[[email protected] ~] # yum -y install gcc glibc glibc-kernheaders glibc-common
glibc-devel make
升级 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

设置为永久升级

[[email protected] ~] # echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

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

5.创建实例⽬录

[[email protected] ~] # mkdir -p /redis/{6001,6002}/{conf,data,log}
[[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

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

8.启动redis并查看

[[email protected] ~] # bash /usr/local/redis-cluster/start-redis-cluster.sh
[[email protected] ~] # ss -anput | grep redis
其他节点以此部署
 

haproxy部署

部署主从两台haproxy服务器

安装haproxy

[[email protected] ~] # yum install -y haproxy.x86_64

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

[[email protected] ~] # cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[[email protected] ~] # vim /etc/haproxy/haproxy.cfg
[[email protected] ~] # systemctl restart haproxy.service
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 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

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
 

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

登陆查看代理服务信息

keepalived部署

部署主从两台keepalived服务

安装keepalived

[[email protected] ~] # yum -y install keepalived

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

[[email protected] ~] # cp /etc/keepalived/keepalived.conf{,.bak}
[[email protected] ~] # vim /etc/keepalived/keepalived.conf
[[email protected] ~] # systemctl restart keepalived.service

#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

测试

测试访问vip