Connecting to a database in a cluster Redis
Connection methods
You can connect to the Redis database in two ways: directly or using Redis Sentinel.
Redis Sentinel is a Redis host management system that provides monitoring, notification, automatic failover, and reporting of up-to-date host addresses to the clients.
Not every Redis client supports connecting via Sentinel. In such cases, you can connect to the host directly. Note that you will need to track the roles of all the hosts yourself. If there is no need for a direct connection, use Sentinel for more reliable host management.
Note
Use port 26379
for host connections via Sentinel and port 6379
for direct connections.
Redis cluster hosts cannot be assigned public IPs. You can only access hosts from within their subnet.
Connecting to databases
In this example, we connect to the Redis host from a virtual machine connected to the same subnet as the host.
-
Create a VM with a public IP in the same subnet as the Redis host.
-
Connect to the VM via SSH.
$ ssh <login>@<VM public IP>
-
Install Redis tools on the VM using a package manager:
UbuntuCentOS$ sudo apt update $ sudo apt-get install redis
$ sudo yum update $ sudo yum install redis
-
Install a certificate for accessing the database:
$ sudo mkdir -p /usr/local/share/ca-certificates/Yandex $ sudo wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" -O /usr/local/share/ca-certificates/Yandex/YandexInternalRootCA.crt
-
Create a variable with the host name and port and master DB name.
$ host=$(redis-cli -h <host address> -p 26379 sentinel get-master-addr-by-name <cluster name> | head -n 1)
-
Check the connection to the host:
$ redis-cli -h $host -a <DB password> ping
If the response is
PONG
, the connection is established successfully.