CICD Pipeline To Deploy To Kubernetes Cluster Using Jenkins | Jenkins Kubernetes Integration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome back to my channel and uh today we are back with another exciting tutorial on devops so as you can see on the screen uh this will be an exciting tutorial where we'll be using jen games docker and kubernetes so and we are going to see a complete step-by-step process how you can integrate jenkins uh kubernetes for our continuous integration and deployment of our application so we will see like uh you know how our application can be deployed to a kubernetes cluster using our ci cd pipeline so we'll see the complete process of that so that you know we can have our application in our github and it will be pushed to our kubernetes cluster and our application deployment and services will be running on the cluster and we should be able to access our application from internet so before i get into the actual process let me just give you a quick highlight on what we are going to do in this tutorial [Music] so [Music] [Music] let's talk about the whole cicd pipeline for our kubernetes deployment using jenkins so let me show you the pictorial representation first then we will talk about each steps what we are going to do so first let's consider you know you're a developer or you are the owner of your application you you're using github or gitlab or any other repository where you'll be saving your uh code so you're going to commit your code to your git repository okay and in our case we are going to use node.js and applications so if you have a different application you can use that as well the concept is same but you'll have to make some modification on the pipeline script so we have a jenkins which will act as our automation or integration uh tool so what we will do is like jenkins will use the code from github where we'll be saving our old jenkins pipeline file or you know or even your deployment files service files and everything will be saved under github your docker file i'll show you all those things so before i know so let me show you how this uh workflow happens so once you have your code uh get you know one of the stage what we will define in the jenkins would be like you can test your application so first you will test your notice application whether the application is fine using your npm tests or you know whatever the defined way in your application so i will show you like in my case you know the application i have defined like a testers.js file which will give the output okay and uh the next step would be like once you have your application uh completely fine then you will do the docker build so we will build your uh no application as a docker image and you know this has to be done on a machine where you have docker installed so you can use any jenkins slave where the docker is installed okay so once the docker image is built so you have our docker uh image available for application then we will push the application into a repository which is our docker repository which is the docker hub so the application uh image will be available in the docker hub so once the docker hub you know then we can deploy our application so for our deployment we are going to use a kubernetes cluster which have multiple nodes so in kubernetes cluster we will pull the image from docker hub and we will create our deployment and services in the kubernetes cluster so our application will be running on the nodes so then we should be able to access from internet so all this process will be done uh using a jenkins pipeline file so where we'll be writing our all whole codes in different stages so our application will be deployed by kubernetes by just one click okay so let's see what are the steps which we are going to do in the whole process so in the whole integration with kubernetes with jenkins what we will do is like first we will create a jenkins instance as a docker container so if you already have it no issues so since some of you may not be aware too easily how you can easily create a jenkins i'm going to use a docker container for jenkins and you know in order to create a docker bills we need to have a slave machine where we have a docker install so we will see how we can make the same machine on which you are running the jenkins as a container we will make some configuration on when we create in the jenkins instant itself so it will uh you know you can run uh the commands on the jenkins master itself you don't have to create a separate slave okay so another step should be like you have to install docker pipeline plug-in because we know in our jenkins yaml file we'll be using some specific uh characters or attributes which will work only if you have a docker pipeline plug-in and we will also install kubernetes continuous plugin there are different ways you can integrate jenkins and kubernetes in our case we are going to use kubernetes continuous plugin and now we will show you how to do that and also in order to configure this kubernetes plugin we need some configuration from our kubernetes master so we'll be using a config file for uh config file from our kubernetes master so i will tell you from where you can get that because there is no clear picture on how to do this all these things in any other tutorial so that's why i'm creating this tutorial which i'm sure definitely will help you so and then once we have all this configuration set up we will write our jenkins pipeline file and uh we will the jenkins pipeline file uh we'll do the docker build so that the image is created then it will push the image to docker registry uh which is the docker hubs then the on the next stage would be like pulling the image from docker registry and deploying to your kubernetes cluster in order to do all these process some of uh things which we have not discussed because we are not going to create a kubernetes cluster or anything so you need to have some free request to do all these steps the first step is like you need to have an application to be deployed in kubernetes so in our case we are going to use a node.js application so you should have a node.js or any other application which is suitable for our pipeline or you'll have to edit your pipeline so you should have your application in github so if you don't have it please check my previous tutorials where you can use those github link or you can use the github link i'm linking into this video description then you should know how to write a docker file so this all things are covered in previous tutorial so we'll also use make use of a docker file where it will tell you how to uh you know build or you can how to build your image so how to build your node.js application there's a structure how you can write the docker file and you also need to have a kubernetes cluster if you don't know how to set up a kubernetes cluster again i will tell you check on my tutorials i have a clear tutorial how to how you can set up a kubernetes cluster or virtual machine itself you don't have to even use cloud then you know you know you need to have a kubernetes deployment and service manifest file or a ml file because we have to use those files to deploy our application into the kubernetes cluster so all these things i have covered in my previous tutorial so you can also check my github link where you'll be getting all these details okay so before i get to the actual demo process i would request you like if you are new to my channel or if you have not subscribed to my channel click on the subscribe button and also like my video share and comment okay so let's get started with our actual uh steps so we'll be using our one of this machine which is having docker installed so in this machine i'm going to create a instance for jenkins so that we can use that instance for our uh jengits pipeline script so the simple uh command is like you know you can run docker run and detach more and you can map the ports and you can name the container and you can run it so this will create a you know jenkins instance uh without any volumes and all those things but the problem here it is you cannot use the same machine which you have this uh jenkins instance running for running our docker pill because this instance is running on this machine and it's act as a that container will act as a jenkins master right so when you run a pipeline script to look for a slave or machine which how docker available to run some of the commands which you use the images or you know docker commands so in this case so i'm just going to edit this uh whole script to make it you know easy so that we can use the same machine as the jenkins monster or when we run some uh commands or pipeline script which will use the same machine to run the docker commands for that i know i'll be running this command where you can see i'm using the quran and i'm defining the user and it should be running on the privileged mode okay so the knowledge we can uh make use of this uh whole process which i told and i'm going to give a name for this container and it's uh you know uh in the detached mode and it's also the map the port uh 5000 50 000 and 480 and this is also important because we are going to map the docker from uh local uh things to the container okay so that docker socks should be mapped and user bin directory and i'm also mapping the home as a volume so that you know we can have our whole contents in this volume so if you run this so you can see our docker container is created okay so now if i run a docker ps the container is run so let me use docker logs iphone f and uh this container id so it's we will get the locks now so it's starting the process because it's still the container is not completed so that jenkins instance is not up so why we are running this because we need to wait for this to be completed so you can see there is an id which is created which is the uh initial login password okay if you can see this is found this is the initial login password which we need to use at the time of login to the jenkins instance so uh if you access the you know ip address of your machine and 480 you can see now jenkins is starting up so we need to wait for it to be loaded so it will uh complete the process and now we can see it's asking to the initial admin password so that's the to unlock uh the jenkins instance you need to write that so for that you just copy this initial password from that screen and you can paste it here you can click on continue so that's the reason i run uh the docker log iphone app okay and you can click on install plugin so we can exit from the screen so we need to wait for the installations to be completed this may take some time for the whole plugins to be installed so i'll skip this screen so the plugins installation is completed then it will ask you to create your first admin account so you can give a name and you can set a password for it and confirm the password and i give a full name and also the email address so once you click on save and continue it will give you the url to access your jenkins which we are already here and save and finish so once you click on start using jenkins we are jenkins is up so we are inside the jenkins now and we need to do the next steps so the next step is like we need to create or you need to install our plugin so you need to go to manage jenkins and if you go to manage plugins you have the plugins in the update and also available and installed you need to go to available and you need to search for docker pipeline so you need to install this plugin because this is used by in our jenkins uh pipeline file so some of the commands will not work if you don't have the pipeline so you can just uh select and install without restart so we need to wait for the plugin to be installed so you can see the plugin installation is completed so you can go back and we need to go back to the manage plugin again and we need to search for kubernetes continuous deploy plugin now the problem here is if you search here you can see the version is 2.3.1 so the installation will work fine but unfortunately our pipeline will fail because what i see is in as per the plugin not github or the jenkins uh page itself it has mentioned like you know the plug-in version is getting failed so what you need to do is you need to go to advanced okay and uh [Music] you need to select upload plug-in so you need to select choose i have already downloaded a plugin which is uh this version i will give you the link for this it's a version 1.0 so it's the kubernetes same plugin but is a lower version so i will link that into this video description so you can go there and you can download the plugin directly from that link and you can select and you can upload it so you just wait for that to be completed right so the plugin installation is completed so now just go back now if you check your plugins in the available you should be able to see the kubernetes uh plugin and not not on the available on the install you should be able to see the kubernetes plugin now so you can see now the kubernetes deploy plugin is there but the version is 1.0 right and you should also have the docker pipeline plugin you can see the docker pipeline plugin as well so what we're doing is we are doing one of the way to do this uh whole process okay so now let me go back to the application first okay so this is the application which we are going to use which is a node application and you know you have our index.js where we get you know when we go to the home page it will tell you like hello from uh response colon or hello from tutorial and also if you go to slash wheel it will tell you this slash ready and it's running on report uh 3000 and you also see like you have a docker file and you know that docker file is you know using this process so that when you run the docker build command it will build your application and you also have a test folder where you can have a test.js so we are not going to use that testing for this uh whole process so we'll skip that but you can add that you know based on your application if you want to add some stage for testing and we have a deployment and service file but i have complained both into a single file called the deployment service dot yaml so once you run it it will create the deployment and as well as the service so we already discussed about how to create your deployment and service and all those things the previous tutorial so i'm not going to run all those things in this section now if you see like we have defined the image here as my docker hub user id and the app name and also the latest stack so we'll be also pushing our image in this uh details because otherwise this image is not available then we should be not able to run this deployment and the service so when we create our jenkins pipeline we'll be defining what uh the image name which has to be pushed at the docker hub so we should uh make use of the same image name what uh we are defining here okay and you can also see like the service we are using uh type load balancer and the port another node port which we are mapping is three one one zero which is mapped to the for three thousand of your application so that's the overall uh you know uh thing what we are going to use now let me talk about uh the pipeline file so this is the pipeline file which we are going to write it in the guitar jenkins pipeline section okay so there are two ways you can create this as a jenkins file on the docker uh or github itself the file in a location where your application is there or you can manually write it to your uh jenkins pipeline section so for easier purpose we just copy and paste it into the pipeline section in this tutorial okay so what we are going to do is as i said we are going to set up an environment variable as dr image name it can be any name you want to give and i know this is my docker hub login and this should be the name of the repository which we are going to create okay so starting with docker image i'm going to give a blank and you know we are setting agents as any so it can be any slave or any slaves which can run okay so we are not defining any specific slave now if you see the first stage is to check out our github so you can see like the step is like git here you know since i don't use any credentials because it's a public project i'm using this so if you don't know how to create all this scheme you have a jenkins pipeline you know syntax editor as well where you can do that so i i'll show you that as well how you can create that if you go to your jenkins and if you click on new item you can select the pipeline and just give a name for your uh you know pipeline file or job you just select the job name as test and you select the pipeline and then just click on ok so our job is ready now so now what we have options is like you have uh general options we have built your triggers advanced option and the pipeline section right so this is the word we are going to write the pipeline script you also have like pipeline script from mscm where you can use the jenkins file uh creator inside your github repository itself now if you see you have a section called pipeline syntax so if you click on that it will take you this place where you can select what kind of script so you can see i'm using git and if you put your github repo here and you know you can select which branch and if you have credential you can save your credential in jenkins and if you select that it will automatically create your generate pipeline it will automatically create the whole uh pipeline script which you can copy and paste okay so you can use that to create the pipeline script if you don't know how to create it so the first stage is like we are going to check out the github so this is the github link if you your github link has to be edited based on what is your github link okay and the second stage is we are going to build the image so here you see like we are going to save that into a variable which is called docker image and we are going to use the argument docker dot build so you not to work i know to work docker dot build that is why we need to have the docker pipeline plugin okay and we are going to set the name as docker image so that's the name coming from here now the pushing the image what we are going to do is we are going to use the registry credential so what does this means like you know we have a credential which need to be saved inside your jenkins so if you go to jenkins and if you go back if you go to manage jenkins go back to your manage credential now here if you go to global you don't have any credential so you need to click on credentials and it should be a username password and you need to enter your uh you know docker login credentials so you should give your username for example for me i will give my login for my docker hub login that will be this username and my password for it so i can enter that password then i can give a name so in my case i have given the name as docker hub login so this has to be matching okay because when we use this uh one it has to look for the credential on the jenkins credential manager so you need to give that name so once you give a name and if you add it it will be added as a credential now when you run this uh registry credential it will pull those user login and password from that so you don't have to manually or you don't have to hard code your username and password okay now you can see we are using that to log into your uh docker hub so you can see docker with registry and we are using the docker hub registry which is the dollarregistry.hub.com and we are using the credential from here which is the docker hub login and we are going to push this image which is the image name and we are going to give the tag as latest so there is also way like if you don't want to push it directly as latest you can use some variables here so you can push it as a first stage like you push it to as a no staging image then you can try all those things then if you do want to add some other stages you can do some testing with that and the next stage you can add like you know the image from staging should be pushed into as a latest so that also possible so you can change the the whole pipeline based on that and the next stage is like once your image is available in docker hub then the next stage is to deploy your kubernetes so now here also we are going to use the no variable or argument from kubernetes deploy and we are going to set the config so we are going to use the deployment.service.yaml which i showed you i have combined both uh deployment and service in single file and i put into the github right and in order to work this you need to have a cube config id so now to create this again you need to have go to your docker credential you need to add a credential and here you need to select kubernetes configuration so this configuration will come only if you have installed your uh kubernetes continuous uh plug-in okay if you have not installed it this option will not come so once you select it you need to give a name so since i'm giving this config configuration name as kubernetes you should match that over here okay so you need to give that uh kubernetes as here as well so that you know you use the same name in your uh there as well otherwise you know if your pipeline is not matching with this credential manager name then do not be able to uh get this correct running now there are different ways you can do it so what i'm going to do is i'm going to enter manually now we need to enter the content here so what content we will run put it here so let me show you how to do that so let's uh move on to our kubernetes cluster so if you don't know how to set up a kubernetes cluster i would request you to check on my videos how to set up a kubernetes cluster so now if you see uh this is my cluster we have two nodes so you can see i have one master and one worker so what we can do is like for configuring the kubernetes config file you need to copy the content from your master node so for that you need to go to your home so if you go to your home and if you list your files you have to include the hidden files and you should be able to see a folder called dot cube so you go to the folder called dot cube and you should be able to see a file called config so if you read this config file you should be able to see some information so see i'm reading so you can see like api cluster certificate authority right so and the server urls all those things are listed here so you need to copy this whole content of your config file so you can just copy this content and you can go to your i know jenkins and you can enter that content over here so you can see the whole content you need to copy from that config files and the config file is located uh inside a dot cube from your home okay so that's the location so you can see this location so which is under root dot cube so that's the place where you have the config file so you can uh read the file from there and you can put those contents over here now the option is like you can you know select from file and you can map it to there as well so the easiest way is like you just copy the whole content over here and put it okay so now i have created the two credential manager uh files so you can see one is for my docker hub login and another one is for kubernetes config file right so these both are required so because we need to log into our docker hub to push our docker image and also we need to log into our kubernetes cluster to deploy our uh the deployment and services so now what you can do is you can go to create a new job okay and you can select pipeline and you can give a name so i will call it as kubernetes jenkins deployment okay and i'll use pipeline and click on ok so we are going to create the job so that you know we can uh push the you know our application to the kubernetes cluster so we'll directly go to the pipeline script and we are going to enter the pipeline script over here so we already discussed about the whole pipeline how it is written so i'm going to copy the whole pipeline file and i'm going to paste it to this pipeline script the other option is you can create a file called jenkins file inside your git repository and you can select the option like a pipeline script from sema so it will automatically take this content from the jenkins file so again i just want to show this how the pipeline is returned so you can see the environment available we are checking out the git repository we are building the image and we are pushing the image to docker hub uh you know using the credential like docker hub login which we saved into your jenkins credential manager and we are pushing our application to kubernetes using the cube config id which is the kubernetes you know credential manager which we deployed and this is coming uh the deployment service yaml from the github repository so just save this okay so before i run the build let me go to my kubernetes cluster again and let me show you like if i go to cube ctl get deployments we don't have any deployments and we don't have any parts running right and we'd also don't have any services running so let's uh you know this is the default uh kubernetes service so we don't have any service for application so let's go and run uh build our job okay so let me run and you can see like if i click on this one you should be able to see the whole uh console output for it so let's wait you know how the pipeline script is happening so we'll be able to see the output so you can see it started running the jenkins job so let's see what what is the status we are getting so the first part is like it's cloning the git repository right so it's checking out and the next stage is build image right so it's going to the build image so you can see it's running the command docker build hyphen t and uh the application name which image name we have set in the environment variable and the docker file is the default locations so you can see these are you know done based on our pipeline script and you can see this is based on the docker file content it's uh creating the docker image so it has already created and tagged as you know docker app latest now it's uh pushing uh the image so you can see it's already logged into your docker hub using the password right so now it's pushing the image to a docker hub since i already run it once so it's uh already have the image there so it's uh otherwise it will take long time to push the image because this image is almost like one gigabyte so you know some of the layers are already there so only the changes will be pushed so you can see it has already pushed the image now it's try starting to deploy the kubernetes uh uh you know to our application to the kubernetes as a deployment and also as a service so it's using this configuration file deployment service dot yaml so let's wait uh to see what is the status for it yeah good so we got the success uh you know as our pipeline script so the pipeline whole pipeline script has been completed and we got the success uh message so if you go to your job you can see we have four stages one is for uh the checkout one is other one is for build image then push image and deploying to kubernetes so all those things are success now and if i go to my kubernetes cluster master and if i run cube ctl get deployment you can see we have our deployment running now so this is the deployment name which we have defined in our a manifest file or aml file and if i go to cube ctl get parts we have our port running for our application as well and also if you go to tube ctl get svc you can see our node service is also running now we can access our application using this port number so let's go to our browser now if you go to the browser and access our application using the ip address and the port number which we have set in the service you can see we are getting the home page and if you put like the routes so we have a route call wheel so you can see that's giving the hello world message and if you go to ready it will give you great it works so all those things are running so our application is deployed perfectly fine in our kubernetes and also our you know the deployments parts and replica sets and everything else running so you know if you have a deployment running you will also have a no replica set since it's we are running only one replica so we have only one if you have more than one which would have seen those replica numbers as well so to just to give a recap like we have done the whole process from github to the continuous integration with jenkins pushing to docker hub you know then uh deploying into our kubernetes cluster we only did not done this step for testing our application before building our image that you can add up into your pipeline where you want to test it before you know building the image or after building you can test with the build image or you can also add other source code checks like you can add sonar cube so that you know you can check your uh scored quality you can also add like artifactory to you know put your uh binaries so all those things you can add it to this pipeline so our concept what we wanted to do is like how you can integrate your jenkins with the kubernetes cluster so that you can push your application into the kubernetes cluster so we there are some uh key things which we discussed like you know how to create your credential manager for docker hub login and also how we can use the cube config file uh for creating the credential manager for kubernetes integration and we also shown you like the complete pipeline for your you know the kubernetes uh jenkins master integration so you can use that pipeline i'll be giving the github link in this video description so you can refer there and you can also see how we have written our deployment and service yaml file which we have used to deploy our application so that is all for this tutorial i hope it's a informative tutorial for you and you have liked it much and so i would request you to subscribe for more videos and also you know like my videos share and comment [Music] you
Info
Channel: Thetips4you
Views: 4,352
Rating: undefined out of 5
Keywords: DevOps Using Jenkins Docker and Kubernetes, kubernetes deployment using jenkins, jenkins integration with kubernetes, jenkins kubernetes pipeline, jenkins kubernetes integration, jenkins kubernetes deployment, jenkins kubernetes continuous deploy plugin, jenkins kubernetes deployment pipeline, jenkins deploy to kubernetes cluster, jenkins deploy to kubernetes, kubernetes jenkins tutorial, Deploy to Kubernetes Cluster, devops, jenkins, kubernetes, cicd, ci cd, pipeline, thetips4you
Id: XE_mAhxZpwU
Channel Id: undefined
Length: 33min 1sec (1981 seconds)
Published: Thu Nov 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.