How to Build and Deploy an app on Kubernetes by GitLab ci cd pipeline

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign welcome to my YouTube channel I am Malik and I am back with another video in this presentation I want to demonstrate how to set up a cicd pipeline using gitlab in this pipeline I have designed a web page module and I will show you how to deploy it as a micro Services container on a kubernetes cluster by gitlab CI CD foreign as you know in these days most of the application are built based on microservices not monolithic so each part of application are done by a separating here we assume we were asked to design a login part of application this one and we need to test it on kubernetes cluster as you see it is on my local computer also here is the HTML source code of this application we need to test it on kubernetes cluster before deliver it and see if it properly works and at the end of this presentation with entering the IP address of kubernetes load and access to this login page so as a designer I design this HTML page and it is ready to test it on kubernetes cluster so the source code here is on my computer so I need to build a container image from the data that I have and push it on container registry then on kubernetes cluster by running proper manifest pull image from the container registry and bring up the container here so we want to do all these steps by using gitlab CI CD okay let's start for running this presentation we need to have a gitlab account it doesn't matter if you are using gitlab.com or your local gitlab server also we need access to a kubernetes cluster if you have a cloud icon on one of the public cloud provider like AWS Google cloud oci and so on you can bring up a cluster and use that and maybe these public clouds charge you also there are another choice to bring up a kubernetes cluster on your local machine if you are using Windows or Mac machine you can install and configure launcher desktop on your computer really I have recorded a video about how to install and configure launcher desktop on Windows machine and I pinned its link about this video and by click on it you can access to it and configure launcher desktop on your computer if you are using Linux Ubuntu distribution or Windows sub Linux you can use multipass package to bring up quickly a kubernetes cluster on your local machine so these two steps are mandatory to run the labs of this presentation in continue we have to create two repository on gitlab server and we are using one of them to make a connection or make a tunnel to kubernetes cluster and the other one will keep our project data here is the summary that we will do on this presentation for this presentation I'm using gitlab.com let's I connect to my account on gitlab and carry it to repository there during the creation I will describe you why we need two Repository okay here is my gitlab account so create new project create blank project and give it a name for example K8 s collection the reason for create this repository is to make a connection between gitlab and kubernetes for that reason I selected k8s Dash connection you can use every name that you want so change it to public also you can make it private it's deep into you for here I'm selecting public and create project also back to the first page and create another project let's back to diagram here I'm going to build repository a and in this repo we will keep our data it means all the codes that I prepared for my project I will upload here so let's back to gitlab again and make it k8s for example data also public and create project let's go into Google I search for integrate kubernetes cluster with gitlab from the first result you see a document that is written connecting a kubernetes cluster with gitlab if you read it here to connect a kubernetes cluster to gitlab you must first install an agent in your cluster so click here installing the agent for kubernetes prerequisite before you can install the agent in your cluster you need an existing kubernetes cluster as I told you in per requisite some minutes ago access to kubernetes Cluster is mandatory also I mentioned if you don't have access to to any public cloud provider you can bring up a local one on your personal computer or your laptop here is my personal computer and I'm using Windows so I installed WSL on my computer and also installed launcher desktop so if you install launcher desktop on your computer automatically you have a kubernetes cluster for testing just type Cube CTL get notes and you see the kubernetes cluster with one node is up and ready to use so back to document on gitlab so an existing kubernetes cluster is ready so going down in installation steps to install the agent in your cluster create an agent configuration file in the first step we have to create an agent configuration to carry it an agent configuration file choose a name for your agent you have here it's meant the name must be unique in the project contains at least 63 characters and so on okay let's see in continue in the repository in the default Branch create an agent configuration file at the root let's copy this one back to gitlab in the kubernetes connection repository that we have created for connection between gitlab and kubernetes here it's the root and just click here new file and put it here here for agent name we have to select a name so let's select k8s connection and at the moment no need to write inside this file and just commit change you see we have a config yaml file and at the moment we don't have any configuration inside this here it's like a firewall and in this file we Define which group which username which repository can access to kubernetes Cluster so in next sub minutes I will completely explain what configuration we should Define here let's back to roots you see here we have created a file with this path here back to documentation here it is mentioned you can leave the file blank for no and configure it later the next step is to register the agent with gitlab we have to register that in gitlab at first how we do that you must register an agent before you can install the agent in your cluster to register an agent we have go to operate kubernetes cluster let's back to gitlab account from the right side going down to operate kubernetes cluster you see it's empty and we don't have any connection to Cluster connect a cluster just click here open and you see the name we have created some seconds ago click on it and register here give you configuration and it is mentioned you have to keep this configuration in a safe place because if you close this page you will lost this configuration agent access token you can copy this token and use in kubernetes cluster to make a connection with your gitlab or you can use Helm so I'm using this section but let's copy this one and keep it in a safe place let's back to terminal in kubernetes cluster and run this configuration copy the first one and in terminal also here when you bring up a cluster by launcher desktop Helm package is also installed so check helm yeah it is installed so let's yeah it's already exist here Helm repo update okay update is complete and copy the last one and enter you see thank you for installing your release is named KAS connection let's back to our repository on gitla let's close this one we don't need it anymore let's refresh see it is connected so the first step successfully done and the tunnel between gitlab and kubernetes cluster is up in the next step let's copy our project data to Second Repository so let's back to other repository as you remember its name was k8s Dash data so at the moment it's empty let's clone it back in terminal and just type git clone and the address LS let's switch inside that you see it's empty git status and you see it's up to date what data we need to push on repository at first I need to project resource that I designed by HTML and it is located here in SRC LS SRC you see CSS file HTML and so on so to use this project as a container we need to build an image as you know for build an image we need a Docker file here I created a Docker file just cats Docker file let's clean and open it again you see inside Docker file I'm using nginx as a base image to show you quickly let's bring up an nginx server by docker Docker run Dash D Dash Dash name web001 Dash p connect Port 8082 from my computer to Port 80 in the docker and the image nginx let's browse it by my browser localhost Port ad82 you see welcome to nginx so I gonna use nginx as a base image to build an image for my project let's back to terminal and let's stop this container on and let's remove it oh let's clear the screen so in my Docker file as you see here I'm using nginx in the second line I wrote hey docker please copy my resources from the SRC folder and paste them in nginx in this path a slash user a slash share slash nginx slash HTML Let's test it manually and everything going well we will use this instruction in gitlab cicd to automatically build an image for us let's continue how to build an image manually just type docker build Dash T it means add a name and tag to New Image that you are going to build so I'm gonna name it sample and tag it with the version one and at the end where is my Docker file is located just type dot it means is located in the root so just enter you see this image with this tag is built just type docker image l s grab sample you see in the first line sample version 1 40 seconds ago was built so far so good let's use this image and see if everything is correct so Docker run in a state of nginx let's using sample version one Docker container is built so let's browse it here if I refresh you see it's my project so at the moment we see how to build an image manually and how it works let's back to terminal and stop this one and remove one nice the next step is how to push this image to a repository that we can use in kubernetes cluster normally there is Docker Hub and we can use Docker hop to push or image there but we don't want to use it because normally every company has its private container registry so here we're gonna use gitlab container registry as a private registry for or project let's back to gitlab going to left side and in deploy section you see container registry at the moment it's empty and we have to push or build image here at first I show you how do it manually and in next minutes you will see how do it automatically in CI CD if I going down you see at first we need to login in gitlab registry so let's copy this one and back to terminal already my credential are exists on my computer and successfully logging to gitlab but if you are first time login like that it will ask you username and password so in first step we are connected in registry gitlab.com through CLI let's see next step in Next Step It show us how to build an image back to terminal and let's add sample weavon image build just Docker images you see here it is built let's going here and show us how to push it to Repository copy and just sample version one you see here it's going to push the image to the gitlab registry there you are it is pushed to gitlab container registry let's check it and refresh this page you see it is here so how we do these steps automatically by CI CD we don't want this image anymore because we need to push it here automatically let's delete it delete done let's back to terminal and copy the project data to the Repository you see I did copy them in the repository let's get status git add git commit minus am at project committed and git push they are pushed let's go in and check repository quickly you see this folder and Docker file copied here for cicd as I describe in one of my video how to start with gitlab cicd we need Chariot a file called dot gitlab dash CI dot y m l if you don't know how to start with gitlab cicd and how it works I suggested to watch my video about this and I have pinned it above this video so here we need to Define job and steps now how we configure this file let's back to Mr Google and in Google just type gitlab container registry CI CD and from the result click here and see the proper document for this step here build and push container images to The Container registry here this step that we did manually so we need this section configure your dot gitlab.ci.yaml file let's going down there is some example just we need this example container registry example with gitlab CI CD just copy this one and paste it in the file that we have created on gitlab let's Bank paste it here let's make it more cleaner Define stages we Define which stage we need to use so we name it build a stage you can name it everything you want but for user friendly I use build here is the name of origin change it to image build image which image we're gonna use to run on gitlab Runner we're gonna use the docker image each stage we are using this stage build which Services Docker let's remove this one Docker in Docker Services what it means gitlab Runner is Docker container and we're gonna run another container image inside that container so it's called Docker in Docker container so in a script section there are three commands you remember them the first one is for logging to gitlab registry the second one for build image and the third one for push image to gitlab registry so for the first one Docker login dashu username there are some built-in variables inside gitlab let's make it more clear let's copy this one which username we gonna use to connect gitlab Repository my username here my username is and which password I'm gonna type my password and also the gitlab registry address in a state of writing all the credential in text here it is better to using built-in variables from gitlab so let's remove this one for the username there is a variable CI registry user automatically gitlab determine this one because you are logging in your Repository for the password also gitlab has your password and no need to Define here just using this variable and which server if using this variable CI registry automatically gitlab will determine it means registry dot gitlab.com it's done this the first line done the second one Docker build CI registry when using CI registry as you see also it here it means registry dot gitlab.com let's back to terminal and show you something remember this command registry dot gitlab.com here it's related to my icon in gitlab the name of the repository and the name of page we're gonna make it so let's copy this one just let's copy it here the first section will return this one registry.gitlab.com so let's remove this one the other section should be copy and paste it here so when this line run it will create an image with this name for us and in the third line it will push that image let's copy this one where to gitlab registry and also we can add an echo message build let's save it as soon as we save it it will go and run and it will build an image and push it to gitlab repository it's add a message by CI CD and commit change this file has been successfully created let's back to section Pipelines and here it show you it is running let's click it here build image you see here it using Docker executor with image docker you see image build let's check it what it happened run Docker file carry it an image and then and push that image to gitlab repository and job sucks it let's go into left side and check deploy container registry and see here the image added here if I click it here you see one minute ago it was pushed here so we could build an image and push it to gitlab container registry by CI CD okay it only remaining part is kubernetes section in kubernetes cluster how we can use this image the image that we push it in gitlab container registry so how we can use that for answer to this question and to find out how to bring up a container inside kubernetes cluster it is better to give you a perspective of kubernetes cluster here the assume this is one not of or kubernetes cluster and it's the IP address of this node here we're gonna to bring up a container with the build image here for bring up a container there is a concept in kubernetes it's called pod pod is a smallest component in kubernetes as your part is like a box and we bring up or container inside the Pod normally in kubernetes we never use the name of container and we use the name of Paul in each part we bring up a container but sometimes we can bring up multi-container for some specific propose but at the moment the assume in each part only there is one container so for bring up or application we need to bring up a container inside a pod with using the image that is container registry so let's bring up a pod here and this is our pod we name it app with using the sample version 1 image when we bring up a container or a pod here it gets an IP from the kubernetes cluster so or application now is up and how we can access to this application here there is a user and want to access this application how can do that is it possible to use this IP in browser and access it the answer is no let's clean these lines because the part is alone inside the cluster and only is accessible from inside the cluster no from the external and also if for any reason then the Pod restarted its IP will be changing it means the Pod never keeps a permanent IP for solving this problem another concept to play and called Services the service is a logical component and its IP is permanent and never change and we put service in front of the Pod so for access to application here we have to access this IP address through service you have some different type of services by default it's called cluster IP type and from its name you can see it is accessible only inside the cluster for example you have some application inside the cluster and they want to communicate with each other so only they can communicate through cluster IP service so it's still our issue is exist how we can overcome or issue we want to access it from the external how we can do that there is another type of Services before going forward this port called port and this port called Target Port it is important in configuration so for access from outside there is another type of service is called note Port then bring up a note Port Services a service will attach a port to the cluster node that will be one by one mapping between service and that Port the problem solved and from the external just we type the IP address of the node and using the note Port we can access to application so far so good but it's still there is one thing that I need to mention it then bring up an application here need to pull image gitlab container registry but as I told you before for access to container registry we need username and password so in the configuration if using them in clear text it's not a good way so what we can do there is another concept inside kubernetes cluster it's called sacred we can define a secret and put the credential inside the secret and called it inside the Pod configuration so we need three manifests one manifest to bring up a pod one for secret to keep credential and one service which type note port to allow us access application from outside let's do it manually and after that we will add them in CI CD how to create this manifest quickly by CLI for create a pod by CLI just we need to type Cube CTO run and give it a name for your pod for example login application and the image name Dash Dash image from which image this part will be carried so let's back to gitlab.com inside the repository from the left side through deploy container registry you see the image we're gonna use just click here and copy here which tag or which version we're gonna use just click here and you see version one let's back to terminal and paste it here and type version one if I press enter here it going and will create a pod with login Dash app name for us for prevent creation at this time just at the end type Dash Dash D R Y dash run equal client Dash or y a m o it will run on fly and not create on kubernetes cluster let's enter you see a manifest will be created so for having this one inside the file just at the end type this one call it pod dot y a m o as you know all the Manifest in kubernetes are based off yaml language inter and just swim pod iano here you are all the configuration you need here some parameters are here at the moment we don't need them one of them is this one just remove this one and another one is resources and also DNS policy and a status it's done but one important note when this part is going to pull this image need what credentials so here we have to add from where can get the credentials leave it as is save it and let's go in and carry it a secret and after that we will Define this sacred inside the Pod for create a sacred just type Cube CTL carry it secret let's enter you see here there are some types of sacred one type is Docker registry you see here create a secret for use with a Docker registry when you're gonna pull image from each registry Docker Hub registry gitlab registry and so on we have to use this type so Secret Docker registry dash dash help a lot of information are here just let going off there are some example here you see here Cube CTL creates sacred Docker registry you have to give it a name and after that Docker server the name of the server here is registry.gitlab.com Docker username your username and Docker password so let's copy from here to here and clean here back here and paste it here sorry let's clean let's for the sacred change it to app Secret and Docker server we have to change it to registry Dot gitlab.com and from the help dash dash Docker username my username and dash dash password Here dash dash Docker password and put my password Here and at the end we add Dash Dash D R Y dash run equal client Dash o yaml and save it for example to Secret dot y a m l enter and a file with name sacred is created let's open it win sacred.yaml you see the username and password and address of gitlab registry R hash here let's delete this line we don't need it anymore just save it and it's done here let's bring up the secret for bring up the sacred Cube CTO apply Dash f acred yamo inter you see sacred created to see the list of secret on kubernetes cluster just type Cube CTL get sacred get sacred income you see app secret is here 40 seconds ago so let's take this name app Secret and add it in part configuration let's open pod configuration and we have to add it here how if you search on internet you will see this parameter let's save it and copy this one and back here and paste it here it's done just save it and let's bring up the Pod here just type Cube CTL apply Dash F and pod name done Cube CPL get pod you see login application is in running status so far so good so the last step is to create a manifest for service just type Cube CTO create service or we can use an abbreviation SVC dash dash help you see there are different type of service that we spoke about them we have to use node port so let's remove this one add this one and again type dash dash help you see there are some information here let's use this example after node Port we have to name it for example login Dash SVC and after that you see dash dash TCP dash dash TCP which Port we have to add it here let's see in presentation the port and Target Port 80 for the service and 80 as Target Port Dash Dash D R Y dash run equal let's bring it here and enter you see this is the service one let's record it in a file logging SVC Dot yaml and Vin login svc.yaml like before and we can change this name like this one and you see the type is not Port here the important section is here selector in the selector we Define the label of pod when we Define it here when we run service it will go and find pod and will attach it to itself let's close from here and open pod you see the label is this one copy this one back to SVC and in selector paste it here and let's delete this one so when we run this service it will go and find a pod with this label let's save it another thing is here when we bring this service up which port in cluster node will be assigned for this service in kubernetes documentation mentioned it will assign a port between 30 000 to 32 767 if we don't Define a portal it will automatically take a port from this range also we can Define it manually let's leave it as is and see what port will assign to this service save it and let's run this service Cube CTL apply Dash F login SVC and enter you see service created with this command tube CTL get SVC you will see the list of services you see the cluster node assign this port to service if we wanted to be sure the Pod is under the management of this service just type Cube CTL describe SVC the name of this service and here you see all the information related to this note Port service if we check end points you see there is an IP here it belong to pod and from where we can be sure this is the IP of the Pod just start Cube CTO get part Dash or wide you see login application it's IP and this is here so at the moment we are sure the service is running and manage this part so let's type Cube CTL get node Dash or Y take the IP of the node and put it in browser and which Port you see this port enter here you are the application is running inside kubernetes cluster so far so good I'm going to stop service and delete the part that we have created manually and we will use CI CD to create them I have deleted pod secret and service let's go in and use CI CD before using cicd I have to upload this created manifest to gitlab repository you see I did copy the file here in this folder and get a status here git add it commit Dash am and push them down let's back to gitlab repo you see k8s file are here now is the time to add configuration in gitlab CI yaml going to build pipeline editor and add another stage here deploy so let's going to gitlab documentation and find the proper configuration for deploy staging in Google search using gitlab cicd with a kubernetes cluster and from the result click here and you see a good document how to using gitlab with kubernetes so let's click done and here you see an example just copy this one and paste in configuration on gitlab Repository paste it here just change jobs name as deploy project and which stage we should use deploy and here an image that we'll use on gitlab Runner let's remove this one we don't need it anymore here Cube config use context path to agent repository if you remember at the start of this presentation we have created an agent in another repository let's go into that Repository here is the repo from the left side in operate kubernetes cluster and here is the name of agent just copy this one back to our configuration agent name each path this is the path also to make it more clear it is better to Define variables here and in variables Define this variable the same as I put it here and in a state of using this one just copy this and paste it here with dollar sign at the first of it so at first it will find this permission to have access to our kubernetes cluster if the connection is okay with this command Cube CTL get part it will show us a list of parts that are exist on kubernetes cluster at the moment still I didn't Define any command for or manifest Let's test and see if everything is okay but let's add another command here Q CTO get notes Dash or wide okay let's save it and see if it is works or no let's click here and see the job the first step is done yes it's for image building the important section is the second one I see it is failed you see no context exist do you know why let's back to presentation at the moment cicd config file is here but another repository is connected to kubernetes Cluster here we have to authorize repository a to make a connection to kubernetes Let's back to gitlab kubernetes connection if you remember we have added a file here config yaml there is no configuration here if you remember from the starting of this presentation I told you this config file works like a firewall and here we have to make a permission which repository has permission to access kubernetes cluster which user has permission which group has permission here we have to Define which one has permission so here we need to add configuration edit let's back to documentation here authorize agent to access projects in in your group also here authorize the agent to access your project let's copy this one copy back to configuration and paste it here CI access which project the name of the other repository let's go into other repository data and it's the name of this repository copy back here so let's save it back to k8s data the repository that or data is there and going to to build section Pipeline and rerun the job again it's running back here to in here again failed let's see what's the error no context exists with name this one this one oh it seems I made a mistake let's back to cicd configuration here you see here I have to I have to correct this one edit at first it should be this one because or because our agent is here and the name of and the name of agent also I have to remove this one because we defined it in variable let's save it you see this time successfully finish let's going inside that and you see it is connected there you see here Cube CTL get parts yeah this web application is running and nodes also you see here the name of the node and the IP of that good good let's add a real configuration there back here and edit this one paste the commands here the first one it shows us a message deployment login application to kubernetes the second line lists the file inside this folder in the road directory this variable retrieve the root address back here it means here k8s files back here k8s files and the next line apply all the file in this directory to kubernetes Cluster here there are three yaml file one for pod one for secret and another one for service and finally it will show us the list of parts and services let's commit based on this CI CD we have to a stage the first one will build an image for us and second one will deploy this image on kubernetes cluster and there will bring up three components a pod a secret and a service let's see here Louis job is running here it shows us job succeed let's see at first show us a message deployment login application to kubernetes Here show us the list of manifest service pod and secret created and get part you see login application container creating and also show us the login SVC is created there with this port and from here also find the IP address of the node copy go into browser and also take the port paste it here and enter done so by CI CD we could build an image push it to container registry and then in deploy section and then deployed or application on kubernetes cluster using the image that was on gitlab container registry I hope you like this presentation and if you like it don't forget to subscribe and also I will appreciate if you comment your feedback thank you so much
Info
Channel: be-devops
Views: 24,105
Rating: undefined out of 5
Keywords: ci cd pipeline to deploy to kubernetes cluster using Gitlab, ci cd pipeline to deploy to kubernetes cluster, gitlab cicd with a kubernetes cluster, ci cd project example, gitlab ci cd project, gitlab ci cd tutorial, gitlab container registry, gitlab kubernetes agent, gitlab integration with kubernetes, ci cd devops project, Rancher kubernetes, install Rancher-Desktop on windows, gitlab ci, gitlab tutorial, continuous integration and deployment, ci cd tools
Id: fwtxi_BRmt0
Channel Id: undefined
Length: 61min 35sec (3695 seconds)
Published: Sun Sep 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.