Deploy Springboot App into AKS Cluster using Jenkins Pipeline | Deploy Springboot to Kubernetes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
are going to learn how to deploy a springboard Docker container into AKs cluster using Jenkins pipeline so let's quickly go over the prerequisites so we need to have spring boot app configured with a Docker file so I have already configured a springboard application in GitHub along with the docker file so we have already dockerized this okay and then we also need to have a Jenkins setup because Jenkins is going to check out the code build the docker image upload the docker image into ACR and then finally Jenkins will deploy into AKs cluster so I also have configured Jenkins as well I mean just a brand new Jenkins does not have anything installed we will go over what are the things we will be installing shortly okay and then Jenkins needs to have Docker installed on the necessary plugins okay and we will also see that in my website and then we need to have AKs cluster set up because that is where we are going to deploy our spring boot to Docker container and then we also need to uh needs to configure ACR as well that is where we will be uploading the docker image and then storing the docker image over there and then AKs needs to have full access in order to download the docker image from ACR so we will also provide that as well okay so that is part of the prerequisites and then as part of the demo um so this is how we are going to approach okay so we are going to set up AKs cluster using Azure CLI of course there are many ways you can set up a cluster I will also show you in a minute in my website and then we need to create credentials in Jenkins for connecting to ACR and then we will also create credentials in Jenkins for connecting to AKs cluster as well after a case cluster is setup we need to create a cube config entry so we will also do that and then we will create a Jenkins Pipeline and we will build the Jenkins Pipeline and then we will finally deploy the docker image into a case cluster and then finally we will access the application the springboot applications uh using the load balancer URL in the browser and then finally we will just clean up everything what we have created right so basically uh we will just pick up a springboot application that has been configured in GitHub we will build the docker image upload the docker image into ACR and then finally we will deploy the docker container springboard Docker container into AKs cluster so I do have wonderful diagram in my website so so this is exactly what we are going to do right so like explain so we have spring boot application configured in GitHub everything has been dockerized so we have Docker file and everything and then we are going to create a Jenkins pipeline that is going to check out code from here here build the docker image and then upload the docker image into ACR and then finally we will be deploying the docker container into AKs cluster using kubernetes CLI plugin right so we will we will install those plugin in Jenkins okay so this is exactly what we are going to do in our demo okay so if you look at the prerequisites yes we need to have AKs cluster uh configured so we will do that let me first go over the uh all the prerequisites here right so we need to have a Jenkins setup so that's my Jenkins right there so gen kids needs to have these plugins configured so we will also do that and then Jenkins also needs to have Docker installed and then Jenkins also needs to have proper permission um like you know you need to add a Jenkins user to the docker group so that Jenkins can build a Docker image so we will also do that and then Jenkins needs to have Cube City and installed because after you uh create the cluster we we want to query the cluster and all right uh so so that's why you know we need to have a cube CTL install um and then we need to configure ACR that is where we are going to upload the docker image uh in in Azure cloud and then our AKs needs to have pool access uh to download the docker image from ACR and then of course we already have a Docker file and then we also need to have a kubernetes manifest file I also have that you can click on here as well not actually this one I have I have created this particular one for this particular demo right so this particular kubernetes manifest file so we will be using it and then uh we also need to have Azure CLI install because using Azure CLI will be creating AKs cluster okay so all this thing you know we need to have all right so why don't we kind of uh you know take care of one by one okay so let me first go to Jenkins so this is my Jenkins uh like I said this is a brand new Jenkins I just configured it I did not install anything in it so let's go and install one by one so Jenkins needs to have Docker Docker pipeline plugin and then Cube CTL CLI plugin so let's go ahead and then install these plugin so go to manage plugins let's go to manage plugins available tab let's search for docker so we are going to install Docker pipeline plugin Docker plugin and then let's also search kubernetes so we also need to install kubernetes CLI plugin as well so three plugins and then click on install without restart so while it is going on let's see further here so we also needs to install Docker in Jenkins so you can actually click on this link so this will take you how to install a Docker so I'm already connected to my ec2 instance over here so let me see if I have a Docker installed as you can see here so this is a Jenkins but uh I don't think I have Docker installed there you go Docker is not installed so let's quickly install Docker as well you can perform all these steps over here under let's install docker and we will add Ubuntu user to the docker group perfect and then you don't have to do this this particular step so you are kind of you can perform Docker commands without pseudo user so that's what that one is you don't have to but that's fine and then let's go ahead and then restart docker perfect so everything is looking good so we did install uh Docker in Jenkins ec2 instance and then let's go ahead and then add few more steps because we will be performing uh Jenkins bill right so so Jenkins user needs to be added to the docker group so that is what that particular step is and then let's restart Jenkins as well and then we will uh reload the system files and then we will stop Docker and then restart docker perfect okay so I think I think we are good with uh Jenkins so Jenkins has all the plugins and then Jenkins also have a Docker install and then it also has proper permission as well perfect all right very good I think I think we are good uh and then we also need to install Cube CTL as well on Jenkins so let's also go ahead and then take care of that because Jenkins will use this particular kubernetes right keep CTL in order to deploy into eks cluster so let's also go ahead and then take care of this as well and then install Cube CTL so cubesital is a command line utility for accessing kubernetes cluster perfect there you go so so we were able to install Perfect very good so let's go back to our instruction here let me maximize this one so so we took care of it now I think we need to go ahead and then create a AKs cluster so if you look at here AKs cluster can be created many different ways okay you can you you can you can directly go into Azure portal and then create a cluster or you can also create using CLI or you can also create a case cluster using terraform but I'm going to choose one of the simplest option to use Azure CLI okay so so basically I'm going to use my my MacBook okay so I'm going to open up item um so I know I'm connected to Jenkins I don't want to use this one let me go to my um you know local machine so I opened up a new window here let me minimize Jenkins okay so there you go so what I'm going to do I'm going to uh create a cluster but make sure you need to install Azure CLI so you can click on this one I have already installed Azure CLI okay you see here Azure CLI is obviously very simple command so you can also follow the instruction over here right I have provided instruction on how to install Azure CLI on Mac OS or if you scroll down I also have provided instruction on how to install on Ubuntu you can also install out of Windows machine as well it's not that difficult uh you can just Google it and then download Azure CLI on your Windows machine as well okay perfect so we are going to use Azure CLI to to set up AKs cluster okay all right so let's let's get started here right so basically uh we need to First login into Azure so how do you do that it's just very simple command just type AZ login all you have to do just select your credentials right select your Microsoft credential so this is my Microsoft credentials so once you select it it will login into Microsoft Azure right so perfect so now I'm logged in into Microsoft cloud perfect now you can close this one no problem so once you're able to log into Microsoft cloud and then we can go ahead and then create a resource Group right so so let's go out and then create a resource Group so before we create the cluster we need to go ahead and then create a resource Group uh so let me go ahead and then perform these commands Okay so I'm going to move to my right let's go ahead and then create a resource Group so this is my Resource Group name and I would like to use this location okay depending on wherever you are you know you can choose the location okay and then let's go ahead and then create AKs cluster with the two worker node okay so so basically I want to create the cluster this is the cluster name I would like to create a cluster under my Resource Group which I just created above and then I would like to have two worker node so that's what that is okay and then I would like to enable monitoring and then generate SSH Keys okay so let's go ahead and then execute these commands Okay perfect so right now it is in the process of creating the AKs cluster so this might take uh four to five minutes depending on your internet speed so I'm going to pause the video and then come back okay so as you can see here cluster have been created perfect now let's try to display the details of the cluster so this is the command we are going to use AKs show okay there you go so we are able to see the cluster information here perfect all right now let's also go ahead and then create Azure container registry so I'm also going to use Azure CLI command for that so so if you see here again we are going to use same Resource Group and then this is the name of the Azure container registry okay and skew is standard and then the location is the same location okay so make sure this is unique if this is not unique then it will complain but let's let's just copy this let's see oh okay so there you go you see here guys so this is already in use so we cannot use this so we have to make this unique so what I'm going to do let's see uh I'm going to say something like three two one zero there you go okay so so this is created and then we also need to connect to the cluster so let me copy this command so what this will do this will update your Q config so let's go ahead and then execute that there you go you see it now so this was able to update the cube config let me do one thing uh I think my Cube config can be really lengthy there you go let's see this is really long so what I'm going to do let me remove my Cube config and then I will recreate that again just give me one minute okay there you go so as you can see here cubeconfig have been created so now let's quickly go and view that there you go so that's our cubeconfig entry perfect okay so so we can query the kubernetes cluster using this cubeconfig file okay now if you like to see how many nodes are there so we can just query the cluster so I'm going to say Cube CTL get notes there you go you see here right so we were able to create a AKs cluster with two worker node so so you can see that you know both are up and running and then if you want to see if there are any parts running obviously we did not deploy anything so there will not be anything over there okay perfect very good right so we were able to uh create a cluster we were able to create Azure container registry as well um and then there is one step we have to do so so we have to provide a pull access for AKs in order to download the docker image okay so we need to do that so how do you do that so we are going to execute this command and then we are going to attach ACR okay so if you remember um our ACR this is not ACR name so we given three to one zero so let me do one thing let me go ahead and copy this command and then that should be three two one zero okay Enter okay so now let's go into the Azure portal and let's verify everything has been created so I'm logging into Azure portal so this was the resource Group what we have created my Resource Group and as you can see here uh we were able to see the cluster and then we are also able to see the ACR as well right so that's the ACR and then we can also see our cluster here as well so that's our cluster okay perfect so let us go back to our instructions here so we took care of creating the cluster we took care of creating ACR we also gave permission for a pull access as well I think we have pretty much everything over here um now I think we can go ahead and then uh you know go into Jenkins and then start our work okay so if you look at here um I have so if you look at here I have provided my code here so all you have to do is uh don't directly check out my code when you build a Jenkins pipeline uh you can fourth my repository you can Fork my repository and then you can make changes um you know in the deployment configuration file like this one kubernetes deployment file and then you can you can actually download the image right so and I will I will explain you know when I do that step okay all right so let's start our uh Jenkins pipeline work right okay but before that uh what we have to do we need to create credentials uh from Jenkins in order to connect to ACR right so we need to establish the credential so for that let's go into um Resource Group go to ACR click on access keys so there you go you all see here so this is the information right so we have to enable admin user so when you enable admin user now it is going to show you what is the username and then what is the password so basically if you see in this diagram right your Jenkins pipeline needs to have access to ACR in order to upload the docker image so we need to create credentials uh in in Jenkins in order to establish that so let's go to manage Jenkins manage credentials let's add a entry add credentials so we are going to connect to ACR so I'm going to say so here it is asking for username and password so you're going to go with username and password so let's go to ACR so this is a username copy that let's add let's paste there and then password so that is the password you can use one of the password and then in the description I'm sorry in the ID I'm going to say ACR in the description I'm going to say ACR credentials okay and then let's create it so we were able to create a credential okay perfect so we are able to see that over here perfect and then let's go into our instructions now we are going to go ahead and then add another entry for Jenkins to connect to AKs cluster using cubeconfig right so if you remember we were able to query the cluster from here okay so we need to get that Cube config information and then we need to create credentials in Jenkins in order to connect to it okay so let's go ahead and then do that okay so I'm going to open up Sublime Text here and then let me create a new file I'm going to save a cube config information over here so make sure you don't have any type here or anything like that and then let's save this so I'm going to save this as cubeconfig November 20 22 okay save and then I'm going to go into Jenkins we're going to add a new credentials and we are going to choose secret file and then choose file we are going to upload our Cube config we are going to select it and then in the ID I'm going to save this as k8s and then I'm going to say cubeconfig or keep configuration whatever it is right and then create it perfect so we have one to connect to ACR and then we have one more to connect to our kubernetes cluster perfect very good now let's go back to the instruction now I think we can start creating a Jenkins pipeline in order to you know check out the code build the docker image and then upload the docker image into ACR and then finally deploy into kubernetes cluster right so I think now we can start our pipeline okay all right so let me go into Jenkins click on new item so let's create a new pipeline so I'm going to say my kubernetes pipeline choose pipeline say okay go to pipeline section so I'm going to go with the hello world and then I'm going to go ahead and then modify this pipeline let me maximize the font here so you all can see it okay so let me check out so we are going to check out code from my repository I made sure this is a public repository so you don't have to use uh credentials to check out but like I said you all would be four Kingman Repository and then checking out from your own repository right so let me check out code and then what is a branch name Branch name is Main let me also modify that generate pipeline script copy that and then paste that over here let me just build now just want to make sure that pipeline is working fine so we were able to check out code perfect everything is good and then let's go to the next stage so we were able to check out the pipeline and if you actually go to Docker file okay so if you look at here we need to build this springboard jar file right so we need to build a springboard jar file so how do you do that so we are going to use May 1 to do that so let me add another stage here steps we are going to use mayweb right clean install but we need to tell our pipeline where Maven is configured three okay okay very good so we were able to uh build the build the jar file very good so we should be able to see all the way down here right we're able to build the jar file perfect so the next stage would be to build the docker image so I'm going to move on to the next stage you want to say build image and steps block and we are going to use script block so we need to upload our Docker image right so build a Docker image and upload so what I'm going to do I'm going to declare a section here I'm going to declare environment section and then we are going to uh mention what is our registry information so if you see here what is our registry name so let me paste that over here so if you remember our registry is nothing but is this one that's our registry my ACR report three two one zero right or you can also copy that over here perfect and then what is our registry credentials so our registry credentials is ACR right that's a remember we created a credentials under management so that is what it is perfect and then um what is our registry URL right so our registry URL is nothing but I will I will show you that is nothing but this one login server perfect okay that's pretty much over there now you can say Docker dot build and then you just have to say registry name right foreign so this should take care of building the docker image and then we also want to upload the docker image as well right so for that you can also add this particular command as well in order to upload the docker image but let's let's go step by step okay I just want to make sure that we were able to build the docker image successfully so click on build now okay very good so we're able to build the docker image and then attack the docker image as well perfect so now we can go ahead and then add the next stage which is nothing but pushing the docker image into ECR not ECS or ACR steps going to say push image okay so let's go ahead and then copy the code so if you see here we are going to use this particular block of code and then our registry URL is nothing but that will be picked up from here okay and then let me also declare that variable called Docker image perfect so that should take care of pushing the image using this registry credential that is nothing but ACR okay all right let's go ahead and then click on build now okay as you can see here we were able to login into ACR and then oh some issue here okay let's see Docker image hmm oh yeah we did not store this value okay so I made that mistake so we are going to say this okay perfect let me click on build now again okay perfect so we were able to successfully push the docker image into ACR so how do you verify that so let's go into repositories there you go you all see here we were able to upload the docker image into ACR very good right and this is how you can download the docker image from the ACR perfect so everything is looking good so if you go back to this diagram let's kind of revisit right so basically we created a Jenkins pipeline to check out code from GitHub and we also build the springboard jar file using Maven and then we also built the docker image and then we uploaded the docker image into ACR so that's where we kind of stopped and then we just have to do one final step which is nothing but deploy the docker image into uh AKs cluster so for that we need to have a kubernetes manifest file so we are going to leverage we are going to use this particular kubernetes manifest file okay and we have to make sure that we have updated all our entries properly so this is the most important line here right so if you see here line number 17 so we have to provide all all the information about our image okay so if you see here let's see if you see here so this is the image information right can you all see that so we have to make sure that we have that properly specified in our in our deployment manifest file so I want to make sure that I have it I think I have it there you go see so that's what important okay very good so basically this is the kubernetes deployment manifest file we are going to use this for deploying this Docker image which has been stored in ACR into AKs cluster okay and then the way we are going to expose the application is using a load balancer okay so that is part of the service okay perfect so let's go into uh our Jenkins so the way we are going to deploy from Jenkins into AKs clusteries using kubernetes CLI plugin so if you remember we already installed that plugin right so what we are going to do we are going to write some piece of code okay so I'll tell you how to take that sample code using that plugin so I'm going to say deploy to kubernetes steps block and then let's Leverage pipeline syntax and in the drop down we are going to choose width if you remember we have you know this is the plugin we installed right so let's select that one and then remember we also added cubeconfig entry so select that one and you don't have to select anything else just click on generate pipeline script so this will give us a sample pipeline script along with this is what important right the credential that has been already updated let's copy this and we are going to use this block of code and then we are going to add one line here we are going to add search I'm going to use Cube CTL apply minus F if you remember which file we have modified here so we have modified this file so we are going to use this particular file for deploying into a case cluster so let's refer that one okay so make sure you for my Repository okay because you cannot use mine so you must Fork from my repository and then you know you check out from your own repository and then you refer this file perfect I think everything is good okay apply save let's go ahead and then click on Bill now so that is our final step let's see all right so here guys so finally we were able to deploy into kubernetes cluster using Jenkins pipeline so let's go ahead and then verify our deployments so let's go into my local machine easy here I'm not in Jenkins this is my local machine this is where we built the a case cluster using Azure CLI Command right so let's check so this is our node and then if I go ahead and then say parts so we should be able to see the parts did you all see that it has been running for last 32 seconds so why it created the three different parts let's check our deployment manifest file look at that so I have a replicas equal to 3 right so we were able to create three different parts now how are we going to access our application so we are going to check the service so let's get the services there we go you all see that so like I said we are going to expose our spring boot application which we deployed into kubernetes cluster as a load balancer and this is a load balancer URL so let's copy this URL and then let's go to the browser then enter wow look at that and and the port number is 80 so you don't have to specify any port number there you go guys so that's our uh you know spring boot application deployed inside kubernetes cluster so now let's go ahead and then enter something look at that right so yeah so that's how guys you know that's how you can configure your Jenkins pipeline in order to deploy into a kubernetes cluster okay and then one last step so now we were able to set up everything and then we also need to know how to clean them up correct that is also important because if you just leave everything up and running so just imagine right how many resources we are talking about we are talking about uh two worker node you're talking about your ACR you're talking about the whole cluster so so Azure will start charging you okay so the good practice would be to clean them up so how do you clean them up I have provided a single command so just like how we have created a cluster using Azure CLI command same way you can also go ahead and then this particular command so which will clean up everything what you have created under that Resource Group okay so this is also good practice so once you're done with the lab exercise make sure you know you also do this as well and this will take a few seconds to clean up the cluster and everything all right so that's it guys you know thank you for watching this video
Info
Channel: DevOps Coach
Views: 8,774
Rating: undefined out of 5
Keywords: SpringBoot App Deployment to AKS Cluster
Id: Tx_CFz4_6Bg
Channel Id: undefined
Length: 38min 57sec (2337 seconds)
Published: Mon Nov 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.