redis多实例搭建

一、安装redis单节点
官网下载二进制源码包安装,包里面为预编译过的,解压后可以直接二进制安装:

[root@localhost ~]# tar -zxf redis
[root@localhost ~]# cd redis
[root@localhost ~]# make && make install


接着进入redis目录下的src目录,src目录下这些文件作用如下
redis-server:Redis服务器的daemon启动程序
redis-cli:Redis命令行操作工具.你也可以用telnet根据其纯文本协议来操作
redis-benchmark:Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能.
redis-stat:Redis状态检测工具,可以检测Redis当前状态参数及延迟状况(部分版本没有此文件)

修改配置文件,一般需要把daemonize no 改为 daemonize yes,bind后的IP改为0.0.0.0,其他的看需要修改:

 

启动redis:

redis-server redis.conf

二、配置redis单机多实例
配置redis集群。

redis安装完成后,创建redis_cluster目录,并分别创建6379/7000/7001/7002/7003节点目录:

[root@localhost ~]#cd /usr/local/
[root@localhost ~]#mkdir -p redis-cluster/{6379,7000,7001,7002,7003,7004}

在每个节点目录里创建redis.conf配置文件,以6379节点为例:

port 6379
bind 0.0.0.0
protected-mode no
daemonize yes
pidfile /usr/local/redis-cluster/6379/redis_6379.pid
cluster-enabled yes
cluster-config-file nodes_6379.conf
cluster-node-timeout 15000
appendonly yes

其余各节点只需修改6379为各自端口号即可。
启动redis主节点和其余节点:

[root@localhost redis-cluster]# cd 6379
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7000/
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7001/
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7002/
[root@localhost redis-cluster]# redis-server redis.conf
[root@localhost redis-cluster]# cd ../7003/
[root@localhost redis-cluster]# redis-server redis.conf

 组件集群

redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1

 

 

 

[testapp@k8s-node1 ~/redis/redis_cluster]$ cat flushCluster.sh 
##判断参数
if [ $# -eq 0 ]
then echo -e "usage: sh $0 ip \nexit!!" exit
fi

##删除文件
rm /home/ap/testapp/redis/redis_cluster/appendonly.aof /home/ap/testapp/redis/redis_cluster/dump.rdb /home/ap/testapp/redis/redis_cluster/nodes-*.conf
echo "remove appendonly.aof dump.rdb nodes-*.conf,but will be re-created once restart done"


##清除redsi库

for port in 6380 6381 6382
do 

##清除redsi库
/home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -h $1 -p ${port} <2>/dev/null
flushdb
cluster reset
exit
/
EOF

echo "reflushdb redis $1:$port finished"
##关闭服务
/home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -p ${port} shutdown
echo "shut down redis service on  $1:$port finished"

##启动服务
/home/ap/testapp/redis/redis-5.0.2/src/redis-server /home/ap/testapp/redis/redis_cluster/redis${port}.conf
echo "start redis service on  $1:$port finished"
done

echo "all port restart finished"

9.为集群中的每一个节点设置密码

编写批量启动脚本

[testapp@k8s-node1 ~/redis/redis_cluster]$ pwd
/home/ap/testapp/redis/redis_cluster
[testapp@k8s-node1 ~/redis/redis_cluster]$ ll
总用量 236
-rw-rw-r-- 1 testapp testapp 92 8月  14 18:56 appendonly.aof
-rw-r--r-- 1 testapp testapp   175 8月  14 18:56 dump.rdb
-rw-rw-r-- 1 testapp testapp   957 8月  14 18:55 flushCluster.sh
-rw-r--r-- 1 testapp testapp   817 8月  14 18:56 nodes-6380.conf
-rw-r--r-- 1 testapp testapp   817 8月  14 18:56 nodes-6381.conf
-rw-r--r-- 1 testapp testapp   817 8月  14 18:56 nodes-6382.conf
-rw-rw-r-- 1 testapp testapp 62340 8月  14 18:30 redis6380.conf
-rw-rw-r-- 1 testapp testapp 6 8月  14 18:55 redis_6380.pid
-rw-rw-r-- 1 testapp testapp 62344 8月  14 16:57 redis6381.conf
-rw-rw-r-- 1 testapp testapp 6 8月  14 18:55 redis_6381.pid
-rw-rw-r-- 1 testapp testapp 62344 8月  14 16:59 redis6382.conf
-rw-rw-r-- 1 testapp testapp 6 8月  14 18:55 redis_6382.pid
-rw-rw-r-- 1 testapp testapp   337 8月  14 19:30 startAll.sh
-rw-rw-r-- 1 testapp testapp   220 8月  14 19:31 stopAll.sh
[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ cat startAll.sh 
/home/ap/testapp/iot/redis/redis-5.0.2/src/redis-server /home/ap/testapp/redis/redis_cluster/redis6380.conf
/home/ap/testapp/iot/redis/redis-5.0.2/src/redis-server /home/ap/testapp/redis/redis_cluster/redis6381.conf
/home/ap/testapp/iot/redis/redis-5.0.2/src/redis-server /home/ap/testapp/redis/redis_cluster/redis6382.conf

[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ cat stopAll.sh 
/home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -p 6380 shutdown
/home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -p 6381 shutdown
/home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -p 6382 shutdown

[testapp@k8s-node1 ~/redis/redis_cluster]$ 

 修改每个redis638*.conf

 masterauth   改为  masterauth test@123

requirepass foobared   改为 requirepass test@123

一向比较懒的我

[testapp@k8s-node2 ~/redis/redis_cluster]$  sed 's/# masterauth /masterauth test@123/g' redis638*.conf|grep test@123 ##尝试修改,并显示结果
masterauth test@123
masterauth test@123
masterauth test@123
[testapp@k8s-node2 ~/iot/redis/redis_cluster]$  sed -i 's/# masterauth /masterauth test@123/g' redis638*.conf  ##加-i表示真正修改源文件

同理

[testapp@k8s-node2 ~/redis/redis_cluster]$  sed  's/# requirepass foobared/requirepass test@123/g' redis638*.conf|grep test@123
masterauth test@123
requirepass test@123
masterauth test@123
requirepass test@123
masterauth test@123
requirepass test@123
[testapp@k8s-node2 ~/redis/redis_cluster]$  sed  -i 's/# requirepass foobared/requirepass test@123/g' redis638*.conf

验证

[testapp@k8s-node2 ~/iot/redis/redis_cluster]$ grep test@123 redis638*.conf
redis6380.conf:masterauth test@123
redis6380.conf:requirepass test@123
redis6381.conf:masterauth test@123
redis6381.conf:requirepass test@123
redis6382.conf:masterauth test@123
redis6382.conf:requirepass test@123

然后重启

sh stopAll.sh

sh startAll.sh

[testapp@k8s-node2 ~/redis/redis_cluster]$ sh stopAll.sh 
[testapp@k8s-node2 ~/redis/redis_cluster]$ sh startAll.sh 
[testapp@k8s-node2 ~/redis/redis_cluster]$ ps -ef|grep redis
testapp   28886 1  0 20:01 ? 00:00:00 /home/ap/testapp/redis/redis-5.0.2/src/redis-server *:6380 [cluster]
testapp   28891 1  0 20:01 ? 00:00:00 /home/ap/testapp/redis/redis-5.0.2/src/redis-server *:6381 [cluster]
testapp   28896 1  0 20:01 ? 00:00:00 /home/ap/testapp/redis/redis-5.0.2/src/redis-server *:6382 [cluster]
testapp   28904  27515  0 20:01 pts/0 00:00:00 grep --color=auto redis
[testapp@k8s-node2 ~/iot/redis/redis_cluster]$ pwd
/home/ap/testapp/iot/redis/redis_cluster
[testapp@k8s-node2 ~/iot/redis/redis_cluster]$ 

验证,不输入密码,报NOAUTH Authentication required

9.1集群相关信息查看

9.1.1集群状态

redis-cli -h ip -p 9379 -a password cluster info
9.1.2.集群节点信息
redis-cli -h ip -p 9379 -a password cluster nodes
9.1.3.节点内存、cpu、key数量等信息(每个节点都需查看)
redis-cli -h ip -p 9379 -a password info
9.1.4.从redis集群中查看key(只需要连接master节点查看)
a.查看该节点的所有key
redis-cli -h ip -p 9379 -a password keys *
b.查看key的value值
redis-cli -h ip -p 9379 -a password -c get key
c.查看key值得有效期
redis-cli -h ip -p 9379 -a password -c ttl key

[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ /home/ap/testapp/iot/redis/redis-5.0.2/src/redis-cli -c -h 192.168.111.131 -p 6380  cluster info ##查看集群信息
NOAUTH Authentication required.
[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ /home/ap/testapp/iot/redis/redis-5.0.2/src/redis-cli -c -h 192.168.111.131 -p 6380 -a test@123 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:638
cluster_stats_messages_pong_sent:498
cluster_stats_messages_sent:1136
cluster_stats_messages_ping_received:498
cluster_stats_messages_pong_received:525
cluster_stats_messages_received:1023
[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ /home/ap/testapp/iot/redis/redis-5.0.2/src/redis-cli -c -h 192.168.111.131 -p 6380 -a test@123 cluster nodes ##查看集群结点信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
301acfbb7929a0416911f892efadfad8a5bf38f9 192.168.111.132:6381@16381 slave 078d0050ee8502ac2e21fd26b63924d5fca1c384 0 1565784658000 5 connected
b56997d7fbbe5df8257733c3ad017edbeb4bdaff 192.168.111.132:6382@16382 slave f1b97d85a94a309b9c67e6b50104cc2e6c2be6b3 0 1565784659541 6 connected
f1b97d85a94a309b9c67e6b50104cc2e6c2be6b3 192.168.111.131:6381@16381 master - 0 1565784656518 2 connected 10923-16383
078d0050ee8502ac2e21fd26b63924d5fca1c384 192.168.111.131:6380@16380 myself,master - 0 1565784657000 1 connected 0-5460
c8952a45cf7019bb1dfd8f8e029dc3415d65b214 192.168.111.131:6382@16382 slave a799a7cf8c7a14bbc72f7411322895e0ff3d4952 0 1565784658534 4 connected
a799a7cf8c7a14bbc72f7411322895e0ff3d4952 192.168.111.132:6380@16380 master - 0 1565784660549 4 connected 5461-10922
[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ 
[testapp@k8s-node1 ~/iot/redis/redis_cluster]$ /home/ap/testapp/iot/redis/redis-5.0.2/src/redis-cli -c -h 192.168.111.131 -p 6380 -a test@123 info ##查看节点节点内存、cpu、key数量等信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Server
redis_version:5.0.2
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:c55a3c91ae2e868
redis_mode:cluster
os:Linux 3.10.0-957.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:21623
run_id:bde9ca1163e34126efb47de9ee76c589dd979b89
tcp_port:6380
uptime_in_seconds:665
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:5503626
executable:/home/ap/testapp/iot/redis/redis-5.0.2/src/redis-server
config_file:/home/ap/testapp/iot/redis/redis_cluster/redis6380.conf

# Clients
connected_clients:2
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:2395096
used_memory_human:2.28M
used_memory_rss:8044544
used_memory_rss_human:7.67M
used_memory_peak:2395096
used_memory_peak_human:2.28M
used_memory_peak_perc:100.05%
used_memory_overhead:2316538
used_memory_startup:1184448
used_memory_dataset:78558
used_memory_dataset_perc:6.49%
allocator_allocated:2488720
allocator_active:2715648
allocator_resident:6713344
total_system_memory:1019797504
total_system_memory_human:972.55M
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.09
allocator_frag_bytes:226928
allocator_rss_ratio:2.47
allocator_rss_bytes:3997696
rss_overhead_ratio:1.20
rss_overhead_bytes:1331200
mem_fragmentation_ratio:3.45
mem_fragmentation_bytes:5712592
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:16914
mem_clients_normal:66600
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1565784065
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:4390912
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
aof_current_size:92
aof_base_size:92
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0

# Stats
total_connections_received:19
total_commands_processed:652
instantaneous_ops_per_sec:0
total_net_input_bytes:25411
total_net_output_bytes:4547
instantaneous_input_kbps:0.02
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:1
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:98873
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:1
slave0:ip=192.168.111.132,port=6381,state=online,offset=896,lag=1
master_replid:bb5feffa6c709a7daac8fe92e722e85f067da375
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:896
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:896

# CPU
used_cpu_sys:1.147768
used_cpu_user:0.126526
used_cpu_sys_children:0.085297
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:1

# Keyspace

9.2 redis.config配置

查看文件中生效配置,即非空非注释行

[testapp@k8s-node2 ~/redis/redis_cluster]$ grep -v "^#" redis6380.conf|grep -v "^$"
protected-mode no
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /home/ap/testapp/redis/redis_cluster/redis_6380.pid
loglevel notice
logfile "/home/ap/testapp/redis/logs/redis_run.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /home/ap/testapp/redis/redis_cluster
masterauth test@123
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass test@123
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file /home/ap/testapp/redis/redis_cluster/nodes-6380.conf
cluster-node-timeout 15000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
[testapp@k8s-node2 ~/redis/redis_cluster]$ 

 9.3持久化相关的配置

rdb快照模式

save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-save-incremental-fsync yes

AOF增量日志模式
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes aof-use-rdb-preamble yes
aof-rewrite-incremental-fsync yes
 

10 集群使用

192.168.111.132:6380中加入keyhello值
[testapp@k8s-node1 ~/redis/redis_cluster]$ /home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -h 192.168.111.131 -p 6380 -a test@123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.111.131:6380> set keyhello valueworld
-> Redirected to slot [8118] located at 192.168.111.132:6380
OK
192.168.111.132:6380> keys *
1) "keyhello"
192.168.111.132:6380> exit
192.168.111.131:6381加入keyhello131-6381
[testapp@k8s-node2 ~/redis/redis_cluster]$ /home/ap/testapp/redis/redis-5.0.2/src/redis-cli -c -h 192.168.111.131 -p 6381 -a test@123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.111.131:6381> keys *
(empty list or set)
192.168.111.131:6381> set keyhello1316381 worldhaha
-> Redirected to slot [3527] located at 192.168.111.131:6380
OK
192.168.111.131:6380> keys
(error) ERR wrong number of arguments for 'keys' command
192.168.111.131:6380> keys *
1) "keyhello1316381"
192.168.111.131:6380> exit
[testapp@k8s-node2 ~/iot/redis/redis_cluster]$ 

客户端查看

redis集群安装多端口多实例部署Linux安装redis时报gcc:未找到命令错误tar解压出错:gzip: stdin: unexpected end of file的解决[ERR] Node xxx is not empty. Either the node already knows other no...2

redis集群安装多端口多实例部署Linux安装redis时报gcc:未找到命令错误tar解压出错:gzip: stdin: unexpected end of file的解决[ERR] Node xxx is not empty. Either the node already knows other no...3

 

 

 

 

参考地址 https://www.huaweicloud.com/articles/2e6ef2ca57422518abefff92897d8000.html

posted @ 2021-04-22 12:42  woaibaobei  阅读(213)  评论(0编辑  收藏  举报