Install the docker
Docker installation resource file is stored in Amazon S3, intermittent connection failure. So when you install Docker, it will be slow. You can install Docker at high speed by executing the following command.
curl -sSL https://get.daocloud.io/docker | sh
Copy the code
Install using daoCloud image, or use proxy if the installation still fails
Uninstall the docker
sudo yum remove docker \
docker-common \
container-selinux \
docker-selinux \
docker-engine
Copy the code
After the Docker is uninstalled, the /var/lib/docker/ directory will retain the original Docker image, network, storage volume and other files. If you need to install a new Docker, delete the /var/lib/docker-/ directory
rm -fr /var/lib/docker/
Copy the code
Install the docker – compose
The curl -l https://get.daocloud.io/docker/compose/releases/download/1.27.4/docker-compose- ` ` uname - s - ` uname -m ` > / usr /local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
Copy the code
Pre-knowledge (Redis Cluster and Docker)
Currently Redis Cluster does not support NATted environments and in general environments where IP addresses or TCP ports are remapped.
Docker uses a technique called port mapping: programs running inside Docker containers may be exposed with a different port compared to the one the program believes to be using. This is useful in order to run multiple containers using the same ports, at the same time, in the same server.
In order to make Docker compatible with Redis Cluster you need to use the host networking mode of Docker. Please check the --net=host
option in the Docker documentation for more information.
IO /topics/clus… In order to make Docker compatible with Redis Cluster, you need to use Docker host network mode.
The environment
Virtual Machine Environment
-
192.168.146.100
-
192.168.146.101
Infrastructure environment
- Centos – release – 7-8.2003
- Docker version 20.10.2
- Docker – compose version 1.27.4
createredis-cluster
folder
Perform the following operations at 192.168.146.100 and 192.168.146.101 respectively
mkdir -p /docker/redis_cluster
cd /docker/redis_cluster
Copy the code
createredis-cluster.tmpl
folder
Perform the following operations at 192.168.146.100 and 192.168.146.101 respectively
vi redis-cluster.tmpl
port ${PORT}
requirepass password
masterauth password
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.146.100 # the IP address
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
:wq
Copy the code
Creating a Configuration File
Perform the following operations at 192.168.146.100 and 192.168.146.101 respectively
for port in `seq 7000 7002`; do \
mkdir -p ${port}/conf \
&& PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
&& mkdir -p ${port}/data; \done
Copy the code
The editordocker-compose.yml
Perform the following operations at 192.168.146.100 and 192.168.146.101 respectively
vi docker-compose.yml
Copy the code
Append the following
version: '3'
services:
# redis_7000
redis_7000:
image: 'redis: 5.0.9'
container_name: 'redis_7000'
network_mode: "host" # host network mode
volumes:
- './7000/data:/data'
- './7000/conf/redis.conf:/usr/local/etc/redis/redis.conf'
command: redis-server /usr/local/etc/redis/redis.conf
# redis_7001
redis_7001:
image: 'redis: 5.0.9'
container_name: 'redis_7001'
network_mode: "host" # host network mode
volumes:
- './7001/data:/data'
- './7001/conf/redis.conf:/usr/local/etc/redis/redis.conf'
command: redis-server /usr/local/etc/redis/redis.conf
# redis_7002
redis_7002:
image: 'redis: 5.0.9'
container_name: 'redis_7002'
network_mode: "host" # host network mode
volumes:
- './7002/data:/data'
- './7002/conf/redis.conf:/usr/local/etc/redis/redis.conf'
command: redis-server /usr/local/etc/redis/redis.conf
Copy the code
Create and start the container service
Perform the following operations at 192.168.146.100 and 192.168.146.101 respectively
docker-compose up -d
Copy the code
Create a Redis cluster
Ensure that the two sub-networks can communicate, access any machine
docker-compose exec redis_7000 bash
Copy the code
Execute the command
Redis -cli -a password --cluster create \ 192.168.146.100:7000 192.168.146.100:7001 192.168.146.100:7002 \ 192.168.146.101:7000 192.168.146.101:7001 192.168.146.101:7002 \ --cluster-replicas 1Copy the code
Enter yes in the prompt option
root@localhost:/data# redis-cli -a password --cluster create \
> 192.168.146.100:7000 192.168.146.100:7001 192.168.146.100:7002 \
> 192.168.146.101:7000 192.168.146.101:7001 192.168.146.101:7002 \
> --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hashslots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 192.168.146.101:7002 to 192.168.146.100:7000 Adding Replica 192.168.146.100:7002 to 192.168.146.101:7000 Adding Replica 192.168.146.101:7001 to 192.168.146.100:7001 M: Cb5afc0707e66a3df98845cab6e31aac0daa9103 192.168.146.100:7000 slots: [0-5460] (5461 slots) master M: C83a15746e4c3b68c7613ae31a9975ad2cb99106 192.168.146.100:7001 slots: [10923-16383] (5461 slots) master S: Cb2ab7545a8d92959b2aab32078baaad5393cad4 192.168.146.100:7002 replicates eea885f8aa9f40452754bc0ebd3d64a4 66550723 M: 66550723 eea885f8aa9f40452754bc0ebd3d64a4 192.168.146.101:7000 slots: [5461-10922] (5462 slots) master S: A58ef728f251675cba5b29bb21ff066f484e032e 192.168.146.101:7001 replicates c83a15746e4c3b68c7613ae31a9975ad2cb99106 S: 494 cda5ad44f1318db745831bdacb44dc64e9d7e 192.168.146.101:7002 replicates cb5afc0707e66a3df98845cab6e31aac0daa9103 Can Iset 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 forthe cluster to join ... Performing Cluster Check (using node 192.168.146.100:7000) M: Cb5afc0707e66a3df98845cab6e31aac0daa9103 192.168.146.100:7000 slots: [0-5460] (5461 slots) master 1 additional up (s) S: a58ef728f251675cba5b29bb21ff066f484e032e 192.168.146.101:7001 slots: (0 slots) slave replicates c83a15746e4c3b68c7613ae31a9975ad2cb99106 M: C83a15746e4c3b68c7613ae31a9975ad2cb99106 192.168.146.100:7001 slots: [10923-16383] (5461 slots) 1 additional master Up (s) s: 494 cda5ad44f1318db745831bdacb44dc64e9d7e 192.168.146.101:7002 slots: (0 slots) slave replicates cb5afc0707e66a3df98845cab6e31aac0daa9103 S: Cb2ab7545a8d92959b2aab32078baaad5393cad4 192.168.146.100:7002 slots: (0 slots) slave replicates 66550723eea885f8aa9f40452754bc0ebd3d64a4 M: 66550723 eea885f8aa9f40452754bc0ebd3d64a4 192.168.146.101:7000 slots: [5461-10922] (5462 slots) 1 additional master replica(s) [OK] All nodes agree about slots configuration. >>> Checkfor open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Copy the code
The cluster is set up successfully.