使用Docker安装部署Redis Cluster集群

本文主要展示Redis Cluster的部署步骤。

1准备阶段:

Dockerfile文件内容

FROM alpine:3.7

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN addgroup -S redis && adduser -S -G redis redis

# grab su-exec for easy step-down from root
RUN apk add --no-cache 'su-exec>=0.2'

ENV REDIS_VERSION 4.0.9
ENV REDIS_DOWNLOAD_URL http://download.redis.io/releases/redis-4.0.9.tar.gz
ENV REDIS_DOWNLOAD_SHA df4f73bc318e2f9ffb2d169a922dec57ec7c73dd07bccf875695dbeecd5ec510

# for redis-sentinel see: http://redis.io/topics/sentinel
RUN set -ex; \
	\
	apk add --no-cache --virtual .build-deps \
		coreutils \
		gcc \
		linux-headers \
		make \
		musl-dev \
	; \
	\
	wget -O redis.tar.gz "$REDIS_DOWNLOAD_URL"; \
	echo "$REDIS_DOWNLOAD_SHA *redis.tar.gz" | sha256sum -c -; \
	mkdir -p /usr/src/redis; \
	tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1; \
	rm redis.tar.gz; \
	\
# disable Redis protected mode [1] as it is unnecessary in context of Docker
# (ports are not automatically exposed when running inside Docker, but rather explicitly by specifying -p / -P)
# [1]: https://github.com/antirez/redis/commit/edd4d555df57dc84265fdfb4ef59a4678832f6da
	grep -q '^#define CONFIG_DEFAULT_PROTECTED_MODE 1$' /usr/src/redis/src/server.h; \
	sed -ri 's!^(#define CONFIG_DEFAULT_PROTECTED_MODE) 1$!\1 0!' /usr/src/redis/src/server.h; \
	grep -q '^#define CONFIG_DEFAULT_PROTECTED_MODE 0$' /usr/src/redis/src/server.h; \
# for future reference, we modify this directly in the source instead of just supplying a default configuration flag because apparently "if you specify any argument to redis-server, [it assumes] you are going to specify everything"
# see also https://github.com/docker-library/redis/issues/4#issuecomment-50780840
# (more exactly, this makes sure the default behavior of "save on SIGTERM" stays functional by default)
	\
	make -C /usr/src/redis -j "$(nproc)"; \
	make -C /usr/src/redis install; \
	\
	rm -r /usr/src/redis; \
	\
	runDeps="$( \
		scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
			| tr ',' '\n' \
			| sort -u \
			| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
	)"; \
	apk add --virtual .redis-rundeps $runDeps; \
	apk del .build-deps; \
	\
	redis-server --version

RUN mkdir /data && chown redis:redis /data
VOLUME /data
WORKDIR /data

CMD ["redis-server"]

redis.conf文件内容

port 7000 ##每个容器的端口设置成不一样
pidfile /var/run/redis_6379.pid
maxmemory 50m
appendonly yes
################################ REDIS CLUSTER  ###############################
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 5000
# cluster-slave-validity-factor 10
# cluster-migration-barrier 1
# cluster-require-full-coverage yes
# cluster-slave-no-failover no

################################## SLOW LOG ###################################
slowlog-log-slower-than 10000
slowlog-max-len 128

在7000、7001、7002、7003、7004、7005目录下面各有一个redis.conf,除了port不一致,其他都一致。

2、部署阶段:

[root@localhost home]# pwd
/home
[root@localhost home]# ll
drwxrwxrwx. 3 root root       44 5月  14 23:41 redis
-rwxrwxrwx. 1 root root  1710169 5月  14 13:04 redis-4.0.0.tar.gz
-rwxrwxrwx. 1 root root 15923244 5月  13 23:35 ruby-2.5.1.tar.gz
[root@localhost home]# cd redis
[root@localhost redis]# ll
drwxrwxrwx. 8 root root   78 5月  14 23:41 cluster_conf
-rwxrwxrwx. 1 root root 2352 5月  15 2018 Dockerfile
1、构建redis的docker镜像
[root@localhost redis]# docker build -f Dockerfile -t lee9213/redis:4.0.9 /home/redis/
2、查看镜像构建成功
[root@localhost redis]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
lee9213/redis       4.0.9               49fb78432b3c        About a minute ago   27.8 MB
docker.io/alpine    3.7                 3fd9065eaf02        4 months ago         4.15 MB
3、启动docker容器,redis规定使用docker集群,容器的网络模式必须是host模式
[root@localhost redis]# docker run -t -P --restart=always --net=host --privileged=true -v /home/redis/cluster_conf/7000/redis.conf:/etc/redis.conf --name redis7000  49fb78432b3c redis-server /etc/redis.conf
[root@localhost redis]# docker run -t -P --restart=always --net=host --privileged=true -v /home/redis/cluster_conf/7001/redis.conf:/etc/redis.conf --name redis7001  49fb78432b3c redis-server /etc/redis.conf
[root@localhost redis]# docker run -t -P --restart=always --net=host --privileged=true -v /home/redis/cluster_conf/7002/redis.conf:/etc/redis.conf --name redis7002  49fb78432b3c redis-server /etc/redis.conf
[root@localhost redis]# docker run -t -P --restart=always --net=host --privileged=true -v /home/redis/cluster_conf/7003/redis.conf:/etc/redis.conf --name redis7003  49fb78432b3c redis-server /etc/redis.conf
[root@localhost redis]# docker run -t -P --restart=always --net=host --privileged=true -v /home/redis/cluster_conf/7004/redis.conf:/etc/redis.conf --name redis7004  49fb78432b3c redis-server /etc/redis.conf
[root@localhost redis]# docker run -t -P --restart=always --net=host --privileged=true -v /home/redis/cluster_conf/7005/redis.conf:/etc/redis.conf --name redis7005  49fb78432b3c redis-server /etc/redis.conf
4、查看redis容器启动成功
[root@localhost redis]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
92ff2de33ab5        49fb78432b3c        "redis-server /etc..."   4 seconds ago       Up 3 seconds                            redis7005
87322567c038        49fb78432b3c        "redis-server /etc..."   11 seconds ago      Up 10 seconds                           redis7004
78d3f4f57c15        49fb78432b3c        "redis-server /etc..."   17 seconds ago      Up 16 seconds                           redis7003
face88181023        49fb78432b3c        "redis-server /etc..."   24 seconds ago      Up 23 seconds                           redis7002
4669751d068f        49fb78432b3c        "redis-server /etc..."   31 seconds ago      Up 30 seconds                           redis7001
69ad687fd7d3        49fb78432b3c        "redis-server /etc..."   38 seconds ago      Up 37 seconds                           redis7000
5、查看redis容器端口
[root@localhost redis]# netstat -tnlp | grep redis
tcp        0      0 0.0.0.0:7000            0.0.0.0:*               LISTEN      58842/redis-server  
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      58876/redis-server  
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      58910/redis-server  
tcp        0      0 0.0.0.0:7003            0.0.0.0:*               LISTEN      58945/redis-server  
tcp        0      0 0.0.0.0:7004            0.0.0.0:*               LISTEN      58979/redis-server  
tcp        0      0 0.0.0.0:7005            0.0.0.0:*               LISTEN      59014/redis-server 
[root@localhost redis]# cd ..
6、解压ruby
[root@localhost home]# tar -zxvf  ruby-2.5.1.tar.gz
[root@localhost home]# cd ruby-2.5.1
7、编译ruby
[root@localhost ruby-2.5.1]# ./configure --prefix=/usr/local/ruby
8、安装ruby
[root@localhost ruby-2.5.1]# make && make install
9、设置ruby环境变量
[root@localhost ruby-2.5.1]# vi ~/.bash_profile
10、将.bash_profile中的PATH=$PATH:$HOME/bin修改为PATH=/usr/local/ruby/bin:$PATH:$HOME/bin
11、使.bash_profile生效
[root@localhost home]# source ~/.bash_profile
12、查看ruby安装成功没有
[root@localhost ruby-2.5.1]# ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
13、安装redis的ruby包,安装过程出现问题,解决方案在最后
[root@localhost ruby-2.5.1]# gem install redis
Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 0 seconds
1 gem installed
[root@localhost ruby-2.5.1]# cd ..
14、解压redis源码,安装过程问题,解决方案在最后
[root@localhost home]# tar -zxvf redis-4.0.0.tar.gz 
[root@localhost home]# cd redis-4.0.0
[root@localhost redis-4.0.0]# make
[root@localhost redis-4.0.0]# cd src
15、创建集群
[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: 043600424fc401d14a2f6602a5b690bf43441144 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: d89c1011fb29402aeed179113201b98eb6ff4a1c 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: fe7acf3f59751166f7ed8d7cf1f90e8dbaac35b4 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 8f9226923a030e6213377aeee225b508b970404f 127.0.0.1:7003
   replicates 043600424fc401d14a2f6602a5b690bf43441144
S: 8112b942c00e9e00bbc329da801274fc953847c6 127.0.0.1:7004
   replicates d89c1011fb29402aeed179113201b98eb6ff4a1c
S: a14bafac7cfd1160564aa1f3362d03203ec9cae8 127.0.0.1:7005
   replicates fe7acf3f59751166f7ed8d7cf1f90e8dbaac35b4
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 127.0.0.1:7000)
M: 043600424fc401d14a2f6602a5b690bf43441144 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d89c1011fb29402aeed179113201b98eb6ff4a1c 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 8f9226923a030e6213377aeee225b508b970404f 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 043600424fc401d14a2f6602a5b690bf43441144
M: fe7acf3f59751166f7ed8d7cf1f90e8dbaac35b4 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: a14bafac7cfd1160564aa1f3362d03203ec9cae8 127.0.0.1:7005
   slots: (0 slots) slave
   replicates fe7acf3f59751166f7ed8d7cf1f90e8dbaac35b4
S: 8112b942c00e9e00bbc329da801274fc953847c6 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d89c1011fb29402aeed179113201b98eb6ff4a1c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

gem install redis出现的问题:

问题1:
    ERROR:  Loading command: install (LoadError)
        cannot load such file -- zlib
    ERROR:  While executing gem ... (NoMethodError)
        undefined method `invoke_with_build_args' for nil:NilClass
解决方案:
  1. 安装zlib库
    yum install zlib-devel
  2. 集成zlib到ruby环境
    # 进入ruby源码文件夹 ,安装ruby自身提供的zlib包 
    cd ext/zlib
    ruby ./extconf.rb
    make && make install
    如果make时会爆出错误:没有规则可以创建“zlib.o”需要的目标“/include/ruby.h”。 停止//make:*** No rule to make target `/include/ruby.h', needed by `zlib.o'.  Stop
    则修改Makefile文件添加变量top_srcdir= ../../
问题2:
    ERROR:  While executing gem ... (Gem::Exception)
        Unable to require openssl, install OpenSSL and rebuild Ruby (preferred) or use non-HTTPS sources
解决方案:
  1. 安装openssl库
    yum install openssl-devel 
  2. 集成openssl到ruby环境
    # 进入ruby源码文件夹 ,安装ruby自身提供的zlib包 
    cd ext/openssl
    ruby ./extconf.rb
    make && make install
    如果make时会爆出错误:没有规则可以创建“openssl.o”需要的目标“/include/ruby.h”。 停止//make:*** No rule to make target `/include/ruby.h', needed by `openssl.o'.  Stop
    则修改Makefile文件添加变量top_srcdir= ../../
redis源码make是出现问题:
问题3:
    出现致命错误:jemalloc/jemalloc.h:没有那个文件或目录
解决方案:
    make MALLOC=libc

阅读更多

没有更多推荐了,返回首页