docker-compose啟動redis主從的實現(xiàn)
更新時間:2026年07月14日 09:35:57 作者:玄德公筆記
本文主要介紹了docker-compose啟動redis主從的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
環(huán)境說明:
| IP | 主機名 | 服務(wù) |
|---|---|---|
| 10.xxx.91.100 | liubei | redis-master |
| 10.xxx.91.101 | liubei02 | redis-slave |
1. 主節(jié)點
- 創(chuàng)建redis-m 目錄,并進入該目錄
- 創(chuàng)建
docker-compose.yml文件內(nèi)容如下:
version: '3'
services:
redis-master:
image: redis:7.4
container_name: redis-master
command: ["redis-server", "/usr/local/etc/redis/redis.conf", "--appendonly", "yes"]
ports:
- "6379:6379"
volumes:
- ./data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
restart: always- 創(chuàng)建
redis.conf文件(至少644權(quán)限)。內(nèi)容如下:
bind 0.0.0.0 port 6379 requirepass KrhLSx2XVs masterauth KrhLSx2XVs appendonly yes
說明:
- requirepass 是客戶端訪問從節(jié)點時的密碼。
- masterauth 是從節(jié)點連接主節(jié)點時使用的密碼。
- replicaof 告訴從節(jié)點同步主節(jié)點的數(shù)據(jù)
2. 從節(jié)點配置
- 創(chuàng)建redis-s 目錄,并進入該目錄
- 創(chuàng)建
docker-compose.yml文件內(nèi)容如下:
version: '3'
services:
redis-slave:
image: harbocto.xxx.com.cn/public/redis:7.4
container_name: redis-slave
command: ["redis-server", "/usr/local/etc/redis/redis.conf", "--appendonly", "yes"]
ports:
- "6379:6379"
volumes:
- ./data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
restart: always- 創(chuàng)建
redis.conf文件(至少644權(quán)限)。內(nèi)容如下:
bind 0.0.0.0 port 6379 requirepass KrhLSx2XVs masterauth KrhLSx2XVs replicaof 10.xxx.91.100 6379 appendonly yes
3. 啟動集群
起來就行,不需要過多操作
- 啟動master
[root@liubei redis-m]# docker-compose up -d
Creating network "redis-m_default" with the default driver
Creating redis-master ... done
[root@liubei redis-m]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------
redis-master docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp- 啟動slave
[root@liubei02 redis-02]# docker-compose up -d Creating network "redis-02_default" with the default driver Creating redis-slave ... done [root@liubei02 redis-02]# docker-compose ps Name Command State Ports ----------------------------------------------------------------------------- redis-slave docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
4. 驗證
4.1 驗證狀態(tài)
- 主節(jié)點上
[root@liubei redis-m]# docker exec -it redis-master redis-cli -a KrhLSx2XVs info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:1 slave0:ip=10.xxx.91.101,port=6379,state=online,offset=1386,lag=0 master_failover_state:no-failover master_replid:3c049bf4538faf754c56c7e2bd5ac40a217b661a master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1386 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1386
- 從節(jié)點上驗證
[root@finaichat-prd-web12 redis-02]# docker exec -it redis-slave redis-cli -a KrhLSx2XVs info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:10.xxx.91.100 master_port:6379 master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_read_repl_offset:1568 slave_repl_offset:1568 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:3c049bf4538faf754c56c7e2bd5ac40a217b661a master_replid2:0000000000000000000000000000000000000000 master_repl_offset:1568 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:1568
4.2 讀寫驗證
- 主節(jié)點寫入
[root@liubei ~]# docker exec -it redis-master redis-cli -a KrhLSx2XVs Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> SET key1 liubei OK
- 從節(jié)點讀取
[root@liubei01 ~]# docker exec -it redis-slave redis-cli -a KrhLSx2XVs Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> get key1 "liubei"
到此這篇關(guān)于docker-compose啟動redis主從的實現(xiàn)的文章就介紹到這了,更多相關(guān)docker-compose redis主從內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
docker+gitlab+gitlab-runner部署詳解
這篇文章主要介紹了docker+gitlab+gitlab-runner部署詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
Docker如何解決tomcat容器啟動成功,無法訪問的問題
這篇文章主要介紹了Docker如何解決tomcat容器啟動成功,無法訪問的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Docker搭建Nginx服務(wù)器的實現(xiàn)步驟
本文主要介紹了Docker搭建Nginx服務(wù)器的實現(xiàn),包括配置目錄、掛載文件、修改配置及端口設(shè)置,實現(xiàn)快速搭建并靈活管理Nginx服務(wù),感興趣的可以了解一下2025-05-05
docker在自定義網(wǎng)絡(luò)中安裝ElasticSearch和Kibana的步驟
這篇文章主要介紹了docker在自定義網(wǎng)絡(luò)中安裝ElasticSearch和Kibana的步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-08-08

