Deploying Redis Sentinel with Portainer and Integrating It into Spring Boot
This tutorial demonstrates how to install Portainer, use it to deploy a Redis‑Sentinel cluster with master‑slave replication and custom bridge networking, test failover, and then integrate the Sentinel setup into a Spring Boot application with Lettuce connection pooling.
Introduction
The article introduces Portainer as a graphical Docker management tool and outlines the steps to install it, deploy a Redis‑Sentinel cluster, and integrate the cluster with a Spring Boot application.
Install Portainer
Create a portainer.yml Docker‑Compose file and start Portainer in foreground:
version: "3"
services:
portainer:
image: portainer/portainer:latest
container_name: portainer
ports:
- "9000:9000"
volumes:
- /app/portainer/data:/data
- /var/run/docker.sock:/var/run/docker.sockRun docker-compose -f portainer.yml up , then access http:// server_ip :9000 to initialize the admin account and enter the local Docker environment.
Deploy Redis‑Sentinel via Portainer
Master‑Slave Deployment
Create redis-cluster.yml defining one master and two slaves. Use non‑standard external ports and set passwords for security.
version: '3'
services:
# master
master:
image: redis
container_name: redis-master
command: redis-server --requirepass xxxx--masterauth xxxx
ports:
- 16379:6379
# slave 1
slave1:
image: redis
container_name: redis-slave-1
ports:
- 16380:6379
command: redis-server --slaveof redis-master 6379 --requirepass xxxx--masterauth xxxx
# slave 2
slave2:
image: redis
container_name: redis-slave-2
ports:
- 16381:6379
command: redis-server --slaveof redis-master 6379 --requirepass xxxx--masterauth xxxxStart with docker-compose -f redis-cluster.yml up and verify containers appear in Portainer.
Create a Bridge Network for Sentinel
In Portainer, add a custom bridge network named redis-sentinel and attach the master and slave containers to it.
Sentinel Deployment
Create redis-sentinel.yml defining three sentinel services, each mounting its own sentinel.conf and joining the redis-sentinel network.
version: '3'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /app/cloud/redis/sentinel/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis
container_name: redis-sentinel-2
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /app/cloud/redis/sentinel/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /app/cloud/redis/sentinel/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
default:
external:
name: redis-sentinelEach sentinel.conf monitors the master (replace the IP with the container’s bridge IP) and includes authentication settings.
port 26379
dir /tmp
sentinel monitor redis-master 172.20.0.2 6379 2
sentinel auth-pass redis-master xxxxx
sentinel down-after-milliseconds redis-master 30000
sentinel parallel-syncs redis-master 1
sentinel failover-timeout redis-master 180000
sentinel deny-scripts-reconfig yesStart the sentinels with docker-compose -f redis-sentinel.yml up , then use Portainer to view logs and confirm failover behavior by stopping the master container; a slave is promoted automatically.
Spring Boot Integration
Add Redis and Lettuce dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Lettuce pool -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>Configure application.yml with sentinel information and Lettuce pool settings:
spring:
redis:
password: xxxxx
sentinel:
master: redis-master
nodes:
sentinel_ip
:26379
password: xxxxx
lettuce:
pool:
max-idle: 10
max-active: 20
min-idle: 5
max-wait: 10000msInject RedisTemplate in a controller and expose simple setKey and getKey endpoints. Example DTO and controller snippets are provided in the article.
@Autowired
private RedisTemplate redisTemplate;
@PostMapping("setKey")
public ResultData
setKey(@RequestBody RedisStrDto dto) {
redisTemplate.opsForValue().set(dto.getKey(), dto.getValue());
return ResultData.success("success");
}
@GetMapping("getKey/{key}")
public ResultData
getKey(@PathVariable String key) {
return ResultData.success(redisTemplate.opsForValue().get(key));
}Testing the endpoints confirms that the Spring Boot application can read and write data through the Redis‑Sentinel cluster.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.