Skip to content

Commit

Permalink
Use /etc/hosts for postgres internal DNS resolving
Browse files Browse the repository at this point in the history
This has 3 primary benefits;
- It is faster than making DNS queries
- It is faster and swift (no TTL) to update
- It allows setting up standbys that uses fully qualified domain names even
when there is no DNS server (i.e. development environments)
  • Loading branch information
byucesoy committed Feb 12, 2024
1 parent d0c790d commit 393cbe4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion model/postgres/postgres_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def configure_hash
net6: _1.net6.to_s
}
},
identity: resource.identity
identity: resource.identity,
hosts: "#{vm.ephemeral_net4} #{resource.identity}"
}
end

Expand Down
13 changes: 13 additions & 0 deletions rhizome/postgres/bin/configure
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ require_relative "../../common/lib/util"

configure_hash = JSON.parse($stdin.read)

# Update /etc/hosts
hosts = <<-HOSTS
127.0.0.1 localhost
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
#{configure_hash["hosts"]}
HOSTS
safe_write_to_file("/etc/hosts", hosts)

# Update postgresql.conf
configs = configure_hash["configs"].map { |k, v| "#{k} = #{v}" }.join("\n")
safe_write_to_file("/etc/postgresql/16/main/conf.d/001-service.conf", configs)
Expand Down
4 changes: 3 additions & 1 deletion spec/model/postgres/postgres_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Vm,
sshable: instance_double(Sshable),
mem_gib: 8,
ephemeral_net4: "1.2.3.4",
ephemeral_net6: NetAddr::IPv6Net.parse("fdfa:b5aa:14a3:4a3d::/64"),
private_subnets: [
instance_double(
Expand Down Expand Up @@ -76,7 +77,8 @@
net6: "fdfa:b5aa:14a3:4a3d::/64"
}
],
identity: "pgubid.postgres.ubicloud.com"
identity: "pgubid.postgres.ubicloud.com",
hosts: "1.2.3.4 pgubid.postgres.ubicloud.com"
}

expect(postgres_server.configure_hash).to eq(configure_hash)
Expand Down

0 comments on commit 393cbe4

Please sign in to comment.