docker配置redis6.0.5集群

docker配置redis6.0集群方案

docker安装

请直接挂载课程配套的Centos7.x镜像, docker官方建议使用CentOS7

(1)yum 包更新到最新

sudo yum update
(2)安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
(3)设置yum源为阿里云

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(4)安装docker

sudo yum install docker-ce
(5)安装后查看docker版本
docker -v

redis安装

docker pull redis

docker inspect redis 看看redis的版本

"Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
    "GOSU_VERSION=1.12",
    "REDIS_VERSION=6.0.5",
    "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.0.5.tar.gz",
    "REDIS_DOWNLOAD_SHA=42cf86a114d2a451b898fcda96acd4d01062a7dbaaad2801d9164a36f898f596"
]

redis配置

  • docker启动redis
docker run -d --net=redis_net1 --ip=172.19.0.2 -p 7000:7000 -v ~/redis_cluster/7000/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redis_cluster/7000/data:/data --name=r1 redis redis-server /usr/local/etc/redis/redis.conf
docker run -d --net=redis_net1 --ip=172.19.0.3 -p 7001:7001 -v ~/redis_cluster/7001/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redis_cluster/7001/data:/data --name=r2 redis redis-server /usr/local/etc/redis/redis.conf
docker run -d --net=redis_net1 --ip=172.19.0.4 -p 7002:7002 -v ~/redis_cluster/7002/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redis_cluster/7002/data:/data --name=r3 redis redis-server /usr/local/etc/redis/redis.conf
docker run -d --net=redis_net1 --ip=172.19.0.5 -p 7003:7003 -v ~/redis_cluster/7003/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redis_cluster/7003/data:/data --name=r4 redis redis-server /usr/local/etc/redis/redis.conf
docker run -d --net=redis_net1 --ip=172.19.0.6 -p 7004:7004 -v ~/redis_cluster/7004/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redis_cluster/7004/data:/data --name=r5 redis redis-server /usr/local/etc/redis/redis.conf
docker run -d --net=redis_net1 --ip=172.19.0.7 -p 7005:7005 -v ~/redis_cluster/7005/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redis_cluster/7005/data:/data --name=r6 redis redis-server /usr/local/etc/redis/redis.conf

创建redis集群

redis-cli --cluster create 172.19.0.2:7000 172.19.0.3:7001 172.19.0.4:7002 172.19.0.5:7003 172.19.0.6:7004 172.19.0.7:7005 --cluster-replicas 1

输出

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.19.0.6:7004 to 172.19.0.2:7000
Adding replica 172.19.0.7:7005 to 172.19.0.3:7001
Adding replica 172.19.0.5:7003 to 172.19.0.4:7002
M: b8fd74f8cdebf46d5e64694db040576e8d7d0609 172.19.0.2:7000
   slots:[0-5460] (5461 slots) master
M: aea806a312049e4c3235e7ea24535a9a45773136 172.19.0.3:7001
   slots:[5461-10922] (5462 slots) master
M: 90251b4a1d3c4357e5e41a1fc20863ff43155096 172.19.0.4:7002
   slots:[10923-16383] (5461 slots) master
S: 4349d4b9c31de77a8d6b32db2a75b4fe1e34c0e0 172.19.0.5:7003
   replicates 90251b4a1d3c4357e5e41a1fc20863ff43155096
S: de30cea9db4d5788f949e7d8939b7b552d7610b3 172.19.0.6:7004
   replicates b8fd74f8cdebf46d5e64694db040576e8d7d0609
S: 673518cedabea7dccf2a5c560fb36ef2154cb860 172.19.0.7:7005
   replicates aea806a312049e4c3235e7ea24535a9a45773136
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 172.19.0.2:7000)
M: b8fd74f8cdebf46d5e64694db040576e8d7d0609 172.19.0.2:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 90251b4a1d3c4357e5e41a1fc20863ff43155096 172.19.0.4:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: aea806a312049e4c3235e7ea24535a9a45773136 172.19.0.3:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 673518cedabea7dccf2a5c560fb36ef2154cb860 172.19.0.7:7005
   slots: (0 slots) slave
   replicates aea806a312049e4c3235e7ea24535a9a45773136
S: 4349d4b9c31de77a8d6b32db2a75b4fe1e34c0e0 172.19.0.5:7003
   slots: (0 slots) slave
   replicates 90251b4a1d3c4357e5e41a1fc20863ff43155096
S: de30cea9db4d5788f949e7d8939b7b552d7610b3 172.19.0.6:7004
   slots: (0 slots) slave
   replicates b8fd74f8cdebf46d5e64694db040576e8d7d0609
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

最终要的是后面两个OK, 如果是这样那么配置成功
现在在docker里面用redis-cli试试看看配置成功了没有

root@ca396eb86f71:/bin# redis-cli -p 7000 cluster nodes

这里我们docker exec连接的redis节点本身就是7000, 如果写上7001可能不会显示下面这些信息了

90251b4a1d3c4357e5e41a1fc20863ff43155096 172.19.0.4:7002@17002 master - 0 1594611038536 3 connected 10923-16383
aea806a312049e4c3235e7ea24535a9a45773136 172.19.0.3:7001@17001 master - 0 1594611037000 2 connected 5461-10922
b8fd74f8cdebf46d5e64694db040576e8d7d0609 172.19.0.2:7000@17000 myself,master - 0 1594611038000 1 connected 0-5460
673518cedabea7dccf2a5c560fb36ef2154cb860 172.19.0.7:7005@17005 slave aea806a312049e4c3235e7ea24535a9a45773136 0 1594611037832 6 connected
4349d4b9c31de77a8d6b32db2a75b4fe1e34c0e0 172.19.0.5:7003@17003 slave 90251b4a1d3c4357e5e41a1fc20863ff43155096 0 1594611038000 4 connected
de30cea9db4d5788f949e7d8939b7b552d7610b3 172.19.0.6:7004@17004 slave b8fd74f8cdebf46d5e64694db040576e8d7d0609 0 1594611038838 5 connected

主机上安装redis

在主机上编译redis6.0(需要注意gcc版本好像), 我在主机上编译的是redis5, 然后给我改成redis6 发现编译时, gcc版本过低了, 主机4.8版本的gcc, 需要更新到9.3

安装redis6.0

  • 检测gcc版本

gcc -v 如果版本低于5, 需要升级
这里我们升级到gcc9

yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
  • 设置gcc版本
#临时修改gcc版本
scl enable devtoolset-9 bash
#永久修改gcc版本
echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile

再看看gcc的版本
只要是9就行

  • 编译redis
cd  redis-6.0.5
make all && make install

编译成功

[root@centOS redis-6.0.5]# make install 
cd src && make install
make[1]: Entering directory `/root/redis-6.0.5/src'
    CC Makefile.dep
make[1]: Leaving directory `/root/redis-6.0.5/src'
make[1]: Entering directory `/root/redis-6.0.5/src'
Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
make[1]: Leaving directory `/root/redis-6.0.5/src'

redis-cli连接集群

[root@centOS redis-6.0.5]# redis-cli -c -h 172.19.0.2 -p 7000
172.19.0.2:7000> set name zhazha
-> Redirected to slot [5798] located at 172.19.0.3:7001
OK
172.19.0.3:7001> get name
"zhazha"
172.19.0.3:7001> 

集群配置成功

注意企业中不这样用, 太慢了, 所以待续

你可能感兴趣的:(java)