この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
いわさです。
Azure Database for MySQLはデフォルトではインターネット経路でのパブリックなアクセスとなっていますが、セキュリティ設定でプライベートなアクセスが可能になります。
今回 Azure App Service や Azure Functions などのリソースからプライベートアクセスが必要になったので、良い機会だったので2020年にGAされたプライベートリンク機能を試しました。
Azure Private Link
Azure Private Linkを使うと、インターネットへ経路を解放しなくても、Azure仮想ネットワーク(VNET)内のリソース間でプライベートな接続が出来るようになります。
それにより、組織のセキュリティポリシーを満たすことが出来たり、オンプレミスとの間でExpressRoute経路でプライベートなアクセスが可能になったりします。
通常の接続経路
仮想マシン作成
まずはプライベートではない接続を行ってみます。
Azure Virtual MachineでLinux Ubuntu Server 18.04-LTSをインストールします。
今回はAzure Database for MySQLへアクセスすることが要件なので、MySQLクライアントをインストールします。
iwasa@linux2021:~$ sudo apt-get -y install mysql-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
…省略…
Setting up mysql-client-5.7 (5.7.32-0ubuntu0.18.04.1) ...
Setting up mysql-client (5.7.32-0ubuntu0.18.04.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.3) ...
iwasa@linux2021:~$
データベース作成
Azure Database for MySQLを新規作成します。
作成後、"接続のセキュリティ"から、"Azureサービスへのアクセスを許可" を "はい" に設定します。
構築完了後、こちらの情報を使って接続を行います。
iwasa@linux2021:~$ mysql -u iwasa@mysqlvnet20210109 -h mysqlvnet20210109.mysql.database.azure.com -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 65469
Server version: 5.6.47.0 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
接続出来ました。
プライベート接続
パブリックアクセスの拒否設定
Azure Database for MySQL のセキュリティ設定より "パブリック ネットワーク アクセスの拒否" を "はい"に変更します。
"Azureサービスへのアクセスを許可"も無効化されます。
設定後、仮想マシンより接続を試みてみましょう。
接続に失敗します。
プライベートエンドポイント接続
プライベート接続するためのエンドポイントを作成します。 Azure Database for MySQL のメニューより"プライベートエンドポイント接続"を選択します。
リソースの種類に"Microsoft.DBforMySQL/servers"を選択し、リソースにプライベート接続を有効化したいMySQLインスタンスを指定します。
仮想ネットワーク、サブネットは接続させたいリソースを鑑みて決めてください。
ここでは、ネットワークセキュリティグループなどについて特に考慮する必要のないように仮想マシンと同一のサブネットで構成しました。
iwasa@linux2021:~$ nslookup mysqlvnet20210109.mysql.database.azure.com
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
mysqlvnet20210109.mysql.database.azure.com canonical name = mysqlvnet20210109.privatelink.mysql.database.azure.com.
Name: mysqlvnet20210109.privatelink.mysql.database.azure.com
Address: 10.0.0.5
名前解決してみると、プラベートアドレスが確認出来ました。
mysqlコマンドで接続してみます。
iwasa@linux2021:~$ mysql -u iwasa@mysqlvnet20210109 -h mysqlvnet20210109.mysql.database.azure.com -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 65449
Server version: 5.6.47.0 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
接続出来ました。
まとめ
2020年にはたくさんのAzureサービスのプライベートリンクが公開されました。
マネージドサービスを使うことが多いAzureですが、プライベートネットワークを構築する機会が増えるような気はしています。