Redis Cluster,redis集群搭建,redis一键编译安装脚本,redis多实例编译安装
- 1、redis多实例一键编译安装脚本
- 2、redis主从搭建,主备搭建
- 3、redis搭建集群
1、redis多实例一键编译安装脚本
参考编译链接:redis编译 单个redis编译安装:编译安装redis redis配置文件参数介绍:redis配置文件和LVS 新建文件 redis_duoshili.sh
#!/bin/bash
. /etc/init.d/functions
#设置redis版本,密码,安装目录,端口号
VERSION=redis-4.0.3
PASSWORD=123456
INSTALL_DIR=/usr/local/redis
DEFAULT_PORT=6379
#如果只要安装单个redis,设置为0就行
DSL_NUM=1
#如网络质量不好可以自行去官网下载,然后删除下面#wget https://download.redis.io/releases/${VERSION}.tar.gz || { action "源码包下载失败" false;exit; }#这句话
#redis版本下载官网:https://download.redis.io/releases/?_ga=2.164994486.889360285.1614915959-1081687905.1611193410
redis_install() {
yum install -y gcc jemalloc-devel -q || { action "安装依赖失败" false;exit; }
wget https://download.redis.io/releases/${VERSION}.tar.gz || { action "源码包下载失败" false;exit; }
tar xf ${VERSION}.tar.gz
cd ${VERSION}
make PREFIX=${INSTALL_DIR}/${DEFAULT_PORT}/ install && action "redis 编译完成" || { action "redis 编译失败" false;exit; }
mkdir -p ${INSTALL_DIR}/${DEFAULT_PORT}/{etc,log,data,run}
cp redis.conf ${INSTALL_DIR}/${DEFAULT_PORT}/etc/redis-${DEFAULT_PORT}.conf
cp sentinel.conf ${INSTALL_DIR}/${DEFAULT_PORT}/etc/redis-${DEFAULT_PORT}-sentinel.conf
if id redis &>/dev/null;then
action "redis 用户已经存在" false
else
useradd -r -s /sbin/nologin redis
action "redis 用户创建成功"
fi
cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
sysctl -p
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled">>/etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
ln -s ${INSTALL_DIR}/${DEFAULT_PORT}/bin/redis-* /usr/bin/
cp src/redis-trib.rb /usr/bin/
}
dsl_install() {
sed -ri -e "/^bind 127.0.0.1/c bind 0.0.0.0" -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/${DEFAULT_PORT}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/${DEFAULT_PORT}/log/redis-${DEFAULT_PORT}.log" -e "/^dbfilename dump.rdb$/c dbfilename dump-${DEFAULT_PORT}.rdb" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/${DEFAULT_PORT}/run/redis-${DEFAULT_PORT}.pid" -e "/^appendonly no$/c appendonly yes" ${INSTALL_DIR}/${DEFAULT_PORT}/etc/redis-${DEFAULT_PORT}.conf
cat > /usr/lib/systemd/system/redis${DEFAULT_PORT}.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=${INSTALL_DIR}/${DEFAULT_PORT}/bin/redis-server ${INSTALL_DIR}/${DEFAULT_PORT}/etc/redis-${DEFAULT_PORT}.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
if [ "$DSL_NUM" -gt 0 ];then
for i in `seq $DSL_NUM` ;do
let i=${DEFAULT_PORT}+i
mkdir ${INSTALL_DIR}/${i}/{bin,etc,log,data,run} -p
cp ${INSTALL_DIR}/${DEFAULT_PORT}/bin/* ${INSTALL_DIR}/${i}/bin/
sed "s/${DEFAULT_PORT}/${i}/g" ${INSTALL_DIR}/${DEFAULT_PORT}/etc/redis-${DEFAULT_PORT}.conf >${INSTALL_DIR}/${i}/etc/redis-${i}.conf
sed "s/${DEFAULT_PORT}/${i}/g" /usr/lib/systemd/system/redis${DEFAULT_PORT}.service >/usr/lib/systemd/system/redis${i}.service
cp ${INSTALL_DIR}/${DEFAULT_PORT}/etc/redis-${DEFAULT_PORT}-sentinel.conf ${INSTALL_DIR}/${i}/etc/redis-${i}-sentinel.conf
done
chown redis.redis -R ${INSTALL_DIR}
systemctl daemon-reload
[ $? -eq 0 ] &&action "${DSL_NUM}个$VERSION创建成功,请查看$INSTALL_DIR目录" || { action "redis 实例创建失败" false;exit; }
elif [ "$DSL_NUM" -eq 0 ];then
chown redis.redis -R ${INSTALL_DIR}
systemctl daemon-reload
[ $? -eq 0 ] &&action "单个$VERSION创建成功,请查看$INSTALL_DIR目录" || { action "单个redis创建失败" false;exit; }
else
continue;
fi
}
redis_install
dsl_install
bash redis_duoshili.sh 开始脚本
出现Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
是因为网络质量不好,可以忽略。
多实例编译完成
确认结果
tree /usr/local/redis/
默认没启动redis服务和设置开机自启,如有需要修改的redis.conf参数自行修改后启动
启动方式:
systemctl start redis6379
systemctl start redis6380
开启启动:
systemctl enable redis6379
systemctl enable redis6380
2、redis主从搭建,主备搭建
环境:
系统版本 | centos 7.8 |
主 | 192.168.116.130 |
从 | 192.168.116.131 |
- 主从复制特点
- 一个主节点可以有多个从节点
- 一个从节点只能有一个主节点
- 数据流向是单向,只能从主节点流向从节点
- 作主从时,从节点的数据会被清空
- 服务器都安装阿里云yum源和关闭防火墙,关闭selinux
cd /etc/yum.repos.d/
yum install -y wget
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
mv CentOS-Base.repo CentOS-Base.repo.bak
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum makecache
systemctl disable firewalld
sed -i 's/SELINUX=enforcing$/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
- 用上面的脚本安装,默认安装都是主的redis
- 主redis配置
vi /usr/local/redis/6379/etc/redis-6379.conf
requirepass 123456 设置密码,备服务器要用到
- 备redis配置
vi /usr/local/redis/6379/etc/redis-6379.conf
slaveof 192.168.116.130 6379 主服务器redis的IP和端口
masterauth 123456 主服务器的密码
2个节点启动服务
systemctl start redis6379
systemctl enable redis6379
- 测试
主节点查看主从信息
redis-cli -h 192.168.116.130 -a '123456' -p 6379 登录redis
set aa aa 设置key和值
get aa 获取值
从节点测试
redis-cli -h 192.168.116.131 -a '123456' -p 6379 登录redis
info replication
set a1 aa 从节点只有复制主节点的数据的功能,没有新增key和值的功能
3、redis搭建集群
原理:
系统版本 | centos 7.8 |
redis版本 | redis-4.0.3 |
redis集群创建工具 | ruby-2.5.5 |
集群1 | 192.168.116.145:6379, 192.168.116.145:6380 |
集群2 | 192.168.116.146:6379, 192.168.116.146:6380 |
集群3 | 192.168.116.147:6379, 192.168.116.147:6380 |
- 服务器都安装阿里云yum源和关闭防火墙,关闭selinux
cd /etc/yum.repos.d/
yum install -y wget
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
mv CentOS-Base.repo CentOS-Base.repo.bak
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum makecache
systemctl disable firewalld
sed -i 's/SELINUX=enforcing$/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
- 根据上面多实例脚本安装并分配redis
主:192.168.116.145:6379
备:192.168.116.145:6380
主:192.168.116.146:6379
备:192.168.116.146:6380
主:192.168.116.147:6379
备:192.168.116.147:6380
- 找一台主节点修改配置文件,以192.168.116.145为例,其他设备用scp命令复制配置文件
vi /usr/local/redis/6379/etc/redis-6379.conf 主节点配置开启集群
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-require-full-coverage no
vi /usr/local/redis/6380/etc/redis-6380.conf 从节点需要设置主节点密码
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-require-full-coverage no
masterauth 123456
将修改好的redis-6379.conf和redis-6380.conf传送到其他设备的配置目录下
scp /usr/local/redis/6379/etc/redis-6379.conf 192.168.116.146:/usr/local/redis/6379/etc/
scp /usr/local/redis/6379/etc/redis-6379.conf 192.168.116.147:/usr/local/redis/6379/etc/
scp /usr/local/redis/6380/etc/redis-6380.conf 192.168.116.146:/usr/local/redis/6380/etc/
scp /usr/local/redis/6380/etc/redis-6380.conf 192.168.116.147:/usr/local/redis/6380/etc/
- 三个节点都开启redis服务,设置开机启动服务
systemctl start redis6379
systemctl start redis6380
systemctl enable redis6379
systemctl enable redis6380
ss -tnl
- 安装ruby
在任意一台主节点上安装ruby,作为集群主控制器,以192.168.116.145为例子:
yum install -y gcc zlib-devel openssl-devel rubygems 安装依赖包
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
tar xf ruby-2.5.5.tar.gz
cd ruby-2.5.5
./configure
make
make install
exit 安装完成后,变量需要重新登录生效,所以需要退出终端,重新登录
gem install redis
vi /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.4.0/lib/redis/client.rb 修改密码,默认为空
修改下面这行
password: "123456",
- 创建集群
redis-trib.rb create --replicas 1 192.168.116.145:6379 192.168.116.146:6379 192.168.116.147:6379 192.168.116.145:6380 192.168.116.146:6380 192.168.116.147:6380
会自动按照顺序进行分配磁盘空间和主备情况
确认后,输入yes,开启自动集群配置
- 确认集群和主从配置成功
redis-cli -h 127.0.0.1 -p 6379 -a 123456 进入redis配置
cluster nodes 查看全部集群节点
info replication 主从关系确认
redis-trib.rb check 192.168.116.145:6379 查看槽位分布情况