Configuring DNS connectivity between Yandex BareMetal and Yandex Virtual Private Cloud to access Managed Service for PostgreSQL via FQDN
Using fully qualified domain names (FQDNs) to connect to cloud services such as Yandex Managed Service for PostgreSQL is a best practice which helps to achive infrastructure abstraction and process automation. FQDNs remain constant, while IP addresses may change when you scale, restore, or migrate services.
However, if a server in the BareMetal segment is connected to the cloud via Yandex Cloud Interconnect, you will have an issue resolving FQDNs of cloud services, since direct DNS queries from an external subnet get blocked.
To resolve this issue, we recommend deploying an intermediate VM in the same subnet as the Managed Service for PostgreSQL cluster to act as a Bind9-based DNS forwarder. This server will accept DNS requests from the BareMetal server, forward them to Yandex Cloud DNS, and return responses to the client, ensuring correct resolution of the cloud service FQDNs.
To configure DNS connectivity:
- Set up your infrastructure.
- Configure a DNS forwarder on a VM.
- Configure a DNS client on the BareMetal server.
- Test FQDN access to the Managed Service for PostgreSQL cluster.
- Check the result.
If you no longer need the resources you created, delete them.
Set up your infrastructure
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can create or select a folder for your infrastructure on the cloud page
Learn more about clouds and folders here.
Required paid resources
The solution support costs include:
- Fee for using a VM and disk (see Yandex Compute Cloud pricing).
- Fee for using a Managed Service for PostgreSQL cluster (see Yandex Managed Service for PostgreSQL pricing).
- Fee for renting the BareMetal servers (see Yandex BareMetal pricing).
- Fee for Yandex Cloud Interconnect resource usage (see Yandex Cloud Interconnect pricing).
Create a Managed Service for PostgreSQL cluster
-
In the management console
, select the folder where you want to create a cluster. -
In the list of services, select Managed Service for PostgreSQL.
-
Click Create cluster.
-
Configure the cluster:
- Cluster name: Specify the cluster name.
- Database: Specify the database name, username, and password.
- Network settings: Select the availability zones and subnets.
-
Click Create cluster.
Note
Creating a Managed Service for PostgreSQL cluster automatically creates a private DNS zone,
mdb.yandexcloud.net, where DNS records for cluster hosts are generated. Host FQDNs have thec-<cluster_ID>-<host_number>.mdb.yandexcloud.netformat. Inside the zone, DNS records for the database are automatically created as well, e.g.,10.129.0.29for the master and10.130.0.15for the replica.
For more information on creating a cluster, see this guide.
Create a VM for a DNS forwarder
- In the management console
, select the folder where you want to create a VM. - In the list of services, select Compute Cloud.
- In the left-hand panel, select
Virtual machines. - Click Create virtual machine.
- Under General information, enter the VM name.
- Under Boot disk image, select the Ubuntu 22.04 LTS image.
- Under Network settings:
- In the Subnet field, select the subnet where the Managed Service for PostgreSQL cluster hosts are located, e.g.,
10.129.0.0/24. - Enter the VM internal IP address, e.g.,
10.129.0.10. - Make sure the availability zone matches the zone hosting the Managed Service for PostgreSQL cluster, e.g.,
ru-central1-b.
- In the Subnet field, select the subnet where the Managed Service for PostgreSQL cluster hosts are located, e.g.,
- Click Create VM.
For more information on creating a VM, see this guide.
Rent a BareMetal server
- In the management console
, select the folder for the server you want to rent. - In the list of services, select BareMetal.
- Click Lease server.
- Set up the server:
- Select the appropriate server configuration.
- Assign or get a private IP address over DHCP, e.g.,
172.16.2.2.
- Click Lease server.
For more information on renting a server, see this guide.
Configure network connectivity
Configure connectivity between the BareMetal and Virtual Private Cloud subnets via Yandex Cloud Interconnect:
- Create a Virtual Private Cloud network with subnets in the availability zones you need.
- Create a BareMetal private subnet.
- Configure a connection via Yandex Cloud Interconnect between the BareMetal subnet and the Virtual Private Cloud subnet hosting the Managed Service for PostgreSQL cluster.
For more information on setting up network connectivity, see this guide.
The examples below use the following parameters:
-
BareMetal subnet:
172.16.2.0/24 -
BareMetal server IP address:
172.16.2.2 -
Virtual Private Cloud subnet with Managed Service for PostgreSQL hosts:
10.129.0.0/24 -
Bind9 VM IP address:
10.129.0.10 -
Virtual Private Cloud DNS resolvers:
10.129.0.2and10.130.0.2Note
Virtual Private Cloud DNS resolvers have addresses of the
10.X.0.2type, whereXis the subnet number. Each subnet in Virtual Private Cloud has its own DNS resolver available.
Create a security group for the DNS forwarder
-
In the management console
, select the folder where you are deploying your infrastructure. -
In the list of services, select Virtual Private Cloud.
-
In the left-hand panel, select
Security groups and click Create security group. -
In the Name field, specify
dns-forwarder-sg. -
In the Network field, select the network the Bind9 VM resides in.
-
Under Rules, create the following traffic management rules:
Traffic
directionDescription Port range Protocol Source /
Destination nameCIDR blocks /
Security groupInbound dns-udp53UDPCIDR172.16.2.0/24Inbound dns-tcp53TCPCIDR172.16.2.0/24Outbound dns-udp-forward53UDPCIDR10.129.0.0/24Outbound dns-tcp-forward53TCPCIDR10.129.0.0/24Outbound dns-udp-forward53UDPCIDR10.130.0.0/24Outbound dns-tcp-forward53TCPCIDR10.130.0.0/24Note
This example uses the
10.129.0.0/24and10.130.0.0/24subnets hosting the10.129.0.2and10.130.0.2DNS resolvers. Replace them with the subnets of your Virtual Private Cloud subnet DNS resolvers. Virtual Private Cloud DNS resolvers have addresses of the10.X.0.2type, whereXis the subnet number. -
If required, add a rule for SSH access to the VM:
Traffic
directionDescription Port range Protocol Source /
Destination nameCIDR blocks /
Security groupIngress ssh22TCPCIDR<administrative_subnet_CIDR> -
Click Create.
Configure a DNS forwarder on a VM
-
Connect to the VM over SSH.
-
Install Bind9:
sudo apt update sudo apt install -y bind9 bind9-utils dnsutils -
Open the
/etc/bind/named.conf.optionsfile and specify the forwarding options:sudo nano /etc/bind/named.conf.optionsConfiguration example:
options { directory "/var/cache/bind"; recursion yes; allow-recursion { 172.16.2.0/24; localhost; }; allow-query { 172.16.2.0/24; localhost; }; forwarders { 10.129.0.2; 10.130.0.2; }; dnssec-validation auto; listen-on { any; }; listen-on-v6 { any; }; }; -
Check the configuration and restart the service:
sudo named-checkconf sudo systemctl restart bind9 sudo systemctl enable bind9 -
Make sure the service is running:
sudo systemctl status bind9 --no-pager -
If your VM is running the
systemd-resolvedlocal resolver which overwrites/etc/resolv.conf, configure it to work with Bind9:sudo systemctl stop systemd-resolved sudo systemctl disable systemd-resolved sudo mv /etc/resolv.conf /etc/resolv.conf.backupCreate a static
/etc/resolv.conffile:sudo nano /etc/resolv.confAdd a record:
nameserver 127.0.0.1 -
Set up a nameserver in Ubuntu. Edit the
/run/systemd/resolve/stub-resolv.conffile:sudo nano /run/systemd/resolve/stub-resolv.confSet
nameserverto127.0.0.1. -
Test the forwarder locally:
dig @127.0.0.1 NS mdb.yandexcloud.net +shortThe response should include the zone's DNS server addresses.
Alternative check:
dig @127.0.0.1 yandex.ru +shortIf forwarding is configured correctly, this command will return the domain's IP address.
Configure a DNS client on the BareMetal server
-
Connect to the BareMetal server over SSH.
-
Set the Bind9 VM's IP address as the DNS server:
sudo nano /etc/resolv.confHere is an example:
nameserver 10.129.0.10 -
If
/etc/resolv.confis managed by a system service, set DNS persistently.Option 1: Use the
systemd-resolvecommand for a specific interface:sudo systemd-resolve --interface ethXX --set-dns 10.129.0.10Where
ethXXis the private network interface.Option 2: Edit the
/etc/systemd/resolved.conffile:sudo nano /etc/systemd/resolved.confSpecify the following:
[Resolve] DNS=10.129.0.10 Domains=~.Apply the settings:
sudo systemctl restart systemd-resolved -
Test DNS resolution:
dig mdb.yandexcloud.net +shortTest resolution of the Managed Service for PostgreSQL cluster host FQDN:
dig c-<cluster_ID>-<host_number>.mdb.yandexcloud.net +shortThe commands should return cluster host IP addresses from the Virtual Private Cloud subnet, e.g.,
10.129.0.29for the master and10.130.0.15for the replica.
Test FQDN access to the Managed Service for PostgreSQL cluster
-
Install the CA certificate:
mkdir -p ~/.postgresql && \ wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" \ --output-document ~/.postgresql/root.crt && \ chmod 0600 ~/.postgresql/root.crt -
Install the PostgreSQL client:
sudo apt update && sudo apt install -y postgresql-client -
Connect to the database using FQDNs:
psql "host=<master_FQDN>,<replica_FQDN> port=6432 sslmode=verify-full dbname=<DB_name> user=<user_name> target_session_attrs=read-write"You can find the connection parameters for your cluster in the management console on the Managed Service for PostgreSQL cluster page.
Check the result
After you complete the setup:
- The server in the BareMetal segment successfully resolves domain names in the
mdb.yandexcloud.netzone. - Managed Service for PostgreSQL сluster host FQDNs are resolved to Virtual Private Cloud IP addresses.
- PostgreSQL connection is established using the FQDNs, so you do not need to manually specify IP addresses.
Warning
-
Use static IP addresses for the DNS forwarder VM and the BareMetal server to avoid DNS resolution issues upon restart.
-
Regularly check Bind9 logs to detect issues with DNS requests:
sudo journalctl -u bind9 -f -
If you change the subnet hosting the Managed Service for PostgreSQL cluster, update the DNS resolver addresses in the
forwardersBind9 configuration parameter.
How to delete the resources you created
To stop using resources and avoid additional costs:
- Delete the VM with the DNS forwarder.
- Delete the security group created for the DNS forwarder.
- If you no longer need the test cluster, delete the Managed Service for PostgreSQL cluster.
- If you used Yandex Cloud Interconnect to set up connectivity for testing purposes only, delete the associated network settings and Cloud Interconnect resources.