安裝環(huán)境準(zhǔn)備
- CentOS
- Docker
創(chuàng)建redis docker基礎(chǔ)鏡像
- 下載安裝包
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# wget http://download.redis.io/releases/redis-5.0.4.tar.gz
- 解壓
[root@izwz962mggaelpqejjtvimz docker_redis_cluster]# tar zxvf redis-5.0.4.tar.gz
- 修改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>