Environment: Docker (Redis:5.0.5 * 3)
docker pull redis:5.0.5
Create three redis containers:
redis-node1:6379
docker create --name redis-node1 -v /data/redis-data/node1:/data -p 6379:6379 redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-1.conf docker create --name redis-node2 -v /data/redis-data/node2:/data -p 6380:6379 redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-2.conf docker create --name redis-node3 -v /data/redis-data/node3:/data -p 6381:6379 redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-3.conf
docker start:
If the above situation occurs,
Exited (1) 3 seconds ago, you can view it through
docker logs:
chmod -R 777 /data
执行「docker inspect redis-node1」得到 redis-node1 ip 信息为:172.17.0.4 执行「docker inspect redis-node2」得到 redis-node2 ip 信息为:172.17.0.3 执行「docker inspect redis-node3」得到 redis-node3 ip 信息为:172.17.0.2
# 这里以进入 node1 为例 docker exec -it redis-node1 /bin/bash # 接着执行组建集群命令(请根据自己的ip信息进行拼接) redis-cli --cluster create 172.17.0.2:6379 172.17.0.3:6379 172.17.0.4:6379 --cluster-replicas 0
redis-cli -c command to connect to the cluster node, and then set the value. After the set value, it will automatically redirect to the 0.2 ip address, and then pass Get Get it. Successful acquisition proves that the cluster is valid.
ip address is assigned internally by the dock, such as:
172.17.0.2 etc. If the project using
redis cluster is not on the same server as the cluster, then the project is You can't use the cluster because access is blocked.
Docker use the host mode network connection type,
Docker in Containers created using host mode do not have their own independent network namespace. They share a network space with the physical machine, and can then share all
ports and IP of the physical machine. In this way This allows the public network to directly access the container. Although this method has security risks, no other feasible mode has been found yet.
host mode and re-create the container:
docker stop redis-node1 redis-node2 redis-node3
docker rm redis-node1 redis-node2 redis-node3 # 清空上面创建的配置文件 rm -rf /data/redis-data/node*
docker create --name redis-node1 --net host -v /data/redis-data/node1:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-1.conf --port 6379 docker create --name redis-node2 --net host -v /data/redis-data/node2:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-2.conf --port 6380 docker create --name redis-node3 --net host -v /data/redis-data/node3:/data redis:5.0.5 --cluster-enabled yes --cluster-config-file nodes-node-3.conf --port 6381
--net network type is specified
host, secondly, in this case, there is no need for port mapping, such as
-p 6379:6379, because the container port service needs to be shared externally at this time, so you only need to specify the externally exposed port
-p 6379,
-p 6380, etc.
# 启动命令 docker start redis-node1 redis-node2 redis-node3 # 进入某一个容器 docker exec -it redis-node1 /bin/bash # 组建集群,10.211.55.4为当前物理机的ip地址 redis-cli --cluster create 10.211.55.4:6379 10.211.55.4:6380 10.211.55.4:6381 --cluster-replicas 0
root@CentOS7:/data# redis-cli 127.0.0.1:6379> cluster nodes 72c291c32815194b64d1f6d0fdf771f5cc04e14a 10.211.55.4:6380@16380 master - 0 1590905997358 2 connected 5461-10922 6a595b67bbff15c94e5874c2d2cd556d6a6a6c17 10.211.55.4:6381@16381 master - 0 1590905998362 3 connected 10923-16383 4e3dbdc8f835dcbc38291c88f08165ee51d53d3d 10.211.55.4:6379@16379 myself,master - 0 1590905997000 1 connected 0-5460 127.0.0.1:6379>
redis-cli -c to connect to the cluster,
set a value, and then get the value from other nodes to see if it is successful:
root@CentOS7:/data# redis-cli -c 127.0.0.1:6379> set wxiaowei 123 -> Redirected to slot [7515] located at 10.211.55.4:6380 OK 10.211.55.4:6380> get wxiaowei "123"
The above is the detailed content of How to build a Redis cluster based on Docker. For more information, please follow other related articles on the PHP Chinese website!