Table of contents
-> 2.2 Redis cluster architecture realizes horizontal expansion of redis
-> 2.3 Redis cluster cluster principle
3. Issues that need special attention after construction
->3.1 [Important]: If a service fails: can it continue to provide services???
---> 3.1.1 If there is a master-slave service in the faulty redis service in the cluster,
---> 3.1.2 If the redis cluster does not follow the official recommendation for master-slave backup
---> 3.2.1 Modify configuration redis.conf
-> 3.3 Will deploying multiple nodes on a single server affect bandwidth
---> 4.3.3 Pay special attention to the host ip
-> 4.4 Operate in the current directory
-> 4.5 Create docker container
-> 4.6 Enter the redis-8010 container
-> 4.7 Create redis-cluster cluster configuration
-> 4.8 The build is successful, enter the cluster redis to view (intranet ip)
-> 4.9 Test, as shown in the figure, also explains the above problems
5. An exception occurs, execute the following again
Next: Java uses redis cluster to operate
1. Prepare the environment:
Cloud server <virtual machine can also>, centos7.9, docker-ce, with redis image
2. Introduction:
-> 2.1 Preface (please use cloud server with 06):
Master-slave mode Sentinel mode will be reflected in the cluster mode. The official recommendation is that the cluster has at least three masters, three slaves and six redis services.
-> 2.2 Redis cluster architecture realizes horizontal expansion of redis
That is, start N redis nodes, distribute and store the entire data in these N redis nodes, and each node stores 1/N of the total data. The redis cluster provides a certain degree of availability through partitioning. Even if some nodes in the cluster fail or cannot communicate, the cluster can continue to process command requests.
-> 2.3 Redis cluster cluster principle
The redis cluster cluster defaults to 16384 hash slots. After the cluster is successfully built, a hash slot needs to be assigned to each master node. When the external data is inserted, crc16 will be performed on the key and then modulo 16384, so as to calculate which node manages the data. Hash slots have been allocated when the cluster is created. If the structure of 3 masters and 3 slaves is adopted, the master-slave mode can automatically switch over the failure of the master-like service through the sentinel mode, thereby realizing the high availability of the class service
3. Issues that need special attention after construction
->3.1 [Important]: If a service fails: can it continue to provide services ???
---> 3.1.1 If there is a master-slave service in the faulty redis service in the cluster,
Then when accessing, return to the slave redis service, the cluster is running normally, refer to the master-slave sentinel mechanism,
Re-election of the master usually takes 30 seconds
---> 3.1.2 If the redis cluster does not follow the official recommendation for master-slave backup
One of the redis services fails, emm, which will cause the entire cluster to be unusable, because the data saved by each Redis node in the Redis cluster is different, and if a node has a problem, the entire data will be lost.
-> 3.2 Understanding:
Suppose it is a cloud platform, each data is stored in different services, and your data service gg happens to be stored, and has not been re-elected from the service, emm Others are not affected <need to do some configuration>, your data cannot be found out
---> 3.2.1 Modify configuration redis.conf
Depends on the parameter cluster-require-full-coverage in redis.conf.
If the master and slave of a certain slot hang up, and cluster-require-full-coverage is yes, the entire cluster hangs up.
If the master and slave of a certain slot hang up If the slaves are all down, and the cluster-require-full-coverage is no, all the slot data cannot be used or stored.
-> 3.3 Will deploying multiple nodes on a single server affect bandwidth
It is quite good at introducing and citing other people's articles in detail: The advantages and disadvantages of Redis Cluster clusters have been introduced
4. Practical operation
-> 4.1 Create Folder
mkdir -p /usr/local/src/docker/redis-cluster
-> 4.2 Enter directory
cd /usr/local/src/docker/redis-cluster
-> 4.3 Create a template
vim redis-cluster.tmpl
--> 4.3.1 File input content
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 10.0.4.*
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
bind 0.0.0.0
protected-mode no
---> 4.3.2 Template comments
port: node port (external communication)
cluster-enabled: enable the cluster
cluster-config-file: The configuration of the cluster is nodes.conf
cluster-node-timeout: connection timeout 5000 milliseconds
cluster-announce-ip: host ip
cluster-announce-port: cluster node mapping port
cluster-announce-bus-port: cluster bus port
appendonly: whether the persistence mode aof is enabled
---> 4.3.3 Pay special attention to the host ip
1 It can be an internal network ip (if you want to remote, it must be a public network ip)
ps: But the public network ip needs to perform the following operations: ===> Portal: 06-redis cluster mode (medium) The cloud service ip of the project test changes to the internal network, etc. (solves most problems)
2 Domain name resolution can be used
3 172.0.0.1 cannot be used
-> 4.4 Operate in the current directory
for port in $(seq 8010 8015); \
do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done
Explain: Shell script cycle parameter passing to generate configuration file
-> 4.5 Create docker container
for port in $(seq 8010 8015); \
do \
docker run -it -d -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true -v /usr/local/src/docker/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
--privileged=true -v /usr/local/src/docker/redis-cluster/${port}/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
done
-> 4.6 Enter the redis-8010 container
docker exec -it redis-8010 bash
-> 4.7 Create redis-cluster cluster configuration
redis-cli --cluster create 10.0.4.*:8010 10.0.4.*:8011 10.0.4.*:8012 10.0.4.*:8013 10.0.4.*:8014 10.0.4.*:8015 --cluster-replicas 1
-> 4.8 The build is successful, enter the cluster redis to view (intranet ip)
redis-cli -c -h 10.0.4.* -p 8010
cluster nodes #View the number of cluster nodes
cluster info #View the basic information of the cluster
-> 4.9 Test, as shown in the figure, also explains the above problems
5. An exception occurs, execute the following again
Batch delete containers Batch delete directory mounts
docker ps -a | grep -i "redis-801*" | awk '{print $1}' | xargs docker stop
docker ps -a | grep -i "redis-801*" | awk '{print $1}' | xargs docker rm -f
#shell脚本 删除redis容器
for i in $(seq 8010 8015); \
do docker rm -f redis-${i}; \
done;
rm -rf 801{0..5}/conf/redis.conf
rm -rf 801{0..5}
[Leave the expansion, the official production environment configuration password and other configurations, follow-up supplements]