惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threat Research - Cisco Blogs
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
U
Unit 42
V
Vulnerabilities – Threatpost
P
Privacy International News Feed
T
Tor Project blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Recorded Future
Recorded Future
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
Latest news
Latest news
GbyAI
GbyAI
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Hacker News: Ask HN
Hacker News: Ask HN
美团技术团队
N
News | PayPal Newsroom
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Hacker News
The Hacker News
The GitHub Blog
The GitHub Blog
V
V2EX
N
News and Events Feed by Topic
T
Troy Hunt's Blog
Security Latest
Security Latest
博客园 - 叶小钗
P
Palo Alto Networks Blog

博客园 - qufo

Golang 线程池 Golang 之 Base62 编码 Golang 之 Qrcode 二维码 Golang 之 key-value LevelDB 无废话 Thrift 之 Hello World( PHP 版). [转] putty 使用密钥登陆 OpenSSH 一个视图引发的血案 破一个行业ERP的感想 Practical Ext JS Projects with Gears中关于Gears描述。 七七前一天,搞定 PHP5 + Oracle 8.1.7 去掉PowerDesigner 15 在 Visual Studio 2008里的不兼容。 回家 庆祝自己通过驾驶员考试 2008.08.08.一个有记住意义的时刻。 adverbux.com 明天路考 blank.security 距离2008年8月8日还有8天。 随时10个可用线程--自己涂鸦的“线程池”
Docker 实现的 redis 主从
qufo · 2016-11-27 · via 博客园 - qufo

计划用 Docker 实现 Redis 的主从,简单主从而已。主的名称叫 redis-master

一步步来。

先新建个Dockerfile ,从alpine 开始,比较简单。

FROM alpine:latest
MAINTAINER qufo qufo@163.com

# 换源
RUN echo 'http://mirrors.ustc.edu.cn/alpine/edge/main' > /etc/apk/repositories
RUN echo '@community http://mirrors.ustc.edu.cn/alpine/edge/community' >> /etc/apk/repositories
RUN echo '@testing http://mirrors.ustc.edu.cn/alpine/edge/testing' >> /etc/apk/repositories

RUN apk update

# 修正时区
ENV TIMEZONE Asia/Shanghai
RUN apk add tzdata
RUN ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
RUN echo $TIMEZONE > /etc/timezone

# 修改一些系统设置
RUN echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
RUN echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local

# 开始安装 redis
RUN apk add redis
COPY start_redis.sh /usr/local/
RUN chmod +x /usr/local/start_redis.sh
RUN chown redis:redis /usr/local/start_redis.sh

EXPOSE 6379

ENTRYPOINT ["sh","/usr/local/start_redis.sh"]
CMD ["master"]

可以看到,我们还需要一个启动脚本。start_redis.sh

redis_role=$1
sed -i 's/protected-mode yes/protected-mode no/g' /etc/redis.conf
sed -i 's/daemonize yes/daemonize no/g'           /etc/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/g'          /etc/redis.conf

echo "Start Redis by "
if [ $redis_role = "master" ]; then
    echo "master"
    redis-server /etc/redis.conf
elif [ $redis_role = "slave" ]; then
    echo "slave"
    sed -i 's/# slaveof <masterip> <masterport>/slaveof redis-master 6379/g' /etc/redis.conf
    redis-server /etc/redis.conf
else
    echo "unknow role!"
fi

这里修改了一些系统配置,然后根据传进来的 role 区别启动主或从模式。其中的主名称是写死了的 redis-master

好了,现在构建,在Dockerfile 和 start_redis.sh 所在的目录下,执行

docker rmi qufo/redis
docker build -t="qufo/redis" .

我们就得到了一个名为 qufo/redis 的 image , 因为采用的是 alpine 的包,所以速度很快,在我的机器上大概7秒就完成了。

通过

看下镜象的信息,只有9M多些。

好了,现在来启动他们,启运完后检查一下是不是都在。

ufo@ubuntu:~$ docker run -d --name=redis-master -p=6379:6379 qufo/redis master
ad7c5a6e16a8d87f04240c7bcb20ef765eab832737f32457e956966a2f282179
ufo@ubuntu:~$ docker run -d --name=node1 --link=redis-master:redis-master  -p=7001:6379 qufo/redis slave
8cef160881f59b017cd915775fa3f76d9456b510b300147779fd8ff4ba9f9a2c
ufo@ubuntu:~$ docker run -d --name=node2 --link=redis-master:redis-master  -p=7002:6379 qufo/redis slave
767011663ef8b93b5326bd4f9940762922ee4a685278806d42b8ce690164de48
ufo@ubuntu:~$ docker run -d --name=node3 --link=redis-master:redis-master  -p=7003:6379 qufo/redis slave
6c4bb442d5adb6773301bac29e6a7a5b1f636d47c67dc21c9ec0ec28cf988602
ufo@ubuntu:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
6c4bb442d5ad        qufo/redis          "sh /usr/local/start_"   38 seconds ago       Up 37 seconds       0.0.0.0:7003->6379/tcp   node3
767011663ef8        qufo/redis          "sh /usr/local/start_"   48 seconds ago       Up 47 seconds       0.0.0.0:7002->6379/tcp   node2
8cef160881f5        qufo/redis          "sh /usr/local/start_"   57 seconds ago       Up 56 seconds       0.0.0.0:7001->6379/tcp   node1
ad7c5a6e16a8        qufo/redis          "sh /usr/local/start_"   About a minute ago   Up About a minute   0.0.0.0:6379->6379/tcp   redis-master

可以看到有一主(redis-master)多从 node1,node2,node3。

现在来试一下,在主里 set ,从里 get 

# 利用映射出来的端品直接连 redis-master
ufo@ubuntu:~/$ redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=172.17.0.3,port=6379,state=online,offset=239,lag=0
slave1:ip=172.17.0.4,port=6379,state=online,offset=239,lag=1
slave2:ip=172.17.0.5,port=6379,state=online,offset=239,lag=1
master_repl_offset:239
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:238
127.0.0.1:6379> set username qufo
OK
127.0.0.1:6379> get username
"qufo"
127.0.0.1:6379> exit
# 可见 为 master 模式,在里面设置和 username
# 通过 7001 连接 node1
ufo@ubuntu:~/$ redis-cli -p 7001
127.0.0.1:7001> info replication
# Replication
role:slave
master_host:redis-master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:327
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
127.0.0.1:7001> get username
"qufo"
127.0.0.1:7001> set username nothing
(error) READONLY You can't write against a read only slave.
127.0.0.1:7001> exit
ufo@ubuntu:~/$ 
# 可见为 slave 模式,可以 get username ,但不能 set username 因为slave 是只读的。

好吧,主从配置完成。