基於docker的 redis叢集之主從複製
阿新 • • 發佈:2018-12-22
環境搭建步驟
一 準備
docker環境(centos7 + docker1.12.1)
redis 3.2.4 wget http://download.redis.io/releases/redis-3.2.4.tar.gz
172.17.0.2:6379 主 172.17.0.3:6379 從 172.17.0.4:6379 從
遺留問題:
1 實現主從後,讀寫分離,客戶端連線程式碼如何寫?
2 主從切換後,客戶端程式碼需要調整嗎?
二 製作映象
Dockerfile內容如下:
FROM centos:6.7
MAINTAINER loomz [email protected]
ENV REFRESHED_AT 2017-03-08
ENV REDIS_HOME /opt/redis/redis_default
ADD redis-3.2.4.tar.gz /opt/redis/
RUN ln -s /opt/redis/redis-3.2.4 $REDIS_HOME
RUN yum -y install gcc
WORKDIR $REDIS_HOME
RUN make && make install
EXPOSE 6379
ENTRYPOINT [ "/usr/local/bin/redis-server", "/etc/redis/redis.conf" ]
三 容器啟動
三個啟動指令碼和配置檔案內容如下:
主master
start.-6379.sh
#!/bin/bash
docker run -d -p 6379:6379 --restart always -h redis-6379 --name redis-6379 -v /home/loomz/dockerfiles/redis_3.2_cluster/redis_cluster/6379:/etc/redis/:ro redis:v3.2.4
redis.conf
port 6379 bind 172.17.0.2 daemonize no pidfile /var/run/redis.pid appendonly yes
從slave
start-6380.sh
#!/bin/bash
docker run -d -p 6380:6379 --restart always -h redis-6380 --name redis-6380 -v /home/loomz/dockerfiles/redis_3.2_cluster/redis_cluster/6380:/etc/redis/:ro redis:v3.2.4
redis.conf
port 6379
bind 172.17.0.3
daemonize no
pidfile /var/run/redis.pid
appendonly yes
slaveof 172.17.0.2 6379
從slave
start-6381.sh
#!/bin/bash
docker run -d -p 6381:6379 --restart always -h redis-6381 --name redis-6381 -v /home/loomz/dockerfiles/redis_3.2_cluster/redis_cluster/6381:/etc/redis/:ro redis:v3.2.4
redis.conf
port 6379
bind 172.17.0.4
daemonize no
pidfile /var/run/redis.pid
appendonly yes
slaveof 172.17.0.2 6379
通過客戶端命令檢視節點情況
redis-cli > info replication
# Replication
role:slave
master_host:172.17.0.4
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1489327889
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
以上方式即實現主從,但如果主的掛掉,從資料庫升級為主資料需要手工處理,使用命令slaveof no one即可提升為主資料庫。
使用哨兵方式:
在任意一個節點使用增加配置檔案sentinel.conf,內容如下:
sentinel monitor mymaster_loomz 172.17.0.2 6379 1
引數說明:
mymaster_loomz 主資料庫名字,自定義即可
172.17.0.2 主資料庫地址
6379 主資料庫埠
1 哨兵最低通過票數
啟動sentinel程序:
/usr/local/bin/sentinel /etc/redis/sentinel.conf
當然生產環境哨兵也需要做叢集,避免單點故障,按照上面的方式啟動多個哨兵程序即可,或者每個節點啟動一個哨兵程序。注意sentinel.conf最後一個引數“哨兵最低通過票數”設定為哨兵總數的超過半數即可,如N / 2 +1
四 客戶端連線程式碼
public static void main(String[] args) {
JedisPoolConfig poolconfig = new JedisPoolConfig();
poolconfig.setMaxIdle(30);
poolconfig.setMaxTotal(1000);
Set<String> sentinels = new HashSet<String>();
sentinels.add("172.17.0.1:26379");
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool("master_loomz",
sentinels, poolconfig, 3000, "redispass");
HostAndPort currentHostMaster = jedisSentinelPool.getCurrentHostMaster();
System.out.println("currentHostMaster : " + currentHostMaster.getHost() + " port: "
+ currentHostMaster.getPort());
Jedis resource = jedisSentinelPool.getResource();
resource.setDataSource(jedisSentinelPool);
String value = resource.get("aa");
System.out.println(value);
resource.close();
}