|
| 1 | +--- |
| 2 | +title: How Service Connector helps Azure Kubernetes Service (AKS) connect to other Azure services |
| 3 | +description: Learn how to use Service Connector in Azure Kubernetes Service (AKS). |
| 4 | +author: houk-ms |
| 5 | +ms.service: service-connector |
| 6 | +ms.topic: conceptual |
| 7 | +ms.date: 03/01/2024 |
| 8 | +ms.author: honc |
| 9 | +--- |
| 10 | +# How to use Service Connector in Azure Kubernetes Service (AKS) |
| 11 | + |
| 12 | +Azure Kubernetes Service (AKS) is one of the compute services supported by Service Connector. This article aims to help you understand: |
| 13 | + |
| 14 | +* What operations are made on the cluster when creating a service connection. |
| 15 | +* How to use the kubernetes resources Service Connector creates. |
| 16 | +* How to troubleshoot and view logs of Service Connector in an AKS cluster. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +* This guide assumes that you already know the [basic concepts of Service Connector](concept-service-connector-internals.md). |
| 21 | + |
| 22 | +## What operations Service Connector makes on the cluster |
| 23 | + |
| 24 | +Depending on the different target services and authentication types selected when creating a service connection, Service Connector makes different operations on the AKS cluster. The following lists the possible operations made by Service Connector. |
| 25 | + |
| 26 | +### Add the Service Connector kubernetes extension |
| 27 | + |
| 28 | +A kubernetes extension named `sc-extension` is added to the cluster the first time a service connection is created. Later on, the extension helps create kubernetes resources in user's cluster, whenever a service connection request comes to Service Connector. You can find the extension in your AKS cluster in the Azure portal, in the Extensions + applications menu. |
| 29 | + |
| 30 | +:::image type="content" source="./media/aks-tutorial/sc-extension.png" alt-text="Screenshot of the Azure portal, view AKS extension."::: |
| 31 | + |
| 32 | +The extension is also where the cluster connections metadata are stored. Uninstalling the extension makes all the connections in the cluster unavailable. The extension operator is hosted in the cluster namespace `sc-system`. |
| 33 | + |
| 34 | +### Create kubernetes resources |
| 35 | + |
| 36 | +Service Connector creates some kubernetes resources to the namespace the user specified when creating a service connection. The kubernetes resources store the connection information, which is needed by the user's workload definitions or application code to talk to target services. Depending on different authentication types, different kubernetes resources are created. For the `Connection String` and `Service Principal` auth types, a kubernetes secret is created. For the `Workload Identity` auth type, a kubernetes service account is also created in addition to a kubernetes secret. |
| 37 | + |
| 38 | +You can find the kubernetes resources created by Service Connector for each service connection on the Azure portal in your kubernetes resource, in the Service Connector menu. |
| 39 | + |
| 40 | +:::image type="content" source="./media/aks-tutorial/kubernetes-resources.png" alt-text="Screenshot of the Azure portal, view Service Connector created kubernetes resources."::: |
| 41 | + |
| 42 | +Deleting a service connection doesn't delete the associated Kubernetes resource. If necessary, remove your resource manually, using for example the kubectl delete command. |
| 43 | + |
| 44 | +### Enable the `azureKeyvaultSecretsProvider` addon |
| 45 | + |
| 46 | +If target service is Azure Key Vault and the Secret Store CSI Driver is enabled when creating a service connection, Service Connector enables the `azureKeyvaultSecretsProvider` add-on for the cluster. |
| 47 | + |
| 48 | +:::image type="content" source="./media/aks-tutorial/keyvault-csi.png" alt-text="Screenshot of the Azure portal, enabling CSI driver for keyvault when creating a connection."::: |
| 49 | + |
| 50 | +Follow the [Connect to Azure Key Vault using CSI driver tutorial](./tutorial-python-aks-keyvault-csi-driver.md)to set up a connection to Azure Key Vault using Secret Store CSI driver. |
| 51 | + |
| 52 | +### Enable workload identity and OpenID Connect (OIDC) issuer |
| 53 | + |
| 54 | +If the authentication type is `Workload Identity` when creating a service connection, Service Connector enables workload identity and OIDC issuer for the cluster. |
| 55 | + |
| 56 | +:::image type="content" source="./media/aks-tutorial/workload-identity.png" alt-text="Screenshot of the Azure portal, using workload identity to create a connection."::: |
| 57 | + |
| 58 | +When the authentication type is `Workload Identity`, a user-assigned managed identity is needed to create the federated identity credential. Learn more from [what are workload identities](/entra/workload-id/workload-identities-overview), or follow the [tutorial](./tutorial-python-aks-storage-workload-identity.md)to set up a connection to Azure Storage using workload identity. |
| 59 | + |
| 60 | +## How to use the Service Connector created kubernetes resources |
| 61 | + |
| 62 | +Different kubernetes resources are created when the target service type and authentication type are different. The following sections show how to use the Service Connector created kubernetes resources in your cluster workloads definition and application codes. |
| 63 | + |
| 64 | +#### Kubernetes secret |
| 65 | + |
| 66 | +A kubernetes secret is created when the authentication type is `Connection String` or `Service Principal`. Your cluster workload definition can reference the secret directly. The following snnipet is an example. |
| 67 | + |
| 68 | +```yaml |
| 69 | +apiVersion: batch/v1 |
| 70 | +kind: Job |
| 71 | +metadata: |
| 72 | + namespace: default |
| 73 | + name: sc-sample-job |
| 74 | +spec: |
| 75 | + template: |
| 76 | + spec: |
| 77 | + containers: |
| 78 | + - name: raw-linux |
| 79 | + image: alpine |
| 80 | + command: ['printenv'] |
| 81 | + envFrom: |
| 82 | + - secretRef: |
| 83 | + name: <SecretCreatedByServiceConnector> |
| 84 | + restartPolicy: OnFailure |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +Then, your application codes can consume the connection string in the secret from environment variable. You can check the [sample code](./how-to-integrate-storage-blob.md) to learn more about the environment variable names and how to use them in your application codes to authenticate to different target services. |
| 89 | + |
| 90 | +#### Kubernetes service account |
| 91 | + |
| 92 | +Both a kubernetes service account and a secret are created when the authentication type is `Workload Identity`. Your cluster workload definition can reference the service account and secret to authenticate through workload identity. The following snipet provides an example. |
| 93 | + |
| 94 | +```yaml |
| 95 | +apiVersion: batch/v1 |
| 96 | +kind: Job |
| 97 | +metadata: |
| 98 | + namespace: default |
| 99 | + name: sc-sample-job |
| 100 | + labels: |
| 101 | + azure.workload.identity/use: "true" |
| 102 | +spec: |
| 103 | + template: |
| 104 | + spec: |
| 105 | + serviceAccountName: <ServiceAccountCreatedByServiceConnector> |
| 106 | + containers: |
| 107 | + - name: raw-linux |
| 108 | + image: alpine |
| 109 | + command: ['printenv'] |
| 110 | + envFrom: |
| 111 | + - secretRef: |
| 112 | + name: <SecretCreatedByServiceConnector> |
| 113 | + restartPolicy: OnFailure |
| 114 | +``` |
| 115 | +
|
| 116 | +You may check the tutorial to learn [how to connect to Azure Storage using workload identity](tutorial-python-aks-storage-workload-identity.md). |
| 117 | +
|
| 118 | +## How to troubleshoot and view logs |
| 119 | +
|
| 120 | +If an error happens and couldn't be mitigated by retrying when creating a service connection, the following methods can help gather more information for troubleshooting. |
| 121 | +
|
| 122 | +### Check Service Connector kubernetes extension |
| 123 | +
|
| 124 | +Service Connector kubernetes extension is built on top of [Azure Arc-enabled Kubernetes cluster extensions](../azure-arc/kubernetes/extensions.md). Use the following commands to investigate if there are any errors during the extension installation or updating. |
| 125 | +
|
| 126 | +1. Install the `k8s-extension` Azure CLI extension. |
| 127 | + |
| 128 | +```azurecli |
| 129 | +az extension add --name k8s-extension |
| 130 | +``` |
| 131 | + |
| 132 | +1. Get the Service Connector extension status. Check the `statuses` property in the command output to see if there are any errors. |
| 133 | + |
| 134 | +```azurecli |
| 135 | +az k8s-extension show \ |
| 136 | + --resource-group MyClusterResourceGroup \ |
| 137 | + --cluster-name MyCluster \ |
| 138 | + --cluster-type managedClusters \ |
| 139 | + --name sc-extension |
| 140 | +``` |
| 141 | + |
| 142 | +### Check kubernetes cluster logs |
| 143 | + |
| 144 | +If there's an error during the extension installation, and the error message in the `statuses` property doesn't provide enough information about what happened, you can further check the kubernetes logs with the followings steps. |
| 145 | + |
| 146 | +1. Connect to your AKS cluster. |
| 147 | + |
| 148 | + ```azurecli |
| 149 | + az aks get-credentials \ |
| 150 | + --resource-group MyClusterResourceGroup \ |
| 151 | + --name MyCluster |
| 152 | + ``` |
| 153 | +1. Service Connector extension is installed in the namespace `sc-system` through helm chart, check the namespace and the helm release by following commands. |
| 154 | + |
| 155 | + - Check the namespace exists. |
| 156 | + |
| 157 | + ```Bash |
| 158 | + kubectl get ns |
| 159 | + ``` |
| 160 | + |
| 161 | + - Check the helm release status. |
| 162 | + |
| 163 | + ```Bash |
| 164 | + helm list -n sc-system |
| 165 | + ``` |
| 166 | +1. During the extension installation or updating, a kubernetes job called `sc-job` creates the kubernetes resources for the service connection. The job execution failure usually causes the extension failure. Check the job status by running the following commands. If `sc-job` doesn't exist in `sc-system` namespace, it should have been executed successfully. This job is designed to be automatically deleted after successful execution. |
| 167 | + |
| 168 | + - Check the job exists. |
| 169 | + |
| 170 | + ```Bash |
| 171 | + kubectl get job -n sc-system |
| 172 | + ``` |
| 173 | + |
| 174 | + - Get the job status. |
| 175 | + |
| 176 | + ```Bash |
| 177 | + kubectl describe job/sc-job -n sc-system |
| 178 | + ``` |
| 179 | + |
| 180 | + - View the job logs. |
| 181 | + |
| 182 | + ```Bash |
| 183 | + kubectl logs job/sc-job -n sc-system |
| 184 | + ``` |
| 185 | + |
| 186 | +## Next steps |
| 187 | + |
| 188 | +Learn how to integrate different target services and read about their configuration settings and authentication methods. |
| 189 | + |
| 190 | +> [!div class="nextstepaction"] |
| 191 | +> [Learn about how to integrate storage blob](./how-to-integrate-storage-blob.md) |
0 commit comments