今でもあなたは私の光丶

docker 安装 redis以及redis集群

安装docker

所有主机都需要

  1. Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。 通过 uname -r 命令查看你当前的内核版本
    $ uname -r
  1. 使用 root 权限登录 Centos。确保 yum 包更新到最新。
   $ sudo yum update
  1. 卸载旧版本(如果安装过旧版本的话)
   $ sudo yum remove docker  docker-common docker-selinux docker-engine
  1. 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
   $ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  1. 设置yum源
   $ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  1. 可以查看所有仓库中所有docker版本,并选择特定版本安装
   $ yum list docker-ce --showduplicates | sort -r
  1. 安装docker
   $ sudo yum install docker-ce  #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版19.03.12-3.el7
   $ sudo yum install <FQPN>  # 例如:sudo yum install docker-ce-19.03.12-3.el7
  1. 启动并加入开机启动
   $ sudo systemctl start docker
   $ sudo systemctl enable docker
  1. 验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
   $ docker version

安装redis镜像

所有主机都需要

  1. 搜索docker redis镜像
   docker search redis;
  1. 下载最新alpine redis镜像
   docker pull redis:alpine
  1. 查看镜像
   docker images

Redis主从复制配置

所有主机执行

redis部署规划

主机wxrwcz1(192.168.0.106)wxrwcz2(192.168.0.107)wxrwcz3(192.168.0.108)
redis master1redis master2redis master3
redis master1 slave1、redis master1 slave2、redis master1 slave3redis master2 slave1、redis master2 slave2、redis master2 slave3redis master3 slave1、redis master3 slave2、redis master3 slave3
  1. 配置redis目录以及配置文件
   cd /data
   mkdir redis
   mkdir /data/redis/data
   mkdir /data/redis/data1
   mkdir /data/redis/data2
   mkdir /data/redis/data3
  1. 下载配置文件redis.conf
    wget http://download.redis.io/redis-stable/redis.conf -O /data/redis/redis.conf

    cp /data/redis/redis.conf /data/redis/redis-slave-1.conf
    cp /data/redis/redis.conf /data/redis/redis-slave-2.conf
    cp /data/redis/redis.conf /data/redis/redis-slave-3.conf
  1. 配置redis主从配置文件
   ###主配置文件
   vi redis.confi

   #bind 127.0.0.1        #如果bind选项为空的话,则允许所有来自于可用网络接口的连接
   daemonize yes          #daemonize yes
   protected-mode no      #保护模式,若为yes,只允许本地客户端连接
   ###从配置文件
   vi /data/redis/redis-slave-1.conf
   vi /data/redis/redis-slave-2.conf
   vi /data/redis/redis-slave-3.conf

   port 6379
   replicaof 172.17.0.1 6379  # 172.17.0.1 宿主机ip
  1. 启动redis
   # docker redis 以配置文件运行:
   #docker run -p <容器端口>:<主机端口> --name <容器名> -v <本地配置文件映射容器配置文件> -v <本地文件夹挂载到容器文件夹> -d(表示以守护进程方式启动容器) <启动redis服务并制定配置文件(容器中的路径)>

   docker run -p 6379:6379 -p 26379:26379 --name redis-master  -v /usr/local/docker/data:/data/redis/data -d redis:alpine

   docker run -p 6380:6379 -p 26380:26379 --name redis-slave1 -v /usr/local/docker/data:/data/redis/data1 -d redis:alpine 

   docker run -p 6381:6379 -p 26381:26379 --name redis-slave2 -v /usr/local/docker/data:/data/redis/data2 -d redis:alpine 

   docker run -p 6382:6379 -p 26382:26379 --name redis-slave3 -v /usr/local/docker/data:/data/redis/data3 -d redis:alpine 
  1. 查看redis 虚拟ip
   docker inspect redis-master | grep IPAddress
   docker inspect redis-slave1 | grep IPAddress
   docker inspect redis-slave2 | grep IPAddress
   docker inspect redis-slave3 | grep IPAddress
  1. 配置主从
   # docker exec -it <redis容器名> redis-cli
   docker exec -it redis-master redis-cli
   docker exec -it redis-slave1 redis-cli
   SLAVEOF 172.17.0.2 6379 
   exit
   docker exec -it redis-slave2 redis-cli
   SLAVEOF 172.17.0.2 6379  
   exit
   docker exec -it redis-slave3 redis-cli 
   SLAVEOF 172.17.0.2 6379 
   exit
  1. 测试,查看配置
   docker exec -it redis-master redis-cli
   #放入一个值
   set test test1
   exit
   docker exec -it redis-slave1 redis-cli
   get test
   exit
   docker exec -it redis-slave2 redis-cli
   get test
   exit
   docker exec -it redis-slave3 redis-cli
   get test
   exit

设置哨兵模式

所有主机执行

  1. 配置哨兵模式
   vi /data/redis/sentinel.conf

   port 26379
   sentinel monitor mymaster 172.17.0.2 6379 2
   daemonize yes
   sentinel down-after-milliseconds mymaster 3000
    docker cp /data/redis/sentinel.conf redis-master:/
    docker cp /data/redis/sentinel.conf redis-slave1:/
    docker cp /data/redis/sentinel.conf redis-slave2:/
    docker cp /data/redis/sentinel.conf redis-slave3:/
    docker exec -it redis-master /bin/sh
    cd / && redis-sentinel sentinel.conf && exit
    docker exec -it redis-slave1 /bin/sh
    cd / && redis-sentinel sentinel.conf && exit
    docker exec -it redis-slave2 /bin/sh
    cd / && redis-sentinel sentinel.conf && exit
    docker exec -it redis-slave3 /bin/sh
    cd / && redis-sentinel sentinel.conf && exit
  1. 测试
   docker stop redis-master
   # docker exec -it redis-slave1 redis-cli 127.0.0.1:6379> info replication    ####结果,说明slave3已经成为主节点    # Replication    role:slave    master_host:172.17.0.5    master_port:6379    master_link_status:up    master_last_io_seconds_ago:1    master_sync_in_progress:0    slave_repl_offset:10811    slave_priority:100    slave_read_only:1    connected_slaves:0    master_replid:d8437c8ed1eade313ee3e1f7980fd4d30e7de571    master_replid2:4938f701e7b7e016b9a63a5961d76e22b1cac60c    master_repl_offset:10811    second_repl_offset:4551    repl_backlog_active:1    repl_backlog_size:1048576    repl_backlog_first_byte_offset:1    repl_backlog_histlen:10811    ####    
# docker exec -it redis-slave3 redis-cli    127.0.0.1:6379> info replication    # Replication    ####结果,说明slave3已经成为主节点    role:master    connected_slaves:2    slave0:ip=172.17.0.4,port=6379,state=online,offset=13674,lag=0    slave1:ip=172.17.0.3,port=6379,state=online,offset=13674,lag=0    master_replid:d8437c8ed1eade313ee3e1f7980fd4d30e7de571    master_replid2:4938f701e7b7e016b9a63a5961d76e22b1cac60c    master_repl_offset:14079    second_repl_offset:4551    repl_backlog_active:1    repl_backlog_size:1048576    repl_backlog_first_byte_offset:43    repl_backlog_histlen:14037    ####### 

wxrwcz2(192.168.0.107)上slave2成为主节点

wxrwcz3(192.168.0.108)上slave3成为主节点

启动redis-master,进入redis查看,发现已变成从节点

   docker start redis-master
   docker exec -it redis-master redis-cli
   info replication

集群搭建

  1. 创建redis配置文件(redis-cluster.tmpl)
   mkdir /data/redis-cluster
   vi /data/redis-cluster/redis-cluster.tmpl
   port ${PORT}                                       ##节点端口
   protected-mode no                                  ##开启集群模式
   cluster-enabled yes                                ##cluster集群模式
   cluster-config-file nodes.conf                     ##集群配置名
   cluster-node-timeout 5000                          ##超时时间
   cluster-announce-ip 192.168.XX.XX                  ##实际为各节点网卡分配ip  先用上网关ip代替
   cluster-announce-port ${PORT}                      ##节点映射端口
   cluster-announce-bus-port 1${PORT}                 ##节点总线端口
   appendonly yes                                     ##持久化模式
   [root@wxrwcz1 ~]# cat /data/redis-cluster/redis-cluster.tmpl 
   port ${PORT}
   protected-mode no
   cluster-enabled yes
   cluster-config-file nodes.conf
   cluster-node-timeout 5000
   cluster-announce-ip 192.168.0.106
   cluster-announce-port ${PORT}
   cluster-announce-bus-port 1${PORT}
   appendonly yes
  1. 创建虚拟网卡
   docker network create --driver bridge --subnet 172.18.0.0/16 redis-net  #创建网卡并且指定子网
  1. 为节点创建配置文件
   cd /data/redis-cluster
   for port in `seq 7010 7018`; do \
     base=7008 \
     && ip=$[port-base] \
     && mkdir -p ./${port}/conf \
     && PORT=${port} TEMP=${ip} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
     && mkdir -p ./${port}/data; \
     done
  1. 查看配置文件
   cat 701*/conf/redis.conf
  1. 启动多个redis容器
   $ for port in `seq 7010 7018`; do \
   docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
   -v /data/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
   -v /data/redis-cluster/${port}/data:/data \
   --restart always --name redis-${port} --net redis-net \
   --sysctl net.core.somaxconn=1024 redis:alpine  redis-server /usr/local/etc/redis/redis.conf; \
   done
  1. ##### 查看容器分配ip
   [root@wxrwcz1 redis-cluster]# docker network inspect redis-net
   [
       {
           "Name": "redis-net",
           "Id": "5c96ff41ce289f34bc4b67b70cd498d3f5fef8174c396bc3bdecf5f51a40ea0e",
           "Created": "2020-07-19T18:16:31.094903235+08:00",
           "Scope": "local",
           "Driver": "bridge",
           "EnableIPv6": false,
           "IPAM": {
               "Driver": "default",
               "Options": {},
               "Config": [
                   {
                       "Subnet": "172.18.0.0/16"
                   }
               ]
           },
           "Internal": false,
           "Attachable": false,
           "Ingress": false,
           "ConfigFrom": {
               "Network": ""
           },
           "ConfigOnly": false,
           "Containers": {
               "329988344e5bb52b3f45670e4688a58418d6a503f3a873c19675056ad5b39012": {
                   "Name": "redis-7012",
                   "EndpointID": "d91b32fb3f811da01668f5579c4e247dddc6829098909ae770d03f64cf9d55e2",
                   "MacAddress": "02:42:ac:12:00:04",
                   "IPv4Address": "172.18.0.4/16",
                   "IPv6Address": ""
               },
               "4018953902d50ee7c42195f56ef99307910bca5dae0ef3adf3738bcda26e95d0": {
                   "Name": "redis-7016",
                   "EndpointID": "1c2d0b2b3b6496b5a23d78c486d9ef353a3d057bbd90552ced28715929d9485a",
                   "MacAddress": "02:42:ac:12:00:08",
                   "IPv4Address": "172.18.0.8/16",
                   "IPv6Address": ""
               },
               "6b9dd3f83b20f72cd2eb7e247fc6067628bed2b8b2d0c1fa725ee8b33bc5535c": {
                   "Name": "redis-7014",
                   "EndpointID": "a0ee36e853fde3ae3aee258c5cd046b265157f995b9e4f6ec79226b71bf568df",
                   "MacAddress": "02:42:ac:12:00:06",
                   "IPv4Address": "172.18.0.6/16",
                   "IPv6Address": ""
               },
               "78dc4b20db830237fefa9ff91218a4d7e838c7c836e3cc31260aabf00436dd80": {
                   "Name": "redis-7015",
                   "EndpointID": "358392c16131961aa4c2ebd3eaf37209596322e62201c025a2a7b367ef012d57",
                   "MacAddress": "02:42:ac:12:00:07",
                   "IPv4Address": "172.18.0.7/16",
                   "IPv6Address": ""
               },
               "7d56abb80aab02f360f00b57276092c895e2391f928fcb78e4fd0a95089178d9": {
                   "Name": "redis-7010",
                   "EndpointID": "e0c420de7a9c2fe6974eb7f4866c036835b2a20b2173b8f2ebc9539e47e13252",
                   "MacAddress": "02:42:ac:12:00:02",
                   "IPv4Address": "172.18.0.2/16",
                   "IPv6Address": ""
               },
               "964e60fe8184c59076e944025f475560e92f5deca20515d50f816ef87bbaafc9": {
                   "Name": "redis-7013",
                   "EndpointID": "081a8c4e6350757a10a2c536080386311a1be42b97a0532f600c230b71dcaafc",
                   "MacAddress": "02:42:ac:12:00:05",
                   "IPv4Address": "172.18.0.5/16",
                   "IPv6Address": ""
               },
               "a05686b962e537af0edd9bda5ae279f25b9a98ae87edad944917ac1d2f19e3c5": {
                   "Name": "redis-7017",
                   "EndpointID": "da4162d5162bffe6e54140b4a0ddc08da0b068ce6b699614923d623f64e56e5a",
                   "MacAddress": "02:42:ac:12:00:09",
                   "IPv4Address": "172.18.0.9/16",
                   "IPv6Address": ""
               },
               "c36c2d5cd2175ac89d5b319d8f9d5f5e2f75e0a5ea2d8d8cffa42f041c3bcc0c": {
                   "Name": "redis-7018",
                   "EndpointID": "34444456b5016ea6f6fa170dcf173067c87645139607291f7ec85b024ee4a21f",
                   "MacAddress": "02:42:ac:12:00:0a",
                   "IPv4Address": "172.18.0.10/16",
                   "IPv6Address": ""
               },
               "d1e137245e8f38fe158b088b8380e8f70b9ad5027aad1fb12e9cbcdf784e3046": {
                   "Name": "redis-7011",
                   "EndpointID": "246302cfcc0856c9062938a4ba35da5f8ec3e20efa90ca168f7dc9b266888a62",
                   "MacAddress": "02:42:ac:12:00:03",
                   "IPv4Address": "172.18.0.3/16",
                   "IPv6Address": ""
               }
           },
           "Options": {},
           "Labels": {}
       }
   ]
  1. 启动集群
   docker exec -it redis-7010 redis-cli --cluster create 172.18.0.2:7010  172.18.0.3:7011  172.18.0.4:7012  172.18.0.5:7013  172.18.0.6:7014  172.18.0.7:7015  172.18.0.8:7016  172.18.0.9:7017  172.18.0.10:7018 --cluster-replicas 2
  1. ##### 连接客户端,查看主从信息
   docker exec -it redis-7010 redis-cli -c -p 7010
   info replication
  1. 查看集群配置是否成功
   docker exec -it redis-7010 /bin/sh
   cat nodes.conf

集群新增一主一从(Master4和Slaver4)

  1. 为新节点创建配置文件
   cd /data/redis-cluster
   for port in `seq 7019 7020`; do \
     base=7008 \
     && ip=$[port-base] \
     && mkdir -p ./${port}/conf \
     && PORT=${port} TEMP=${ip} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
     && mkdir -p ./${port}/data; \
     done
  1. 启动新的redis 容器
   for port in `seq 7019 7020`; do \
   docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
   -v /data/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
   -v /data/redis-cluster/${port}/data:/data \
   --restart always --name redis-${port} --net redis-net \
   --sysctl net.core.somaxconn=1024 redis:alpine  redis-server /usr/local/etc/redis/redis.conf; \
   done
  1. 查看新的容器,ip
   docker network inspect redis-net
  1. 集群添加
   docker exec -it redis-7010 redis-cli --cluster add-node 172.18.0.11:7019 172.18.0.2:7010
   docker exec -it redis-7010 redis-cli -c -p 7010
   cluster nodes;
  1. 添加完主节点需要对主节点进行hash槽分配,这样该主节才可以存储数据。
   docker exec -it redis-7010 redis-cli --cluster reshard 172.18.0.11:7019 
   # 要分配的槽数量
   How many slots do you want to move (from 1 to 16384)? 3000
   # 输入接收槽的结点id
   What is the receiving node ID?
   481958023b907f8e8ff35e8d0dd0d69ce544d715
   # 输入源结点id
   Please enter all the source node IDs.
   Type 'all' to use all the nodes as source nodes for the hash slots.
   Type 'done' once you entered all the source nodes IDs.
   # 输入all
   all
   # 输入yes开始移动槽到目标结点id
   yes
   # 查看结果
   docker exec -it redis-7010 redis-cli -c -p 7010
   cluster nodes
  1. 添加从节点
   # docker exec -it redis-7010 redis-cli --cluster add-node 新节点的ip和端口 旧节点ip和端口 --cluster-slave --cluster-master-id 主节点id
   docker exec -it redis-7010 redis-cli --cluster add-node 172.18.0.12:7020 172.18.0.11:7019 --cluster-slave --cluster-master-id 481958023b907f8e8ff35e8d0dd0d69ce544d715

查看结果

   docker exec -it redis-7010 redis-cli -c -p 7010
   cluster nodes

通过JedisCluster向RedisCluster添加数据和取出数据

将redisdemo.zip解压并导入idea

添加数据

cn.wxrwcz.redisdemo.RedisdemoApplicationTests#testSet

测试set方法

    @Test
    void testSet() {
        jedisCluster.set("test", "38967");
    }

取出数据

cn.wxrwcz.redisdemo.RedisdemoApplicationTests#testGet

    @Test
    void testGet() {
        String string = jedisCluster.get("test");
        System.out.println(string);
    }