准备环境
从官方网站下载最新的稳定版本
$ wget https://download.redis.io/releases/redis-6.2.4.tar.gz
$ tar xzf redis-6.2.4.tar.gz
$ cd redis-6.2.4
$ make
make test使用tcl的版本低,就更新下
apt-get install tcl
执行make test,成功后就可以运行redis了
$ echo "PATH=/home/user/redis/redis-6.2.4/src:$PATH" >> ~/.zshrc
$ source ~/.zshrc
$ redis-server
$ redis-server redis6379.conf
30438:C 17 Jun 2021 12:31:35.231 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30438:C 17 Jun 2021 12:31:35.231 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=30438, just started
30438:C 17 Jun 2021 12:31:35.231 # Configuration loaded
30438:M 17 Jun 2021 12:31:35.232 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30438:M 17 Jun 2021 12:31:35.232 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 30438
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
30438:M 17 Jun 2021 12:31:35.232 # Server initialized
30438:M 17 Jun 2021 12:31:35.232 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
30438:M 17 Jun 2021 12:31:35.232 * Loading RDB produced by version 6.2.4
Redis主从
Redis的主从设置只要执行slaveof ip port 就可以了,当然也可以配置文件设置
使用下载源文件的配置redis.conf,拷贝两个文件做主从
修改配置文件的以下内容
port 6379
pidfile /home/user/redis/pid/6379.pid
dir /home/user/redis/lib/6379/
保持两个文件中的端口,配置文件,数据文件,日志等不同就可以,默认日志配置是标准输出可以不管。这里设置6379和6380两个配置文件。
$ redis-server redis6379.conf
30438:C 17 Jun 2021 12:31:35.231 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30438:C 17 Jun 2021 12:31:35.231 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=30438, just started
30438:C 17 Jun 2021 12:31:35.231 # Configuration loaded
30438:M 17 Jun 2021 12:31:35.232 * Increased maximum number of open files to 10032 (it was originally set to 1024).
30438:M 17 Jun 2021 12:31:35.232 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 30438
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
30438:M 17 Jun 2021 12:31:35.232 # Server initialized
30438:M 17 Jun 2021 12:31:35.232 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
30438:M 17 Jun 2021 12:31:35.232 * Loading RDB produced by version 6.2.4
两个服务都启动后在另外预设为从服务的客户端中执行命令
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380> info
# Server
... //这里省略一堆输出
...
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
看到上面的role已经改为slave
然后在mater下看下添加一些key在从服务器上是否有影响
127.0.0.1:6379> set port 6379
OK
在从服务器下执行get命令
127.0.0.1:6380> get port
"6379"
这样主从设置就ok了。
Redis Cluster设置
根据官方文档要求修改配置文件,并准备至少3个实例,最好是6个实例做到主从配置,这里只是演示只设置3个实例的效果
配置文件改为
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
appendonly yes
其他两个实例的配置文件,修改端口和 cluster-config-file配置为nodes-XXX.conf
XXX为端口号, 同时设置好每个实例的日志和数据存储目录
启动Redis-Server,这里就不展示每一个的启动画面了
$ redis-server conf/redis6379.conf
4909:C 18 Jun 2021 15:45:21.701 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4909:C 18 Jun 2021 15:45:21.701 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=4909, just started
4909:C 18 Jun 2021 15:45:21.701 # Configuration loaded
4909:M 18 Jun 2021 15:45:21.702 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4909:M 18 Jun 2021 15:45:21.702 * monotonic clock: POSIX clock_gettime
4909:M 18 Jun 2021 15:45:21.702 * No cluster configuration found, I'm 7f4d249cf670cd9015af61964ed45c720c01f1ae
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in cluster mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 4909
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
4909:M 18 Jun 2021 15:45:21.705 # Server initialized
4909:M 18 Jun 2021 15:45:21.705 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4909:M 18 Jun 2021 15:45:21.706 * Ready to accept connections
4909:M 18 Jun 2021 15:52:23.913 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
4909:M 18 Jun 2021 15:52:23.972 # IP address for this node updated to 127.0.0.1
4909:M 18 Jun 2021 15:52:28.882 # Cluster state changed: ok
$ redis-server conf/redis6380.conf
$ redis-server conf/redis6381.conf
可以从启动输出日志里面看到cluster的相关信息
执行Cluster启动设置
文档说明是redis-trib.rb脚本创建启动,但是执行后提示使用redis-cli执行。
$ redis-trib.rb create --replicas 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381
WARNING: redis-trib.rb is not longer available!
You should use redis-cli instead.
All commands and features belonging to redis-trib.rb have been moved
to redis-cli.
In order to use them you should call redis-cli with the --cluster
option followed by the subcommand name, arguments and options.
Use the following syntax:
redis-cli --cluster SUBCOMMAND [ARGUMENTS] [OPTIONS]
Example:
redis-cli --cluster create 127.0.0.1:6380 127.0.0.1:6381 --cluster-replicas 127.0.0.1:6379
To get help about all subcommands, type:
redis-cli --cluster help
正确的cluster建立命令
$ redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 7f4d249cf670cd9015af61964ed45c720c01f1ae 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
M: 1e2b502694404174ffe002eb153c371805acb014 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
M: f79d6d16b940e706f08c416f26fa52f8a75c2273 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
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 127.0.0.1:6379)
M: 7f4d249cf670cd9015af61964ed45c720c01f1ae 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
M: 1e2b502694404174ffe002eb153c371805acb014 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
M: f79d6d16b940e706f08c416f26fa52f8a75c2273 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
中间有提示对master和槽位的分配,没有问题就输入yes就可以,后面会自动生成cluster 配置文件。
[OK] All 16384 slots covered.
最后输出这个就表示每个槽位都有一个节点在处理,一切正常。
下面来验证下效果:
通过redis-cli连接几个节点看看
$ redis-cli
127.0.0.1:6379> set key 1
(error) MOVED 12539 127.0.0.1:6381
127.0.0.1:6379> info
# Server
redis_version:6.2.4
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:3597310f3a270994
redis_mode:cluster
os:Linux 5.4.0-74-generic x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:c11-builtin
gcc_version:7.5.0
process_id:4909
process_supervised:no
run_id:22233f73aeb4f225ee7cc3b8210ecf4bab525f46
tcp_port:6379
server_time_usec:1624003248141743
uptime_in_seconds:927
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:13390512
executable:/home/mark/redis/redis-server
config_file:/home/mark/redis/conf/redis6379.conf
io_threads_active:0
...
...
...
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:f259cb2f37d380de34a094e648b4e48b3dd48d43
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:0.682022
used_cpu_user:0.815773
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000
used_cpu_sys_main_thread:0.675267
used_cpu_user_main_thread:0.819111
# Modules
# Errorstats
errorstat_ERR:count=2
errorstat_MOVED:count=1
# Cluster
cluster_enabled:1
set key1234567 1
OK
127.0.0.1:6379> keys *
1) "key1234567"
可以看到目前6379端口的服务已经在cluster下了,通过set 命令发现使用key为参数会被要求移到6381的服务节点,set 使用 key1234567参数,会被保存在当前节点.
我们再通过 redis-cli -c -p 6379 命令测试下,-c 是使用cluster mode执行
$ redis-cli -c -p 6379
127.0.0.1:6379> set key 2
-> Redirected to slot [12539] located at 127.0.0.1:6381
OK
127.0.0.1:6381> exit
$ redis-cli -c -p 6381
127.0.0.1:6381> keys *
1) "key"
127.0.0.1:6381> get key
"2"
127.0.0.1:6381>
现在看起来是ok的。
引用: