简体   繁体   English

运行 airflow initdb 引发错误“postgres”以解决:使用 airflow 元数据库的 postgres 的名称或服务未知

[英]Running airflow initdb raises error `"postgres" to address: Name or service not known` using postgres for airflow meta database

Versions:版本:

Terraform==.12地形==.12

docker==19.03.8泊坞窗==19.03.8

python==3.8蟒蛇==3.8

postgreSQL==9.6 postgreSQL==9.6

apache-airflow==1.10.10阿帕奇气流==1.10.10

Description:描述:

The goal is to create a container for apache airflow local development.目标是为 apache airflow 本地开发创建一个容器。 I'm using a Postgres database as the backend to store metadata for airflow since it's required to use the Airflow LocalExecutor.我使用 Postgres 数据库作为后端来存储 airflow 的元数据,因为它需要使用 Airflow LocalExecutor。 Lastly, I'm using Terraform to create Airflow and Postgres containers.最后,我使用 Terraform 创建 Airflow 和 Postgres 容器。

Problem:问题:

The error is raised within the dev_airflow container's entrypoint.sh.该错误在dev_airflow容器的 entrypoint.sh 中引发。 Specifically airflow initdb raised an error related to the sql_alchemy_conn string.特别airflow initdb引发了与 sql_alchemy_conn 字符串相关的错误。 The sql_alchemy_conn string is exported as an env variable before running airflow initdb . sql_alchemy_conn 字符串在运行airflow initdb之前作为环境变量导出。 The sql_alchemy_conn's values are valid and represent a live postgres database. sql_alchemy_conn 的值是有效的并且代表一个实时的 postgres 数据库。

I've tried these sql_alchemy_conn strings:我试过这些 sql_alchemy_conn 字符串:

sql_alchemy_conn="postgresql://postgres_user:password@postgres:5432/postgres_db"
sql_alchemy_conn="postgresql+psycopg2://postgres_user:password@postgres:5432/postgres_db"

which resulted in the error:这导致了错误:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "postgres" to address: Name or service not known

I've tried these sql_alchemy_conn strings:我试过这些 sql_alchemy_conn 字符串:

sql_alchemy_conn="postgresql://postgres_user:password@localhost:5432/postgres_db"
sql_alchemy_conn="postgresql+psycopg2://postgres_user:password@localhost:5432/postgres_db"

which resulted in the error:这导致了错误:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

Terraform main.tf used to compose docker containers: Terraform main.tf 用来组成docker个容器:

provider "docker"{}

resource "docker_image" "postgres" {
  name     = "postgres:9.6"
}

resource "docker_container" "postgres" {
  image = docker_image.postgres.name
  name  = "postgres"

  env = [
    format("POSTGRES_USER=%s", var.postgres_user),
    format("POSTGRES_PASSWORD=%s", var.postgres_password),
    format("POSTGRES_DB=%s", var.postgres_db),
  ]

  ports {
    internal = var.postgres_port
  }
}

resource "docker_image" "local_airflow" {
  name         = "local_airflow:latest"
  keep_locally = false
}

resource "docker_container" "airflow" {
  image = docker_image.local_airflow.name
  name  = "dev_airflow"
  ports {
    internal = 8080
    external = 8080
  }

  env = [
    format("AIRFLOW__CORE__EXECUTOR=%s", lookup(var.executor, var.environment)), 
    format("AIRFLOW__CORE__LOAD_EXAMPLES=%s", var.load_examples),
    format("AIRFLOW__CORE__SQL_ALCHEMY_CONN=%s", var.sql_alchemy_conn)
  ]

  volumes {
      host_path = "/airflow/dags"
      container_path = "/usr/local/airflow/dags"
  }
  volumes {
    host_path = "/airflow/plugins"
    container_path = "/usr/local/airflow/plugins"
  }
  depends_on = [docker_container.postgres]

  command = ["webserver"]

  entrypoint = ["/entrypoint.sh"]

  healthcheck {
    test = ["CMD-SHELL", "[ -f /usr/local/airflow/airflow-webserver.pid ]"]
    interval = "30s"
    timeout = "30s"
    retries = 3
  }

  restart = "always"
}

Maybe this resources are deployed on default bridge.network.也许这个资源部署在默认的 bridge.network 上。

Try to create a custom.network尝试创建一个 custom.network

https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources.network https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources.network

and then, with this output, run this container in.network https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container .networks_advanced - (Optional, block) See Networks Advanced below for details.然后,使用这个 output,运行这个容器 in.network https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs/resources/container .networks_advanced -(可选,块)参见下面的网络高级. If this block has priority to the deprecated.network_alias and.network properties.如果此块优先于 deprecated.network_alias 和 .network 属性。

It's the same that you use with docker CLI.这与您使用 docker CLI 的方式相同。

docker network create foo
docker run --rm --network=foo bar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用气流的 postgres 数据库获取应用程序数据 - Using airflow's postgres database for application data 基于 postgres 的 airflow 网络服务器上的错误 - error on airflow webserver based on postgres Docker 图像未运行,数据库错误:django.db.utils.OperationalError 无法将主机名“postgres”转换为地址:名称或服务未知 - Docker image not running, db error: django.db.utils.OperationalError could not translate host name "postgres" to address: Name or service not known Heroku上偶尔出现Postgres错误:无法将主机名“<pg URL>”转换为地址:名称或服务未知(PG ::错误) - Occasional Postgres error on Heroku: could not translate host name “<pg URL>” to address: Name or service not known (PG::Error) 无法将主机名“postgres”转换为地址:名称或服务未知 - could not translate host name "postgres" to address: Name or service not known 无法将主机名“postgres”转换为地址:名称或服务未知 - DOCKER - Could not translate host name "postgres" to address: Name or service not known - DOCKER 如何在 Airflow 中将 Postgres 设置为后端数据库? - how to set Postgres as backend database in Airflow? 在气流中模拟 Postgres 响应 - Mocking Postgres Response in Airflow 在 Airflow 中找不到 Postgres 模块? - Postgres module not found in Airflow? 在 Airflow 中为 postgres 创建默认用户时出错 - Error when creating default user for postgres in Airflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM