Stunnel to DigitalOcean Redis DB
Connecting to your DigitalOcean DBaaS Redis server
The DigitalOcean DBaaS Redis offering requires that connections be made over secure connections using SSL/TLS. Unfortunately the default redis-cli client does not support SSL/TLS connections. So to get your clients connected you’ll need to either use a third-party client or library that supports secured connections, or use Stunnel to create a tunnel through which your redis-cli can connect.
We’ll quickly go through setting up Stunnel and getting it connected to your Redis server. Keep in mind that your application will most likely use a library, but this can be useful if you have a management node or a jumpbox/bastion node.
Prerequisites
We’ll be using an Ubuntu 18.04 server in SFO2. The size doesn’t really matter for this example, but if you do plan to use this moving forward, be sure to give yourself enough room to run your tool set from it as well as having room for other users on your admin staff. Towards the bottom of the Droplet creation page you’ll want to set a tag on your Droplet. I’ll be using jumpbox as my tag. Also, please be sure to secure the server by setting up a firewall, disallowing root SSH login, disable password authentication, and change the default SSH port. We won’t be doing that in this example, but keep those things in mind.
Once the Droplet is up and running, go ahead and create yourself a Redis server using DigitalOcean’s DBaaS offering. The provisioning time is pretty quick, but we’ll use that time to start setting up some tools on the Ubuntu server.
Installing tools on your jumpbox
Connect to your Droplet over SSH. Let’s go ahead and update the Droplet and install redis-cli and stunnel.
$ sudo apt-get update && sudo apt-get upgrade -y;
$ sudo apt-get install -y redis-tools stunnel4;
Let’s make sure stunnel starts on boot.
$ sudo vim /etc/default/stunnel4
Now go ahead and replace ENABLED=0
with ENABLED=1
and save and quit the file.
Retrieve connection information
By now your Redis server is probably up and running and ready to be configured. You should be able to start by adding trusted sources. This is where you’re going to insert the Droplet tag you set up when creating your Droplet, which in our example is jumpbox.
The next prompt is going to ask you to set an eviction policy for Redis. You can decide on what works best for your application. I’ll be leaving it as noeviction since this is just an example.
The next step is going to show you connectivity information for both public and private connections. Since we’re going to be connecting from an internal jumpbox, let’s switch that over to the private tab. Keep that screen up or copy the details over to a secure location, you’ll need the host and port information to configure stunnel.
Configure stunnel
Jump back over to your terminal which should be connected to your Droplet running Ubuntu. Create the following file /etc/stunnel/redis.conf. This file will get read by stunnel when you start it back up and create the tunnel for you. Start editing the file save it with the following info:
$ sudo vim /etc/stunnel/redis.conf
pid = /run/stunnel-redis.pid
client = yes
[redis-client]
connect = private_host_name:25061
accept = 127.0.0.1:25061
Then stop and start the stunnel service.
$ sudo systemctl stop stunnel4.service
$ sudo systemctl start stunnel4.service
You can then run ss -plant
and see the service listening on 127.0.0.1 with the local port you specified. This also means you can set up multiple connections to different servers by just using different local ports. Let’s go ahead and try connecting using redis-cli
.
$ redis-cli -h 127.0.0.1 -p 25061 -a "yourpasswordhere"
You should now see the redis prompt where you can enter in the PING command to get a PONG response.
127.0.0.1:25061> PING
PONG
127.0.0.1:25061>
That’s it. You’re all set and can now connect to your Redis server to run scheduled or ad-hoc tasks for management and troubleshooting. Hope this helps. If there are any recommendations you want to make to improve this process, please feel free to comment.