1.登录可以使用redis客户端工具的Pod,使用redis-cli连接到redis集群

[root@k8s-master01 example]# kubectl exec -it drc-example-distributedrediscluster-0-0 -n redis-cluster -- sh
/data # redis-cli -h example-distributedrediscluster
example-distributedrediscluster:6379>

注意:如果Redis客户端和Redis集群不是同一个Namespace,需要添加.redis-cluster

2.输入info获取所有信息

example-distributedrediscluster:6379> info
# Server
redis_version:5.0.4
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b0d03e9600ca5411
redis_mode:cluster
os:Linux 4.19.12-1.el7.elrepo.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.3.0
process_id:1
run_id:0453074eb547d1bf2531305d515054884ff8454e
tcp_port:6379
uptime_in_seconds:11441
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:10566624
executable:/data/redis-server
config_file:/conf/redis.conf

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

# Memory
used_memory:2649376
used_memory_human:2.53M
used_memory_rss:14897152
used_memory_rss_human:14.21M
used_memory_peak:2710176
used_memory_peak_human:2.58M
used_memory_peak_perc:97.76%
used_memory_overhead:2564936
used_memory_startup:1449744
used_memory_dataset:84440
used_memory_dataset_perc:7.04%
allocator_allocated:2701088
allocator_active:2981888
allocator_resident:9908224
total_system_memory:8339460096
total_system_memory_human:7.77G
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.10
allocator_frag_bytes:280800
allocator_rss_ratio:3.32
allocator_rss_bytes:6926336
rss_overhead_ratio:1.50
rss_overhead_bytes:4988928
mem_fragmentation_ratio:5.71
mem_fragmentation_bytes:12289776
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:16922
mem_clients_normal:49694
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:1688276995
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:2281472
aof_enabled:0
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

# Stats
total_connections_received:2474
total_commands_processed:14179
instantaneous_ops_per_sec:1
total_net_input_bytes:523226
total_net_output_bytes:1164905
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.01
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:478
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=172.17.125.51,port=6379,state=online,offset=15596,lag=1
master_replid:331b1fa94e072e28eb14cb9da8fba3851919dbd5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:15596
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:15596

# CPU
used_cpu_sys:11.595049
used_cpu_user:0.295313
used_cpu_sys_children:0.002172
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:1

# Keyspace

3.输入info memory获取内存相关信息

example-distributedrediscluster:6379> info memory
# Memory
used_memory:2648400
used_memory_human:2.53M
used_memory_rss:14897152
used_memory_rss_human:14.21M
used_memory_peak:2710176
used_memory_peak_human:2.58M
used_memory_peak_perc:97.72%
used_memory_overhead:2564936
used_memory_startup:1449744
used_memory_dataset:83464
used_memory_dataset_perc:6.96%
allocator_allocated:2759080
allocator_active:3084288
allocator_resident:9908224
total_system_memory:8339460096
total_system_memory_human:7.77G
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.12
allocator_frag_bytes:325208
allocator_rss_ratio:3.21
allocator_rss_bytes:6823936
rss_overhead_ratio:1.50
rss_overhead_bytes:4988928
mem_fragmentation_ratio:5.71
mem_fragmentation_bytes:12289776
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:16922
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

4.输入info replication获取主从复制相关信息

example-distributedrediscluster:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=172.17.125.51,port=6379,state=online,offset=15764,lag=1
master_replid:331b1fa94e072e28eb14cb9da8fba3851919dbd5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:15778
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:15778

5.输入info clients获取当前客户端连接数

example-distributedrediscluster:6379> info clients
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

6.输入info cpu获取当前占用CPU

example-distributedrediscluster:6379>  info cpu
# CPU
used_cpu_sys:11.950655
used_cpu_user:0.304621
used_cpu_sys_children:0.002172
used_cpu_user_children:0.000000

7.输入info cluster查看是否为集群模式。其中1代表是,0代表否

example-distributedrediscluster:6379> info cluster
# Cluster
cluster_enabled:1

8.输入info keyspace查看当前数据库key总数

example-distributedrediscluster:6379>  info keyspace
# Keyspace

9.插入数据

example-distributedrediscluster:6379> set b 2
OK
example-distributedrediscluster:6379> set c 3
(error) MOVED 7365 172.18.195.47:6379

说明:这是因为Redis集群采用分片机制,每个数据插入时会选择一个对应的槽存放数据,如果该槽位不在这个已经连接的实例上,就需要进行“下一跳”,也就是会连接到具有该槽位的节点上,再写入数据,当然如果该槽位正好位于正在连接的这个实例上,就会直接写入数据。

需要连接到172.18.195.47示例进行数据写入,观察到数据已成功写入

example-distributedrediscluster:6379> exit
/data # redis-cli -h 172.18.195.47
172.18.195.47:6379> set c 3
OK
172.18.195.47:6379> get c
"3"

注意:如果需要集群外部连接至Kubernetes内部的Redis集群,必须采用Redis集群的代理工具