Skip to content

Commit 633e4c1

Browse files
authored
Merge pull request #228691 from schaffererin/aks-static-ip
Resolving GitIssues
2 parents 3fb7d92 + cff0b8c commit 633e4c1

File tree

1 file changed

+81
-74
lines changed

1 file changed

+81
-74
lines changed

articles/aks/static-ip.md

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,146 @@
11
---
2-
title: Use static IP with load balancer
2+
title: Use a static IP with a load balancer in Azure Kubernetes Service (AKS)
33
titleSuffix: Azure Kubernetes Service
44
description: Learn how to create and use a static IP address with the Azure Kubernetes Service (AKS) load balancer.
55
author: asudbring
66
ms.author: allensu
77
ms.subservice: aks-networking
88
ms.topic: how-to
9-
ms.date: 11/14/2020
10-
9+
ms.date: 02/27/2023
1110

1211
#Customer intent: As a cluster operator or developer, I want to create and manage static IP address resources in Azure that I can use beyond the lifecycle of an individual Kubernetes service deployed in an AKS cluster.
1312
---
1413

1514
# Use a static public IP address and DNS label with the Azure Kubernetes Service (AKS) load balancer
1615

17-
By default, the public IP address assigned to a load balancer resource created by an AKS cluster is only valid for the lifespan of that resource. If you delete the Kubernetes service, the associated load balancer and IP address are also deleted. If you want to assign a specific IP address or retain an IP address for redeployed Kubernetes services, you can create and use a static public IP address.
16+
When you create a load balancer resource in an Azure Kubernetes Service (AKS) cluster, the public IP address assigned to it is only valid for the lifespan of that resource. If you delete the Kubernetes service, the associated load balancer and IP address are also deleted. If you want to assign a specific IP address or retain an IP address for redeployed Kubernetes services, you can create and use a static public IP address.
1817

1918
This article shows you how to create a static public IP address and assign it to your Kubernetes service.
2019

2120
## Before you begin
2221

23-
This article assumes that you have an existing AKS cluster. If you need an AKS cluster, see the AKS quickstart [using the Azure CLI][aks-quickstart-cli], [using Azure PowerShell][aks-quickstart-powershell], or [using the Azure portal][aks-quickstart-portal].
24-
25-
You also need the Azure CLI version 2.0.59 or later installed and configured. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][install-azure-cli].
26-
27-
This article covers using a *Standard* SKU IP with a *Standard* SKU load balancer. For more information, see [IP address types and allocation methods in Azure][ip-sku].
22+
* This article assumes that you have an existing AKS cluster. If you need an AKS cluster, see the AKS quickstart [using the Azure CLI][aks-quickstart-cli], [using Azure PowerShell][aks-quickstart-powershell], or [using the Azure portal][aks-quickstart-portal].
23+
* You need the Azure CLI version 2.0.59 or later installed and configured. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][install-azure-cli].
24+
* This article covers using a *Standard* SKU IP with a *Standard* SKU load balancer. For more information, see [IP address types and allocation methods in Azure][ip-sku].
2825

2926
## Create a static IP address
3027

31-
Create a static public IP address with the [az network public ip create][az-network-public-ip-create] command. The following creates a static IP resource named *myAKSPublicIP* in the *myResourceGroup* resource group:
28+
1. Use the `az aks show`[az-aks-show] command to get the node resource group name of your AKS cluster, which follows this format: `MC_<resource group name>_<AKS cluster name>_<region>`.
3229

33-
```azurecli-interactive
34-
az network public-ip create \
35-
--resource-group myResourceGroup \
36-
--name myAKSPublicIP \
37-
--sku Standard \
38-
--allocation-method static
39-
```
30+
```azurecli-interactive
31+
az aks show \
32+
--resource-group myResourceGroup \
33+
--name myAKSCluster
34+
--query nodeResourceGroup
35+
--output tsv
36+
```
4037
41-
> [!NOTE]
42-
> If you are using a *Basic* SKU load balancer in your AKS cluster, use *Basic* for the *sku* parameter when defining a public IP. Only *Basic* SKU IPs work with the *Basic* SKU load balancer and only *Standard* SKU IPs work with *Standard* SKU load balancers.
43-
44-
The IP address is displayed, as shown in the following condensed example output:
45-
46-
```json
47-
{
48-
"publicIp": {
49-
...
50-
"ipAddress": "40.121.183.52",
51-
...
52-
}
53-
}
54-
```
38+
2. Use the [`az network public ip create`][az-network-public-ip-create] command to create a static public IP address. The following example creates a static IP resource named *myAKSPublicIP* in the *MC_myResourceGroup_myAKSCluster_eastus* node resource group.
5539
56-
You can later get the public IP address using the [az network public-ip list][az-network-public-ip-list] command. Specify the name of the node resource group and public IP address you created, and query for the *ipAddress* as shown in the following example:
40+
```azurecli-interactive
41+
az network public-ip create \
42+
--resource-group MC_myResourceGroup_myAKSCluster_eastus \
43+
--name myAKSPublicIP \
44+
--sku Standard \
45+
--allocation-method static
46+
```
5747
58-
```azurecli-interactive
59-
$ az network public-ip show --resource-group myResourceGroup --name myAKSPublicIP --query ipAddress --output tsv
48+
> [!NOTE]
49+
> If you're using a *Basic* SKU load balancer in your AKS cluster, use *Basic* for the `--sku` parameter when defining a public IP. Only *Basic* SKU IPs work with the *Basic* SKU load balancer and only *Standard* SKU IPs work with *Standard* SKU load balancers.
6050
61-
40.121.183.52
62-
```
51+
3. After you create the static public IP address, use the [`az network public-ip list`][az-network-public-ip-list] command to get the IP address. Specify the name of the node resource group and public IP address you created, and query for the *ipAddress*.
52+
53+
```azurecli-interactive
54+
az network public-ip show --resource-group MC_myResourceGroup_myAKSCluster_eastus --name myAKSPublicIP --query ipAddress --output tsv
55+
```
6356
6457
## Create a service using the static IP address
6558
66-
Before creating a service, ensure the cluster identity used by the AKS cluster has delegated permissions to the other resource group. For example:
59+
1. Before creating a service, use the [`az role assignment create`][az-role-assignment-create] command to ensure the cluster identity used by the AKS cluster has delegated permissions to the node resource group.
60+
61+
```azurecli-interactive
62+
az role assignment create \
63+
--assignee <Client ID> \
64+
--role "Network Contributor" \
65+
--scope /subscriptions/<subscription id>/resourceGroups/<MC_myResourceGroup_myAKSCluster_eastus>
66+
```
67+
68+
> [!IMPORTANT]
69+
> If you customized your outbound IP, make sure your cluster identity has permissions to both the outbound public IP and the inbound public IP.
70+
71+
2. Create a file named `load-balancer-service.yaml` and copy in the contents of the following YAML file, providing your own public IP address created in the previous step and the node resource group name.
72+
73+
```yaml
74+
apiVersion: v1
75+
kind: Service
76+
metadata:
77+
annotations:
78+
service.beta.kubernetes.io/azure-load-balancer-resource-group: MC_myResourceGroup_myAKSCluster_eastus
79+
name: azure-load-balancer
80+
spec:
81+
loadBalancerIP: 40.121.183.52
82+
type: LoadBalancer
83+
ports:
84+
- port: 80
85+
selector:
86+
app: azure-load-balancer
87+
```
88+
89+
3. Use the `kubectl apply` command to create the service and deployment.
6790
68-
```azurecli-interactive
69-
az role assignment create \
70-
--assignee <Client ID> \
71-
--role "Network Contributor" \
72-
--scope /subscriptions/<subscription id>/resourceGroups/<resource group name>
91+
```console
92+
kubectl apply -f load-balancer-service.yaml
7393
```
7494

75-
> [!IMPORTANT]
76-
> If you customized your outbound IP make sure your cluster identity has permissions to both the outbound public IP and this inbound public IP.
95+
## Apply a DNS label to the service
7796

78-
To create a *LoadBalancer* service with the static public IP address, add the `loadBalancerIP` property and the value of the static public IP address to the YAML manifest. Create a file named `load-balancer-service.yaml` and copy in the following YAML. Provide your own public IP address created in the previous step. The following example also sets the annotation to the resource group named *myResourceGroup*. Provide your own resource group name.
97+
If your service uses a dynamic or static public IP address, you can use the `service.beta.kubernetes.io/azure-dns-label-name` service annotation to set a public-facing DNS label. This publishes a fully qualified domain name (FQDN) for your service using Azure's public DNS servers and top-level domain. The annotation value must be unique within the Azure location, so it's recommended to use a sufficiently qualified label. Azure automatically appends a default suffix in the location you selected, such as `<location>.cloudapp.azure.com`, to the name you provide, creating the FQDN.
7998

8099
```yaml
81100
apiVersion: v1
82101
kind: Service
83102
metadata:
84103
annotations:
85-
service.beta.kubernetes.io/azure-load-balancer-resource-group: myResourceGroup
104+
service.beta.kubernetes.io/azure-dns-label-name: myserviceuniquelabel
86105
name: azure-load-balancer
87106
spec:
88-
loadBalancerIP: 40.121.183.52
89107
type: LoadBalancer
90108
ports:
91109
- port: 80
92110
selector:
93111
app: azure-load-balancer
94112
```
95113
96-
Create the service and deployment with the `kubectl apply` command.
114+
To see the DNS label for your load balancer, run the following command:
97115
98116
```console
99-
kubectl apply -f load-balancer-service.yaml
117+
kubectl describe service azure-load-balancer
100118
```
101119

102-
## Apply a DNS label to the service
103-
104-
If your service is using a dynamic or static public IP address, you can use the service annotation `service.beta.kubernetes.io/azure-dns-label-name` to set a public-facing DNS label. This publishes a fully qualified domain name for your service using Azure's public DNS servers and top-level domain. The annotation value must be unique within the Azure location, so it's recommended to use a sufficiently qualified label.
105-
106-
Azure will then automatically append a default suffix, such as `<location>.cloudapp.azure.com` (where location is the region you selected), to the name you provide, to create the fully qualified DNS name. For example:
120+
The DNS label will be listed under the `Annotations`, as shown in the following condensed example output:
107121

108-
```yaml
109-
apiVersion: v1
110-
kind: Service
111-
metadata:
112-
annotations:
113-
service.beta.kubernetes.io/azure-dns-label-name: myserviceuniquelabel
114-
name: azure-load-balancer
115-
spec:
116-
type: LoadBalancer
117-
ports:
118-
- port: 80
119-
selector:
120-
app: azure-load-balancer
122+
```console
123+
Name: azure-load-balancer
124+
Namespace: default
125+
Labels: <none>
126+
Annotations: service.beta.kuberenetes.io/azure-dns-label-name: myserviceuniquelabel
127+
...
121128
```
122129

123-
> [!NOTE]
130+
> [!NOTE]
124131
> To publish the service on your own domain, see [Azure DNS][azure-dns-zone] and the [external-dns][external-dns] project.
125132
126133
## Troubleshoot
127134

128-
If the static IP address defined in the *loadBalancerIP* property of the Kubernetes service manifest does not exist, or has not been created in the node resource group and no additional delegations configured, the load balancer service creation fails. To troubleshoot, review the service creation events with the [kubectl describe][kubectl-describe] command. Provide the name of the service as specified in the YAML manifest, as shown in the following example:
135+
If the static IP address defined in the *loadBalancerIP* property of the Kubernetes service manifest doesn't exist or hasn't been created in the node resource group and there are no additional delegations configured, the load balancer service creation fails. To troubleshoot, review the service creation events using the [`kubectl describe`][kubectl-describe] command. Provide the name of the service specified in the YAML manifest, as shown in the following example:
129136

130137
```console
131138
kubectl describe service azure-load-balancer
132139
```
133140

134-
Information about the Kubernetes service resource is displayed. The *Events* at the end of the following example output indicate that the *user supplied IP Address was not found*. In these scenarios, verify that you have created the static public IP address in the node resource group and that the IP address specified in the Kubernetes service manifest is correct.
141+
The output will show you information about the Kubernetes service resource. The following example output shows a `Warning` in the `Events`: "`user supplied IP address was not found`." In this scenario, make sure you've created the static public IP address in the node resource group and that the IP address specified in the Kubernetes service manifest is correct.
135142

136-
```
143+
```console
137144
Name: azure-load-balancer
138145
Namespace: default
139146
Labels: <none>
@@ -157,22 +164,22 @@ Events:
157164

158165
## Next steps
159166

160-
For additional control over the network traffic to your applications, you may want to instead [create an ingress controller][aks-ingress-basic]. You can also [create an ingress controller with a static public IP address][aks-static-ingress].
167+
For additional control over the network traffic to your applications, you may want to [create an ingress controller][aks-ingress-basic]. You can also [create an ingress controller with a static public IP address][aks-static-ingress].
161168

162169
<!-- LINKS - External -->
163170
[kubectl-describe]: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#describe
164171
[azure-dns-zone]: https://azure.microsoft.com/services/dns/
165172
[external-dns]: https://github.com/kubernetes-sigs/external-dns
166173

167174
<!-- LINKS - Internal -->
168-
[aks-faq-resource-group]: faq.md#why-are-two-resource-groups-created-with-aks
169175
[az-network-public-ip-create]: /cli/azure/network/public-ip#az_network_public_ip_create
170176
[az-network-public-ip-list]: /cli/azure/network/public-ip#az_network_public_ip_list
171-
[az-aks-show]: /cli/azure/aks#az_aks_show
172177
[aks-ingress-basic]: ingress-basic.md
173178
[aks-static-ingress]: ingress-static-ip.md
174179
[aks-quickstart-cli]: ./learn/quick-kubernetes-deploy-cli.md
175180
[aks-quickstart-portal]: ./learn/quick-kubernetes-deploy-portal.md
176181
[aks-quickstart-powershell]: ./learn/quick-kubernetes-deploy-powershell.md
177182
[install-azure-cli]: /cli/azure/install-azure-cli
178183
[ip-sku]: ../virtual-network/ip-services/public-ip-addresses.md#sku
184+
[az-role-assignment-create]: /cli/azure/role/assignment#az-role-assignment-create
185+
[az-aks-show]: /cli/azure/aks#az-aks-show

0 commit comments

Comments
 (0)