Deploying PostgreSQL and Redis behind Traefik in the Cloud
Tags: postgres, redis, traefik, devops
Published: 2021-12-20 03:09:25

> Useful for development purposes and faster environment setup.

In this article we are going to deploy [PostgreSQL](https://www.postgresql.org) and [Redis](https://redis.io) behind [Traefik](https://doc.traefik.io/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](https://github.com/kanzitelli/postgres-and-redis-behind-traefik).

-----

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
```

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](https://www.docker.com). 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](https://www.digitalocean.com) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/horuufi6ki9dmb02qth4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxb8mnm0akjqqwlmrvt0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1wtj9uf4ydih3z7cgs1.png)

-----

## 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](https://doc.traefik.io/traefik/).

After that, we can describe PostgreSQL and Redis services.

{% gist https://gist.github.com/kanzitelli/8a23774ba3d03850f81645c343deafb4 file=docker-compose.yml %}

-----

## 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](https://github.com/kanzitelli/postgres-and-redis-behind-traefik) 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
```

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
```

`.env` file should look like this:

```
# Postgres
DB_NAME=db
DB_USER=admin
DB_PASS=pass_12345qwerty
```

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

```
> sh build.sh
```

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

```
> docker ps
```


![docker ps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fyqleoeowead1jacg1mr.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8m0c0msmlw6zdoqz2rm.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oog7pp296qm95u9qb53.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1foa589opi7bzoo29clh.png)

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

![Postico tables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/741dnoq4knbgsxjdyuhb.png)

-----

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