Redis 单点部署
安装依赖
下载redis源码包
离线部署时将源码包提前下载好上传至需要部署的服务器上即可。
下载方式1:离线下载redis地址:redis-5.0.4.tar.gz
下载方式2:执行👇命令可以直接下载redis源码包
curl -O http://download.redis.io/releases/redis-5.0.4.tar.gz
|
如果没有安装curl,需要先安装 curl sudo yum install -y curl
如果是离线的服务器可以参考我的另一篇文章《Centos7 初步操作指北》 可以先配置离线yum源然后执行👆安装curl的命令。
上传源码包并解压
tar -zxvf /***/redis-5.0.4.tar.gz
cd /***/redis-5.0.4
|
编译并安装到指定目录
make
make PREFIX=/usr/local/redis install
make && make PREFIX=/usr/local/redis install
|
指定配置文件
mkdir -p /usr/local/redis/config
cp /***/redis-5.0.4/redis.conf /usr/local/redis/config/
vi /usr/local/redis/config/redis.conf
daemonize yes
requirepass 123456
|
设置开机自启
创建redis.service文件:vi /etc/systemd/system/redis.service
添加如下内容:
[Unit] Description=Redis After=network.target
[Service] Type=forking ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/config/redis.conf ExecStop=/usr/local/redis/bin/redis-server -s stop PrivateTmp=true User=root Group=root
[Install] WantedBy=multi-user.target
|
常用命令如下:
- 启动redis:systemctl start redis
- 关闭redis:systemctl stop redis
- 设置开机自启:systemctl enable redis
- 关闭开机自启:systemctl disable redis
- 查看运行状态:systemctl status redis
Redis 集群部署
用三台虚拟机模拟6个节点,一台虚机2个节点,创建出3主3从(3 master 3 slave)
虚拟机系统:centos7,IP:10.0.0.77,10.0.0.78,10.0.0.79
yum install -y gcc-c++
curl -O http://download.redis.io/releases/redis-5.0.4.tar.gz
tar -zxvf redis-5.0.4.tar.gz cd redis-5.0.4
make && make install PREFIX=/usr/local/redis install
cp /usr/local/redis/bin/* /usr/bin
mkdir /usr/local/redis/redis-cluster
mkdir /usr/local/redis/redis-cluster/7001 /usr/local/redis/redis-cluster/7002 cp redis.conf /usr/local/redis/redis-cluster/7001 cp redis.conf /usr/local/redis/redis-cluster/7002
vi /usr/local/redis/redis-cluster/7001/redis.conf
port 7001 bind 10.0.0.77 daemonize yes protected-mode no pidfile /var/run/redis_7001.pid cluster-enabled yes cluster-config-file nodes-7001.conf cluster-node-timeout 15000 appendonly yes masterauth 123456 requirepass 123456
vi /etc/systemd/system/redis-cluster7001.service
[Unit] Description=redis-cluster After=network.target [Service] Type=forking ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis-cluster/7001/redis.conf ExecStop=/usr/local/redis/bin/redis-cli -c -h 10.0.0.77 -p 7001 shutdown PrivateTmp=true User=root Group=root [Install] WantedBy=multi-user.target
systemctl enable --now redis-cluster7001
redis-cli --cluster create 10.0.0.77:7001 10.0.0.77:7002 10.0.0.78:7003 10.0.0.78:7004 10.0.0.79:7005 10.0.0.79:7006 --cluster-replicas 1 -a 123456
>>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 10.0.0.78:7004 to 10.0.0.77:7001 Adding replica 10.0.0.79:7006 to 10.0.0.78:7003 Adding replica 10.0.0.77:7002 to 10.0.0.79:7005 M: d28293bf7b1bdcdedd6f86ef640dbf02eb1bc3c8 10.0.0.77:7001 slots:[0-5460] (5461 slots) master S: 546f9aef447edc559e1af236e8c46b1622b9f450 10.0.0.77:7002 replicates 8157d10d78d5f23bc6c12900eb20b04d4c389bb6 M: c8cb73195d6cb4b1d01f5f2abd743240317f0013 10.0.0.78:7003 slots:[5461-10922] (5462 slots) master S: 58fbce09f2e686a1f66b58f1521b9a39908cb87a 10.0.0.78:7004 replicates d28293bf7b1bdcdedd6f86ef640dbf02eb1bc3c8 M: 8157d10d78d5f23bc6c12900eb20b04d4c389bb6 10.0.0.79:7005 slots:[10923-16383] (5461 slots) master S: 6b9fc42e2737dcb85d84e2744d1fa00b9bbe5a57 10.0.0.79:7006 replicates c8cb73195d6cb4b1d01f5f2abd743240317f0013 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join .... >>> Performing Cluster Check (using node 10.0.0.77:7001) M: d28293bf7b1bdcdedd6f86ef640dbf02eb1bc3c8 10.0.0.77:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 546f9aef447edc559e1af236e8c46b1622b9f450 10.0.0.77:7002 slots: (0 slots) slave replicates 8157d10d78d5f23bc6c12900eb20b04d4c389bb6 M: c8cb73195d6cb4b1d01f5f2abd743240317f0013 10.0.0.78:7003 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 8157d10d78d5f23bc6c12900eb20b04d4c389bb6 10.0.0.79:7005 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 58fbce09f2e686a1f66b58f1521b9a39908cb87a 10.0.0.78:7004 slots: (0 slots) slave replicates d28293bf7b1bdcdedd6f86ef640dbf02eb1bc3c8 S: 6b9fc42e2737dcb85d84e2744d1fa00b9bbe5a57 10.0.0.79:7006 slots: (0 slots) slave replicates c8cb73195d6cb4b1d01f5f2abd743240317f0013 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
redis-cli -c -h 10.0.0.78 -p 7003
cluster info
cluster nodes
10.0.0.78:7003> set hello world -> Redirected to slot [866] located at 10.0.0.77:7001 OK
redis-cli -c -h 10.0.0.79 -p 7005 10.0.0.79:7005> get hello -> Redirected to slot [866] located at 10.0.0.77:7001 "world"
|
设置密码
$ redis-cli -h 10.0.0.77 -p 7001 -c 10.0.0.77:7001> config set requirepass 'password' // 设置密码 10.0.0.77:7001> config set masterauth 'password' // 设置连接密码 10.0.0.77:7001> config rewrite // 把config set 操作写入配置文件中
|
新增删除节点
./redis-cli --cluster add-node 10.0.0.80:7007 10.0.0.80:7008
redis-cli --cluster add-node {新节点IP}:{新节点端口} {任意集群节点IP}:{对应端口}
./redis-cli --cluster check 10.0.0.77:7001
./redis-cli --cluster del-node 10.0.0.77:7001 d9da56a8f068d5529a7771addf586d14e14ca888
|
masterauth和requirepass
requreipass和master的作用?
masterauth作用:主要是针对master对应的slave节点设置的,在slave节点数据同步的时候用到。
requirepass作用:对登录权限做限制,redis每个节点的requirepass可以是独立、不同的。
是否只设置requirepass就可以?masterauth是否需要同步设置?
redis启用密码认证一定要requirepass和masterauth同时设置。
如果主节点设置了requirepass登录验证,在主从切换,slave在和master做数据同步的时候首先需要发送一个ping的消息给主节点判断主节点是否存活,再监听主节点的端口是否联通,发送数据同步等都会用到master的登录密码,否则无法登录,log会出现响应的报错。也就是说slave的masterauth和master的requirepass是对应的,所以建议redis启用密码时将各个节点的masterauth和requirepass设置为相同的密码,降低运维成本。当然设置为不同也是可以的,注意slave节点masterauth和master节点requirepass的对应关系就行。