hash 网站favicon图标制作 Scala dedecms VMware oracle pdf mvvm bootstrap后台管理系统模板 mysql当前时间减一天 oracle查看数据库状态 monkey安装 js对象添加元素 mysql分页查询sql语句 java手机验证码 pyhton中异常和模块 Navicat pythonset python中文 java正则匹配 java中的tostring java平台 java判断语句 java求阶乘 java调用方法 java定义 java连接sql 计算机操作系统第四版 笔记本外接显示器好吗 c语言程序100例 美国地址生成器 mathcad15 战斗的召唤 大势至usb监控 批量插入数据 如何用ai设计字体 ps反向选择的快捷键 cdlinux教程 思源黑体 ps光照效果
当前位置: 首页 > 学习教程  > 编程语言

KeepAlived+Redis+Haproxy实现主从热备、负载均衡、秒级切换

2020/9/19 14:21:25 文章标签:

redis

修改主机名
[root@localhost ~]# hostnamectl --static set-hostname redis01
[root@localhost ~]# hostnamectl --static set-hostname redis02
[root@localhost ~]# hostnamectl --static set-hostname redis03

修改hosts
[root@redis01 ~]# cat >> /etc/hosts <<-EOF
192.168.49.168 redis01
192.168.49.169 redis02
192.168.49.146 redis03
EOF

修改最大可打开文件数
[root@redis01 ~]#cat >> /etc/security/limits.conf << EOF

  • soft nofile 102400
  • hard nofile 102400
    EOF

tcp监听队列大小
[root@redis01 ~]# echo “net.core.somaxconn = 32767” >> /etc/sysctl.conf
[root@redis01 ~]# sysctl -p

OOM相关
[root@redis01 ~]# echo “vm.overcommit_memory=1” >> /etc/sysctl.conf
[root@redis01 ~]# sysctl -p

开启内核的“Transparent Huge Pages (THP)”特性
[root@redis01 ~]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@redis01 ~]# echo “echo never > /sys/kernel/mm/transparent_hugepage/enabled” >> /etc/rc.local
####/etc/rc.local 开机后运行
[root@redis01 ~]# chmod +x /etc/rc.local

安装 redis 并配置 redis-cluster
安装gcc套装
[root@redis01 ~]# yum -y install gcc glibc glibc-kernheaders glibc-common glibc-devel make
升级gcc
[root@redis01 ~]# yum -y install centos-release-scl
[root@redis01 ~]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@redis01 ~]# scl enable devtoolset-9 bash
设置永久升级
[root@redis01 ~]# echo “source /opt/rh/devtoolset-9/enable” >>/etc/profile
redis01 安装
[root@redis01 ~]# cd /usr/local/src
[root@redis01 ~]# wget http://download.redis.io/releases/redis-6.0.5.tar.gz
[root@redis01 ~]# tar -zxvf redis-6.0.5.tar.gz
[root@redis01 ~]# cd redis-6.0.5/
[root@redis01 ~]# make
[root@redis01 ~]# make install PREFIX=/usr/local/redis-cluster

创建实例目录
mkdir -p /redis/{6001,6002}/{conf,data,log}
配置
[root@redis01 ~]#cd /redis/6001/conf/
[root@redis01 conf]# cat >> redis.conf << EOF
bind 0.0.0.0
protected-mode no
port 6001
dir /redis/6001/data
cluster-enabled yes
cluster-config-file /redis/6001/conf/nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
requirepass redis
pidfile /redis/6001/redis.pid
logfile /redis/6001/log/redis.log
EOF

redis01 6002 配置文件
[root@redis01 conf]# sed ‘s/6001/6002/g’ redis.conf > /redis/6002/conf/redis.conf

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C29DOdva-1600495686052)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\image-20200919095755123.png)]

启动redis
[root@redis01 ~]# bash /usr/local/redis-cluster/start-redis-cluster.sh

查看 redis 启动状态
[root@redis01 ~]# ss -anput | grep redis

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HYNtnch0-1600495686057)(C:\Users\dell\AppData\Roaming\Typora\typora-user-images\image-20200919095927682.png)]

查看redis进程启动状态
[root@redis01 ~]# ps -ef | grep redis

在这里插入图片描述

创建redis cluster
[root@redis01 bin]# cd /usr/local/redis-cluster/bin
[root@redis01 bin]# ./redis-cli --cluster create 192.168.49.168:6001 192.168.49.168:6002 192.168.49.169:6001 192.168.49.169:6002 192.168.49.146:6001 192.168.49.146:6002 --cluster-replicas 1
在这里插入图片描述

设置命令执行工具(软链接)
root@redis01 ~]# ln -s /usr/local/redis-cluster/bin/redis-cli /bin/redis-cli
[root@redis65 bin]# redis-cli -c -p 6001
查看集群中的节点
在这里插入图片描述

验证集群

在这里插入图片描述
在这里插入图片描述

redis cluster 集群加上认证
登录到 redis 节点设置登录验证
[root@redis01 ~]# redis-cli -h 192.168.152.133 -p 6001 -c
192.168.152.133:6001> config set masterauth redispws
OK
192.168.152.133:6001> config set requirepass redispws
OK
192.168.152.133:6001> auth redispws
OK
192.168.152.133:6001> config rewrite
OK

通过认证登录redis
[root@redis01 ~]# redis-cli -h 192.168.152.133 -p 6001 -c -a ‘redis’

haproxy

[root@haproxy-lb1 ~]# yum -y install haproxy
[root@haproxy-lb1 ~]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[root@haproxy-lb1 ~]# sed -i -r ‘/1*#/d;/^$/d’ /etc/haproxy/haproxy.cfg
[root@haproxy-lb1 ~]# 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
mode http
bind *:1314
stats enable
stats hide-version
stats uri /haproxy
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE

listen redis
bind *:6379
mode tcp
balance roundrobin
server redis1 192.168.49.168:6001 check maxconn 2000
server redis2 192.168.49.168:6002 check maxconn 2000
server redis3 192.168.49.169:6001 check maxconn 2000
server redis4 192.168.49.169:6002 check maxconn 2000
server redis5 192.168.49.146:6001 check maxconn 2000
server redis6 192.168.49.146:6002 check maxconn 2000

systemctl start haproxy
systemctl status haproxy
在这里插入图片描述
在这里插入图片描述

配置日志
创建日志目录
[root@haproxy-lb1 ~]# mkdir /var/log/haproxy
[root@haproxy-lb1 ~]# chmod a+w /var/log/haproxy
开启 rsyslog 记录 haproxy 日志:
[root@haproxy-lb1 ~]# vim /etc/rsyslog.conf

Provides UDP syslog reception

$ModLoad imudp
$UDPServerRun 514

Provides TCP syslog reception

$ModLoad imtcp
$InputTCPServerRun 514

haproxy log

local0.* /var/log/haproxy/haproxy.log # 添加

修改 rsyslog 配置文件
[root@haproxy-lb1 ~]# vim /etc/sysconfig/rsyslog

Options for rsyslogd

Syslogd options are deprecated since rsyslog v3.

If you want to use them, switch to compatibility mode 2 by “-c 2”

See rsyslogd(8) for more details

SYSLOGD_OPTIONS="-r -m 0 -c 2"

重启 rsyslog 使日志配置生效
[root@haproxy-lb1 ~]# systemctl restart rsyslog

检查 haproxy 日志配置是否有效
[root@haproxy-lb1 ~]# haproxy -c -f /etc/haproxy/haproxy.cfg


Configuration file is valid # 有效,警告可以处理,一般都是 log类型,option tcplog, http的option forwardfor启动haproxy:

重启 haproxy
[root@haproxy-lb1 ~]# systemctl start haproxy.service

访问haproxy 查询日志
cat /var/log/haproxy/haproxy.log

keepalived

安装
[root@haproxy-lb1 ~]# yum -y install keepalived

配置主
[root@haproxy-lb1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
router_id director1
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.49.200/24
}
}
配置备
! Configuration File for keepalived

global_defs {
router_id director2
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.49.200/24
}
}

启动KeepAlived(主备均启动)
[root@haproxy-lb1 ~]# systemctl enable keepalived
[root@haproxy-lb1 ~]# systemctl start keepalived
[root@haproxy-lb1 ~]# ip addr
在这里插入图片描述


  1. ↩︎


本文链接: http://www.dtmao.cc/news_show_200097.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?