系列文章目录
python+vue3+fastapi+ai 学习_浪淘沙jkp的博客-CSDN博客https://blog.csdn.net/jiangkp/category_12623996.html
前言
安装redis 我会以三种方式安装,在5月4号修改完成
第一、直接最简单安装,适用于测试环境玩玩
第二、conf配置安装
第三、集群环境安装
一、redis用途
Redis是一个开源的内存数据库,常用于缓存、消息队列、会话存储、实时分析等方面。其主要用途包括:
-
缓存:Redis可以将常用的数据存储在内存中,以提高数据访问速度,减轻后端数据库的压力。
-
消息队列:Redis支持发布/订阅模式,可以用作消息队列系统,实现异步通信。
-
会话存储:Redis可以用来存储用户会话信息,实现分布式会话管理。
-
实时分析:Redis支持多种数据结构,包括列表、集合、哈希表等,可以用于实时数据分析和统计。
-
分布式锁:Redis提供了分布式锁的实现方式,可以用来控制并发访问。
总的来说,Redis具有高性能、高可用性和丰富的数据结构,适用于多种场景下的数据存储和处理需求
二、简单安装
1、安装
docker run -d --name redis_test --restart=always -p 6379:6379 redis --requirepass 123456
-d 后台运行
--name redis_test 设置容器名为 redis_test
--restart=always 开机自启
-p 6379:6379 端口映射
redis 镜像名 直接获取光网lastest,可以指定版本
--requirepass 123456 密码为 123456
victor_t400@victort400:~$ docker run -d --restart=always --name victorredis redis -p 6379:6379 --requirepass 123456
Unable to find image 'redis:latest' locally
latest: Pulling from library/redis
b0a0cf830b12: Pull complete
214d0afb35ca: Pull complete
16a9d12e7a2c: Pull complete
cb9709829e8b: Pull complete
00e912971fa2: Pull complete
f7ebca356832: Pull complete
4f4fb700ef54: Pull complete
c16c264be546: Pull complete
Digest: sha256:f14f42fc7e824b93c0e2fe3cdf42f68197ee0311c3d2e0235be37480b2e208e6
Status: Downloaded newer image for redis:latest
b8922e41585aff4de14edc02b8caab80a6bef91decaecd5c0b50ac593a4fe275
注意:上面这条命令网上拷贝的不行,放在文件里编译了也不行,直接敲好了,我找谁去
ps下是肯定的
victor_t400@victort400:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8922e41585a redis "docker-entrypoint.s…" About a minute ago Restarting (1) 34 seconds ago victorredis
restarting啥意思?
victor_t400@victort400:~$ docker logs b8922e41585a
*** FATAL CONFIG FILE ERROR (Redis 7.2.4) ***
Reading the configuration file, at line 2
>>> '"-p" "6379:6379"'
Bad directive or wrong number of arguments*** FATAL CONFIG FILE ERROR (Redis 7.2.4) ***
Reading the configuration file, at line 2
>>> '"-p" "6379:6379"'
Bad directive or wrong number of arguments*** FATAL CONFIG FILE ERROR (Redis 7.2.4) ***
Reading the configuration file, at line 2
>>> '"-p" "6379:6379"'
Bad directive or wrong number of arguments
victor_t400@victort400:~$ sudo lsof -nP -iTCP -sTCP:LISTEN #6379 没有被占用
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root 93u IPv6 5638 0t0 TCP *:22 (LISTEN)
systemd-r 586 systemd-resolve 15u IPv4 7612 0t0 TCP 127.0.0.53:53 (LISTEN)
systemd-r 586 systemd-resolve 17u IPv4 7614 0t0 TCP 127.0.0.54:53 (LISTEN)
container 733 root 8u IPv4 8382 0t0 TCP 127.0.0.1:37721 (LISTEN)
sshd 1200 root 3u IPv6 5638 0t0 TCP *:22 (LISTEN)
victor_t400@victort400:~$ ^C
victor_t400@victort400:~$ ^C
victor_t400@victort400:~$ docker stop b8922e41585a
b8922e41585a
victor_t400@victort400:~$ docker kill b8922e41585a
Error response from daemon: Cannot kill container: b8922e41585a: Container b8922e41585aff4de14edc02b8caab80a6bef91decaecd5c0b50ac593a4fe275 is not running
victor_t400@victort400:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
victor_t400@victort400:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8922e41585a redis "docker-entrypoint.s…" 50 minutes ago Exited (1) 36 seconds ago victorredis
a18ad488d054 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago elegant_edison
victor_t400@victort400:~$ docker rm b8922e41585a
b8922e41585a
victor_t400@victort400:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a18ad488d054 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago elegant_edison
victor_t400@victort400:~$ ^C
victor_t400@victort400:~$ docker run -d --restart=always --name victorredis -p 6379:6379 redis --requirepass 123456
341e11bffc02971d177b23d32779b5ff11da559832b050bf585985918439a76b
victor_t400@victort400:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
341e11bffc02 redis "docker-entrypoint.s…" 7 seconds ago Up 5 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp victorredis
a18ad488d054 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago elegant_edison
victor_t400@victort400:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
341e11bffc02 redis "docker-entrypoint.s…" 11 seconds ago Up 9 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp victorredis
victor_t400@victort400:~$
有问题
docker run -d --restart=always --name victorredis redis -p 6379:6379 --requirepass 123456
没有问题,看出问题了吗,反正我是没有
docker run -d --restart=always --name victorredis -p 6379:6379 redis --requirepass 123456
2、测试
本地测试
victor_t400@victort400:~$ docker exec -it 341e11bffc02 bash
root@341e11bffc02:/data# redis-cli
127.0.0.1:6379> set a 1
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> get a
"1"
127.0.0.1:6379>
版本 7.2.4 这应该是最新的
root@341e11bffc02:/data# redis-cli --version
redis-cli 7.2.4
外部连接
用nvicat 16版本,需要的可以留言,我可以给个百度网盘地址
连接不上,估计是端口没有开放吧!去试试再说。
victor_t400@victort400:~$ sudo ufw allow 6379
Rule added
Rule added (v6)
victor_t400@victort400:~$ sudo ufw allow 3306
Rule added
Rule added (v6)
victor_t400@victort400:~$ sudo ufw status
Status: activeTo Action From
-- ------ ----
22 ALLOW Anywhere
6379 ALLOW Anywhere
3306 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
6379 (v6) ALLOW Anywhere (v6)
3306 (v6) ALLOW Anywhere (v6)victor_t400@victort400:~$ sudo ufw reload
Firewall reloaded
再连接
点击底下的ok,不是中间的,进入下面界面,说明连到redis了,可以操作的
测试用测试用,开发玩着用,生产是不能上的,就数据不能安全的保存,就不能用
三、单机指定 redis.conf 配置文件安装
这也是我们一般情况下的安装方式,测试生产两不误
我们把前面安装的redis删除掉,免得还要改端口号
1、删除上一节安装的redis
victor_t400@victort400:~/redis$ docker stop 341e11bffc02
341e11bffc02
victor_t400@victort400:~/redis$ docker rm 341e11bffc02
341e11bffc02
victor_t400@victort400:~/redis$ ps
PID TTY TIME CMD
2378 pts/1 00:00:00 bash
3060 pts/1 00:00:00 ps
victor_t400@victort400:~/redis$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
victor_t400@victort400:~/redis$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a18ad488d054 hello-world "/hello" 8 hours ago Exited (0) 8 hours ago elegant_edison
victor_t400@victort400:~/redis$
2、建映射目录,下载redis.conf
版本7.2.4,前面得到过
redis.conf里修改3个地方
port 6379===》port 6479
bind 127.0.0.1 -::1===》bind 0.0.0.0
requirepass foobared===》requirepass 123456
appendonly no===》appendonly yes
victor_t400@victort400:/home$ cd victor_t400/
victor_t400@victort400:~$ mkdir redisvictor_t400@victort400:~$ cd redis
victor_t400@victort400:~/redis$ mkdir data
victor_t400@victort400:~/redis$ ls
datavictor_t400@victort400:~/redis$ wget https://raw.githubusercontent.com/redis/redis/7.2/redis.conf
3、启动redis容器
docker run --restart=always --privileged=true \
-p 6379:6379 \
--name redis-conf \
-v /home/victor_t400/redis/redis.conf:/etc/redis/redis.conf \
-v /home/victor_t400/redis/data:/data \
-d redis:latest redis-server /etc/redis/redis.conf
victor_t400@victort400:~/redis$ docker run --restart=always \
-p 6379:6379 \
--name redis-conf \
-v /home/victor_t400/redis/redis.conf:/etc/redis/redis.conf \
-v /home/victor_t400/redis/data:/data \
-d redis:latest redis-server /etc/redis/redis.conf
0aec664502c3013bcb3b2fa2cee12d5f2272e49ec125d35bbc1fa2ad522aa918
victor_t400@victort400:~/redis$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0aec664502c3 redis:latest "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis-conf
4、测试
其实上面那个“爱了爱了“连接也是可以连接的,端口 IP 密码都一样
5、我们看看log,改了下
victor_t400@victort400:~/redis$ docker logs redis-conf
1:C 03 May 2024 10:18:03.574 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 03 May 2024 10:18:03.574 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 03 May 2024 10:18:03.574 * Redis version=7.2.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 03 May 2024 10:18:03.574 * Configuration loaded
1:M 03 May 2024 10:18:03.575 * monotonic clock: POSIX clock_gettime
1:M 03 May 2024 10:18:03.576 # Failed to write PID file: Permission denied
1:M 03 May 2024 10:18:03.576 * Running mode=standalone, port=6379.
1:M 03 May 2024 10:18:03.576 * Server initialized
1:M 03 May 2024 10:18:03.576 * Ready to accept connections tcp
victor_t400@victort400:~/redis$ sudo vi /etc/sysctl.conf在最后添加vm.overcommit_memory = 1
victor_t400@victort400:~/redis$ sudo sysctl -p
vm.overcommit_memory = 1
非常意外的发现data下没有数据,我还重新装了一遍,还是没有,结果在里面加了点数据,就有了,哎,我能怎么说呢,这个玩意是dockers里面的redis引起映射的data目录变化,有数据才会有映射吧
四、集群安装
大晚上写的集群安装今天早上发现没了,没点发布,哎,造孽啊,正好装得不满意,redis创建集群时卡在Waiting for the cluster to join,全部删了,重新来过,昨天安装设置配置为
- docker端口号→宿主机对外暴露的端口:8000→8000,8001→8001,8002→8002,8003→8003,8004→8004,8005→8005
- 以上一条在安装时没成功,成功了一次,不知道改了什么就不成功了,呵呵,所以我没上传,导致我再来一边,其实分析了一下,节点容器是拥有单独的内部ip(后面会有实际操作),相当于是一个个体,直接用默认6379就行
- docker端口号→宿主机对外暴露的端口变为:8001→8001,8002→8002,8003→8003,8004→8004,8005→8005 8002→8006
- 网传每个cluster需要打开两个TCP,官网有描述,是真的,别不信,名字叫做集群总线端口,内部通信用的,前面一条是与客户通信的端口,如果你设置了其它端口,那么就是其它端口+10000,比如8001就是18001
- 挂载目录:data(数据) → /home/redis/node-端口号/data/
conf(配置文件)→ /home/redis/node-端口号/conf/redis.conf - 集群模式:三主三从
- 节点容器名称:redis-8001,redis-8002,redis-8003,redis-8004,redis-8005,redis-8006
1、删除以前安装集群历史
以下是以前装的,现在变了,后面会贴上去,和节点容器名称:redis-1,redis-2,redis-3,redis-4,redis-5,redis-6有关
for port in $(seq 8001 8006); do docker stop redis-${port} done for port in $(seq 8001 8006); do docker rm redis-${port} done
victor_t400@victort400:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
victor_t400@victort400:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
victor_t400@victort400:~$
删干净吧
docker rmi [IMAGE_ID_OR_NAME] 删除一个image
docker rmi $(docker images -q) 删除所有
我全删了
victor_t400@victort400:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
非常干净了
好像还有点删啊,这个目录删除吧,否则,其实也没关系,后面我目录变了
sudo rm -rf /home/redis
2、开放所有需要端口
sudo ufw allow 8001
8001~8006 18001~18006 一共13个全部打开,16739应该不用,这个是内部的吧,我打开了,就不管了
全打开吧
victor_t400@victort400:/home$ sudo ufw status
Status: activeTo Action From
-- ------ ----
22 ALLOW Anywhere
6379 ALLOW Anywhere
3306 ALLOW Anywhere
8000 ALLOW Anywhere
8001 ALLOW Anywhere
8002 ALLOW Anywhere
8003 ALLOW Anywhere
8004 ALLOW Anywhere
8005 ALLOW Anywhere
18000 ALLOW Anywhere
18001 ALLOW Anywhere
18002 ALLOW Anywhere
18003 ALLOW Anywhere
18004 ALLOW Anywhere
18005 ALLOW Anywhere
16379 ALLOW Anywhere
18006 ALLOW Anywhere
8006 ALLOW Anywhere
3、创建redis集群所需的network
#创建docker内部网络
docker network create redis-net
#查询网卡信息
docker network ls
#删除
docker network rm redis-net
#查询网关
docker network inspect redis-net | grep "Gateway" | grep --color=auto -P '(\d{1,3}.){3}\d{1,3}' -o
#查询详情,可以查询各容器分配的ip地址
docker network inspect redis-net
victor_t400@victort400:~$ docker network create redis-net
a09e80dd07168838838e5d90e06a3ff09a7278f9ae4a710427c80efc953e596cvictor_t400@victort400:~$ docker network ls
NETWORK ID NAME DRIVER SCOPE
40f6bef4b332 bridge bridge local
d009f7a9343b host host local
f9c89145d9ab none null local
a09e80dd0716 redis-net bridge local
80b376b422b8 victornet bridge local
victor_t400@victort400:~$ docker network inspect redis-net | grep "Gateway" | grep --color=auto -P '(\d{1,3}.){3}\d{1,3}' -o
172.19.0.1
victor_t400@victort400:~$ docker network inspect redis-net
[
{
"Name": "redis-net",
"Id": "a09e80dd07168838838e5d90e06a3ff09a7278f9ae4a710427c80efc953e596c",
"Created": "2024-05-04T01:35:38.052341737Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
还没有创造节点,所以没有节点信息,后面会看到的
4、分别创建个节点的配置文件,并添加配置信息
for port in $(seq 8001 8006);
do
sudo mkdir -p /home/redis/node-${port}/conf
sudo touch /home/redis/node-${port}/conf/redis.conf
sudo chmod 777 /home/redis/node-${port}/conf/redis.conf
sudo cat << EOF > /home/redis/node-${port}/conf/redis.conf
port ${port}
bind 0.0.0.0
protected-mode no
daemonize no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 20000
cluster-announce-ip 192.168.1.124
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
appendonly yes
EOF
done
参数说明:
port:节点端口;
appendonly:是否开启 AOF 持久化模式,默认 no;
cluster-enabled:是否开启集群模式,默认 no;
cluster-config-file:集群节点信息文件;
cluster-node-timeout:集群节点连接超时时间;
cluster-announce-ip:集群节点 IP
cluster-announce-port:集群节点映射端口;
cluster-announce-bus-port:集群节点总线端口。requirepass 123456 这里可以设置密码,放上面就行了。,我没设置,局域网玩玩
5、启动redis容器
for port in $(seq 8001 8006); \
do \
docker run -it -p ${port}:${port} -p 1${port}:1${port}\
--privileged=true -v /home/redis/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
--privileged=true -v /home/redis/node-${port}/data:/data \
--restart always --name redis-${port} \
--sysctl net.core.somaxconn=1024 -d --net redis-net redis redis-server /usr/local/etc/redis/redis.conf
done
参数说明:
-it:交互
-d:后台运行,容器启动完成后打印容器
–privileged:是否让docker 应用容器 获取宿主机root权限(特殊权限-)
-p :端口映射
-v:文件挂载
–sysctl参数来设置系统参数,通过这些参数来调整系统性能
–restart always:在容器退出时总是重启容器
–name :给容器取名
–net redis-net:使用我们创建的虚拟网卡
如果以前没有pull image,这时候会先下载一个redis镜像
状态:
Digest: sha256:f14f42fc7e824b93c0e2fe3cdf42f68197ee0311c3d2e0235be37480b2e208e6
Status: Downloaded newer image for redis:latest
42bd3b3a1bc34fee885ea3fb6cc295c9017fe82ff429d0f18a2e14e121794801
9dd01223dfdfb49f6589d8497a78176b9b3543208a24f883bf33dfc126069de3
e8f5745bb1abe19bf63e071dca75ebbf1049c8c8ef662b2a0b41e17b977de1ce
f4311c6159995cffa5d1084b2f44460e7998b03a2b410682773ef3ddd70b8b7b
e1d8e72bb0335c2fe435e294c917efcaa87068982e8091356c8c624badd2bf8d
2c7c197087127358ae7586ea9cf8d8e01904b1bc77d8e93235f2345b89b2729d
victor_t400@victort400:/home/redis/node-1/conf$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c7c19708712 redis "docker-entrypoint.s…" 48 seconds ago Up 46 seconds 0.0.0.0:8006->6379/tcp, :::8006->6379/tcp, 0.0.0.0:18006->16379/tcp, :::18006->16379/tcp redis-6
e1d8e72bb033 redis "docker-entrypoint.s…" 50 seconds ago Up 48 seconds 0.0.0.0:8005->6379/tcp, :::8005->6379/tcp, 0.0.0.0:18005->16379/tcp, :::18005->16379/tcp redis-5
f4311c615999 redis "docker-entrypoint.s…" 52 seconds ago Up 50 seconds 0.0.0.0:8004->6379/tcp, :::8004->6379/tcp, 0.0.0.0:18004->16379/tcp, :::18004->16379/tcp redis-4
e8f5745bb1ab redis "docker-entrypoint.s…" 54 seconds ago Up 52 seconds 0.0.0.0:8003->6379/tcp, :::8003->6379/tcp, 0.0.0.0:18003->16379/tcp, :::18003->16379/tcp redis-3
9dd01223dfdf redis "docker-entrypoint.s…" 56 seconds ago Up 54 seconds 0.0.0.0:8002->6379/tcp, :::8002->6379/tcp, 0.0.0.0:18002->16379/tcp, :::18002->16379/tcp redis-2
42bd3b3a1bc3 redis "docker-entrypoint.s…" 59 seconds ago Up 56 seconds 0.0.0.0:8001->6379/tcp, :::8001->6379/tcp, 0.0.0.0:18001->16379/tcp, :::18001->16379/
这个看着舒服多了,以前安装的没有用默认的6379,在这个信息里也有,说明有默认的端口号起作用了,这个总是不好的,对吧。
victor_t400@victort400:/home/redis/node-1/conf$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c7c19708712 redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8006->6379/tcp, :::8006->6379/tcp, 0.0.0.0:18006->16379/tcp, :::18006->16379/tcp redis-6
e1d8e72bb033 redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8005->6379/tcp, :::8005->6379/tcp, 0.0.0.0:18005->16379/tcp, :::18005->16379/tcp redis-5
f4311c615999 redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8004->6379/tcp, :::8004->6379/tcp, 0.0.0.0:18004->16379/tcp, :::18004->16379/tcp redis-4
e8f5745bb1ab redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8003->6379/tcp, :::8003->6379/tcp, 0.0.0.0:18003->16379/tcp, :::18003->16379/tcp redis-3
9dd01223dfdf redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8002->6379/tcp, :::8002->6379/tcp, 0.0.0.0:18002->16379/tcp, :::18002->16379/tcp redis-2
42bd3b3a1bc3 redis "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:8001->6379/tcp, :::8001->6379/tcp, 0.0.0.0:18001->16379/tcp, :::18001->16379/tcp redis-1
红色字代表创建了,启动了,好了 ,只是第一步
查看一下分配ip情况,是否与你预期一样
victor_t400@victort400:/home/redis/node-1/conf$ docker network inspect redis-net
[
{
"Name": "redis-net",
"Id": "a09e80dd07168838838e5d90e06a3ff09a7278f9ae4a710427c80efc953e596c",
"Created": "2024-05-04T01:35:38.052341737Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"2c7c197087127358ae7586ea9cf8d8e01904b1bc77d8e93235f2345b89b2729d": {
"Name": "redis-6",
"EndpointID": "74d01dc6370822276420257575d8caabf9963d9cb10793230dc8bddf53d300a9",
"MacAddress": "02:42:ac:13:00:07",
"IPv4Address": "172.19.0.7/16",
"IPv6Address": ""
},
"42bd3b3a1bc34fee885ea3fb6cc295c9017fe82ff429d0f18a2e14e121794801": {
"Name": "redis-1",
"EndpointID": "a7f7a4dfc9f8b61d23b99f3e7e152c5fb0770e988fb2f7ea9318a9e1da131f7b",
"MacAddress": "02:42:ac:13:00:02",
"IPv4Address": "172.19.0.2/16",
"IPv6Address": ""
},
"9dd01223dfdfb49f6589d8497a78176b9b3543208a24f883bf33dfc126069de3": {
"Name": "redis-2",
"EndpointID": "9538a2815fc70275383f55a8086c457ba5e1f4d7e3d5c3979e4d78d5a092a97c",
"MacAddress": "02:42:ac:13:00:03",
"IPv4Address": "172.19.0.3/16",
"IPv6Address": ""
},
"e1d8e72bb0335c2fe435e294c917efcaa87068982e8091356c8c624badd2bf8d": {
"Name": "redis-5",
"EndpointID": "96e9e35065b256d9e04df2bbd7178c395f9c4acfed5082f86d993f440e5f10e5",
"MacAddress": "02:42:ac:13:00:06",
"IPv4Address": "172.19.0.6/16",
"IPv6Address": ""
},
"e8f5745bb1abe19bf63e071dca75ebbf1049c8c8ef662b2a0b41e17b977de1ce": {
"Name": "redis-3",
"EndpointID": "85e94ba076a8d39a70242fc8d8909446e496cf9c970d1345443c4c880bf1bd73",
"MacAddress": "02:42:ac:13:00:04",
"IPv4Address": "172.19.0.4/16",
"IPv6Address": ""
},
"f4311c6159995cffa5d1084b2f44460e7998b03a2b410682773ef3ddd70b8b7b": {
"Name": "redis-4",
"EndpointID": "fb857dd8ae6a28fd50b7aa41835315d4ddaa7a279ee60ac8f8b696954157aa9e",
"MacAddress": "02:42:ac:13:00:05",
"IPv4Address": "172.19.0.5/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
6、创建Redis Cluster集群
(1)、任意进入一个容器内部
docker exec -it redis-8001 /bin/bash
(2)、启动集群
redis-cli --cluster create \
192.168.1.124:8001 \
192.168.1.124:8002 \
192.168.1.124:8003 \
192.168.1.124:8004 \
192.168.1.124:8005 \
192.168.1.124:8006 \
--cluster-replicas 1
root@1313926bf245:/data# redis-cli --cluster create \
192.168.1.124:8001 \
192.168.1.124:8002 \
192.168.1.124:8003 \
192.168.1.124:8004 \
192.168.1.124:8005 \
192.168.1.124:8006 \
--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 192.168.1.124:8005 to 192.168.1.124:8001
Adding replica 192.168.1.124:8006 to 192.168.1.124:8002
Adding replica 192.168.1.124:8004 to 192.168.1.124:8003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 0e8f0813c125d42e2308f5eaabee618d69bb8a9c 192.168.1.124:8001
slots:[0-5460] (5461 slots) master
M: 3a942f79e568f88a4acbee7177e29327fde5d404 192.168.1.124:8002
slots:[5461-10922] (5462 slots) master
M: 41f6b40d95dd90b5220af4ff47b9fe24621c6e1e 192.168.1.124:8003
slots:[10923-16383] (5461 slots) master
S: 005116c21e3e67b28e737943ef3f06b881ba2999 192.168.1.124:8004
replicates 0e8f0813c125d42e2308f5eaabee618d69bb8a9c
S: 0773701fd0b0705979c4a6bc8d0876fe6481301e 192.168.1.124:8005
replicates 3a942f79e568f88a4acbee7177e29327fde5d404
S: f4953da8e2e62618d00dbfc1cc53d7cd18e489dd 192.168.1.124:8006
replicates 41f6b40d95dd90b5220af4ff47b9fe24621c6e1e
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 192.168.1.124:8001)
M: 0e8f0813c125d42e2308f5eaabee618d69bb8a9c 192.168.1.124:8001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 41f6b40d95dd90b5220af4ff47b9fe24621c6e1e 192.168.1.124:8003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 0773701fd0b0705979c4a6bc8d0876fe6481301e 192.168.1.124:8005
slots: (0 slots) slave
replicates 3a942f79e568f88a4acbee7177e29327fde5d404
S: 005116c21e3e67b28e737943ef3f06b881ba2999 192.168.1.124:8004
slots: (0 slots) slave
replicates 0e8f0813c125d42e2308f5eaabee618d69bb8a9c
M: 3a942f79e568f88a4acbee7177e29327fde5d404 192.168.1.124:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: f4953da8e2e62618d00dbfc1cc53d7cd18e489dd 192.168.1.124:8006
slots: (0 slots) slave
replicates 41f6b40d95dd90b5220af4ff47b9fe24621c6e1e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
(3)、注意问题
注意:安装时人如果碰到,redis创建集群时卡在Waiting for the cluster to join......
那么不好意思有问题了
我在安装过程中的问题:
- redis-cli --cluster create \
192.168.1.124:8001 \
192.168.1.124:8002 \
192.168.1.124:8003 \
192.168.1.124:8004 \
192.168.1.124:8005 \
192.168.1.124:8006 \
--cluster-replicas 1 这里的ip地址时宿主机ip地址,不是docker里的127.19.0.*,本来我开始用的是主机地址,后来找了个教程,搞得我,哎,难啊,其实想想也是啊,我这是一台电脑上装,一般集群都是几台电脑,肯定是外部ip地址啊,至少是个外部局域网地址,要不怎么搞成集群呢?你用docker里的内部网络,两台电脑之间如何连接呢?还是有点小蠢,至少装了5次,还好这个删除安装非常快。提醒删除时要删除/home/redis目录,有数据的注意保存 - 端口要开放,怎么开放前面写了的,特别是+10000的端口,
- 不要完全相信网上的教程,有些照着来也不行,原因未知,自我安慰下就是环境不同导致结果也不同
7、测试
(1)、内网
root@1313926bf245:/data# redis-cli -c -h 127.19.0.2 -p 8001 --raw
127.19.0.2:8001> set a 1
-> Redirected to slot [15495] located at 192.168.1.124:8003
OK
192.168.1.124:8003> get a
1
192.168.1.124:8003>
8001里存进去,8003里读出来
(2)外网nvicat,试了可以
上面图可以看出内网存的在这里能看到,而且我用8001和8002登录都一样
总结
安装虽然顺利,但是还是碰到一些问题,不停查,不停装。。。。。
后面我们将要安装mysql,这个好像也有集群?