This project helps to do the below workflow:
-
Trigger pipeline in Jenkins on code push to github.
-
Build a docker image and push it to Azure container registry.
-
Deploy to azure AKS cluster.
-
Access to this Github repo with required files.
-
Azure account with an active subscription.
-
Jenkins setup with below-installed plugins, credentials, and tools.
-
Credentials: Add the below credentials in Jenkins credential manager.
- GitHub Personal access token (PAT) to trigger Jenkins pipeline on GitHub commit (only for private GitHub repos)
-
Plugins:
- GitHub Branch Source Plugin
- Kuberenetes CLI Plugin
- Docker Pipeline
- Azure credentials.
-
Tools: Install the below tools in the Jenkins machine by logging in as "jenkins" user. (Enter password if asked)
sudo su -s /bin/bash jenkins- docker
- helm
- kubectl
- Azure CLI
-
-
Create a new multibranch pipeline in Jenkins add the source as GitHub repo URL and set auto trigger for all branches. (Use GitHub PAT if repo is private)
-
Verify Jenkins is running or else start it. (Options: start, stop, restart)
sudo service jenkins startExpand if you are running Jenkins in a local port.
Install "ngrok" and run the below command with your Jenkins port to get the public URL for Jenkins
ngrok port 8080- Copy the ngrok URL from the terminal, open it in the browser, and log in.
- This will be our public Jenkins URL.
-
Use Jenkins URL in
- Set up Jenkins URL in GitHub repository webhook.
- Optional: Set up Jenkins URL in VScode for pipeline lint. (With Jenkins pipeline lint plugin)
-
Add Maven in Jenkins.
- Go to "<jenkins_base_url>/manage/configureTools" --> Maven installations.
- Add Maven with variable name "Maven3".
-
Create Azure resources and Azure service principal. Click here to create new.
-
Create Azure credentials in Jenkins. help
-
Update "Jenkinsfile" with created resources names.
-
Create helm chart for your project.(A working helm chart "aksdeploychart" is available, but below are the steps to create a new helm chart.)
-
At the root of your project run below command to create a helm chart.
helm create <you_chart_name> -
A new folder will be created with <you_chart_name>, delete files excluding the below files
│ aks-store-quickstart.yaml │ Dockerfile │ Jenkinsfile │ pom.xml │ README.md │ ├───<you_chart_name> │ │ .helmignore │ │ Chart.yaml │ │ values.yaml │ │ │ └───templates │ deployment.yaml │ service.yaml │ serviceaccount.yaml │ _helpers.tpl -
Update
<you_chart_name>/values.ymlwith below values.replicaCount: 1- Update docker image details.
image: repository: <your_repo_name>/<your_image_name> pullPolicy: Always tag: "latest" - Fill below fields.
nameOverride: "<name>" fullnameOverride: "<full_name>" - Update "name" in
"serviceAccount"section.name: "<service_account_name>" - Update below service parameters.
service: type: LoadBalancer port: 8085
-
-
Commit to GitHub or manually start the pipeline.
-
View the pipeline log. The below stages should be run successfully.
- Build and push images to Azure CR.
- Deploy to Kubernetes.
-
The app will be installed in the cluster and can be accessed using the External IP of the cluster.
-
In Jenkins machine
- Login as "jenkins" user.
sudo su -s /bin/bash jenkins - Get the external IP of the cluster.
kubectl get service store-front - Access the deployed app using the IP address.
- Login as "jenkins" user.
- Clean up resources in Azure by deleting the Azure resource group.
az group delete --name <resource_group_name> --yes --no-wait - Clear and log out from all resources.
-
Delete AKS credentials
rm -rf /var/lib/jenkins/.kube -
Remove kubectl contexts.
kubectl config unset current-context -
Delete Azure service principal.
az ad app delete --id <your-service-principal-app-id> -
Logout from Azure.
az logout -
(Optional) Stop ngrok terminal CNTRL+C.
-
Stop Jenkins service.
sudo service jenkins stop
-
-
AKS credentials for kubectl will be stored in /var/lib/jenkins/.kube/config
-
Azure List all logged accounts.
az account list -
kubectl commands for debug or info
- List nodes
kubectl get nodes - Get k8s contexts
kubectl config get-contexts - Get the list of namespaces
kubectl get namespace - Get deployments in all namespaces
kubectl get deployments --all-namespaces=true - Get deployments in specific namespace
kubectl get deployments --namespace <k8s-namespace-name>
- List nodes
Warning
Be cautious about creating resources in Azure and delete all resources after use.
Create an Azure resources using azure CLI in different machine.
- Run the below commands, follow the instructions, and log in to your Azure account.
az login --use-device-code - Create a Resource Group.
az group create --location centralus --name <aks_resource_group> - Create Azure Container Registry.
az acr create --resource-group <aks_resource_group> --name <acr_name> --sku Standard --location centralus - Create AKS cluster with two worker nodes and attach Azure container registry.
az aks create --resource-group <aks_resource_group> --name <aks_name> --node-count 2 --generate-ssh-keys --attach-acr <acr_name> - Create an Azure service principal using your subscription ID with a "Contributor" role in the created Azure resource group. Store the output somewhere safe.
az ad sp create-for-rbac --name <servicePrincipalName> --role Contributor --scopes /subscriptions/<subscriptionID>/resourceGroups/<aks_resource_group>
Create Azure credentials in Jenkins.
- Ensure Azure credentials plugin is installed in Jenkins.
- While creating select "Kind" as "Azure Service Principal".
- Fill in Azure service principal details.
Field Value Subscription ID Your Azure subscription ID Client ID appId Client Secred password Tenant ID tenant