乐趣区

关于前端:Docker-redis集群部署实战

后面曾经简略相熟过 redis 的下载安装应用,明天接着部署 redis 集群(cluster),简略领会一下 redis 集群的高可用个性。

环境筹备

Redis 是 C 语言开发,装置 Redis 须要先将 Redis 的源码进行编译,编译依赖 gcc 环境

装置gcc-c++

yum install gcc-c++

查看版本

[[email protected] redis]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
[[email protected] redis]# 

Redis 的四种模式

  1. 单机模式
  2. master+slave(主从)
  3. sentinel(哨兵)
  4. cluster(集群)

创立 redis 网卡

[[email protected] ~]# docker network create redis --subnet 172.38.0.0/16
0350d84612bef09bc32b39f15ac6b608dae17cbd75d04b282c0bd37c0283bb7c
[[email protected] ~]# docker network ls
NETWORK ID     NAME           DRIVER    SCOPE
feafa30d4051   bridge         bridge    local
e8bf4fced9e2   host           host      local
0096a971fd2c   mynet          bridge    local
6263db0933b9   none           null      local
0350d84612be   redis          bridge    local
799426d70aa2   test-network   bridge    local
[[email protected] ~]# 

创立 6 个 redis 服务

通过编写脚本创立 6 个 redis 服务

  • cluster-enabled:是否启动集群,选值:yes、no
  • cluster-config-file 配置文件.conf:指定节点信息,主动生成
  • cluster-node-timeout 毫秒值:配置节点连贯超时工夫
  • appendonly:是否开启长久化,选值:yes、no
  • daemonize: 守护过程启动(后盾启动)

/var 目录下创立脚本文件create_redis_script.sh

touch create_redis_script.sh

编写脚本内容

vim create_redis_script.sh,写入上面的内容

for port in $(seq    1 6); \
do \
mkdir -p /var/redisNode/node-${port}/conf
touch /var/redisNode/node-${port}/conf/redis.conf
cat << EOF >/var/redisNode/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
#daemonize yes
EOF
done

查看脚本文件

cat create_redis_script.sh

赋予 create_redis_script.sh 文件可执行权限

[[email protected] ~]# chmod o+x create_redis_script.sh

执行脚本,创立 redis 服务

./create_redis_script.sh

查看 redis 节点

[[email protected] redisNode]# pwd
/var/redisNode
[[email protected] redisNode]# ls
node-1  node-2  node-3  node-4  node-5  node-6
[[email protected] redisNode]#

查看 redis.conf 配置文件

启动 redis 服务

  • -v 挂载数据卷
  • redis:5.0.9-alpine3.11 redis 镜像
  • 6379 是 redis 端口:映射云服务器端口 6371 – 6376
  • 16379 是 redis 集群 TCP 端口:映射云服务器端口 16371 – 16376
  • –net 应用自定义 redis 网络
  • –ip 是本容器绑定的 ip:172.38.0.11 – 16
  • –name 容器名字:redis-1 – 6
  • redis-server /etc/redis/redis.conf 追加命令,通过 redis.conf 启动

启动形式一(不举荐):

一个一个启动

启动第一个 redis 节点

docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /var/redisNode/node-1/data:/data \
-v /var/redisNode/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

如上,相似的,递增 ip、port 和 name,启动其它五个 redis 节点,太麻烦了,能够通过编写脚本来一次启动 6 个服务

启动形式二(举荐):

编写启动脚本,6 个全副启动

创立 start_redis_script.sh 文件

touch start_redis_script.sh

编写脚本内容

vim start_redis_script.sh,写入上面的内容

for port in $(seq    1 6); \
do \
echo ${port}
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /var/redisNode/node-${port}/data:/data \
-v /var/redisNode/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf; \
done

赋予 start_redis_script.sh 文件可执行权限

[[email protected] ~]# chmod o+x start_redis_script.sh

执行脚本,启动 redis 容器服务

./start_redis_script.sh

查看 redis 容器

[[email protected] var]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED              STATUS              PORTS                                              NAMES
0d3c18b61098   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   2 seconds ago        Up 1 second         0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp   redis-6
ee896fb093da   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   13 seconds ago       Up 12 seconds       0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp   redis-5
a83bb7b16dff   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   48 seconds ago       Up 47 seconds       0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp   redis-4
ffe4c9619bed   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   58 seconds ago       Up 57 seconds       0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp   redis-3
a316a31e273f   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp   redis-2
71c072f57c29   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   9 minutes ago        Up 9 minutes        0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp   redis-1
[[email protected] var]# 

创立集群

进入任意一个 Redis 实例

进入 redis- 1 容器

docker exec -it redis-1 /bin/sh

创立集群主节点

  • Redis Cluster 最低要求是 3 个主节点
  • –cluster-replicas 参数为数字,1 示意每个主节点须要 1 个从节点。
# redis-1 容器内
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1

测试连贯集群

连贯集群 -c

# redis-1 容器内
/data # redis-cli -c
127.0.0.1:6379>

查看集群信息 cluster info

127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:292
cluster_stats_messages_pong_sent:307
cluster_stats_messages_sent:599
cluster_stats_messages_ping_received:302
cluster_stats_messages_pong_received:292
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:599
127.0.0.1:6379>

查看节点信息 cluster nodes

127.0.0.1:6379> cluster nodes
b3f737d0d77b823794087011f1501fd43a2ea316 172.38.0.12:[email protected] master - 0 1633332452560 2 connected 5461-10922
506ff2bf1020d468e7d7a81ddbfb616a7e9b7d96 172.38.0.13:[email protected] master - 0 1633332453000 3 connected 10923-16383
7f57f8a01493fa9e46e24ad8a9e46995634f7442 172.38.0.15:[email protected] slave 70ebdca0264c079bb2c33452e6388d37706da282 0 1633332453000 5 connected
70ebdca0264c079bb2c33452e6388d37706da282 172.38.0.11:[email protected] myself,master - 0 1633332452000 1 connected 0-5460
f839b6924977e99d3b74324f2ef70b3678e1a257 172.38.0.14:[email protected] slave 506ff2bf1020d468e7d7a81ddbfb616a7e9b7d96 0 1633332453762 4 connected
bbc4dd0639b90e9ddfb5cef278be04a24276cbdb 172.38.0.16:[email protected] slave b3f737d0d77b823794087011f1501fd43a2ea316 0 1633332453000 6 connected
127.0.0.1:6379> 

测试主从节点替换

验证高可用

set 一个值,而后停掉对应的服务,再次 get,验证是否能够失去值

set 值

127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
172.38.0.13:6379>

发现 a 值设置在了 172.38.0.13 容器上,而后咱们试着去停掉 redis- 3 容器的服务,而后再次 get, 看一下后果

get 值

172.38.0.13:6379> get a
"b"
172.38.0.13:6379>

进行 redis-3 容器里的 redis 服务

docker stop redis-3

再次取值

172.38.0.13:6379> get a
Could not connect to Redis at 172.38.0.13:6379: Host is unreachable
(34.04s)
not connected> exit
# 重连集群
/data # redis-cli -c
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
172.38.0.14:6379> 

发现 a 值被存储到了 172.38.0.14 机器上,172.38.0.14172.38.0.13 的从节点,当主节点 172.38.0.13 产生故障时,主动替换到 172.38.0.13,这就是 高可用

查看 redis 节点服务信息

发现 172.38.0.13 节点 fail, 172.38.0.14 变成master

docker 搭建 redis 集群实现🎈🎈


我是 甜点 cc

酷爱前端,也喜爱专研各种跟本职工作关系不大的技术,技术、产品趣味宽泛且浓重,期待着一个守业机会。本号次要致力于分享集体经验总结,心愿能够给一小部分人一些渺小帮忙。

心愿能和大家一起致力营造一个良好的学习气氛,为了集体和家庭、为了我国的互联网物联网技术、数字化转型、数字经济倒退做一点点奉献。数风流人物还看中国、看今朝、看你我。

退出移动版