Skip to content

Commit 1704016

Browse files
yuwzhoNetyyyy
andauthored
Add Azure Kubernetes Service Samples (#73)
* aks * update * update * update * update * update * update * update * update * update comments --------- Co-authored-by: Muyao <v-muyaofeng@microsoft.com>
1 parent b71a50b commit 1704016

File tree

71 files changed

+4420
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+4420
-36
lines changed

azure-kubernetes-service/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Azure Kubernetes Service Documentation
2+
3+
This folder contains documentation for setting up and deploying various components on Azure Kubernetes Service (AKS) for the Spring PetClinic microservices project.
4+
5+
## Table of Contents
6+
7+
1. [Create Kubernetes Service](./docs/01-create-kubernetes-service.md)
8+
2. [Create Eureka Server](./docs/02-create-eureka-server.md)
9+
3. [Create Config Server](./docs/03-create-config-server.md)
10+
4. [Create Spring Boot Admin](./docs/04-create-spring-boot-admin.md)
11+
5. [Create Application Supporting Service](./docs/05-create-application-supporting-service.md)
12+
6. [Containerize Application](./docs/06-containerize-application.md)
13+
7. [Deploy Application](./docs/07-deploy-application.md)
14+
8. [Get Log and Metrics](./docs/08-get-log-and-metrics.md)
15+
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
## Introduction
2+
This document provides a step-by-step guide to create an Azure Kubernetes Service (AKS) cluster, integrate it with Azure Container Registry (ACR), and set up Azure Key Vault and Nginx ingress with a CA certificate.
3+
4+
## Prerequisites
5+
- Azure CLI installed
6+
- Azure subscription
7+
- Sufficient permissions to create resources in the Azure subscription:
8+
- **Contributor** - Creates resources and all other Azure resources.
9+
- **User Access Administrator** - Assign necessary roles.
10+
11+
## Outputs
12+
- Azure Container Registry (ACR)
13+
- Azure Kubernetes Service (AKS) connected to ACR
14+
- Azure Key Vault
15+
- Nginx ingress with CA certificate
16+
17+
## Steps
18+
19+
### 1. Clone the repo
20+
Clone the git repo and go to the working folder.
21+
```bash
22+
cd spring-petclinic-microservices/azure-kubernetes-service
23+
```
24+
25+
### 2. Set Variables
26+
27+
Update `resources/var.sh` and set up the variables for your environment.
28+
```
29+
source resources/var.sh
30+
az account set -s ${SUBSCRIPTION}
31+
32+
echo "RESOURCE_GROUP=${RESOURCE_GROUP}"
33+
echo "AKS_NAME=${AKS_NAME}"
34+
echo "ACR_NAME=${ACR_NAME}"
35+
echo "KEYVAULT_NAME=${KEYVAULT_NAME}"
36+
echo "WORKSPACE_NAME=${WORKSPACE_NAME}"
37+
```
38+
39+
### 3. Create Resource Group
40+
Create a resource group to host all the Azure resources.
41+
```bash
42+
az group create -n ${RESOURCE_GROUP} -l eastus2
43+
```
44+
45+
### 4. Create Azure Container Registry
46+
Create Azure Container Registry (ACR). This ACR will be used to:
47+
- Build application components
48+
- Store application images built by buildpack
49+
50+
```bash
51+
az acr create -g ${RESOURCE_GROUP} -n ${ACR_NAME} --sku Premium
52+
```
53+
54+
### 5. Create AKS
55+
1. Enable `EncryptionAtHost`, may take 10+ minutes to finish
56+
```bash
57+
az feature register --namespace Microsoft.Compute --name EncryptionAtHost
58+
```
59+
Run `az feature register --namespace Microsoft.Compute --name EncryptionAtHost` to wait for its state to be `Registered`.
60+
61+
1. Create workspace
62+
```
63+
az monitor log-analytics workspace create --resource-group ${RESOURCE_GROUP} --workspace-name ${WORKSPACE_NAME}
64+
```
65+
66+
1. Create AKS.
67+
Below commands guide you to create the AKS. For more information on the features enabled in the AKS cluster, refer to the following documentation:
68+
69+
- [Attach Azure Container Registry to AKS](https://learn.microsoft.com/en-us/azure/aks/cluster-container-registry-integration)
70+
- [Enable Workload Identity](https://learn.microsoft.com/en-us/azure/aks/workload-identity-overview)
71+
- [Azure Load Balancer SKU](https://learn.microsoft.com/en-us/azure/load-balancer/skus)
72+
- [Cluster Autoscaler](https://learn.microsoft.com/en-us/azure/aks/cluster-autoscaler)
73+
- [Network concepts](https://learn.microsoft.com/en-us/azure/aks/concepts-network)
74+
- [Encryption at Host](https://learn.microsoft.com/en-us/azure/aks/enable-host-encryption)
75+
- [Outbound Type](https://learn.microsoft.com/en-us/azure/aks/egress-outboundtype)
76+
- [Node pools](https://learn.microsoft.com/en-us/azure/aks/create-node-pools)
77+
- [Storage concepts](https://learn.microsoft.com/en-us/azure/aks/concepts-storage)
78+
- [Monitor AKS](https://learn.microsoft.com/en-us/azure/aks/monitor-aks)
79+
80+
81+
```
82+
WORKSPACE_ID=$(az monitor log-analytics workspace show --resource-group ${RESOURCE_GROUP} --workspace-name ${WORKSPACE_NAME} --query id -o tsv)
83+
```
84+
85+
```
86+
az aks create \
87+
-g ${RESOURCE_GROUP} \
88+
-n ${AKS_NAME} \
89+
--attach-acr ${ACR_NAME} \
90+
--enable-workload-identity \
91+
--load-balancer-sku standard \
92+
--enable-cluster-autoscaler \
93+
--max-count 40 \
94+
--min-count 1 \
95+
--network-plugin azure \
96+
--no-ssh-key \
97+
--enable-encryption-at-host \
98+
--outbound-type loadBalancer \
99+
--enable-oidc-issuer \
100+
--enable-aad \
101+
--vm-set-type VirtualMachineScaleSets \
102+
--os-sku Mariner \
103+
--node-osdisk-size 100 \
104+
--node-osdisk-type Ephemeral \
105+
--node-vm-size Standard_D4as_v4 \
106+
--enable-azure-monitor-metrics \
107+
--enable-addons monitoring \
108+
--workspace-resource-id ${WORKSPACE_ID}
109+
```
110+
111+
> Note: After creating the AKS, it may take some time to update. During this time, the following commands will fail.
112+
113+
```
114+
az aks nodepool add \
115+
--cluster-name ${AKS_NAME} \
116+
-g ${RESOURCE_GROUP} \
117+
-n nodepool2 \
118+
--enable-cluster-autoscaler \
119+
--enable-encryption-at-host \
120+
--max-count 40 \
121+
--min-count 1 \
122+
--node-osdisk-size 200 \
123+
--node-osdisk-type Ephemeral \
124+
--node-vm-size Standard_D8as_v4 \
125+
--os-sku Mariner \
126+
--os-type Linux \
127+
--node-count 1
128+
```
129+
130+
> Note: This command also needs some time to finish updating.
131+
132+
```
133+
az aks nodepool add \
134+
--cluster-name ${AKS_NAME} \
135+
-g ${RESOURCE_GROUP} \
136+
-n nodepool3 \
137+
--enable-cluster-autoscaler \
138+
--enable-encryption-at-host \
139+
--max-count 40 \
140+
--min-count 1 \
141+
--node-osdisk-size 200 \
142+
--node-osdisk-type Ephemeral \
143+
--node-vm-size Standard_D16as_v4 \
144+
--os-sku Mariner \
145+
--os-type Linux \
146+
--node-count 1
147+
```
148+
149+
1. Retrieve access token. This command gets the admin access for the AKS cluster. For more access management, see https://learn.microsoft.com/en-us/azure/aks/azure-ad-rbac?tabs=portal
150+
151+
```
152+
az aks get-credentials --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME} --overwrite-existing --admin
153+
```
154+
155+
1. Install or update the kubectl CLI
156+
```
157+
az aks install-cli
158+
```
159+
160+
1. Verify you can connect to the AKS
161+
162+
```
163+
kubectl get ns
164+
```
165+
166+
### 6. Create Azure Keyvault and cert
167+
168+
1. Get AKS outbound IPs and record these IPs as `<AKS-outbound-ip>`
169+
```
170+
az aks show -g ${RESOURCE_GROUP} -n ${AKS_NAME} --query networkProfile.loadBalancerProfile.effectiveOutboundIPs[].id
171+
az resource show --ids <the ID from previous output> --query properties.ipAddress -o tsv
172+
```
173+
174+
1. Get AKS Vnet IDs
175+
```
176+
NODE_RESOURCE_GROUP=$(az aks show --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME} --query nodeResourceGroup -o tsv)
177+
az resource list --resource-type microsoft.network/virtualnetworks -g ${NODE_RESOURCE_GROUP} --query "[?starts_with(name, 'aks-vnet')].name" -o tsv
178+
```
179+
180+
List all subnets under the vnet, record these ids as `<subnet-ids>`
181+
```
182+
az network vnet subnet list --resource-group ${NODE_RESOURCE_GROUP} --vnet-name <vnetName> --query "[].id" -o tsv
183+
```
184+
185+
1. Create Azure KeyVault
186+
`az keyvault create --resource-group ${RESOURCE_GROUP} --name ${KEYVAULT_NAME} --network-acls-ips <AKS-outbound-ip> --network-acls-vnets <subnet-ids>`
187+
188+
1. Assign access to yourself
189+
```
190+
# Get your Azure AD user ID
191+
USER_ID=$(az ad signed-in-user show --query id --output tsv)
192+
KEYVUALT_ID=$(az keyvault show --name ${KEYVAULT_NAME} --query id --output tsv)
193+
# Assign yourself the necessary permissions
194+
az role assignment create --role "Key Vault Certificates Officer" --assignee ${USER_ID} --scope ${KEYVUALT_ID}
195+
```
196+
197+
1. Create a self-signed certificate or import your CA cert to the Keyvault, ref: https://learn.microsoft.com/en-us/azure/key-vault/certificates/tutorial-import-certificate?tabs=azure-portal
198+
> Here suggest to create a wildcard domain cert, like `*.demo.com`.
199+
200+
### 7. Enable Nginx in Kubernetes
201+
Below steps guide how to enable the Nginx as add-on in the AKS cluster. For more details can view [Managed NGINX ingress with the application routing add-on](https://learn.microsoft.com/en-us/azure/aks/app-routing).
202+
203+
1. Enable Nginx
204+
```
205+
az extension add -n aks-preview --upgrade
206+
az aks approuting enable --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME}
207+
208+
KEYVUALT_ID=$(az keyvault show --name ${KEYVAULT_NAME} --query id --output tsv)
209+
az aks approuting update --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME} --nginx External --enable-kv --attach-kv ${KEYVUALT_ID}
210+
```
211+
212+
1. Retrieve the Nginx public IP and note the "EXTERNAL-IP"
213+
214+
```
215+
kubectl get svc nginx -n app-routing-system
216+
```
217+
218+
1. Go to your DNS zone to add record.
219+
- Add A record to point the domain in your TLS cert to the external IP you obtained. E.g. `demo.com` points to the IP address.
220+
- Add a wildcard CName record to the A record. E.g. `*.demo.com` points to the `demo.com`
221+
222+
## Next Steps
223+
224+
- Follow [02-create-eureka-server](./02-create-eureka-server.md) to create and deploy a Eureka Server on Azure Kubernetes Service.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
## Introduction
2+
3+
In this guide, you will learn how to create and deploy a Eureka Server on Azure Kubernetes Service (AKS). Eureka Server is a service registry that allows microservices to register themselves and discover other registered services. See more details in [Service Discovery: Eureka Server](https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html).
4+
5+
> This guide uses a generic Eureka server. If you want to use the discovery server specific to this project, package the code in the `spring-petclinic-discovery-server` directory and deploy it. Refer to the subsequent application deployment guide for detailed instructions.
6+
7+
## Prerequisites
8+
9+
- Follow [01-create-kubernetes-service](./01-create-kubernetes-service.md) to create Azure Kubernetes Service and Azure Container Registry.
10+
- Maven
11+
- Azure CLI
12+
- Docker
13+
14+
## Outputs
15+
16+
By the end of this guide, you will have a running Eureka Server on your AKS cluster.
17+
18+
## Steps
19+
20+
### Prepare the Eureka Server Image
21+
22+
1. **Setup variables**
23+
24+
Set up the variables used to deploy Eureka Server:
25+
```bash
26+
source resources/var.sh
27+
az account set -s ${SUBSCRIPTION}
28+
29+
echo "RESOURCE_GROUP=${RESOURCE_GROUP}"
30+
echo "AKS_NAME=${AKS_NAME}"
31+
echo "ACR_NAME=${ACR_NAME}"
32+
echo "EUREKA_IMAGE_TAG=${EUREKA_IMAGE_TAG}"
33+
```
34+
35+
1. **Package the Eureka Server**
36+
37+
Go to the `azure-kubernetes-service/resources/eureka/eureka-server` folder and build the Eureka server package:
38+
39+
```bash
40+
cd resources/eureka/eureka-server
41+
mvn clean package -DskipTests
42+
```
43+
44+
1. **Build the Docker image**
45+
46+
Use Azure Container Build to build the Eureka image. For more details, see [Automate container image builds and maintenance with Azure Container Registry tasks](https://learn.microsoft.com/en-us/azure/container-registry/container-registry-tasks-overview).
47+
48+
```azurecli
49+
az acr build --registry ${ACR_NAME} --image eureka-server:${EUREKA_IMAGE_TAG} target/docker
50+
```
51+
52+
### Deploy the Eureka Server
53+
54+
1. **Edit the Kubernetes Resource File**
55+
56+
Locate the `eureka-server.yaml` file in the `azure-kubernetes-service/resources/eureka` directory. Edit the following code snippet:
57+
58+
- **`<eureka-image-tag>`**: Update to the value of `${EUREKA_IMAGE_TAG}`
59+
- **`<acr-name>`**: Update to the value of `${ACR_NAME}`
60+
61+
```yaml
62+
containers:
63+
- name: eureka-server
64+
image: "<acr-name>.azurecr.io/eureka-server:<eureka-image-tag>"
65+
```
66+
67+
1. **Apply the Kubernetes Configuration**
68+
69+
Apply the configuration using kubectl to create the Eureka Server:
70+
71+
```bash
72+
kubectl apply -f ../eureka-server.yaml
73+
```
74+
75+
The `eureka-server.yaml` file contains the necessary Kubernetes resources to deploy the Eureka Server. It includes:
76+
77+
- **Service**: Exposes the Eureka Server on port 8761.
78+
- **ConfigMap**: Stores configuration data for the Eureka Server can be consumed by other deployments.
79+
- **Deployment**: Manages the deployment of the Eureka Server, including resource requests and limits, probes for liveness and readiness, and lifecycle hooks.
80+
81+
1. **Verify the Deployment**
82+
83+
Use the following command to check the status of the Eureka Server pod:
84+
85+
```bash
86+
kubectl get pods
87+
```
88+
89+
If successful, you should see something like:
90+
91+
```
92+
NAME READY STATUS RESTARTS AGE
93+
eureka-server-867c8c97b6-nvqjx 1/1 Running 0 36m
94+
```
95+
96+
**Tip**: If the pod is not running, check for errors using:
97+
98+
```bash
99+
kubectl describe pod <pod-name>
100+
kubectl logs <pod-name>
101+
```
102+
103+
## Next Steps
104+
105+
- Follow [03-create-config-server](./03-create-config-server.md) to create and deploy a Spring Cloud Config Server on Azure Kubernetes Service.
106+

0 commit comments

Comments
 (0)