Skip to content

Commit f541bbe

Browse files
authored
Merge pull request #280231 from maud-lv/ml-aks
Add tutorial: Connect to Azure SQL Database (AKS)
2 parents 44639dd + d0c1e45 commit f541bbe

File tree

8 files changed

+151
-2
lines changed

8 files changed

+151
-2
lines changed

articles/aks/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,8 @@
891891
href: deploy-application-template.md
892892
- name: Deploy Kubernetes applications with Azure CLI
893893
href: deploy-application-az-cli.md
894+
- name: Connect an AKS app to Azure SQL Database
895+
href: ../service-connector/tutorial-python-aks-sql-database-connection-string.md?toc=/azure/aks/toc.json&bc=/azure/aks/breadcrumb/toc.json
894896
- name: Dapr
895897
items:
896898
- name: Install the Dapr extension

articles/aks/breadcrumb/TOC.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@
5252
topicHref: /azure/aks/index
5353
- name: Azure Kubernetes Service
5454
tocHref: /compliance/regulatory/
55+
topicHref: /azure/aks/index
56+
- name: Azure Kubernetes Service
57+
tocHref: /azure/service-connector/
5558
topicHref: /azure/aks/index

articles/service-connector/index.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ landingContent:
8585
url: tutorial-python-aks-openai-connection-string.md
8686
- text: Connect to Azure OpenAI in AKS using workload identity
8787
url: tutorial-python-aks-openai-workload-identity.md
88+
- text: Connect to Azure SQL Database
89+
url: tutorial-python-aks-sql-database-connection-string.md
8890
- linkListType: how-to-guide
8991
links:
9092
- text: Manage authentication
99.9 KB
Loading
50.7 KB
Loading
47.6 KB
Loading

articles/service-connector/toc.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ items:
9696
href: tutorial-python-aks-keyvault-csi-driver.md
9797
- name: Connect to Azure Storage using workload identity
9898
href: tutorial-python-aks-storage-workload-identity.md
99-
- name: Connect to Azure OpenAI in AKS using a connection string
99+
- name: Connect to Azure OpenAI using a connection string
100100
href: tutorial-python-aks-openai-connection-string.md
101-
- name: Connect to Azure OpenAI in AKS using workload identity
101+
- name: Connect to Azure OpenAI using workload identity
102102
href: tutorial-python-aks-openai-workload-identity.md
103+
- name: Connect to Azure SQL Database using a connection string
104+
href: tutorial-python-aks-sql-database-connection-string.md
103105
- name: Concepts
104106
expanded: false
105107
items:
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
title: Connect an AKS app to Azure SQL Database
3+
titlesuffix: Service Connector
4+
description: Learn how to connect an app hosted on Azure Kubernetes Service (AKS) to Microsoft Azure SQL Database.
5+
#customer intent: As a developer, I want to connect my application hosted on AKS to Azure SQL Database.
6+
author: maud-lv
7+
ms.author: malev
8+
ms.service: service-connector
9+
ms.topic: tutorial
10+
ms.date: 07/23/2024
11+
---
12+
13+
# Tutorial: Connect an AKS app to Azure SQL Database (preview)
14+
15+
In this tutorial, you learn how to connect an application deployed to AKS, to an Azure SQL Database, using service connector (preview). You complete the following tasks:
16+
17+
> [!div class="checklist"]
18+
> * Create an Azure SQL Database resource
19+
> * Create a connection between the AKS cluster and the database with Service Connector.
20+
> * Update your container
21+
> * Update your application code
22+
> * Clean up Azure resources.
23+
24+
## Prerequisites
25+
26+
* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/).
27+
* An application deployed to AKS.
28+
* [!INCLUDE [azure-cli-prepare-your-environment.md](~/reusable-content/azure-cli/azure-cli-prepare-your-environment-no-header.md)]
29+
30+
## Create an Azure SQL Database
31+
32+
1. Create a resource group to store the Azure resources you create in this tutorial using the [`az group create`](/cli/azure/group#az_group_create) command.
33+
34+
```azurecli-interactive
35+
az group create \
36+
--name $RESOURCE_GROUP \
37+
--location eastus
38+
```
39+
40+
1. Follow the instructions to [create an Azure SQL Database](/azure/azure-sql/database/single-database-create-quickstart) in the resource group you created in the previous step. Make note of the server name, database name, and the database credentials for use throughout this tutorial.
41+
42+
## Create a service connection in AKS with Service Connector (preview)
43+
44+
### Register the Service Connector and Kubernetes Configuration resource providers
45+
46+
Register the Service Connector and Kubernetes Configuration resource providers using the [`az provider register`](/cli/azure/provider#az-provider-register) command.
47+
48+
```azurecli-interactive
49+
az provider register --namespace Microsoft.ServiceLinker
50+
```
51+
52+
```azurecli-interactive
53+
az provider register --namespace Microsoft.KubernetesConfiguration
54+
```
55+
56+
> [!TIP]
57+
> You can check if these resource providers are already registered using the `az provider show --namespace "Microsoft.ServiceLinker" --query registrationState` and `az provider show --namespace "Microsoft.KubernetesConfiguration" --query registrationState` commands. If the output is `Registered`, then the service provider is already registered.
58+
59+
60+
### Create a new connection
61+
62+
Create a service connection between your AKS cluster and your SQL database in the Azure portal or the Azure CLI.
63+
64+
### [Azure portal](#tab/azure-portal)
65+
66+
1. In the [Azure portal](https://portal.azure.com/), navigate to your AKS cluster resource.
67+
2. Select **Settings** > **Service Connector (Preview)** > **Create**.
68+
3. On the **Basics** tab, configure the following settings:
69+
70+
* **Kubernetes namespace**: Select **default**.
71+
* **Service type**: Select **SQL Database**.
72+
* **Connection name**: Use the connection name provided by Service Connector or enter your own connection name.
73+
* **Subscription**: Select the subscription that includes the Azure SQL Database service.
74+
* **SQL server**: Select your SQL server.
75+
* **SQL database**: Select your SQL database.
76+
* **Client type**: The code language or framework you use to connect to the target service, such as **Python**.
77+
78+
:::image type="content" source="media/tutorial-ask-sql/create-connection.png" alt-text="Screenshot of the Azure portal showing the form to create a new connection to a SQL database in AKS.":::
79+
80+
4. Select **Next: Authentication**. On the **Authentication** tab, enter your database username and password.
81+
5. Select **Next: Networking** > **Next: Review + create** >**Create**.
82+
6. Once the deployment is successful, you can view information about the new connection in the **Service Connector** pane.
83+
84+
### [Azure CLI](#tab/azure-cli)
85+
86+
Create a service connection to the SQL database using the [`az aks connection create sql`](/cli/azure/aks/connection/create#az-aks-connection-create-sql) command. You can run this command in two different ways:
87+
88+
* generate the new connection step by step.
89+
90+
```azurecli-interactive
91+
az aks connection create sql
92+
```
93+
94+
* generate the new connection at once. Make sure you replace the following placeholders with your own information: `<source-subscription>`, `<source_resource_group>`, `<cluster>`, `<target-subscription>`, `<target_resource_group>`, `<server>`, `<database>`, and `<***>`.
95+
96+
```azurecli-interactive
97+
az aks connection create sql \
98+
--source-id /subscriptions/<source-subscription>/resourceGroups/<source_resource_group>/providers/Microsoft.ContainerService/managedClusters/<cluster> \
99+
--target-id /subscriptions/<target-subscription>/resourceGroups/<target_resource_group>/providers/Microsoft.Sql/servers/<server>/databases/<database> \
100+
--secret name=<secret-name> secret=<secret>
101+
```
102+
103+
---
104+
105+
## Update your container
106+
107+
Now that you created a connection between your AKS cluster and the database, you need to retrieve the connection secrets and deploy them in your container.
108+
109+
1. In the [Azure portal](https://portal.azure.com/), navigate to your AKS cluster resource and select **Service Connector (Preview)**.
110+
1. Select the newly created connection, and then select **YAML snippet**. This action opens a panel displaying a sample YAML file generated by Service Connector.
111+
1. To set the connection secrets as environment variables in your container, you have two options:
112+
113+
* Directly create a deployment using the YAML sample code snippet provided. The snippet includes highlighted sections showing the secret object that will be injected as the environment variables. Select **Apply** to proceed with this method.
114+
115+
:::image type="content" source="media/tutorial-ask-sql/sample-yaml-snippet.png" alt-text="Screenshot of the Azure portal showing the sample YAML snippet to create a new connection to a SQL database in AKS.":::
116+
117+
* Alternatively, under **Resource Type**, select **Kubernetes Workload**, and then select an existing Kubernetes workload. This action sets the secret object of your new connection as the environment variables for the selected workload. After selecting the workload, select **Apply**.
118+
119+
:::image type="content" source="media/tutorial-ask-sql/kubernetes-snippet.png" alt-text="Screenshot of the Azure portal showing the Kubernetes snippet to create a new connection to a SQL database in AKS.":::
120+
121+
## Update your application code
122+
123+
As a final step, update your application code to use your environment variables, by [following these instructions](how-to-integrate-sql-database.md#connection-string).
124+
125+
## Clean up resources
126+
127+
If you no longer need the resources you created when following this tutorial, you can remove them by deleting the Azure resource group.
128+
129+
Delete your resource group using the [`az group delete`](/cli/azure/group#az_group_delete) command.
130+
131+
```azurecli-interactive
132+
az group delete --resource-group $RESOURCE_GROUP
133+
```
134+
135+
## Related content
136+
137+
Read the following articles to learn more about Service Connector concepts and how it helps AKS connect to Azure services:
138+
139+
* [Use Service Connector to connect AKS clusters to other cloud services](./how-to-use-service-connector-in-aks.md)
140+
* [Learn about Service Connector concepts](./concept-service-connector-internals.md)

0 commit comments

Comments
 (0)