Centos 7 Redis 6.2.6 集群搭建及维护
一、环境
系统:CentOS Linux release 7.8.2003 (Core)
redis: redis-6.2.6.tar.gz (官网:https://redis.io/ )
切换到redis 主目录下载 # wget https://download.redis.io/releases/redis-6.2.6.tar.gz
redis 主目录: /usr/local/redis-cluster
在redis主目录中创建配置文件目录:conf 数据目录:data 日志目录:log
# mkdir /usr/local/redis-cluster/{conf,data,log} -p
模式:三主三从 (6台主机,如本博客实例,或3台主机,每台主机通过不同端口区分起两个redis-server,并在redis )
二、安装
1、前期准备
# yum -y install wget vim #安装 wget
# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config #关闭 selinux ,如果有防火墙,关闭防火墙
# cd /usr/local/redis-cluster ; wget https://download.redis.io/releases/redis-6.2.6.tar.gz #切换到redis 主目录,并下载软件
# 修改以下参数
echo '* soft nofile 102400' >> /etc/security/limits.conf echo '* hard nofile 204800' >> /etc/security/limits.conf echo '* soft memlock unlimited' >> /etc/security/limits.conf echo '* hard memlock unlimited' >> /etc/security/limits.conf echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6 echo 1 > /proc/sys/net/ipv6/conf/default/disable_ipv6 echo 'vm.max_map_count=262144' >> /etc/sysctl.conf echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf sysctl -p
2、安装:
# yum -y install gcc
# tar xzf redis-6.2.6.tar.gz
# cd redis-6.2.6
# make MALLOC=libc
# make install PREFIX=/usr/local/redis-cluster
# mkdir /usr/local/redis-cluster/{conf,data,log} -p
3、修改配置文件
# cp redis-6.2.6/redis.conf /usr/local/redis-cluster/conf/
# vim /usr/local/redis-cluster/conf/redis.conf
75行:bind 服务器IP 98行:port 7000 #服务端口 257行:daemonize yes 289行: pidfile /usr/local/redis-cluster/data/redis.pid 302行:logfile /usr/local/redis-cluster/log/redis.log 431行:dbfilename dump.rdb 454行:dir /usr/local/redis-cluster/data/ 484行:masterauth "myredis" #配置密码 901行: requirepass "myredis" #配置密码 1385行:cluster-enabled yes 1399行:cluster-node-timeout 5000
# 将此配置文件同步到其他redis 设备,并修改相应配置
4、配置 redis.service
#vim /etc/systemd/system/redis.service
[Unit] Description=The redis-cluster-server Process Manager After=syslog.target network.target [Service] Type=forking ExecStart=/usr/local/redis-cluster/bin/redis-server /usr/local/redis-cluster/conf/redis.conf PrivateTmp=true [Install] WantedBy=multi-user.target
# 此配置同步到其他设备上。
5、启动服务
# systemctl start redis
# systemctl status redis
# systemctl stot redis
6、配置环境变量
echo 'export REDIS_HOME=/usr/local/redis-cluster' >> /etc/profile echo 'export PATH=$REDIS_HOME/bin:$PATH' >> /etc/profile source /etc/profile
7、启动集群
在六台服务器的redis实例成功启动后,在第一台上执行命令创建集群(注意如果在redis.conf中设置了密码,这里就要加上-a '密码',如果没有密码,就不加-a参数):
# redis-cli --cluster create --cluster-replicas 1 192.168.7.35:7000 192.168.7.48:7000 192.168.7.37:7000 192.168.5.12:7000 192.168.5.27:7000 192.168.5.11:7000 -a myredis
# 前三台为主,后三天为从,如果在提示中确认主从没有问题,输入yes
8、连接集群测试
#redis-cli -h 192.168.7.35 -p 7000 -c #连接集群需要添加 “-c ” 参数
# cluster nodes #查看集群节点
# cluster info #查看集群信息
192.168.7.35:7000> AUTH myredis OK 192.168.7.35:7000> ping PONG 192.168.7.35:7000> AUTH myredis OK 192.168.5.11:7000> AUTH myredis OK 192.168.5.11:7000> set name my_frist_redis OK 192.168.5.11:7000> get name "my_frist_redis" 192.168.5.11:7000>