Skip to content

Commit 4390bf4

Browse files
yuwzhosophiasodomainnameninpan-msSneezry
authored
Add deploy to AKS (#212)
* Add skeleton * Update * Update 01-create-kubernetes-service.md * Add one application * Add Nginx * Add how to deploy eureka server * Update 02-create-eureka-server.md * Update 06-containerize-application.md * Update 06-containerize-application.md * configserver & gateway yaml * Add code * Add config server creation * Add gateway doc * Update eureka server document * Update 02-create-eureka-server.md * Update 02-create-eureka-server.md * Update eureka server doc * Update 02-create-eureka-server.md * Update 03-create-spring-admin.md * Rename 03-create-spring-admin.md to 03-create-spring-boot-admin.md * Update README.md * Create configserver-config.yaml * Update 03-create-spring-boot-admin.md * Add configmap * Add other applications * add sba * Update 03-create-spring-boot-admin.md * Update 09-get-log-and-metrics.md * Update eureka doc for the ConfigMap part * Update service name according to gateway * Add Gateway TLS * Change uri of routes to http * Add azure monitor * Create AKS * add * Update document before setup application * Update the vars * add application * Add all applcations * Add view log and metrics * Add next steps * Add readme * fix typos * Update * gitattributes * Update to HTTP * add * Update some descriptions and commands (#2) * update * update * Apply suggestions from code review --------- Co-authored-by: Yuwei Zhou <[email protected]> * Add gitingore * Remove changelog in applicaiton * Remove expose to public * Add dessciption * Add version * Add dependencies * Update * correct links * Correct links --------- Co-authored-by: Weiping Pan <[email protected]> Co-authored-by: Jeff <[email protected]> Co-authored-by: ninpan-ms <[email protected]> Co-authored-by: Ningting Pan <[email protected]> Co-authored-by: Zhe Li <[email protected]> Co-authored-by: Zhe Li <[email protected]> Co-authored-by: Muyao Feng <[email protected]> Co-authored-by: Dingmeng Xue <[email protected]>
1 parent f4e8a21 commit 4390bf4

File tree

72 files changed

+5293
-1
lines changed

Some content is hidden

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

72 files changed

+5293
-1
lines changed

.gitattributes

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Set default behavior to automatically normalize line endings
2+
* text=auto
3+
4+
# Explicitly declare text files you want to always be normalized and converted to native line endings on checkout.
5+
*.java text
6+
*.xml text
7+
*.properties text
8+
*.md text
9+
*.txt text
10+
11+
# Declare files that will always have CRLF line endings on checkout.
12+
*.bat text eol=crlf
13+
*.cmd text eol=crlf
14+
15+
# Declare files that will always have LF line endings on checkout.
16+
*.sh text eol=lf
17+
18+
# Denote all files that are truly binary and should not be modified.
19+
*.png binary
20+
*.jpg binary
21+
*.jpeg binary
22+
*.gif binary
23+
*.ico binary
24+
*.pdf binary
25+
*.zip binary
26+
*.jar binary
27+
*.war binary
28+
*.ear binary

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,10 @@ hello-world/
5858

5959
# vsCode
6060
.vscode
61+
62+
**/.gradle
63+
**/.idea
64+
**/.vscode
65+
**/.classpath
66+
67+
**/target

apps/acme-cart/redis_conn.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from os import environ
66
from azure_vault import vault_secret
77
from distutils.util import strtobool
8+
from azure.identity import DefaultAzureCredential
89

910
def redis_connection(logger):
1011

@@ -17,6 +18,7 @@ def redis_connection(logger):
1718
redis_port = environ['REDIS_PORT'] if environ.get('REDIS_PORT') not in (None, '') else 6380
1819

1920
redis_password = environ['REDIS_PASSWORD'] if environ.get('REDIS_PASSWORD') not in (None, '') else None
21+
user_name = environ['REDIS_USERNAME'] if environ.get('REDIS_USERNAME') not in (None, '') else None
2022

2123
tlsEnabledEnv = auth_url = environ['REDIS_TLS_ENABLED'] if environ.get('REDIS_TLS_ENABLED') not in (None, '') else 'true'
2224
tlsEnabled = strtobool(tlsEnabledEnv)
@@ -32,7 +34,9 @@ def redis_connection(logger):
3234
redis_conn = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, db=0, ssl=tlsEnabled)
3335
elif redis_host not in (None, ''):
3436
logger.info('initiating redis connection with no password')
35-
redis_conn = redis.StrictRedis(host=redis_host, port=redis_port, password=None, db=0)
37+
cred = DefaultAzureCredential()
38+
token = cred.get_token('https://redis.azure.com/.default')
39+
redis_conn = redis.StrictRedis(host=redis_host, port=redis_port, ssl=True, decode_responses=True, username=user_name, password=token.token, db=0)
3640
else:
3741
logger.info('initiating redis connection with no host or password (using redislite)')
3842
redis_conn = Redis('redis.db')

azure-kubernetes-service/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 Acme Fitness Store project.
4+
5+
## Table of Contents
6+
7+
1. [01-create-kubernetes-service.md](./docs/01-create-kubernetes-service.md)
8+
- 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.
9+
10+
2. [02-create-eureka-server.md](./docs/02-create-eureka-server.md)
11+
- Instructions to create and deploy a Eureka Server on AKS for service discovery.
12+
13+
3. [03-create-config-server.md](./docs/03-create-config-server.md)
14+
- Guide to create and deploy a Spring Cloud Config Server on AKS for centralized configuration management.
15+
16+
4. [04-create-spring-boot-admin.md](./docs/04-create-spring-boot-admin.md)
17+
- Steps to create and deploy a Spring Boot Admin server on AKS for managing and monitoring Spring Boot applications.
18+
19+
5. [05-create-application-gateway.md](./docs/05-create-application-gateway.md)
20+
- Instructions to create and deploy a Spring Cloud Gateway on AKS for API Gateway functionality.
21+
22+
6. [06-create-application-supporting-service.md](./docs/06-create-application-supporting-service.md)
23+
- Guide to set up supporting services like PostgreSQL and Redis Cache for your applications on AKS.
24+
25+
7. [08-containerize-application.md](./docs/08-containerize-application.md)
26+
- Steps to build and push application images to Azure Container Registry.
27+
28+
8. [09-02-deploy-application-connect-spring-cloud-component.md](./docs/09-02-deploy-application-connect-spring-cloud-component.md)
29+
- Instructions to deploy the Acme Payment application on AKS and connect it to Spring Cloud components.
30+
31+
9. [09-03-deploy-spring-boot-application-connect-postgresql.md](./docs/09-03-deploy-spring-boot-application-connect-postgresql.md)
32+
- Guide to deploy the Acme Catalog application on AKS and connect it to a PostgreSQL database.
33+
34+
10. [09-04-deploy-dotnet-application-connect-postgresql.md](./docs/09-04-deploy-dotnet-application-connect-postgresql.md)
35+
- Steps to deploy the Acme Order application on AKS and connect it to a PostgreSQL database.
36+
37+
11. [09-05-deploy-python-application-connect-with-redis.md](./docs/09-05-deploy-python-application-connect-with-redis.md)
38+
- Instructions to deploy the Acme Cart application on AKS and connect it to a Redis cache.
39+
40+
12. [10-get-log-and-metrics.md](./docs/10-get-log-and-metrics.md)
41+
- Guide to view logs and metrics for your AKS cluster to monitor containerized applications effectively.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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 acme-fitness-store/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+
1. 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+
60+
Run `az feature register --namespace Microsoft.Compute --name EncryptionAtHost` to wait for its state to be `Registered`.
61+
62+
1. Create workspace
63+
```
64+
az monitor log-analytics workspace create --resource-group ${RESOURCE_GROUP} --workspace-name ${WORKSPACE_NAME}
65+
```
66+
67+
1. Create AKS.
68+
Below commands guide you to create the AKS. For more information on the features enabled in the AKS cluster, refer to the following documentation:
69+
70+
- [Attach Azure Container Registry to AKS](https://learn.microsoft.com/en-us/azure/aks/cluster-container-registry-integration)
71+
- [Enable Workload Identity](https://learn.microsoft.com/en-us/azure/aks/workload-identity-overview)
72+
- [Azure Load Balancer SKU](https://learn.microsoft.com/en-us/azure/load-balancer/skus)
73+
- [Cluster Autoscaler](https://learn.microsoft.com/en-us/azure/aks/cluster-autoscaler)
74+
- [Network concepts](https://learn.microsoft.com/en-us/azure/aks/concepts-network)
75+
- [Encryption at Host](https://learn.microsoft.com/en-us/azure/aks/enable-host-encryption)
76+
- [Outbound Type](https://learn.microsoft.com/en-us/azure/aks/egress-outboundtype)
77+
- [Node pools](https://learn.microsoft.com/en-us/azure/aks/create-node-pools)
78+
- [Storage concepts](https://learn.microsoft.com/en-us/azure/aks/concepts-storage)
79+
- [Monitor AKS](https://learn.microsoft.com/en-us/azure/aks/monitor-aks)
80+
81+
82+
```
83+
WORKSPACE_ID=$(az monitor log-analytics workspace show --resource-group ${RESOURCE_GROUP} --workspace-name ${WORKSPACE_NAME} --query id -o tsv)
84+
```
85+
86+
```
87+
az aks create \
88+
-g ${RESOURCE_GROUP} \
89+
-n ${AKS_NAME} \
90+
--attach-acr ${ACR_NAME} \
91+
--enable-workload-identity \
92+
--load-balancer-sku standard \
93+
--enable-cluster-autoscaler \
94+
--max-count 40 \
95+
--min-count 1 \
96+
--network-plugin azure \
97+
--no-ssh-key \
98+
--enable-encryption-at-host \
99+
--outbound-type loadBalancer \
100+
--enable-oidc-issuer \
101+
--enable-aad \
102+
--vm-set-type VirtualMachineScaleSets \
103+
--os-sku Mariner \
104+
--node-osdisk-size 100 \
105+
--node-osdisk-type Ephemeral \
106+
--node-vm-size Standard_D4as_v4 \
107+
--enable-azure-monitor-metrics \
108+
--enable-addons monitoring \
109+
--workspace-resource-id ${WORKSPACE_ID}
110+
```
111+
112+
> Note: After creating the AKS, it may take some time to update. During this time, the following commands will fail.
113+
114+
```
115+
az aks nodepool add \
116+
--cluster-name ${AKS_NAME} \
117+
-g ${RESOURCE_GROUP} \
118+
-n nodepool2 \
119+
--enable-cluster-autoscaler \
120+
--enable-encryption-at-host \
121+
--max-count 40 \
122+
--min-count 1 \
123+
--node-osdisk-size 200 \
124+
--node-osdisk-type Ephemeral \
125+
--node-vm-size Standard_D8as_v4 \
126+
--os-sku Mariner \
127+
--os-type Linux \
128+
--node-count 1
129+
az aks nodepool add \
130+
--cluster-name ${AKS_NAME} \
131+
-g ${RESOURCE_GROUP} \
132+
-n nodepool3 \
133+
--enable-cluster-autoscaler \
134+
--enable-encryption-at-host \
135+
--max-count 40 \
136+
--min-count 1 \
137+
--node-osdisk-size 200 \
138+
--node-osdisk-type Ephemeral \
139+
--node-vm-size Standard_D16as_v4 \
140+
--os-sku Mariner \
141+
--os-type Linux \
142+
--node-count 1
143+
```
144+
145+
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
146+
147+
```
148+
az aks get-credentials --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME} --overwrite-existing --admin
149+
```
150+
151+
1. Install or update the kubectl CLI
152+
```
153+
az aks install-cli
154+
```
155+
156+
1. Verify you can connect to the AKS
157+
158+
```
159+
kubectl get ns
160+
```
161+
162+
### 6. Create Azure Keyvault and cert
163+
164+
1. Get AKS outbound IPs and record these IPs as `<AKS-outbound-ip>`
165+
```
166+
az aks show -g ${RESOURCE_GROUP} -n ${AKS_NAME} --query networkProfile.loadBalancerProfile.effectiveOutboundIPs[].id
167+
az resource show --ids <the ID from previous output> --query properties.ipAddress -o tsv
168+
```
169+
170+
1. Get AKS Vnet IDs
171+
```
172+
NODE_RESOURCE_GROUP=$(az aks show --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME} --query nodeResourceGroup -o tsv)
173+
az resource list --resource-type microsoft.network/virtualnetworks -g ${NODE_RESOURCE_GROUP} --query "[?starts_with(name, 'aks-vnet')].name" -o tsv
174+
```
175+
176+
List all subnets under the vnet, record these ids as `<subnet-ids>`
177+
```
178+
az network vnet subnet list --resource-group ${NODE_RESOURCE_GROUP} --vnet-name <vnetName> --query "[].id" -o tsv
179+
```
180+
181+
1. Create Azure KeyVault
182+
`az keyvault create --resource-group ${RESOURCE_GROUP} --name ${KEYVAULT_NAME} --network-acls-ips <AKS-outbound-ip> --network-acls-vnets <subnet-ids>`
183+
184+
1. Assign access to yourself
185+
```
186+
# Get your Azure AD user ID
187+
USER_ID=$(az ad signed-in-user show --query id --output tsv)
188+
KEYVUALT_ID=$(az keyvault show --name ${KEYVAULT_NAME} --query id --output tsv)
189+
# Assign yourself the necessary permissions
190+
az role assignment create --role "Key Vault Certificates Officer" --assignee ${USER_ID} --scope ${KEYVUALT_ID}
191+
```
192+
193+
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
194+
> Here suggest to create a wildcard domain cert, like `*.demo.com`.
195+
196+
### 7. Enable Nginx in Kubernetes
197+
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).
198+
199+
1. Enable Nginx
200+
```
201+
az extension add -n aks-preview --upgrade
202+
az aks approuting enable --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME}
203+
204+
KEYVUALT_ID=$(az keyvault show --name ${KEYVAULT_NAME} --query id --output tsv)
205+
az aks approuting update --resource-group ${RESOURCE_GROUP} --name ${AKS_NAME} --nginx External --enable-kv --attach-kv ${KEYVUALT_ID}
206+
```
207+
208+
1. Retrieve the Nginx public IP and note the "EXTERNAL-IP"
209+
210+
```
211+
kubectl get svc nginx -n app-routing-system
212+
```
213+
214+
1. Go to your DNS zone to add record.
215+
- 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.
216+
- Add a wildcard CName record to the A record. E.g. `*.demo.com` points to the `demo.com`
217+
218+
## Next Steps
219+
220+
- Follow [02-create-eureka-server](./02-create-eureka-server.md) to create and deploy a Eureka Server on Azure Kubernetes Service.

0 commit comments

Comments
 (0)