docker(redis)

docker(redis)

redis

# 拉取镜像
docker pull redis:6.0.5
# 先将配置文件放入到指定目录下,下面纯粹是在浪费篇幅,可以直接在百度下载。
# 启动
docker run --restart=always --log-driver json-file --log-opt max-size=100m --log-opt max-file=2 -p 6666:6666 --name cxyredis -v /usr/local/database/redis/redis.conf:/etc/redis/redis.conf -v /usr/local/database/redis:/data -d redis:6.0.5 redis-server /etc/redis/redis.conf --appendonly yes --requirepass "123456"

docker run --restart=always --log-driver json-file --log-opt max-size=100m --log-opt max-file=2 -p 6666:6666 --name cxyredis -v /usr/local/docker/redis/redis.conf:/etc/redis/redis.conf -v /usr/local/docker/redis:/data -d redis:6.0.5 redis-server /etc/redis/redis.conf --appendonly yes --requirepass "lauyang3959."

启动redis配置文件

文件在上级目录文件夹中。

哨兵

# 配置文件挂载一下,最低配置,内容如下 /etc/redis/sentinel.conf
sentinel monitor myredis 127.0.0.1 7000 1
# 创建一个哨兵容器
docker run --restart=always --log-driver json-file --log-opt max-size=100m --log-opt max-file=2 -p 7003:7003 --name redisSentinel -v /usr/local/database/redis/redis.conf:/etc/redis/redis.conf -v /usr/local/database/redis/redis-cluster/sentinel.conf:/etc/redis/sentinel.conf -d redis:6.0.5 redis-server /etc/redis/redis.conf --appendonly yes
# 访问	redis-sentinel /etc/redis/sentinel.conf
# 启动哨兵
[root@centos8 redis-cluster]# docker exec -it 48f8725736c3 redis-sentinel /etc/redis/sentinel.conf

主从配置

# 编写模版文件名为:redis-cluster.tmpl ,路径放在 /usr/local/database/redis/redis-cluster ---------------------
# 基本配置
protected-mode yes
port ${port}
bind 0.0.0.0 
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_${port}.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump${port}.rdb
rdb-del-sync-files no
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128

 # requirepass 
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no


appendonly no

appendfilename "appendonly.aof"

appendfsync everysec


auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb


aof-load-truncated yes


aof-use-rdb-preamble yes

lua-time-limit 5000
slowlog-log-slower-than 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

stream-node-max-bytes 4096
stream-node-max-entries 100

activerehashing yes
hz 10
dynamic-hz yes



# 创建配置脚本-----------------------------------------------------------------------------------------------

# 主目录
dir_redis_cluster='/usr/local/database/redis/redis-cluster'
# 节点地址号 从2开始
idx=1
# 逐个创建各节点目录和配置文件 三个
for port in `seq 7000 7002`; do
    # 创建存放redis数据路径
    mkdir -p ${dir_redis_cluster}/${port}/data;
    # 通过模板个性化各个节点的配置文件
    idx=$(($idx+1));
    port=${port} ip=`echo ${gateway} | sed "s/1$/$idx/g"` \
        envsubst < ${dir_redis_cluster}/redis-cluster.tmpl \
        > ${dir_redis_cluster}/${port}/redis-${port}.conf
done
# 配置并启动-----------------------------------------------------------------------------------------------
# 创建容器配置并运行 redis.conf后面你的版本,默认是最新。
for port in `seq 7000 7002`; do
    docker run --name redis-${port} --net redis-cluster-net -d \
        -p ${port}:${port} -p 1${port}:1${port} \
        -v ${dir_redis_cluster}/${port}/data:/data \
        -v ${dir_redis_cluster}/${port}/redis-${port}.conf:/usr/local/etc/redis/redis.conf redis \
        redis-server /usr/local/etc/redis/redis.conf
done
# 查看集群功能是否开启,这里需要让它不成功 info cluster-----------------------------------------------------------
[root@centos8 ~]# docker exec -it redis-7000 redis-cli -p 7000 info cluster
# Cluster
cluster_enabled:0
# 其他操作(注意自己的路径)--------------------------------------------------------------------------
#!/bin/bash
# 外部输入命令
com=$1
# 主目录
dir_redis_cluster='/usr/local/database/redis/redis-cluster'
# redis集群网关
gateway='172.18.0.1'

case ${com} in
	create)
        idx=1;
		for port in `seq 7000 7005`; do
            # 创建存放redis数据路径
			mkdir -p ${dir_redis_cluster}/${port}/data;
            # 通过模板个性化各个节点的配置文件
            idx=$(($idx+1));
            port=${port} ip=`echo ${gateway} | sed "s/1$/$idx/g"` \
                envsubst < ${dir_redis_cluster}/redis-cluster.tmpl \
                > ${dir_redis_cluster}/${port}/redis-${port}.conf
		done
	;;
    build)
        # 创建容器配置并运行
        for port in `seq 7000 7005`; do
            docker run --name redis-${port} --net redis-cluster-net -d \
            	-p ${port}:${port} -p 1${port}:1${port} \
                -v ${dir_redis_cluster}/${port}/data:/data \
                -v ${dir_redis_cluster}/${port}/redis-${port}.conf:/usr/local/etc/redis/redis.conf redis \
                redis-server /usr/local/etc/redis/redis.conf
        done
    ;;
    start | begin)
        # 运行容器
    	for port in `seq 7000 7002`; do
            docker start redis-${port}
        done
    ;;
    stop | end)
        # 停止容器运行
        for port in `seq 7000 7002`; do
            docker stop redis-${port}
        done
    ;;
    rm)
        # 删除已有容器
        for port in `seq 7000 7002`; do
            docker rm redis-${port}
        done
    ;;
    restart)
        # 重启已有容器
    	for port in `seq 7000 7002`; do
            docker restart redis-${port}
        done
    ;;
    destroy)
        # 删除集群目录及配置
        for port in `seq 7000 7002`; do
            rm -rf ${dir_redis_cluster}/${port}
        done
    ;;
    *)
        echo "Usage:	./build [create|build|start|stop|rm|restart|destroy]"
    ;;
esac
# 重点如下。-------------------------------------------------------------------------------------------------
[root@centos8 redis-cluster]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                        NAMES
2157b7f9d97d        redis               "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds        0.0.0.0:7002->7002/tcp, 6379/tcp, 0.0.0.0:17002->17002/tcp   redis-7002
f1ac84714bf7        redis               "docker-entrypoint.s…"   6 seconds ago       Up 4 seconds        0.0.0.0:7001->7001/tcp, 6379/tcp, 0.0.0.0:17001->17001/tcp   redis-7001
0db76b9cbe64        redis               "docker-entrypoint.s…"   6 seconds ago       Up 5 seconds        0.0.0.0:7000->7000/tcp, 6379/tcp, 0.0.0.0:17000->17000/tcp   redis-7000
# 主机
127.0.0.1:7000> info replication #查看主从信息
# Replication
role:master
connected_slaves:2
slave0:ip=172.18.0.1,port=7001,state=online,offset=392,lag=0
slave1:ip=172.18.0.1,port=7002,state=online,offset=392,lag=1
master_replid:e2c07eadf81344899ffd7cec60036686885c2947
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:392
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:392
# 从机,得用ip,我真是服了。就算是本机127也不行。SLAVEOF 192.168.106.129 7000
[root@centos8 ~]# docker exec -it redis-7001 /bin/bash
root@cb475fe3b9e0:/data# redis-cli -h 192.168.106.129 -p 7001
192.168.106.129:7001> info replication
# Replication
role:master
connected_slaves:0
master_replid:868aadcb711e66461f5cd50b5ef59cf9cacbb26c
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
192.168.106.129:7001> SLAVEOF 192.168.106.129 7000 #设置为从机,二号机也是一样。
OK
192.168.106.129:7001> info replication 
# Replication
role:slave
master_host:192.168.106.129
master_port:7000
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:336
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:e2c07eadf81344899ffd7cec60036686885c2947
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:336
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:336

集群模式

# 普通方式
port 6378
pidfile /var/run/redis_6378.pid
logfile "/dev/redis78.log"
dbfilename dump6378.rdb
# docker方式-----------------------------------------------------------------------------------------------
# 创建属于redis的集群网络------------------------------------------------------------------------------------
docker network create redis-cluster-net
# 查看ip 172.18.0.0----------------------------------------------------------------------------------------
[root@centos8 ~]# docker network inspect redis-cluster-net
[
    {
        "Name": "redis-cluster-net",
        "Id": "c3550af56a4c5a4894e04963b5b747361122e3666f58bd1e88e854f4b295f316",
        "Created": "2020-07-29T04:42:39.479861603-04:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "2d6356126ef5b579a189af1e89e8b777cdb12f538a2e03bb8816e4308a20490f": {
                "Name": "redis-7002",
                "EndpointID": "183d44748f7586bc812b0a714678de09cc7b931b8f08fcc5d24bfb303a1893d3",
                "MacAddress": "02:42:ac:12:00:04",
                "IPv4Address": "172.18.0.4/16",
                "IPv6Address": ""
            },
            "32525d24f5264aebe9bcabe20c5ae9456b4f9a9945d08e622fe65bb5818b513a": {
                "Name": "redis-7000",
                "EndpointID": "262490e1bd204de27db1c22128efacee6bff77e49aefae24df8c6fb8b76d0744",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "50403e09aae463ff61a2a23621eb1d1b93b7fa40f0edb12b279d79fbba9eea4b": {
                "Name": "redis-7001",
                "EndpointID": "c10219728746f7e328d3528f56dcf011b0f8fbc42f628a05dd377146bb0d9d85",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "637d28ddb99c7bf82fcfb222d0244918b43977a220e8f61caea264c4aa6e97ea": {
                "Name": "redis-7003",
                "EndpointID": "4b6ac8bdd02c91019b97989c78d411f966cce93f2833e799be1570f065720499",
                "MacAddress": "02:42:ac:12:00:05",
                "IPv4Address": "172.18.0.5/16",
                "IPv6Address": ""
            },
            "b0eeb13258499f64a5d4308dbbbbef53747af9a185c01af78f1f0c597b299d99": {
                "Name": "redis-7005",
                "EndpointID": "53be06c5a30cb88848edd2e57b3c559b701d03a9aab541dd414d6ba38240a99f",
                "MacAddress": "02:42:ac:12:00:07",
                "IPv4Address": "172.18.0.7/16",
                "IPv6Address": ""
            },
            "f954451e3775a9921566d6b91007ff196eeb385f4ee4e12a91bb14dda14dabed": {
                "Name": "redis-7004",
                "EndpointID": "da9bac581d4cc0586c11ecd2eaceb6408fe45f06b94abf99f85a31cc44c002ba",
                "MacAddress": "02:42:ac:12:00:06",
                "IPv4Address": "172.18.0.6/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]
# 编写模版文件名为:redis-cluster.tmpl ,路径放在 /usr/local/database/redis/redis-cluster ---------------------
# 基本配置
## 开放端口
port ${port}
## 不作为守护进程
daemonize no
## 启用aof持久化模式
appendonly yes

# 集群配置
## 开启集群配置
cluster-enabled yes
## 存放集群节点的配置文件 系统自动建立
cluster-config-file nodes-${port}.conf
## 节点连接超时时间
cluster-node-timeout 50000  
## 实际为各节点网卡分配ip
cluster-announce-ip ${ip}
## 节点映射端口
cluster-announce-port ${port}
## 节点总线端口
cluster-announce-bus-port 1${port}
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-require-full-coverage yes

# 创建配置脚本-----------------------------------------------------------------------------------------------

# 主目录
dir_redis_cluster='/usr/local/database/redis/redis-cluster'
# docker redis集群网关
gateway='172.18.0.1'
# 节点地址号 从2开始
idx=1
# 逐个创建各节点目录和配置文件
for port in `seq 7000 7005`; do
    # 创建存放redis数据路径
    mkdir -p ${dir_redis_cluster}/${port}/data;
    # 通过模板个性化各个节点的配置文件
    idx=$(($idx+1));
    port=${port} ip=`echo ${gateway} | sed "s/1$/$idx/g"` \
        envsubst < ${dir_redis_cluster}/redis-cluster.tmpl \
        > ${dir_redis_cluster}/${port}/redis-${port}.conf
done
# 配置并启动-----------------------------------------------------------------------------------------------
# 创建容器配置并运行 redis.conf后面你的版本,默认是最新。
for port in `seq 7000 7005`; do
    docker run --name redis-${port} --net redis-cluster-net -d \
        -p ${port}:${port} -p 1${port}:1${port} \
        -v ${dir_redis_cluster}/${port}/data:/data \
        -v ${dir_redis_cluster}/${port}/redis-${port}.conf:/usr/local/etc/redis/redis.conf redis \
        redis-server /usr/local/etc/redis/redis.conf
done
# 查看集群功能是否开启 info cluster--------------------------------------------------------------------------
[root@centos8 ~]# docker exec -it redis-7000 redis-cli -p 7000 info cluster
# Cluster
cluster_enabled:1
# 节点连接,一条条执行 --------------------------------------------------------------------------
docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.3 7001
docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.4 7002
docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.5 7003
docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.6 7004
docker exec -it redis-7000 redis-cli -p 7000 cluster meet 172.18.0.7 7005
# 进入7000执行 cluster nodes --------------------------------------------------------------------------
127.0.0.1:7000> cluster nodes
33cc63c86da4d85152990cf30534ced11b7abd73 172.18.0.2:7000@17000 myself,master - 0 1596013060000 1 connected
5137b18fe4f3d975c1513f5761ffa8b9615ff077 172.18.0.3:7001@17001 master - 0 1596013059000 2 connected
a61eab3d027b9ee4f521717cfba37b5ef8f19ad5 172.18.0.4:7002@17002 master - 0 1596013060598 3 connected
f543363850c50bb90cd34694e4dc7235df4ea399 172.18.0.5:7003@17003 master - 0 1596013061613 0 connected
955809a4a1a643719eafd3fc04f98ed9800cacb6 172.18.0.6:7004@17004 master - 0 1596013059587 4 connected
10e55a0222db69b711e146ef134adb45725693c1 172.18.0.7:7005@17005 master - 0 1596013058000 5 connected
# 设置主从节点,注意对应你自己的节点--------------------------------------------------------------------------
# 设置7001节点为7000节点的从节点
docker exec -it redis-7001 redis-cli -p 7001 cluster replicate 33cc63c86da4d85152990cf30534ced11b7abd73 # 7001 --> 7000
# 设置7003节点为7002节点的从节点
docker exec -it redis-7003 redis-cli -p 7003 cluster replicate a61eab3d027b9ee4f521717cfba37b5ef8f19ad5 # 7003 --> 7002
# 设置7005节点为7004节点的从节点
docker exec -it redis-7005 redis-cli -p 7005 cluster replicate 955809a4a1a643719eafd3fc04f98ed9800cacb6 # 7005 --> 7004
# 将16384个槽分配到3个主节点去, 每个节点平均分的5461个槽---------------------------------------------------------
# 7000 0~5460
docker exec -it redis-7000 redis-cli -p 7000 cluster addslots {0..5460}
# 7002 5461~10920
docker exec -it redis-7002 redis-cli -p 7002 cluster addslots {5461..10920}
# 7004 10920~16383
docker exec -it redis-7004 redis-cli -p 7004 cluster addslots {10921..16383}
# 测试------------------------------------------------------------------------------------------------------
# cluster slots
127.0.0.1:7000> cluster slots
1) 1) (integer) 0
   2) (integer) 5460
   3) 1) "172.18.0.2"
      2) (integer) 7000
      3) "33cc63c86da4d85152990cf30534ced11b7abd73"
   4) 1) "172.18.0.3"
      2) (integer) 7001
      3) "5137b18fe4f3d975c1513f5761ffa8b9615ff077"
2) 1) (integer) 10921
   2) (integer) 16383
   3) 1) "172.18.0.6"
      2) (integer) 7004
      3) "955809a4a1a643719eafd3fc04f98ed9800cacb6"
   4) 1) "172.18.0.7"
      2) (integer) 7005
      3) "10e55a0222db69b711e146ef134adb45725693c1"
3) 1) (integer) 5461
   2) (integer) 10920
   3) 1) "172.18.0.4"
      2) (integer) 7002
      3) "a61eab3d027b9ee4f521717cfba37b5ef8f19ad5"
   4) 1) "172.18.0.5"
      2) (integer) 7003
      3) "f543363850c50bb90cd34694e4dc7235df4ea399"

# 其他操作(注意自己的路径)--------------------------------------------------------------------------
#!/bin/bash
# 外部输入命令
com=$1
# 主目录
dir_redis_cluster='/usr/local/database/redis/redis-cluster'
# redis集群网关
gateway='172.18.0.1'

case ${com} in
	create)
        idx=1;
		for port in `seq 7000 7005`; do
            # 创建存放redis数据路径
			mkdir -p ${dir_redis_cluster}/${port}/data;
            # 通过模板个性化各个节点的配置文件
            idx=$(($idx+1));
            port=${port} ip=`echo ${gateway} | sed "s/1$/$idx/g"` \
                envsubst < ${dir_redis_cluster}/redis-cluster.tmpl \
                > ${dir_redis_cluster}/${port}/redis-${port}.conf
		done
	;;
    build)
        # 创建容器配置并运行
        for port in `seq 7000 7005`; do
            docker run --name redis-${port} --net redis-cluster-net -d \
            	-p ${port}:${port} -p 1${port}:1${port} \
                -v ${dir_redis_cluster}/${port}/data:/data \
                -v ${dir_redis_cluster}/${port}/redis-${port}.conf:/usr/local/etc/redis/redis.conf redis \
                redis-server /usr/local/etc/redis/redis.conf
        done
    ;;
    start | begin)
        # 运行容器
    	for port in `seq 7000 7005`; do
            docker start redis-${port}
        done
    ;;
    stop | end)
        # 停止容器运行
        for port in `seq 7000 7005`; do
            docker stop redis-${port}
        done
    ;;
    rm)
        # 删除已有容器
        for port in `seq 7000 7005`; do
            docker rm redis-${port}
        done
    ;;
    restart)
        # 重启已有容器
    	for port in `seq 7000 7005`; do
            docker restart redis-${port}
        done
    ;;
    destroy)
        # 删除集群目录及配置
        for port in `seq 7000 7005`; do
            rm -rf ${dir_redis_cluster}/${port}
        done
    ;;
    *)
        echo "Usage:	./build [create|build|start|stop|rm|restart|destroy]"
    ;;
esac

# 最终效果
[root@centos8 ~]# docker exec -it redis-7000 redis-cli -c -p 7000
127.0.0.1:7000> set name 1
-> Redirected to slot [5798] located at 172.18.0.4:7002
OK
172.18.0.4:7002> keys *
1) "name"
172.18.0.4:7002> set age 18
-> Redirected to slot [741] located at 172.18.0.2:7000
OK

# java测试
public class TestRedisConnect {
    @Test
    public void connectCluster() {
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("127.0.0.1", 7000));
        nodes.add(new HostAndPort("127.0.0.1", 7001));
        nodes.add(new HostAndPort("127.0.0.1", 7002));
        nodes.add(new HostAndPort("127.0.0.1", 7003));
        nodes.add(new HostAndPort("127.0.0.1", 7004));
        nodes.add(new HostAndPort("127.0.0.1", 7005));

        JedisCluster cluster = new JedisCluster(nodes, 5000);

        System.out.println(cluster.get("hello"));

        cluster.set("test2", "6739");
        System.out.println(cluster.get("test2"));

        Map<String, String> inviteePhone = new HashMap<>(5);
        inviteePhone.put("inviterID", "1001");
        inviteePhone.put("status", "0");
        // hash表 批处理
        cluster.hmset("inviteePhone", inviteePhone);

        System.out.println(cluster.hget("inviteePhone", "inviterID"));
        System.out.println(cluster.hget("inviteePhone", "status"));
    }
}

image-20200722164014516