Article Directory
1 Brief description of redis
1.1 Supported data types
Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered set).
Reference address: https://www.runoob.com/redis/redis-data-types.html
1.2 Redis ha high availability solution
Single point of failure: Highly available at least 3 nodes including 1 master and 2 slaves. When the master node stops service, one of all slave nodes will be selected as the master node. When the master node does not stop service, but the monitoring status does not respond due to network problems, the default is stopped. When serving, there will be two hosts, and there will be two data sources when copying data from the node, causing data synchronization problems.
Solution: Redis recommends using the sentinel mode to realize automatic recovery by monitoring the master and slave. When the master node stops the service, it will be downgraded to the slave node. When the monitoring and the offline master node service starts, the offline master node will automatically queue for the upgrade.
2 Build a redis cluster
2.1 Set the redis startup file
- The startup method is: docker-compose up -d
- The file directory is: /usr/local/docker/redis/
- The file name is: docker-compose.yml
- The content of the file is:
version: '3'
services:
master:
restart: always
container_name: redis-master
image: redis
ports:
- 6379:6379
slave1:
restart: always
container_name: redis-slave-1
image: redis
ports:
- 6380:6379
command: redis-server --slaveof redis-master 6379
slave2:
restart: always
container_name: redis-slave-2
image: redis
ports:
- 6381:6379
command: redis-server --slaveof redis-master 6379
2.2 Set sentinel's configuration file
- The file directory is: /usr/local/docker/redis/sentinel/
- The file name is:
sentinel1.conf、sentinel2.conf、sentinel3.conf
- The content of the file is: the content of the 3 files are the same
port 26379
dir /tmp
#自定义集群名,ip为主节点的ip,2为最小投票数
sentinel monitor mymaster 192.168.30.148 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
2.3 Set the startup file of sentinel
- The startup method is: docker-compose up -d
- The file directory is: /usr/local/docker/redis/sentinel/
- The file name is: docker-compose.yml
- The content of the file is:
version: '3'
services:
sentinel1:
image: redis
container_name: sentinel1
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis
container_name: sentinel2
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis
container_name: sentinel3
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
2.4 Whether the verification is successful
2.4.1 Verify that redis is started successfully
Through the redis desktop manager tool, enter the ip and port and verify the connection
2.4.2 Verify that sentinel started successfully
Command verification:
docker exec -it sentinel1 bash #进入容器
redis-cli -p 26379 #连接redis
sentinel master mymaster #查看监控节点信息