Redis
Redis is an in memory data structure storage. It will be used to share PHP-sessions between the apache servers. To guarantee a robust deployment three redis servers will be configured.
- 192.168.1.54 redis (virtual IP-address)
- 192.168.1.55 redis01 (master)
- 192.168.1.56 redis02 (replica)
- 192.168.1.57 redis03 (replica)
Software
apt-get install redis-server redis-sentinel
Configuration
Redis Server
Change the contents of the redis configuration file in /etc/redis/redis.conf.
Add the IP-address for each of the servers. Each of the redis servers needs to listen on its own IP-address. Make sure to add its IP-address in the beginning on the line. (running as a cluster currently requires this due to a bug, so we will do this although we are not setting up a cluster).
bind 192.168.1.55 127.0.0.1
Enable redis to listen on the network, instead of only listening on the loopback device.
protected-mode no
For better dataintegrity enable this.
appendonly yes
Now restart the redis master
service redis-server restart
And stop the replicas
service redis-server stop
On each of the replicas, set them up af a replica of the master. (This was earlier called slaveof)
replicaof 192.168.1.55 6379
Start the replicas again
services redis-server start
You should now be able to see the replicas from the master redis-cli -h 192.168.1.55.
192.168.1.55:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.1.56,port=6379,state=online,offset=2615,lag=0 slave1:ip=192.168.1.57,port=6379,state=online,offset=2615,lag=1 master_replid:79a21f08669303e71990ea8819830f30c94384c6 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:2615 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:2615
Redis Sentinel
Stop the sentinel
service redis-sentinel stop
Change the contents of the redis configuration file in /etc/redis/sentinel.conf.
Add the IP-address for each of the servers. Each of the redis servers needs to listen on its own IP-address.
bind 192.168.1.55 127.0.0.1
Configure each of the hosts to monitor the redis master server, and at least 2 sentinels should agree to change the master.
sentinel monitor mymaster 192.168.1.55 6379 2
Finally set the timeout before the failover happens
sentinel failover-timeout mymaster 10000
Start the sentinel
service redis-sentinel start
References
https://www.haproxy.com/blog/haproxy-advanced-redis-health-check/
https://blog.usejournal.com/first-step-to-redis-cluster-7712e1c31847
https://www.willandskill.se/en/setup-a-highly-available-redis-cluster-with-sentinel-and-haproxy/
https://www.tecmint.com/setup-redis-replication-in-centos-8/
https://www.tecmint.com/setup-redis-high-availability-with-sentinel-in-centos-8/
https://www.willandskill.se/en/setup-a-highly-available-redis-cluster-with-sentinel-and-haproxy/