DEV Community

Cover image for Deploying PostgreSQL and Redis behind Traefik in the Cloud
Batyr
Batyr

Posted on

Deploying PostgreSQL and Redis behind Traefik in the Cloud

Useful for development purposes and faster environment setup.

In this article we are going to deploy PostgreSQL and Redis behind Traefik proxy on a server for $5 and make it available publicly pointing to your subdomain.

If you'd like to try it out now, here is Github repo.


When you start a new project, you will probably need a place to store some data and manage your users' information. There are plenty databases out there but we are going to stick with the most known one - PostgreSQL.

By the time, when you set up your database and start storing data there, you might also need to have a caching layer on the server that gives you ability to store and access some temporary information like tokens. In order to handle this, you could use Redis which we are going to deploy on par with PostgreSQL behind the Traefik proxy.

As we are going to deploy our setup on a server, we will make it available for the outside world with the following connection urls:

Postgres:
  Domain: postgresql://admin:pass@db.website.com:5432/db
  IP: postgresql://admin:pass@46.101.120.53:5432/db

Redis:
  Domain: redis://db.website.com:6379
  IP: redis://46.101.120.53:6379
Enter fullscreen mode Exit fullscreen mode

So you can have a shared database among the team mates without messing local setups.

In order to fully complete the guide, you will need to have some familiarity with Terminal and Docker. If you'd like to make PostgreSQL and Redis available from your domain, rather than direct server's IP address, you should have a registered domain and access to a dashboard where you can change DNS records.


Setting up a server

We are going to use DigitalOcean to set up a droplet for $5, however, feel free to use any cloud service you like.

Navigate to a dashboard where you create new droplet and fill in the configurations you prefer. I, personally, use a preconfigured droplet with Docker from DO's marketplace.

Setting up DigitalOcean's new droplet

You will also need to specify a name, region and some other configurations for the server. Then, click create and wait till the process is done.

Created droplet

Before proceeding to the next step, let's check if we can connect to our droplet through the console. DigitalOcean provides a built-in console, so we can just open it from the browser.

Press on the droplet to open its configuration, and in the right top corner, you will see Console button. After pressing it, Terminal window connected to the droplet will pop up.

DigitalOcean console


Building Docker Compose

After we have set up the server, we can create a docker-compose.yml file with Traefik, PostgreSQL and Redis services.

If you are not familiar with Traefik, it's a great proxy server that I use already 3 years for all the projects. Starting from the second version, they have made it possible to configure TCP ports that gives us ability to expose PostreSQL and Redis containers to public.

Let's start by creating our first Docker Compose service - Traefik. We need to describe entry points for PostgreSQL and Redis containers telling Traefik which ports to use for requests redirection. Other configurations look pretty intuitive and simple. If you'd like to learn more about Traefik, check out the documentation.

After that, we can describe PostgreSQL and Redis services.


Deploying in the Cloud

We have set up our server and prepared the Docker Compose file which we are going to deploy there.

In order to make the process easy, I have created a Github repository which we will clone and just run one command.

Let's connect to our server (I am going to use DigitalOcean Console) and clone the repository:

> git clone https://github.com/kanzitelli/postgres-and-redis-behind-traefik.git backend
> cd backend
Enter fullscreen mode Exit fullscreen mode

As we use some environment variables for the Docker Compose setup, we are going to create .env file and fill it with your values:

> nano .env
Enter fullscreen mode Exit fullscreen mode

.env file should look like this:

# Postgres
DB_NAME=db
DB_USER=admin
DB_PASS=pass_12345qwerty
Enter fullscreen mode Exit fullscreen mode

Once you have put all the values to the .env file, you can run a build script:

> sh build.sh
Enter fullscreen mode Exit fullscreen mode

After the building process is finished, you can check which Docker processes are running:

> docker ps
Enter fullscreen mode Exit fullscreen mode

docker ps

In order to make it available publicly we have to create DNS A-record pointing to our server's IP, or you can just use your server's IP address.

DNS A-record

DNS records update could take up to 72 hours but usually it happens during 5 minutes. If it doesn't want to resolve the host, then try different WiFi or use mobile data.


Checking the setup

Now if you open db.website.com:6969, you will see Traefik's dashboard:

Traefik Dashboard

We can see if everything is running alright by establishing a connection to the database. You can use any PostgreSQL client, I am going to show it in Postico.

Postico

After hitting Connect button, you should see all the tables!

Postico tables


Now you can start using your remote PostgreSQL and Redis in your project!

This approach is very handy for development purposes but close to production conditions. You could measure the latency of connection and other metrics.

Feel free to ask questions, propose any improvements and just say hi!

https://batyr.io | https://twitter.com/kanzitelli

Top comments (0)