欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker学习笔记-Redis集群搭建

程序员文章站 2022-07-05 13:22:37
...

Linux版本:Ubuntu 16.04.2 LTS

Docker版本:Docker version 17.09.0-ce, build afdb6d4

Docker Compose版本:docker-compose version 1.22.0, build f46880fe

JDK版本:java version "1.8.0_102"

Redis版本:redis-5.0.5

相关构建配置文件如下:

Redis Dockerfile

From ubuntu:16.04.4
MAINTAINER xxxxx

RUN apt-get update && apt-get install -y build-essential make net-tools iputils-ping vim iptables

ADD redis-5.0.5.tar.gz /usr/local/
RUN cd /usr/local/redis-5.0.5 && make && make install

ENV REDIS_HOME=/usr/local/redis-5.0.5 \
    REDIS_CONF_DIR=/usr/local/redis-5.0.5/conf \
    REDIS_DATA_DIR=/usr/local/redis-5.0.5/data \
    REDIS_LOG_DIR=/usr/local/redis-5.0.5/logs \
    REDIS_PID_DIR=/usr/local/redis-5.0.5/pids

RUN mkdir -p "$REDIS_CONF_DIR" "$REDIS_DATA_DIR" "REDIS_LOG_DIR" "REDIS_PID_DIR"

WORKDIR $REDIS_HOME
VOLUME ["$REDIS_DATA_DIR", "$REDIS_LOG_DIR", "$REDIS_PID_DIR"]

EXPOSE 6379 16379

COPY redis.conf /usr/local/redis-5.0.5/conf/
COPY redis-entrypoint.sh /
RUN chmod +x /redis-entrypoint.sh
ENTRYPOINT ["/redis-entrypoint.sh"]

redis.conf

#bind 127.0.0.1
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
#daemonize yes
supervised no
pidfile /usr/local/redis-5.0.5/pids/redis.pid
loglevel notice
logfile /usr/local/redis-5.0.5/logs/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis-5.0.5/data
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
cluster-enabled yes
cluster-config-file /usr/local/redis-5.0.5/conf/cluster.conf
cluster-node-timeout 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

redis-entrypoint.sh

#!/bin/bash

set -e

redis-server /usr/local/redis-5.0.5/conf/redis.conf

exec "[email protected]"

docker build --no-cache -t redis:5.0.5 images/redis

docker-compose.yml

version: '3.4'

services:
  redis1:
    image: redis:5.0.5
    restart: always
    hostname: redis1
    container_name: redis1
    ports:
    - 16373:6379
    - 26373:16379
    volumes:
    - "/home/docker/redis-5.0.5/cluster/6373/data:/usr/local/redis-5.0.5/data"
    - "/home/docker/redis-5.0.5/cluster/6373/logs:/usr/local/redis-5.0.5/logs"
    - "/home/docker/redis-5.0.5/cluster/6373/pids:/usr/local/redis-5.0.5/pids"
    environment:
      REDIS_PORT: 6373

  redis2:
    image: redis:5.0.5
    restart: always
    hostname: redis2
    container_name: redis2
    ports:
    - 16374:6379
    - 26374:16379
    volumes:
    - "/home/docker/redis-5.0.5/cluster/6374/data:/usr/local/redis-5.0.5/data"
    - "/home/docker/redis-5.0.5/cluster/6374/logs:/usr/local/redis-5.0.5/logs"
    - "/home/docker/redis-5.0.5/cluster/6374/pids:/usr/local/redis-5.0.5/pids"
    environment:
      REDIS_PORT: 6374

  redis3:
    image: redis:5.0.5
    restart: always
    hostname: redis3
    container_name: redis3
    ports:
    - 16375:6379
    - 26375:16379
    volumes:
    - "/home/docker/redis-5.0.5/cluster/6375/data:/usr/local/redis-5.0.5/data"
    - "/home/docker/redis-5.0.5/cluster/6375/logs:/usr/local/redis-5.0.5/logs"
    - "/home/docker/redis-5.0.5/cluster/6375/pids:/usr/local/redis-5.0.5/pids"
    environment:
      REDIS_PORT: 6375

  redis4:
    image: redis:5.0.5
    restart: always
    hostname: redis4
    container_name: redis4
    ports:
    - 16376:6379
    - 26376:16379
    volumes:
    - "/home/docker/redis-5.0.5/cluster/6376/data:/usr/local/redis-5.0.5/data"
    - "/home/docker/redis-5.0.5/cluster/6376/logs:/usr/local/redis-5.0.5/logs"
    - "/home/docker/redis-5.0.5/cluster/6376/pids:/usr/local/redis-5.0.5/pids"
    environment:
      REDIS_PORT: 6376

  redis5:
    image: redis:5.0.5
    restart: always
    hostname: redis5
    container_name: redis5
    ports:
    - 16377:6379
    - 26377:16379
    volumes:
    - "/home/docker/redis-5.0.5/cluster/6377/data:/usr/local/redis-5.0.5/data"
    - "/home/docker/redis-5.0.5/cluster/6377/logs:/usr/local/redis-5.0.5/logs"
    - "/home/docker/redis-5.0.5/cluster/6377/pids:/usr/local/redis-5.0.5/pids"
    environment:
      REDIS_PORT: 6377

  redis6:
    image: redis:5.0.5
    restart: always
    hostname: redis6
    container_name: redis6
    ports:
    - 16378:6379
    - 26378:16379
    volumes:
    - "/home/docker/redis-5.0.5/cluster/6378/data:/usr/local/redis-5.0.5/data"
    - "/home/docker/redis-5.0.5/cluster/6378/logs:/usr/local/redis-5.0.5/logs"
    - "/home/docker/redis-5.0.5/cluster/6378/pids:/usr/local/redis-5.0.5/pids"
    environment:
      REDIS_PORT: 6378

  redis7:
    image: redis:5.0.5
    restart: always
    hostname: redis7
    container_name: redis7
    ports:
    - 16379:6379
    - 26379:16379
    volumes:
    - "/home/docker/redis-5.0.5/cluster/6379/data:/usr/local/redis-5.0.5/data"
    - "/home/docker/redis-5.0.5/cluster/6379/logs:/usr/local/redis-5.0.5/logs"
    - "/home/docker/redis-5.0.5/cluster/6379/pids:/usr/local/redis-5.0.5/pids"
    environment:
      REDIS_PORT: 6378
    command: redis-cli --cluster create 172.25.0.2:6379 172.25.0.3:6379 172.25.0.4:6379 172.25.0.5:6379 172.25.0.6:6379 172.25.0.7:6379 --cluster-replicas 1
    depends_on:
    - redis1
    - redis2
    - redis3
    - redis4
    - redis5
    - redis6

docker-compose up -d

进入容器redis7测试验证Redis集群

# docker exec -it redis7 /bin/bash

redis-5.0.0版本开始,redis-trib.rb的功能被redis-cli替代

# redis-cli --cluster check 172.25.0.2:6379

# redis-cli --cluster info 172.25.0.2:6379

# redis-cli --cluster add-node 172.25.0.9:6379

# redis-cli --cluster add-node 172.25.0.10:6379 --cluster-slave --cluster-master-id 58f0b38a4e605b3369d3843a89a2b4a164ed21e8

# redis-cli --cluster del-node 172.25.0.10:6379 48f0b38a4e605b3369d3843a89a2b4a164ed21e8

# redis-cli -c -h 172.25.0.2 -p 6379

172.25.0.2:6379> cluster info

 

Redis 5 以前版本集群环境的构建可以参考:

Redis 4.x.x Cluster Dockerfile

From redis:4.x.x
MAINTAINER xxxxx

ADD zlib-1.2.11.tar.gz /usr/local/
RUN cd /usr/local/zlib-1.2.11 && ./configure && make && make install

ADD openssl-1.0.2l.tar.gz /usr/local/
RUN cd /usr/local/openssl-1.0.2l && ./config && make && make install

ADD ruby-2.6.3.tar.gz /usr/local/
RUN cd /usr/local/ruby-2.6.3 && ./configure --with-openssl-dir=/usr/local/openssl-1.0.2l && make && make install

COPY redis-4.1.2.gem /usr/local/redis-4.x.x/
RUN gem install -l /usr/local/redis-5.0.5/redis-4.1.2.gem

COPY redis-cluster-entrypoint.sh /
RUN chmod +x /redis-cluster-entrypoint.sh
ENTRYPOINT ["/redis-cluster-entrypoint.sh"]

redis-trib.rb create --replicas 1 172.25.0.2:6379 172.25.0.3:6379 172.25.0.4:6379 172.25.0.5:6379 172.25.0.6:6379 172.25.0.7:6379

 

相关标签: redis docker