Skip to content

Commit 4ad3c74

Browse files
committed
DevOps: Push DevOps-Project-17
* Deploying an app to AKS using Azure DevOps & Azure Cloud Shell Signed-off-by: NotHarshhaa <reddyharshhaa12@gmail.com>
1 parent f5abf7d commit 4ad3c74

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

DevOps Project-17/README.md

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
# Deploying an app to AKS using Azure DevOps & Azure Cloud Shell
2+
3+
# Prerequisites
4+
5+
1. Access to an Azure Account
6+
7+
2. Access to Azure DevOps and PAT Token
8+
9+
3. Access to a GitHub Account
10+
11+
4. Create an Azure DevOps Organization. Head [here](https://aex.dev.azure.com/) & click the “Create a new organization” button.
12+
13+
5. All of the following commands should be run in Azure Cloud Shell. Access the shell [here](https://shell.azure.com/) from any browser and logging into your Azure account.
14+
15+
16+
You can use the PowerShell screen, but in this walkthrough I use Bash. Type “bash” in the terminal to switch to bash commands.
17+
18+
## Overall Architecture
19+
20+
[![Overall Architecture](https://res.cloudinary.com/practicaldev/image/fetch/s--aST7vxoo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9yjcvrdm1uibekjf354m.PNG)](https://res.cloudinary.com/practicaldev/image/fetch/s--aST7vxoo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9yjcvrdm1uibekjf354m.PNG)
21+
22+
23+
*I used Cloud Skew to create the above diagram. Highly recommend you check it out (It's FREE).*
24+
25+
* **Azure DevOps & GitHub** are great, easy to use SaaS products - GitHub and Azure Pipelines will help you to achieve your source control and CI/CD needs. The source code is in a Git repository in GitHub (your application, infrastructure, and pipeline code), and your CI/CD pipeline is an Azure YAML Pipeline.
26+
27+
* **Azure Container Registry (ACR)** is an Azure-native container registry, much like Docker Hub but it’s Azure’s container registry solution, so it integrates with other Azure resources and uses Azure Active Directory for added security. The Azure Pipeline in this demo is building and pushing the Docker image to the ACR (a new version of the image is created on every successful run of the pipeline execution).
28+
29+
* **Azure Kubernetes Service (AKS)** is a serverless, managed container orchestration service. AKS runs directly on Azure as a PaaS service and provides you with a Kubernetes environment to deploy and manage your containerized Docker application. This managed Kubernetes environment is what runs your Kubernetes resources in this demo.
30+
31+
* **Azure Active Directory** is the built-in Azure identity management solution. In this demo, it is important for you because you need a Service Principal (an identity based on an Azure AD App Registration). This Service Principal is used to create a secure, identity-based authenticated connection (a Service Connection to the Azure Resource Manager) so you can deploy the resources with the correct permissions to the correct Azure Subscription.
32+
33+
34+
## Initial Setup
35+
36+
* Add the Azure DevOps extension to your cloud shell session:
37+
38+
39+
```bash
40+
az extension add --name azure-devops
41+
```
42+
43+
* Add context for your shell to reference your DevOps organization:
44+
45+
46+
```bash
47+
az devops configure --defaults organization=https://dev.azure.com/insertorgnamehere/
48+
```
49+
50+
* Set the **AZURE\_DEVOPS\_EXT\_PAT** environment variable at the process level. Now run any command without having to sign in explicitly:
51+
52+
53+
```bash
54+
export AZURE_DEVOPS_EXT_PAT=insertyourpattokenhere
55+
```
56+
57+
* Create a new Azure DevOps project:
58+
59+
60+
```bash
61+
az devops project create --name k8s-project
62+
```
63+
64+
* Set the default project to work with:
65+
66+
67+
```bash
68+
az devops configure --defaults project=k8s-project
69+
```
70+
71+
## Deploying the Infrastructure
72+
73+
* Create a resource group to logically organize the Azure resources you will be creating:
74+
75+
76+
```bash
77+
az group create --location westeurope --resource-group my-aks-rg
78+
```
79+
80+
* Create a service principal. Your AKS cluster will use this service principal to access the Azure Container Registry and pull container images.
81+
82+
83+
**IMPORTANT: Copy the output of the following command, you will need it later:**
84+
85+
```bash
86+
az ad sp create-for-rbac --skip-assignment
87+
```
88+
89+
* Create an AKS cluster to deploy your app into (this is where you use the output from the previous command)
90+
91+
92+
**IMPORTANT: Sometimes you will get an error like "400 Client Error: Bad Request for url" - It is a known issue & re-running the command again usually works:**
93+
94+
### [az role assignment create fails in Cloud Shell: 400 Client Error: Bad Request for url: http://localhost:50342/oauth2/token #9345](https://github.com/Azure/azure-cli/issues/9345)
95+
96+
```bash
97+
az aks create -g my-aks-rg -n myakscluster -c 1 --generate-ssh-keys --service-principal "insertappidhere" --client-secret "insertpasswordhere"
98+
```
99+
100+
* Create an Azure Container Registry (ACR). This will be the repository for our containers used in AKS:
101+
102+
103+
```bash
104+
az acr create -g my-aks-rg -n insertuniqueacrnamehere --sku Basic --admin-enabled true
105+
```
106+
107+
* To allow AKS to pull images from ACR, we must set our Azure RBAC permissions for the service principal:
108+
109+
110+
```bash
111+
ACR_ID=$(az acr show --name ghostauacr --resource-group my-aks-rg --query "id" --output tsv)
112+
113+
CLIENT_ID=$(az aks show -g my-aks-rg -n myakscluster --query "servicePrincipalProfile.clientId" --output tsv)
114+
115+
az role assignment create --assignee $CLIENT_ID --role acrpull --scope $ACR_ID
116+
```
117+
118+
## Deploying the Application
119+
120+
* Fork this GitHub repo (open this link in a new tab and click "fork"):
121+
122+
### [ghostinthewires](https://github.com/ghostinthewires) / [**k8s-application**](https://github.com/ghostinthewires/k8s-application)
123+
124+
* Now clone it to the terminal session within cloud shell:
125+
126+
127+
```bash
128+
git clone https://github.com/<your-github-username-goes-here>/k8s-application.git
129+
130+
cd k8s-application
131+
```
132+
133+
* Create a pipeline in Azure DevOps:
134+
135+
136+
```bash
137+
az pipelines create --name "k8s-application-pipeline"
138+
```
139+
140+
* Follow the prompts in your terminal to set up the pipeline:
141+
142+
143+
1. Enter your GitHub username; press enter
144+
145+
2. Enter your GitHub password; press enter
146+
147+
3. Confirm by entering your GitHub password again; press enter
148+
149+
4. (If Enabled) Enter your two factor authentication code
150+
151+
5. Enter a service connection name (e.g. k8sapplicationpipeline); press enter
152+
153+
6. Choose \[3\] to deploy to Azure Kubernetes Service; press enter
154+
155+
7. Select the k8s cluster you just created; press enter
156+
157+
8. Choose \[2\] for the “default” Kubernetes namespace; press enter
158+
159+
9. Select the ACR you just created; press enter
160+
161+
10. Enter a value for image name (press enter to accept the default); press enter
162+
163+
11. Enter a value for the service port (press enter to accept the default); press enter
164+
165+
12. Enter a value for enable review app flow for pull requests (press enter without typing a value)
166+
167+
13. Choose \[1\] to continue with generated YAML; press enter
168+
169+
14. Choose \[1\] to commit directly to the master branch; press enter
170+
171+
172+
## **CONGRATULATIONS!**
173+
174+
**You have created an Azure DevOps Project! Wait a few minutes for the container to build, push to ACR, then deploy to AKS.**
175+
176+
* Access your AKS cluster by getting the kubeconfig credentials:
177+
178+
179+
```bash
180+
az aks get-credentials --resource-group my-aks-rg --name myakscluster
181+
```
182+
183+
* View the Kubernetes resources your project has created:
184+
185+
186+
```bash
187+
kubectl get all
188+
```
189+
190+
[![kubectl get all](https://res.cloudinary.com/practicaldev/image/fetch/s--kSIvGnYa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5ckrh8renuq0nerc3j88.PNG)](https://res.cloudinary.com/practicaldev/image/fetch/s--kSIvGnYa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5ckrh8renuq0nerc3j88.PNG)
191+
192+
* Copy the service IP address (under “External IP”) and paste into a new browser tab with ":8888" (e.g. 51.137.4.161:8888) on to the end.
193+
194+
195+
## **This should be your final result!**
196+
197+
[![Super k8s Demo](https://res.cloudinary.com/practicaldev/image/fetch/s--IlcXQ_4m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rak18btkdex17vlu4my7.PNG)](https://res.cloudinary.com/practicaldev/image/fetch/s--IlcXQ_4m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rak18btkdex17vlu4my7.PNG)
198+
199+
## Summary
200+
201+
In a relatively short period of time, you have created a new project in Azure DevOps. Within that project, you have set up a CI/CD pipeline. That pipeline built your application inside of a container, pushed that container to a container repository, and deployed the container to AKS. Finally allowing you to view your web application running in AKS from the web via a Kubernetes service. You are amazing, well done!
202+
203+
**IMPORTANT: Head back over to your forked repo and check out the file "azure-pipelines.yml". You should see the line "trigger: – master" which means every time we make a change to the master branch, a new build will kick off automatically. Magic!**
204+
205+
Now that you have a fully working application deployed to AKS, I bet you can't wait to dive in and see how it all works under the hood.
206+
207+
# Thank you
208+
Thank you for taking the time to work on this tutorial/labs. Let me know what you thought!
209+
210+
#### Author by [Harshhaa Reddy](https://github.com/NotHarshhaa)
211+
212+
### Ensure to follow me on GitHub. Please star/share this repository!

0 commit comments

Comments
 (0)