0

我有一个地形化的 Azure MySQL 实例和一个在 Azure 容器实例中运行的 WordPress docker 实例。两者都很好,但我看不到自动允许从容器实例访问 MySQL 的方法,因为 1)流量不是通过外部 IP 地址来的,2)我不知道实际的 IP 地址在哪里正在创建,并且 3) 我看不到确定 IP 地址是什么的方法。

resource "azurerm_container_group" "wp-container-group" {
   name                = var.container_group_name
   location            = azurerm_resource_group.wordpress-resource-group.location
   resource_group_name = azurerm_resource_group.wordpress-resource-group.name
   ip_address_type     = "public"
   dns_name_label      = var.dns_label
   os_type             = "Linux"

   container {
      name   = "wordpress"
      image  = "wordpress:latest"
      ...
   }
   ...
}


resource "azurerm_mysql_server" "wordpress_mysql" {
   name                = "foo-bar"
   location            = azurerm_resource_group.wordpress-resource-group.location
   resource_group_name = azurerm_resource_group.wordpress-resource-group.name

   ....
}


resource "azurerm_mysql_database" "wp-db" {
   name                = "wordpress"
   resource_group_name = azurerm_resource_group.wordpress-resource-group.name
   server_name         = azurerm_mysql_server.wordpress_mysql.name
   charset             = "utf8"
   collation           = "utf8_general_ci"
}

这设置为允许来自外部 IP 地址的流量:

resource "azurerm_mysql_firewall_rule" "allow_container" {
   name                = "allow_wordpress_container"
   resource_group_name = azurerm_resource_group.wordpress-resource-group.name
   server_name         = azurerm_mysql_server.wordpress_mysql.name
   start_ip_address    = azurerm_container_group.wp-container-group.ip_address
   end_ip_address      = azurerm_container_group.wp-container-group.ip_address
}

当我 SSH 进入容器实例并尝试通过命令行连接时mysql,它告诉我它使用的 IP 地址与外部地址不同——内部地址在 52.xxx 范围内。我可以手动将此 IP 地址添加为防火墙规则,但我想自动添加。

所以我的问题是:这个 52.xxx 地址在哪里分配,如何在 Terraform 中访问它,以便我可以自动配置容器实例和 mysql 之间的防火墙规则?

4

2 回答 2

1

与容器实例关联的出站 IP 地址不可用作容器的属性。IP 地址也不能保证在容器重启后仍然存在,因此它不是防火墙规则的可靠标识符。

在这种情况下,最简单的解决方案是在数据库防火墙中“允许访问 Azure 服务”。这是通过创建一个azurerm_sql_firewall_rule拥有start_ip_addressend_ip_address设置为“0.0.0.0”来实现的

于 2020-06-24T02:01:20.213 回答
0

请注意,“允许访问 Azure 服务”是指访问所有 Azure 服务,即使不是您的服务。Azure 门户允许在配置 Azure 数据库的网络连接时选中“允许从 Azure 中的 Azure 服务和资源公开访问此服务器组”,这看起来不错。但相关的工具提示显示“此选项将防火墙配置为允许来自分配给任何 Azure 服务或资产的 IP 地址的连接,包括来自其他客户订阅的连接。

并且还允许 IP 0.0.0.0 到 255.255.255.255 访问您的数据库打开了通往全世界的大门......

于 2022-02-01T15:16:09.117 回答