…based on Spring Boot, Docker, Jib, Azure, Jenkins, GitLab, Docker Hub and Kubernetes
Hi. The purpose of this article is a bit gnarly. It is supposed to show off a microservice architecture based on some status quo tools and frameworks and hopefully teach you how to build one yourself in the process. I will provide also several skeleton microservice projects (in GitLab), which will enable everything necessary for the whole process to be functional. The idea is to describe how a production setup could look like. I will also describe shortly the development process which would be associated with such an architecture as well as the software release process. All of the accounts and tools I’ll be using can be obtained “””free of charge””” for a certain period of time, which should be enough for you to grab the basic concepts and to test a little bit around. Hopefully, in the end you will be able to persuade your next big customer to hire you as a microservice expert. I’ll fast forward through all the configuration steps which need to be done for you to be able to deploy a template service via Jenkins to a Kubernetes cluster hosted in Azure. In the second part of this article I will elaborate on some of the decisions made in the design of this generic architecture. Keep in mind that the whole setup is not new or overly brilliant. Also a good architecture is always tailored to the domain it is supposed to serve. So see this only as an example of what you could encounter in a middle sized software company.
Create a Free Azure Account
Go to: https://azure.microsoft.com/en-us/free/
Just be aware that this is a “12 months of free services” subscription which will be pretty expensive after that trial period. Make sure to delete all instances you’ve created, if you don’t want to continue using Azure.
Run a Jenkins Instance as described here:
https://docs.microsoft.com/en-us/azure/jenkins/install-jenkins-solution-template
Fork the Template Service
https://gitlab.com/vikstoit/template-service/-/forks/new
Create a new Jenkins Pipeline
Create a new pipeline and name it “template-service”
In the section “Pipeline Script” add the code from:
https://gitlab.com/vikstoit/template-service/blob/master/jenkins/Jenkins_gitlab_token_workaround
Create Gitlab Access Token
… as described here https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html
Create a Docker Hub Registry
.. as described here https://docs.docker.com/docker-hub/repos/ and name it tempate-service
Create Jenkins Credentials
1. Gitlab Access Token:
2. MAVEN_SETTINGS secret file: https://gitlab.com/vikstoit/template-service/blob/master/mvn_settings/README
3. REGISTRY_SECRET:
Create the registry (Docker Hub) secret to be used by the Kubernetes cluster later:
Spin up a Kubernetes Cluster on Azure
.. as described here: https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough
Createa a resource group using the online shell (https://shell.azure.com/):
az group create --name templateResourceGroup --location westeurope
You should get the output:
{
"id": "/subscriptions/8f0db926-01fe-413d-bd17-9c011cfda692/resourceGroups/templateResourceGroup",
"location": "westeurope",
"managedBy": null,
"name": "templateResourceGroup",
"properties": {
"provisioningState": "Succeeded"
},
"tags": null,
"type": "Microsoft.Resources/resourceGroups"
}
Then create the actual cluster:
az aks create --resource-group templateResourceGroup --name templateAKSCluster --node-count 1 --enable-addons monitoring --generate-ssh-keys
Output:
SSH key files '/home/viktor/.ssh/id_rsa' and '/home/viktor/.ssh/id_rsa.pub' have been generated under ~/.ssh to allow
SSH access to the VM. If usingmachines without permanent storage like Azure Cloud Shell without an attached file
share, back up your keys to a safe location
This will take a while, so if you want go grab a coffee and check the status later by running:
az aks show -g templateResourceGroup -n templateAKSCluster -o table
Output:
Name Location ResourceGroup KubernetesVersion ProvisioningState Fqdn
-------------------------------------------------------------------------------------------------------
templateAKSCluster westeurope templateResourceGroup 1.14.8 Creating templateak-templateresource-8f0db9-60cb8a62.hcp.westeurope.azmk8s.io
Get the cluster credentials
You will need the credentials so that you can connect to your kubernetes cluster from jenkins and your command line.
Execute the following command to get the credentials.
az aks get-credentials --resource-group templateResourceGroup --name templateAKSCluster --admin
Then download the config file (/home/YOUR_USERNAME/.kube/config) using the download button:
Then go to Jenkins -> Credential -> System -> Global Credentials. Chose “Kind: Secret file” and set the ID to “kubeconfig”. Use the “Choose” button to the select the file you just downloaded to your local disk.
Install kubectl on the jenkins master
Now ssh into your Jenkins master by running:
ssh YOUR_JENKINS_USERNAME@YOUR_JENKINS_DNS_NAME
and execute the following commands as described here https://kubernetes.io/docs/tasks/tools/install-kubectl/:
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version --client
The output after the last command should be something like this:
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.2", GitCommit:"59603c6e503c87169aea6106f57b9f242f64df89", GitTreeState:"clean", BuildDate:"2020-01-18T23:30:10Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"linux/amd64"}
Run your Jenkins Pipeline
This will deploy the latest code as a docker image to Docker Hub and roll it out in your Kubernetes cluster.
Check the availability of your service
Go to the online azure shell and enter
kubectl get service -l app=nginx-ingress --namespace ingress-basic
This will display the External IP of your ingress.
To access your service go to http://YOUR_INGRESS_EXTERNAL_IP/template-service/template.
To be continued…