From the course: Automating Kubernetes with GitOps

Exploring pipelines in Jenkins - Kubernetes Tutorial

From the course: Automating Kubernetes with GitOps

Exploring pipelines in Jenkins

- All right, so how does Jenkins fit into the picture? Well, we have these GitOps stages. And this can be picked up by a Kubernetes operator. Resulting in your Kubernetes resources. But this GitOps operator is not the only way that you can do it. You can also use Jenkins. And what is going to happen if you use Jenkins? Well, it is all about the location of your stages. So where are the stages? Well, if you go for the GitOps operator, your stages are here. They are a part of this GitOps operator functionality. And if you use Jenkins, you define your stages in a Jenkins shop. Why would that make sense? Well, it's a different tool that can help you accomplish the same. Maybe you like Jenkins. Maybe you like Kubernetes operator. It's a free world. You make the choice. So at this point you are ready to create a Jenkins CI/CD pipeline. A pipeline itself in Jenkins is a script that is written in the so-called Groovy syntax. And in this script, different stages are defined, and each stage consists of a couple of steps. And these should correspond to the DevOps stages that you want to use. Jenkins pipelines can use specific agents to perform tasks, and that is what makes Jenkins very powerful. And if you don't want to use an agent, that's fine as well. Just use shell commands in that case. Each command that is executed as a shell command must be installed on the Jenkins computer or in the container image. And that's very important. This is also why you might better want to use the agents because the agents are taking care of the dependencies, and it makes them a little bit easier to use as shell commands. So a simple pipeline script that contains different stages is available in the course git repository as the file jenkinspipe. And notice that the git repository that is used in the file, you can use git branch "branchname" to indicate which branch you want to use and add url to refer to the specific git repository. The repository will be cloned to the local Jenkins environment, and it will be updated each time the Jenkins pipeline runs. Jenkins will always check if anything new has happened. Let's go check this out. Here you can see the steps that I'm going to run. Let me run them for you. So this demo starts at localhost port 8080. Notice that this is an http url, not an htps url. And it's telling us that we need to unlock Jenkins. So Jenkins has a generated password, and this generated password is right here in var/lib/jenkins/secrets, et cetera. So I need to look it up. Definitely that should require sudo privileges. And there we can see the generated password. And now we need to bring that in. And then we can continue. Next, it is suggesting to install some plugins. I would always do that because these plugins make sure that you don't face too many dependency problems. They take care that most of the common functionality is installed, so install them. It might take a minute or two, but you better do that before doing anything else. All right, so now it's asking to create the first admin user. You can just skip if you want to. For the simple demo, we don't really need it. For the instance configuration, just click save and finish to make sure that the correct link is used by Jenkins all over. Now we can start using Jenkins. So what are we going to do? Well, we are going to create new item. And we need a name for the new item. Jenkinspipeline. Then we need to select the type. Well, the type is going to be a pipeline. Next you click okay. And that will bring you in the interface where you can define the pipeline. Here we need to select pipeline. And once we have selected pipeline, we can go to the pipeline script. And in this pipeline script, well, we are going to use the pipeline from the course git repository. It's in Jenkinspipe. And here we go, let me copy it, and then I will explain what it is doing. Oh boy, for readability we better have a look at this. So what do we see in the pipeline? Well choosing the agent any, that's not a specific agent, which means that it's basically using shell commands. Then we have a definition of the stages. There is three stages: build, test, and release. It's like simplified GitOps, but simplified is doing okay. And every stage needs steps. Well here it's one step stages for any. And that's okay. So we have a git branch master. A git branch master. Take care of that, this is deprecated terminology, but apparently the master branch in this container's git repository has never been updated. It needs to match the name of an existing branch on GitHub. Otherwise, it won't work. Next, it's using docker build -t, alpmap alpmap. So in this git repository there is a docker file, and Jenkins is going to build this docker file for us. And once we have a container image, that is what we are going to test. That's in the next stage, so the next stage is sh docker images. It just runs the command docker images. And if that command is successful, then the test is successful, and then we can release. And this simplified release is just using docker run alpmap to run the container image for us. So as you can see, it's simple, but it really is a GitOps workflow. And if this is what you like, then you click save. And then on the menu in the left you can click build now. And observe what is happening. So as you can see, we can see the different stages right here. So the build stage, and oh boy, am I getting a field? Yes, I am getting a field. So shell script, script returned exit code one, and we need to check the logs for more details. And the logs is telling me this command has failed. And this command has failed with an error "permission denied while trying to connect "to the Docker daemon socket." Aha, you know what this is? This is the classical error where the user who is running it, this is not a member of the Docker group. We completely overlooked that. Now, which user is going to run this? That should be the Jenkins user. So let me do a sudo usermod -aG docker on Jenkins. And that will make the user Jenkins a member of the Docker group. There we go. Now the user Jenkins is not currently logged in. So in order to make sure that this group membership is properly picked up, we need to restart Jenkins. So sudo systemctl restart Jenkins. And after the restart, we can try it again. We'll see. Let me start a clean browser window. And from there, http://localhost:8080. Our username was not remembered, well that is the admin username. And we can update that, and look, there we have the Jenkins pipeline. So I'm getting back to the Jenkins pipeline where we can see the dramatically filled attempts before. And we can try it again using build now. And now we see blue, and blue is better. So it was the group membership that it's all about. Now if you want to know what is going on, then you can have a look right here. So just click the build time and date. And then from the menu on the left click console output. And in the console output you can see everything that is happening. So in the background, this Jenkins pipeline script is building a container image. And after building the container image, look, here is the test, docker images, and docker images was successful. And because it was successful, it has proceeded to the next step, docker run alpmap. That's what we wanted. So this console output is nice for analyzing what is going on. And if you just want to have a high level overview, just hover over the different stages in your Jenkins script, and you'll see all of the details. And this concludes our short introduction to Jenkins. In a GitOps environment, Jenkins is a useful utility. It's not the only utility that you might want to use. It's an option. Some people like it, and, well, you could use it. You can also do it in a different way. We'll talk about different ways in the upcoming lessons.

Contents