PostgreSQL을 시작할 때 WireGuard 주소를 바인딩할 수 없습니다.

PostgreSQL을 시작할 때 WireGuard 주소를 바인딩할 수 없습니다.

VPN 터널을 설정하기 위해 시스템 시작 시 wg-quick.service를 실행합니다.

root@db ~ # cat /usr/lib/systemd/system/[email protected]
[Unit]
Description=WireGuard via wg-quick(8) for %I
After=network-online.target nss-lookup.target
Wants=network-online.target nss-lookup.target
PartOf=wg-quick.target
Documentation=man:wg-quick(8)
Documentation=man:wg(8)
Documentation=https://www.wireguard.com/
Documentation=https://www.wireguard.com/quickstart/
Documentation=https://git.zx2c4.com/wireguard-tools/about/src/man/wg-quick.8
Documentation=https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/wg-quick up %i
ExecStop=/usr/bin/wg-quick down %i
Environment=WG_ENDPOINT_RESOLUTION_RETRIES=infinity

[Install]
WantedBy=multi-user.target

postgresql이 wireguard 주소를 수신 대기하도록 하고 싶습니다 10.100.0.107.

root@db ~ # cat /etc/postgresql/13/main/conf.d/db1.conf | grep listen
listen_addresses = '127.0.0.1,10.100.0.107' # what IP address(es) to listen on;

다시 시작한 후 postgresql 로그에 다음 오류가 표시됩니다.

2021-06-23 19:44:26.389 UTC [831] LOG:  starting PostgreSQL 13.3 (Ubuntu 13.3-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
2021-06-23 19:44:26.389 UTC [831] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2021-06-23 19:44:26.395 UTC [831] LOG:  could not bind IPv4 address "10.100.0.107": Cannot assign requested address
2021-06-23 19:44:26.395 UTC [831] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2021-06-23 19:44:26.395 UTC [831] WARNING:  could not create listen socket for "10.100.0.107"
2021-06-23 19:44:26.395 UTC [831] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-06-23 19:44:26.411 UTC [880] LOG:  database system was shut down at 2021-06-23 19:43:14 UTC
2021-06-23 19:44:26.422 UTC [831] LOG:  database system is ready to accept connections

불행하게도 postgresql은 10.100.0.107.

서버를 다시 시작한 후 postgresql을 다시 시작하면 도움이 됩니다. 또한 설정 listen_addresses='*'도 도움이 됩니다.

127.0.0.1하지만 지정된 주소 ( , ) 에 대한 연결만 허용하고 싶습니다 10.100.0.107. wg-quick 서비스를 성공적으로 초기화한 후 postgresql 서비스를 시작하는 방법은 무엇입니까?

감사해요!

답변1

postgresql 서비스 시작과 wireguard 터널 장치 구성 사이에 경쟁 조건이 있습니다.

이 문제를 처리하는 방법에는 여러 가지가 있습니다.

  1. After=/Wants=Postgres 서비스에 대한 직접적인 종속성
  2. Postgres를 임의의 IP 주소에 바인딩하고(예: 또는 추가하여 0.0.0.0) ::방화벽/패킷 필터에만 의존하여 Postgres의 와이어가드 주소/인터페이스(및 로컬 호스트) 액세스를 제한합니다.*listen_addresses
  3. 비로컬 바인딩을 활성화합니다.

이 사용 사례의 경우 systemd 종속성을 구성하는 것은 지루하고 오류가 발생하기 쉬우며 배포/systemd 버전에 따라 달라집니다. 마지막으로, wireguard 장치에 IP 주소가 할당된 후에만 wireguard 관련 서비스가 안정적으로 시작 신호를 보내는지 확신할 수 없습니다.

불행히도 Postgres는 인터페이스가 일시적으로 사라진 경우를 대비하여 몇 분 후에 지정된 주소에 대한 바인딩을 다시 시도하는 바인딩 재시도 기능을 지원하지 않는 것 같습니다.


Linux 비기본 바인딩 지원은 기본적으로 비활성화되어 있지만 sysctl을 사용하여 구성할 수 있습니다. 예:

sysctl net.ipv4.ip_nonlocal_bind=1

( /etc/sysctl.d/영구 구성 참조)

이후에는 Wireguard 장치가 구성되지 않은 경우에도 시작 중 Postgres 바인드 작업이 항상 성공합니다.

테스트하려면:

networkctl down wg0
ip -o a
systemctl restart postgresql.service
networkctl up wg0

관련 정보