第一步:進行偽集的群安裝
1.1:創建redis-cluster目錄
我們需要先創建名為redis-cluster目錄,在這個目錄下創建一個docker-compose.yml文件
docker-compose.yml
version: '2.2' x-image: &default-image bitnami/redis-cluster:7.0 x-restart: &default-restart always services: redis-node-0: image: *default-image restart: *default-restart container_name: redis-node-0 ports: - 6379:6379 volumes: - ./redis-cluster_data-0:/bitnami/redis/data environment: - 'REDIS_PASSWORD=liubei@161' - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5' redis-node-1: image: *default-image restart: *default-restart container_name: redis-node-1 ports: - 16379:6379 volumes: - ./redis-cluster_data-1:/bitnami/redis/data environment: - 'REDIS_PASSWORD=liubei@161' - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5' redis-node-2: image: *default-image restart: *default-restart container_name: redis-node-2 ports: - 26379:6379 volumes: - ./redis-cluster_data-2:/bitnami/redis/data environment: - 'REDIS_PASSWORD=liubei@161' - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5' redis-node-3: image: *default-image restart: *default-restart container_name: redis-node-3 ports: - 36379:6379 volumes: - ./redis-cluster_data-3:/bitnami/redis/data environment: - 'REDIS_PASSWORD=liubei@161' - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5' redis-node-4: image: *default-image restart: *default-restart container_name: redis-node-4 ports: - 46379:6379 volumes: - ./redis-cluster_data-4:/bitnami/redis/data environment: - 'REDIS_PASSWORD=liubei@161' - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5' redis-node-5: image: *default-image restart: *default-restart container_name: redis-node-5 ports: - 56379:6379 volumes: - ./redis-cluster_data-5:/bitnami/redis/data depends_on: - redis-node-0 - redis-node-1 - redis-node-2 - redis-node-3 - redis-node-4 environment: - 'REDIS_PASSWORD=liubei@161' - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
1.2:進行持久化存儲
這些可以創建的目錄我們能夠直接啟動,但我們在容器中沒有使用root用戶,大部分服務器都不會有寫權限。因此
我們要先給這些目錄讀寫權限。最后看對應宿主機是哪個用戶,我們再將目錄屬主改為哪個用戶即可。
儲存目錄
mkdir redis-cluster_data-{0..5} chmod 777 redis-cluster-data-*
結果如下
[root@liubei redis-cluster]# ll 總用量 7 -rw-r----- 1 root root 2481 7月 6 10:21 docker-compose.yml drwxrwxrwx 3 root root 61 7月 6 10:43 redis-cluster_data-0 drwxrwxrwx 3 root root 61 7月 6 10:43 redis-cluster_data-1 drwxrwxrwx 3 root root 61 7月 6 10:43 redis-cluster_data-2 drwxrwxrwx 3 root root 77 7月 6 10:51 redis-cluster_data-3 drwxrwxrwx 3 root root 77 7月 6 10:51 redis-cluster_data-4 drwxrwxrwx 3 root root 77 7月 6 10:51 redis-cluster_data-5
1.3:啟動集群
啟動
# docker-compose up -d
結果如下
[root@liubei redis-cluster]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------- redis-node-0 /opt/bitnami/scripts/redis ... Up 0.0.0.0:6379->6379/tcp redis-node-1 /opt/bitnami/scripts/redis ... Up 0.0.0.0:16379->6379/tcp redis-node-2 /opt/bitnami/scripts/redis ... Up 0.0.0.0:26379->6379/tcp redis-node-3 /opt/bitnami/scripts/redis ... Up 0.0.0.0:36379->6379/tcp redis-node-4 /opt/bitnami/scripts/redis ... Up 0.0.0.0:46379->6379/tcp redis-node-5 /opt/bitnami/scripts/redis ... Up 0.0.0.0:56379->6379/tcp
1.4:初始化集群
我們先進入其中的一個容器,執行以下這條命令
I have no name!@d4a8a3ef35ce:/$ redis-cli -a liubei@161 --cluster create redis-node-0:6379 redis-node-1:6379 redis-node-2:6379 redis-node-3:6379 redis-node-4:6379 redis-node-5:6379 --cluster-replicas 1
執行結果如下
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica redis-node-4:6379 to redis-node-0:6379 Adding replica redis-node-5:6379 to redis-node-1:6379 Adding replica redis-node-3:6379 to redis-node-2:6379 M: 4cdf0f24f67eadb3b7a610fd9b5409a571cce8d3 redis-node-0:6379 slots:[0-5460] (5461 slots) master M: ee85d84a95793ee031a4b45fe3600ef81ecef7d1 redis-node-1:6379 slots:[5461-10922] (5462 slots) master M: f48ab32421dfe4405b73129d88f64a4ce4d076e3 redis-node-2:6379 slots:[10923-16383] (5461 slots) master S: 0315d864aec40531c9630d5e21959aea6837236e redis-node-3:6379 replicates f48ab32421dfe4405b73129d88f64a4ce4d076e3 S: ec43dee472ce9f1531ccdbd0853cd672519ec2fe redis-node-4:6379 replicates 4cdf0f24f67eadb3b7a610fd9b5409a571cce8d3 S: c834ba17bfdf0d498533355022e548b040083ed9 redis-node-5:6379 replicates ee85d84a95793ee031a4b45fe3600ef81ecef7d1 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 redis-node-0:6379) M: 4cdf0f24f67eadb3b7a610fd9b5409a571cce8d3 redis-node-0:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: ec43dee472ce9f1531ccdbd0853cd672519ec2fe 172.29.0.2:6379 slots: (0 slots) slave replicates 4cdf0f24f67eadb3b7a610fd9b5409a571cce8d3 M: f48ab32421dfe4405b73129d88f64a4ce4d076e3 172.29.0.4:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: ee85d84a95793ee031a4b45fe3600ef81ecef7d1 172.29.0.5:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 0315d864aec40531c9630d5e21959aea6837236e 172.29.0.6:6379 slots: (0 slots) slave replicates f48ab32421dfe4405b73129d88f64a4ce4d076e3 S: c834ba17bfdf0d498533355022e548b040083ed9 172.29.0.7:6379 slots: (0 slots) slave replicates ee85d84a95793ee031a4b45fe3600ef81ecef7d1 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
執行這條代碼,查看集群狀態
redis-cli -h redis-node-0 -a liubei@161 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:174 cluster_stats_messages_pong_sent:203 cluster_stats_messages_sent:377 cluster_stats_messages_ping_received:198 cluster_stats_messages_pong_received:174 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:377 total_cluster_links_buffer_limit_exceeded:0 I have no name!@c193f9814adb:/$
然后看一下集群節點狀態
redis-cli -h redis-node-0 -a liubei@161 cluster nodes
結果
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 921dd4fc8977562273e8f0f297d7809f5d785a96 192.168.0.2:6379@16379 myself,master - 0 1657079063000 1 connected 0-5460 e3be1ac5c4b5a8ea5789dd62aaa550cece718504 192.168.0.5:6379@16379 slave 921dd4fc8977562273e8f0f297d7809f5d785a96 0 1657079062000 1 connected d45ea5232290aefd17ec87229b2b68be76061b84 192.168.0.6:6379@16379 slave 505a1c9a1e3bc8b7685a6cdf6aa1d82bd48cc95f 0 1657079063278 3 connected 88f0d7a2377bf876105925e330793091f2390a20 192.168.0.4:6379@16379 slave 477f835648ca3ddffa48af99a2e162e541277186 0 1657079062274 2 connected 505a1c9a1e3bc8b7685a6cdf6aa1d82bd48cc95f 192.168.0.7:6379@16379 master - 0 1657079064281 3 connected 10923-16383 477f835648ca3ddffa48af99a2e162e541277186 192.168.0.3:6379@16379 master - 0 1657079065285 2 connected 5461-10922
第二步:測試
2.1:進行讀寫測試
用集群模式登錄
redis-cli -h redis-nod-1 -a liubei@161 -c
數據載入
redis-node-1:6379> set name liubei OK
另一個節點上讀取
I have no name!@ab2a18399901:/$ redis-cli -h redis-node-5 -a liubei@161 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. redis-node-5:6379> get name -> Redirected to slot [5798] located at 192.168.0.3:6379 "liubei" 192.168.0.3:6379>
讀取后我們發現,連接會跳到存儲這個key的節點上(192.168.0.3)
2.2:key所在節點
隨便登錄一個節點
redis-cli -h redis-node-0 -a liubei@161 -c
我們就會看到節點哈希槽編號的范圍
redis-node-0:6379> cluster slots 1) 1) (integer) 0 2) (integer) 5460 3) 1) "192.168.0.2" 2) (integer) 6379 3) "921dd4fc8977562273e8f0f297d7809f5d785a96" 4) (empty array) 4) 1) "192.168.0.5" 2) (integer) 6379 3) "e3be1ac5c4b5a8ea5789dd62aaa550cece718504" 4) (empty array) 2) 1) (integer) 5461 2) (integer) 10922 3) 1) "192.168.0.3" 2) (integer) 6379 3) "477f835648ca3ddffa48af99a2e162e541277186" 4) (empty array) 4) 1) "192.168.0.4" 2) (integer) 6379 3) "88f0d7a2377bf876105925e330793091f2390a20" 4) (empty array) 3) 1) (integer) 10923 2) (integer) 16383 3) 1) "192.168.0.7" 2) (integer) 6379 3) "505a1c9a1e3bc8b7685a6cdf6aa1d82bd48cc95f" 4) (empty array) 4) 1) "192.168.0.6" 2) (integer) 6379 3) "d45ea5232290aefd17ec87229b2b68be76061b84" 4) (empty array)
再查看一下name的哈希槽編號
redis-node-0:6379> cluster keyslot name (integer) 5798
可以看到,哈希槽編號的范圍我們可以看到5798 這個編號在 192.168.0.3 上,如果想要在其他節點上讀取到這個key值,就需要集群模式登錄
集群模式驗證是否準確
redis-node-0:6379> get name -> Redirected to slot [5798] located at 192.168.0.3:6379 "liubei" 192.168.0.3:6379>
集群模式登錄,查找這個key,我們的連接會跳到了 192.168.0.3上。在非集群模式登錄查看的話,
如果登錄到其他節點,會提示我們這個key存在了192.168.0.3上
I have no name!@ab2a18399901:/$ redis-cli -h 192.168.0.5 -a liubei@161 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.0.5:6379> get name (error) MOVED 5798 192.168.0.3:6379
想要查詢到值只有登錄這個節點才行
I have no name!@ab2a18399901:/$ redis-cli -h 192.168.0.3 -a liubei@161 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.0.3:6379> get name "liubei"
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/127571.html
摘要:環境要求安裝了的主機本文示例環境為準備鏡像首先把所有需要用到的鏡像拉取下來容器編排是容器進行編排的工具,定義和運行多容器的應用,可以一條命令啟動多個容器。 環境要求:安裝了docker的主機 (本文示例環境為centos7.4) 準備鏡像 首先把所有需要用到的鏡像拉取下來 # nginx $ docker pull nginx # php & php-fpm $ docker pul...
摘要:環境要求安裝了的主機本文示例環境為準備鏡像首先把所有需要用到的鏡像拉取下來容器編排是容器進行編排的工具,定義和運行多容器的應用,可以一條命令啟動多個容器。 環境要求:安裝了docker的主機 (本文示例環境為centos7.4) 準備鏡像 首先把所有需要用到的鏡像拉取下來 # nginx $ docker pull nginx # php & php-fpm $ docker pul...
閱讀 1168·2022-09-27 09:47
閱讀 1064·2022-09-27 09:28
閱讀 1528·2022-09-27 09:16
閱讀 835·2022-09-27 08:21
閱讀 1005·2022-09-27 08:08
閱讀 1152·2022-09-18 12:33
閱讀 816·2022-09-16 08:01
閱讀 869·2022-09-15 12:27