Now Let's Look at the Sentinel Init Container
Compared with Redis, the Sentinel init container is much simpler:
Unlike the Redis init container, this one isn't trying to decide:
Am I master or replica?
Sentinel isn't a Redis data node.
Its init container simply needs to:
Create the configuration file that the Sentinel process will use.
Let's start with:
This is shell syntax for writing multiple lines into a file.
Everything until the final:
gets written into:
So conceptually:
creates a file containing:
In our case, we're creating Sentinel's configuration.
Next:
This tells Sentinel:
Listen on TCP port 5000.
That's why the Redis init container contacts Sentinel using:
The two configurations agree:
Next:
Our setup uses hostnames such as:
So this setting allows Sentinel to resolve/use those hostnames when working with Redis nodes.
This is important because the configuration isn't giving Sentinel a fixed Redis pod IP.
It's giving it a Kubernetes DNS name.
Next:
This tells Sentinel to announce hostnames rather than relying only on IP addresses when communicating node information.
Again, this matches the general design of this setup:
Avoid depending on changing pod IPs.
Prefer stable StatefulSet DNS identities.
The Redis side also explicitly announces its stable FQDN with:
So both pieces are designed around hostname-based identities.
Now:
There are several pieces here.
Let's separate them:
mymaster
This is the logical name Sentinel gives this Redis master group.
Think:
It's also why the Redis init container asks:
The names need to match.
redis-0.redis.redis.svc.cluster.local
This is the initial Redis master address Sentinel should monitor.
Again, this does not mean:
redis-0 can never stop being master.
It means:
When Sentinel first starts, begin by looking at redis-0 as the master.
After a failover, Sentinel can track a different master.
6379
That's the Redis server port.
2
This is the quorum value.
You have three Sentinel instances:
The configured quorum is:
So Sentinel uses a quorum of two when determining that the monitored master is objectively down.
At a simplified level:
Next:
5000 milliseconds is five seconds.
This configures how long a Sentinel waits without acceptable replies before considering the monitored instance subjectively down.
In simplified terms:
That local judgment is part of Sentinel's failure-detection process.
Next:
60000 milliseconds is 60 seconds.
This configures Sentinel's failover timeout for the mymaster group.
The setting participates in Sentinel's failover timing and retry behavior.
For understanding this particular init container, the simple mental model is:
Next:
Imagine this:
Sentinel promotes:
Now other replicas may need to synchronize with the new master.
parallel-syncs 1 says only one replica should be reconfigured/synchronized at a time during this part of failover.
With our small three-node setup, you can think of it as controlling how aggressively replicas synchronize after promotion.
Finally:
Sentinel needs credentials to communicate with the password-protected Redis nodes.
But we don't want to hard-code the password directly into this YAML.
So the init container gets it from a Kubernetes Secret.
Where Does $REDIS_PASSWORD Come From?
This part:
means:
Create an environment variable called REDIS_PASSWORD.
But don't hard-code its value.
Instead, get it from:
So conceptually:
When the shell creates sentinel.conf, $REDIS_PASSWORD is expanded to the value provided to the init container.
What Happens After the Sentinel Init Container Finishes?
The init container creates:
and exits.
The main Sentinel container then starts with:
So:
And remember: this happens separately in all three Sentinel pods.
How the Two Init Containers Work Together
This is the part that finally made the architecture click for me.
The Sentinel init container says:
"I'm going to create a configuration telling Sentinel where the Redis master starts and how to monitor it."
The Redis init container says:
"I'm going to find out who the current master is and configure this Redis pod appropriately."
So:
First-Time Startup
Imagine the whole system is being deployed for the first time and Redis starts before Sentinel.
redis-0:
redis-1:
redis-2:
We get:
Then Sentinel starts.
Each Sentinel init container creates a config saying:
Start monitoring redis-0 as "mymaster".
The Sentinel processes start monitoring the Redis deployment.
What Happens During a Failover?
Now suppose:
fails.
The Sentinels detect the failure and, assuming the necessary Sentinel conditions are met, a failover can occur.
Suppose redis-2 gets promoted.
Now:
Sentinel now knows:
This is where the Redis init logic becomes particularly useful.
What Happens When a Redis Pod Restarts After Failover?
Suppose redis-1 gets restarted.
Its init container starts from scratch.
It asks:
Can I contact Sentinel?
Yes.
Then:
Who is master?
Sentinel responds:
The init container therefore writes:
into redis-1's config.
Then Redis starts.
So:
Now imagine redis-2 itself restarts.
Its init container asks Sentinel:
Who is master?
Sentinel says:
The script compares:
and realizes:
That's me!
So it does not add a replicaof directive.
It starts as the master.
The Mental Model Worth Remembering
If you forget every shell command in these configurations, remember these two sentences.
Redis init container
"Before Redis starts, figure out who the current master is and configure myself as either master or replica."
Sentinel init container
"Before Sentinel starts, create the configuration that tells Sentinel what Redis group to monitor and how to monitor it."
Or even shorter:
Once you see it that way, all the shell scripting is really just implementation detail.
Next, we will have a look into examples of full statefulset yamls of both Redis and Sentinel
#k8s #redis #sentinel
0 Comments