Environmental Science:
1. There is only one Linux server
2. Use docker compose for deployment (the use of docker compose will not be introduced here)
1,docker images
Use redis in your own image
2,docker network ls
View the existing network segment (Note: it must be the same network segment, otherwise it will be inaccessible)
You can also create a new network segment: docker network create -- subnet = 172.19 0.0/16 mynetwork
3. redis docker compose YML file
Here, you can use your own network segment to modify: network_mode: own network segment name
--- version: '3' services: # Container for master node redis-server-master: image: redis container_name: redis-server-master restart: always network_mode: redis_server_default # Specify the time zone to ensure that the time in the container is correct environment: TZ: "Asia/Shanghai" volumes: # Mapping profiles and data directories - /opt/sentinel/redis_server/redis6379.conf:/etc/redis/redis.conf - /opt/sentinel/redis_server/data6379:/data ports: - 8379:6379 command: redis-server /etc/redis/redis.conf # Container from node 1 redis-server-slave-1: image: redis container_name: redis-server-slave-1 restart: always network_mode: redis_server_default depends_on: - redis-server-master environment: TZ: "Asia/Shanghai" volumes: - /opt/sentinel/redis_server/redis6380.conf:/etc/redis/redis.conf - /opt/sentinel/redis_server/data6380:/data ports: - 9380:6379 command: redis-server /etc/redis/redis.conf # Container from node 2 redis-server-slave-2: image: redis container_name: redis-server-slave-2 restart: always network_mode: redis_server_default depends_on: - redis-server-master environment: TZ: "Asia/Shanghai" volumes: - /opt/sentinel/redis_server/redis6381.conf:/etc/redis/redis.conf - /opt/sentinel/redis_server/data6381:/data ports: - 9381:6379 command: redis-server /etc/redis/redis.conf
4. Go to the corresponding directory and create a redis configuration file
Modify the corresponding configuration file redis6380 Conf and redis6381 conf
slaveof 172.18.0.2 6379 of which 172.18 0.2 ip address of the main redis service (after the redis service is started, you can view it through docker inspect redis server Master)
You can also configure in the yml file: command followed by -- slaveof 172.18 0.2 6379
Protected mode no allows remote access
requirepass own password (the master and slave passwords are consistent)
Master auth master service password (master and slave passwords are consistent)
Execute the command: docker compose up - D
After success, execute the command: docker ps -a# can see that it has been started successfully
Execute the command to check whether the master-slave synchronization is successful: you can see that there are two slave redis services
View the slave server: the status is up
Here, the master-slave of redis has been deployed.
5. Sentry docker compose YML profile
--- version: '3' services: redis-sentinel-1: image: redis container_name: redis-sentinel-1 restart: always volumes: - /opt/sentinel/sentinel_server/sentinel26379.conf:/etc/redis/sentinel.conf network_mode: redis_server_default ports: - 26379:26379 # Specify the time zone to ensure that the time in the container is correct environment: TZ: "Asia/Shanghai" command: redis-sentinel /etc/redis/sentinel.conf redis-sentinel-2: image: redis container_name: redis-sentinel-2 restart: always volumes: - /opt/sentinel/sentinel_server/sentinel26380.conf:/etc/redis/sentinel.conf network_mode: redis_server_default ports: - 26380:26379 environment: TZ: "Asia/Shanghai" command: redis-sentinel /etc/redis/sentinel.conf redis-sentinel-3: image: redis container_name: redis-sentinel-3 restart: always volumes: - /opt/sentinel/sentinel_server/sentinel26381.conf:/etc/redis/sentinel.conf network_mode: redis_server_default ports: - 26381:26379 environment: TZ: "Asia/Shanghai" command: redis-sentinel /etc/redis/sentinel.conf
6. To the corresponding directory,
Configuration file sentinel26379 conf,sentinel26380.conf,sentinel26381.conf
Note: the myids of the three sentinel configuration files cannot be the same. You can delete the myid in the configuration file, and the startup will regenerate. The three configuration files can be the same. The configuration is as follows:
port 26379 dir "/tmp" sentinel deny-scripts-reconfig yes sentinel monitor mymaster 172.18.0.2 6379 2 sentinel auth-pass mymaster redis Service password
Execute the command to start: docker compose up - D
After success, you can view the configuration file to: you can see that the current primary redis service is 172.18 0.2(redis-server-master)
7. Test whether the new primary redis service can be elected normally
Execute the command: docker ps -a# the current services are started
The current primary redis service is 172.18 0.2(redis-server-master)
Execute the command: docker stop # redis server master
View: docker ps -a
Check the sentry configuration file. It has changed. The main service has changed to 172.18 zero point three
Check the master-slave status of the previous slave server again:
Here, the sentinel cluster is over.