In this lab we will configure our small Rails app to connect to a database and Redis server running in the VM.
In the Rails app, modify config.database.yml
as follows:
- Comment out the line
database: link_development
- Add this new line instead
url: <%= ENV.fetch("DATABASE_URL", "") %>
- Export a database dump from your local environment and save it to a file in
the
vagrant\data
folder. For example, you might use this command if you are currently outside thedeploying
repo folder:
pg_dump link_development > deploying-rails-to-kubernetes/vagrant/data/link_development.sql
- Log into the datastores VM:
vagrant ssh workshop_datastores
.
- Switch to the postgres user:
sudo su - postgres
- Connect to the postgres database and create the
link
database with:
psql
create database link;
\c link
exit
- Import the data:
psql link < link_development.sql
- Confirm that the data is present:
psql
\c link
select * from users;
select * from entries;
Note that this step is insecure and should be used only in a limited test env.
- Edit
postgresql.conf
:
sudo vim /etc/postgresql/12/main/postgresql.conf
- Find the property labeled
listen_addresses
and change the value fromlocalhost
to*
:
listen_addresses: '*'
- Edit
pg_hba.conf
:
sudo vim /etc/postgresql/12/main/pg_hba.conf
- At the end of the file, add these two lines:
host all all 172.16.100.1/32 trust
host all all 172.16.100.10/32 trust
- Restart the Postgres service:
sudo service postgresql restart
Note that this step is insecure and should be used only in a limited test env.
- Edit
redis.conf
sudo vim /etc/redis/redis.conf
- Find the
bind
directives and add a new line:
bind 172.16.100.20
- Find the
protected-mode
property and change it fromyes
tono
:
protected-mode no
- Restart the Redis service:
sudo service redis restart
Add these two lines to the .env
file in your application:
DATABASE_URL=postgresql://[email protected]:5432/link
REDIS_URL=redis://172.16.100.20:6379/15
Start the application and visit it in a browser to confirm things are running as
expected. Visit the /sidekiq
route and confirm that the Redis URL at bottom of
the page is the one from the environment variable.