一、Docker Compose介紹
Docker官方的網站是這樣介紹Docker Compose的:
Compose是用於定義和運行多容器Docker應用程序的工具。通過Compose,您可以使用YAML文件來配置應用程序的服務。然後,使用一個命令,就可以從配置中創建並啓動所有服務。
這裏Docker Compose給我的感受就是便捷、快速。只需編寫一個docker-compose.yml文件,然後通過命令docker-compose up -d,這裏就可以搭建多個服務起來,非常適合搭建集羣環境。
二、安裝Docker Compose工具
通過命令安裝Compose工具,安裝Compose的前提是安裝了Docker環境,如何安裝和快速使用Docker,可以翻看我之前的那篇博客--Docker快速上手之搭建SpringBoot項目,這裏不做贅述了。
sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
給安裝腳本添加執行權限
sudo chmod +x /usr/local/bin/docker-compose
這裏可以使用命令查看是否安裝成功
docker-compose -v
到這裏Compose工具就安裝好了
三、使用Compose搭建Redis主從服務器
主從複製:主節點負責寫數據,從節點負責讀數據,主節點定期把數據同步到從節點保證數據的一致性。
從性能方面,redis複製功能增加了讀的性能,理論上說,每增加一倍的服務器,整個系統的讀能力就增加一倍。
選擇好路徑,通過命令創建文件夾redis,然後進入文件夾創建docker-compose.yml,或者在本地創建,然後通過Xftp工具上傳至服務器。docker-compose.yml寫入如下內容。
version: '2' services: master: image: redis ## 鏡像 container_name: redis-master ##容器別名 command: redis-server --requirepass 123456 ##redis密碼 ports: - "6379:6379" ##暴露端口 networks: - sentinel-master slave1: image: redis container_name: redis-slave-1 ports: - "6380:6379" command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 depends_on: - master networks: - sentinel-master slave2: image: redis container_name: redis-slave-2 ports: - "6381:6379" command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 depends_on: - master networks: - sentinel-master
啓動redis集羣
docker-compose up -d
docker ps查看運行的實例,這裏我們需要用到主節點的ip地址,注意不是電腦的ip哦,是節點的ip地址。
docker inspect 主節點容器id
這裏先記着這個ip地址,後面使用到哨兵需要綁定這個主節點的ip。
四、哨兵sentinel模式搭建
version: '2' services: sentinel1: image: redis ## 鏡像 container_name: redis-sentinel-1 ports: - "26379:26379" command: redis-sentinel /usr/local/etc/redis/sentinel.conf volumes: - "./sentinel.conf:/usr/local/etc/redis/sentinel.conf" sentinel2: image: redis ## 鏡像 container_name: redis-sentinel-2 ports: - "26380:26379" command: redis-sentinel /usr/local/etc/redis/sentinel.conf volumes: - "./sentinel2.conf:/usr/local/etc/redis/sentinel.conf" sentinel3: image: redis ## 鏡像 container_name: redis-sentinel-3 ports: - "26381:26379" command: redis-sentinel /usr/local/etc/redis/sentinel.conf volumes: - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf networks: default: external: name: redis_sentinel-master
繼續在此目錄編寫文件,編寫sentinel.conf文件
port 26379 dir /tmp #172.18.0.3填寫自己的主節點ip sentinel monitor mymaster 172.18.0.3 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 10000 sentinel deny-scripts-reconfig yes
對命令的解釋
第三行表示Redis監控一個叫做mymaster的運行在172.18.0.3:6379的master,投票達到2則表示master以及掛掉了。
第四行設置主節點的密碼。
第五行表示在一段時間範圍內sentinel向master發送的心跳PING沒有回覆則認爲master不可用了。
第六行的parallel-syncs表示設置在故障轉移之後,同時可以重新配置使用新master的slave的數量。數字越低,更多的時間將會用故障轉移完成,但是如果slaves配置爲服務舊數據,你可能不希望所有的slave同時重新同步master。因爲主從複製對於slave是非阻塞的,當停止從master加載批量數據時有一個片刻延遲。通過設置選項爲1,確信每次只有一個slave是不可到達的。
第七行表示10秒內mymaster還沒活過來,則認爲master宕機了。
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf
啓動redis哨兵模式
docker-compose up -d
五、故障轉移測試
上面既然說到了哨兵可以自動轉移故障,也就是當主節點宕機的時候,它們能通過選舉制度,在從節點中選出一個節點來代替主節點。
我們先來看看主節點此時的信息,這裏a288c55db497是我主節點容器的ip,這裏先是進入容器,再進入redis客戶端,再輸入密碼,然後使用info查看節點信息。
這裏可以看到這個節點的身份是master也就是主節點,然後有兩個連接着的從節點
現在我們停掉這個節點,看看哨兵是否可以實現故障轉移
docker stop a288c55db497
然後選擇其中一個從節點進入客戶端,輸入info查看信息
這裏發現其中一臺從節點已經變成主節點了,而且連接的從節點,也變成了一個。
瞭解Docker Compose可參考:https://www.jianshu.com/p/658911a8cff3
瞭解Redis主從哨兵可參考:https://www.cnblogs.com/leeSmall/p/8398401.html