Docker 安裝 Redis 5.x集群

安裝環(huán)境準(zhǔn)備

  1. CentOS
  2. Docker

創(chuàng)建redis docker基礎(chǔ)鏡像

  1. 下載安裝包
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# wget http://download.redis.io/releases/redis-5.0.4.tar.gz
  1. 解壓
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# tar zxvf redis-5.0.4.tar.gz
  1. 修改redis.conf配置文件
  • NETWORK配置,修改為0.0.0.0,是為了接收外部連接
################################## NETWORK #####################################

# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1
 bind 0.0.0.0
  • 默認(rèn)客戶端登錄不需要密碼逆粹,開啟后需要使用密碼挥吵,如果是部署在自己的機器上可以使用默認(rèn)不開啟
################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared
  requirepass 123456

如果主節(jié)點開啟了客戶端密碼驗證機制,則需要配置挤牛,不配置集群情況下,Slave連接不上master節(jié)點

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
  masterauth 123456
  • 關(guān)閉安全模式
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
#protected-mode yes
 protected-mode no
  • 設(shè)置日志路徑
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
#logfile ""
 logfile "/var/log/redis/redis-server.log"
  • 設(shè)置AOF模式開啟(持久化)
############################## APPEND ONLY MODE ###############################

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

appendonly yes
  • 集群相關(guān)配置
################################ REDIS CLUSTER  ###############################
#
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however
# in order to mark it as "mature" we need to wait for a non trivial percentage
# of users to deploy it in production.
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
# cluster-enabled yes
  cluster-enabled yes

# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
# cluster-config-file nodes-6379.conf
  cluster-config-file nodes-6379.conf

# Cluster node timeout is the amount of milliseconds a node must be unreachable
# for it to be considered in failure state.
# Most other internal time limits are multiple of the node timeout.
#
# cluster-node-timeout 15000
  cluster-node-timeout 15000
  • Dockerfile
FROM centos
MAINTAINER huangyifei justin_yf@sina.com

ENV REDIS_HOME /usr/local
ADD redis-5.0.4.tar.gz /
RUN mkdir -p $REDIS_HOME/redis
ADD redis-5.0.4/redis.conf $REDIS_HOME/redis/

RUN yum -y update
RUN yum -y install vim
RUN yum install -y gcc make

WORKDIR /redis-5.0.4
RUN make
RUN mv /redis-5.0.4/src/redis-server $REDIS_HOME/redis/


WORKDIR /
RUN rm -rf /redis-5.0.4

RUN yum remove -y gcc make

VOLUME ["/var/log/redis"种蘸,"/appendonly.aof"]

EXPOSE 6379
  • 制作執(zhí)行鏡像
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# docker build -t cluster-redis .
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# docker images cluster-redis
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cluster-redis       latest              46e49549692a        51 seconds ago      770 MB
  • 設(shè)置鏡像標(biāo)簽
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# docker tag cluster-redis cluster-redis:5.0.4
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# docker images cluster-redis
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cluster-redis       5.0.4               46e49549692a        11 minutes ago      770 MB
cluster-redis       latest              46e49549692a        11 minutes ago      770 MB

制作Reids節(jié)點鏡像

  • 編寫Dockerfile
FROM cluster-redis:5.0.4
MAINTAINER huangyifei justin_yf@sina.com

ENTRYPOINT ["/usr/local/redis/redis-server","/usr/local/redis/redis.conf"]
  • 構(gòu)建redis節(jié)點鏡像
[root@izwz962mggaelpqejjtvimz docker_redsi_node]# docker build -t nodes-redis:5.0.4 .
  • 查看鏡像
[root@izwz962mggaelpqejjtvimz ~]# docker ps -a
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS                     PORTS                    NAMES
a0eaad0e335a        nodes-redis:5.0.4          "/usr/local/redis/..."   About an hour ago   Up About an hour           6379/tcp                 redis-node6
d289938a3c8f        nodes-redis:5.0.4          "/usr/local/redis/..."   About an hour ago   Up About an hour           6379/tcp                 redis-node5
5e6f16a9bbbb        nodes-redis:5.0.4          "/usr/local/redis/..."   About an hour ago   Up About an hour           6379/tcp                 redis-node4
0cf5311e5d99        nodes-redis:5.0.4          "/usr/local/redis/..."   2 hours ago         Up 2 hours                 6379/tcp                 redis-node3
beef6e750e60        nodes-redis:5.0.4          "/usr/local/redis/..."   2 hours ago         Up 2 hours                 6379/tcp                 redis-node2
9b21d9a2464b        nodes-redis:5.0.4          "/usr/local/redis/..."   2 hours ago         Up 2 hours                 0.0.0.0:6379->6379/tcp   redis-node1

運行redis集群

  • 運行redis容器
[root@izwz962mggaelpqejjtvimz docker_redsi_node]# docker run -d --name redis-node1 -p 6379:6379 nodes-redis:5.0.4
cfbfb4d3d5126c6f986021cd3f55837cf9037447440e2c044ba99d7375479d7a
[root@izwz962mggaelpqejjtvimz docker_redsi_node]# docker run -d --name redis-node2 -p nodes-redis:5.0.4
0c4d097017fb54110f82f3fcffd3f0035067086f3a4f1906f39b57cfb457c50e
[root@izwz962mggaelpqejjtvimz docker_redsi_node]# docker run -d --name redis-node3 -p nodes-redis:5.0.4
c055331f76baae52e6753cbdbc8b2024b830a462185b7276ee785ee48f92503e
  • 查看容器
[root@izwz962mggaelpqejjtvimz docker_redsi_node]# docker ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED              STATUS              PORTS                    NAMES
c055331f76ba        nodes-redis:5.0.4          "/usr/local/redis/..."   35 seconds ago       Up 34 seconds       0.0.0.0:6381->6379/tcp   redis-node3
0c4d097017fb        nodes-redis:5.0.4          "/usr/local/redis/..."   52 seconds ago       Up 51 seconds       0.0.0.0:6380->6379/tcp   redis-node2
cfbfb4d3d512        nodes-redis:5.0.4          "/usr/local/redis/..."   About a minute ago   Up About a minute   0.0.0.0:6379->6379/tcp   redis-node1
e9e382ce5dec        sso-service                "/bin/sh -c '/usr/..."   3 weeks ago          Up 2 weeks          0.0.0.0:8082->8080/tcp   sso-service
f66c22c8c0ff        docker.io/mariadb:latest   "docker-entrypoint..."   3 weeks ago          Up 2 weeks          0.0.0.0:3306->3306/tcp   mariadb
  • 啟動集群
[root@izwz962mggaelpqejjtvimz src]# ./redis-cli --cluster create 172.17.0.3:6379 172.17.0.5:6379 172.17.0.6:6379 172.17.0.7:6379 172.17.0.8:6379 172.17.0.9:6379 --cluster-replicas 1 -a 123456
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 172.17.0.8:6379 to 172.17.0.3:6379
Adding replica 172.17.0.9:6379 to 172.17.0.5:6379
Adding replica 172.17.0.7:6379 to 172.17.0.6:6379
M: 7567e7e809175552c8c1d91709071767605c337c 172.17.0.3:6379
   slots:[0-5460] (5461 slots) master
M: 8a76d2a39d6abdd98097869f6b2c9041205dfe3a 172.17.0.5:6379
   slots:[5461-10922] (5462 slots) master
M: 7219456b036427623d210e83131cde537b54fcbd 172.17.0.6:6379
   slots:[10923-16383] (5461 slots) master
S: 766ac71ba36e430607622fdf459c57100f81ca46 172.17.0.7:6379
   replicates 7219456b036427623d210e83131cde537b54fcbd
S: f05fc4685bc928090c72983ec2cdfe04fc03d8b2 172.17.0.8:6379
   replicates 7567e7e809175552c8c1d91709071767605c337c
S: f32b2042406a40d146abbcf9686e8ae82dc56ebf 172.17.0.9:6379
   replicates 8a76d2a39d6abdd98097869f6b2c9041205dfe3a
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 172.17.0.3:6379)
M: 7567e7e809175552c8c1d91709071767605c337c 172.17.0.3:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: f05fc4685bc928090c72983ec2cdfe04fc03d8b2 172.17.0.8:6379
   slots: (0 slots) slave
   replicates 7567e7e809175552c8c1d91709071767605c337c
M: 8a76d2a39d6abdd98097869f6b2c9041205dfe3a 172.17.0.5:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f32b2042406a40d146abbcf9686e8ae82dc56ebf 172.17.0.9:6379
   slots: (0 slots) slave
   replicates 8a76d2a39d6abdd98097869f6b2c9041205dfe3a
M: 7219456b036427623d210e83131cde537b54fcbd 172.17.0.6:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 766ac71ba36e430607622fdf459c57100f81ca46 172.17.0.7:6379
   slots: (0 slots) slave
   replicates 7219456b036427623d210e83131cde537b54fcbd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 檢查集群狀態(tài)
[root@izwz962mggaelpqejjtvimz src]# ./redis-cli --cluster info localhost:6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
localhost:6379 (7567e7e8...) -> 0 keys | 5461 slots | 1 slaves.
172.17.0.5:6379 (8a76d2a3...) -> 0 keys | 5462 slots | 1 slaves.
172.17.0.6:6379 (7219456b...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
  • 客戶端檢查某一Master集群狀態(tài)
[root@izwz962mggaelpqejjtvimz redis-5.0.4]# ./src/redis-cli -h localhost -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
localhost:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:e67fbbdc16211eeda02bacdf242b76145fae8cbb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
  • 集群架構(gòu)


    image.png

a皮服、一個集群里面有M1恩伺、M2、M3三個節(jié)點,其中節(jié)點 M1包含 0 到 5500號哈希槽裹纳,節(jié)點M2包含5501 到 11000 號哈希槽越驻,節(jié)點M3包含11001 到 16384號哈希槽讳苦。如果M2宕掉了芒炼,就會導(dǎo)致5501 到 11000 號哈希槽不可用,從而使整個集群不可用滨彻。
b藕届、一個集群里面有M1-S1、M2-S2亭饵、M3-S3六個主從節(jié)點休偶,其中節(jié)點 M1包含 0 到 5500號哈希槽,節(jié)點M2包含5501 到 11000 號哈希槽辜羊,節(jié)點M3包含11001 到 16384號哈希槽椅贱。如果是M2宕掉懂算,集群便會選舉S2為新節(jié)點繼續(xù)服務(wù),整個集群還會正常運行庇麦。當(dāng)M2计技、S2都宕掉了,這時候集群就不可用了山橄。

  • 測試集群
[root@izwz962mggaelpqejjtvimz src]# ./redis-cli -c -h localhost -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
localhost:6379> set a 100
-> Redirected to slot [15495] located at 172.17.0.6:6379
OK
172.17.0.6:6379> get a
"100"
172.17.0.6:6379>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末垮媒,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子航棱,更是在濱河造成了極大的恐慌睡雇,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件饮醇,死亡現(xiàn)場離奇詭異它抱,居然都是意外死亡,警方通過查閱死者的電腦和手機朴艰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門观蓄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人祠墅,你說我怎么就攤上這事侮穿。” “怎么了毁嗦?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵亲茅,是天一觀的道長。 經(jīng)常有香客問我狗准,道長克锣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任腔长,我火速辦了婚禮袭祟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘饼酿。我一直安慰自己榕酒,他們只是感情好胚膊,可當(dāng)我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布故俐。 她就那樣靜靜地躺著,像睡著了一般紊婉。 火紅的嫁衣襯著肌膚如雪药版。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天喻犁,我揣著相機與錄音槽片,去河邊找鬼何缓。 笑死,一個胖子當(dāng)著我的面吹牛还栓,可吹牛的內(nèi)容都是我干的碌廓。 我是一名探鬼主播,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼剩盒,長吁一口氣:“原來是場噩夢啊……” “哼谷婆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起辽聊,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤纪挎,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后跟匆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體异袄,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年玛臂,在試婚紗的時候發(fā)現(xiàn)自己被綠了烤蜕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡垢揩,死狀恐怖玖绿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叁巨,我是刑警寧澤斑匪,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站锋勺,受9級特大地震影響蚀瘸,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜庶橱,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一贮勃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧苏章,春花似錦寂嘉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至并淋,卻和暖如春寓搬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背县耽。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工句喷, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留镣典,地道東北人。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓唾琼,卻偏偏與公主長得像兄春,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子锡溯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,107評論 2 356