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

    技术2025-11-04  23

    redis集群部署

    服务架构

    所有节点操作

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

    2.修改系统参数

    [root@hyf-redis01 ~]# cat >> /etc/security/limits.conf << EOF

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

    3.环境设置:

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

    [root@hyf-redis01 ~] # echo "net.core.somaxconn = 32767" >> /etc/sysctl.conf #OOM 相关: vm.overcommit_memory [root@hyf-redis01 ~] # echo "vm.overcommit_memory=1" >> /etc/sysctl.conf # 查看修改的内容 [root@hyf-redis01 ~] # sysctl -p # 开启内核的 “Transparent Huge Pages (THP)” 特性 [root@hyf-redis01 ~] # echo never > /sys/kernel/mm/transparent_hugepage/enabled # 临时⽣效 [root@hyf-redis01 ~] # echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local [root@hyf-redis01 ~] # chmod +x /etc/rc.local # 永久⽣效⽅式

    4.开始部署redis

    安装gcc [root@hyf-redis01 ~] # yum -y install gcc glibc glibc-kernheaders glibc-common glibc-devel make 升级gcc [root@hyf-redis01 ~] # yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils [root@hyf-redis01 ~] # yum -y install centos-release-scl [root@hyf-redis01 ~] # scl enable devtoolset-9 bash

    设置为永久升级

    [root@hyf-redis01 ~] # echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

    redis安装

    [root@hyf-redis01 ~] # cd /usr/local/src [root@hyf-redis01 src] # wget http://download.redis.io/releases/redis- 6.0.5.tar.gz [root@hyf-redis01 src] # tar -xzf redis-6.0.5.tar.gz [root@hyf-redis01 src] # cd redis-6.0.5/ [root@hyf-redis01 redis-6.0.5] # make [root@hyf-redis01 redis-6.0.5] # make install PREFIX=/usr/local/redis-cluster

    5.创建实例⽬录

    [root@hyf-redis01 ~] # mkdir -p /redis/{6001,6002}/{conf,data,log} [root@hyf-redis01 ~] # cd /redis/6001/conf/ [root@hyf-redis01 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配置⽂件

    [root@hyf-redis01 conf] # sed 's/6001/6002/g' redis.conf > /redis/6002/conf/redis.conf

    7.编辑启动redis脚本

    [root@hyf-redis01 ~] # 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@hyf-redis01 ~] # chmod +x /usr/local/redis-cluster/start-redis-cluster.sh

    8.启动redis并查看

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

    haproxy部署

    部署主从两台haproxy服务器

    安装haproxy

    [root@hyf-redis01 ~] # yum install -y haproxy.x86_64

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

    [root@hyf-redis01 ~] # cp -rf /etc/haproxy/haproxy.cfg{,.bak} [root@hyf-redis01 ~] # vim /etc/haproxy/haproxy.cfg [root@hyf-redis01 ~] # 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

    [root@hyf-redis02 ~] # yum -y install keepalived

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

    [root@hyf-redis01 ~] # cp /etc/keepalived/keepalived.conf{,.bak} [root@hyf-redis01 ~] # vim /etc/keepalived/keepalived.conf [root@hyf-redis01 ~] # 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地址

    [root@hyf-redis01 ~] # ip a

     

    测试

    测试访问vip

     

     

    Processed: 0.062, SQL: 11