"From Code to Cloud: A Kubernetes Odyssey with Jenkins and Grafana."
DEVOPS

"From Code to Cloud: A Kubernetes Odyssey with Jenkins and Grafana."

EKS Kubernetes to deploy Jenkins CI/CD tool using Automated way Operator and manage Jenkins Pipeline as Code by Jenkins Kubernetes CRD and Application jobs launches in dynamic provisioning node our Kubernetes deployment pods and monitoring of infrastructure log by Grafana Loki and Prom tail and Metrics by Prometheus Monitoring Tool.


"Jenkins, Kubernetes, Grafana, Prometheus - Your All-in-One DevOps Dream Team!"

"Mastering CI/CD in Kubernetes: A Comprehensive Guide with EKS, Jenkins, and Prometheus Monitoring"

In today's fast-paced software development landscape, mastering Continuous Integration and Continuous Deployment (CI/CD) is a crucial skill. With the rise of containerization and orchestration platforms like Kubernetes, deploying and managing CI/CD pipelines has become a complex but highly efficient process. In this article, we'll embark on a journey to build a real-world application-based project, starting from the fundamentals of Amazon Elastic Kubernetes Service (EKS) and progressing to advanced topics like Jenkins, Prometheus monitoring, and more. Buckle up, as we delve deep into the world of CI/CD in Kubernetes!

Getting Started with EKS Kubernetes:

Our adventure begins with the basics of EKS Kubernetes, Amazon's managed Kubernetes service. We'll learn how to set up an EKS cluster, configure nodes, and get our Kubernetes environment up and running. EKS provides the foundation for our CI/CD pipeline, offering scalability, resilience, and ease of management.

Automating Jenkins CI/CD with Operators:

Jenkins is a powerhouse when it comes to CI/CD, and we'll harness its capabilities to streamline our development workflow. But what's even more exciting is deploying Jenkins using Kubernetes Operators. We'll explore how Operators automate complex application management tasks, making the installation and configuration of Jenkins a breeze.

Managing Jenkins Pipelines as Code:

Modern CI/CD practices demand that pipelines are treated as code. We'll delve into Jenkins Pipeline as Code, using Jenkins Kubernetes Custom Resource Definitions (CRDs) to define and manage our CI/CD pipelines. This approach ensures version control, collaboration, and reproducibility of our pipelines.

Dynamic Pod Provisioning for Application Jobs:

In the world of Kubernetes, resource allocation and scaling are dynamic. We'll learn how to configure application jobs to launch in dynamically provisioned pods within our Kubernetes deployment. This flexibility ensures optimal resource utilization and scalability as per workload demands.

Monitoring Infrastructure Logs with Grafana Loki and Prom tail:

Effective monitoring is vital for CI/CD pipelines. We'll explore how Grafana Loki and Prom tail work in tandem to help us collect, store, and query log data from our Kubernetes infrastructure. This combination provides real-time insights into application performance and issues, enhancing our ability to troubleshoot and optimize.

Metrics Monitoring with Prometheus:

To complete our monitoring stack, we'll integrate Prometheus, a leading open-source monitoring and alerting toolkit. Prometheus allows us to collect and query performance metrics from our Kubernetes cluster and applications. We'll set up custom alerts to proactively address issues, ensuring the reliability of our CI/CD pipeline.

Advanced Use Cases:

But the journey doesn't end here. We'll also cover advanced use cases, including creating an end-to-end pipeline using the operator, deploying pods with Helm charts, and exploring more Kubernetes features to enhance our CI/CD process.

By the end of this journey, you'll have a comprehensive understanding of deploying Jenkins CI/CD on EKS Kubernetes, managing pipelines as code, dynamically provisioning pods, and implementing robust monitoring with Grafana Loki and Prometheus. You'll be equipped with the skills needed to excel in the world of CI/CD in Kubernetes, making you a valuable asset in the realm of modern software development. So, let's embark on this exciting voyage into the heart of DevOps and Kubernetes! ????

Let's dive into the project!

OPERATORS: Kubernetes' operator pattern concept lets you extend the cluster's behaviors without modifying the code of Kubernetes itself by linking controllers to one or more custom resources. Operators are clients of the Kubernetes API that act as controllers for a Custom Resource.

In this blog, we embark on a journey to harness the full potential of Kubernetes Operators by automating the deployment of Jenkins CI/CD and streamlining the setup of essential monitoring tools on an EKS cluster.

This task will involve the following key steps:

  1. Installation of AWS CLI, eksctl, kubectl, and Helm.
  2. Leveraging Helm, to install Jenkins-Operator on our EKS cluster.
  3. We will define our Jenkins instance configuration that will be created and managed by Jenkins-operator on EKS.
  4. With the Jenkins instance we created, we will set up dynamic provisioning of Jenkins agents.
  5. Again using helm we will deploy Grafana Loki-stack for monitoring infrastructure logs, system health and performance.Prerequisites for a Seamless Start:To embark on this exciting journey into the world of CI/CD with EKS Kubernetes, make sure you have these essential tools ready to roll:

  1. AWS CLI (Command Line Interface): This powerful tool will be your gateway to interact seamlessly with AWS services, ensuring a smooth experience throughout your CI/CD setup.
  2. eksctl - The EKS Magician: Install eksctl, a wizard-like command-line utility designed to simplify the creation and management of your EKS clusters. It's your key to effortless cluster orchestration.
  3. kubectl - Your Kubernetes Sidekick: Equip yourself with kubectl, the trusty sidekick for Kubernetes enthusiasts. This command-line tool will be your go-to companion for interfacing with your EKS cluster.
  4. Helm - The Kubernetes Maestro: Helm, the Kubernetes package manager, is your ticket to efficiently managing Jenkins, Prometheus, Grafana, Loki, and other components. It streamlines the installation and orchestration of these vital tools, making your journey even more enjoyable.

With these prerequisites in place, you're all set to dive into the exhilarating world of CI/CD on EKS Kubernetes. Let the automation, scalability, and efficiency begin! ?????

1. Setting Up AWS EKS Cluster

1.1. Create an EKS cluster using eksctl:

We will create an EKS cluster on us-east-1 region with default configurations using the following command:

$ eksctl create cluster --name devopscluster --region us-east-1        

1.2. Configure kubectl to connect to your EKS cluster:

Once the cluster is created and initialized, your kubeconfig file will be automatically updated using which you can access your cluster, if not then you can update your kubeconfig file manually by this aws command.

$ aws eks --region us-east-1 update-kubeconfig --name devopscluster        

Check your nodes status using kubectl command.

$ kubectl get nodes -owide        


2. CI-CD using Jenkins-Operator on EKS

Jenkins Operator: It is a Kubernetes Native Operator which manages operations for Jenkins on Kubernetes. It has been built with Immutability and declarative Configuration as Code in mind.

2.1 Install Jenkins Operator using helm:

First, we will create a dedicated namespace for Jenkins where we will install our operator:

$ kubectl create namespace Jenkins         

Now let's install the operator by first adding the helm repository, then installing it using the following commands.

$ helm repo add jenkins https://raw.githubusercontent.com/jenkinsci/kubernetes-operator/master/chart        
$ helm install my-jenkins-operator jenkins/jenkins-operator -n jenkins --set jenkins.enabled=false        

We will not enable the Jenkins now which will deploy the Jenkins instance, we will deploy the Jenkins instance via YAML file in next step.

We can see that we have got a Jenkins resource in our Kubernetes cluster.

2.2 Launch Jenkins instance:

Using the Jenkins CRD created by Jenkins Operator we can create a Jenkins resource in our Kubernetes cluster that will have kind "Jenkins" which will create a Jenkins instance for us.

The main purpose of using the operator here is that we are able to manage the Jenkins via Kubernetes way.

Here is the manifest file jenkins_instance.yml for creating the Jenkins instance:

apiVersion: jenkins.io/v1alpha2
kind: Jenkins
metadata:
  name: my-jenkins-instance
  namespace: jenkins
spec:
  service:
    type: LoadBalancer
    port: 8080
  configurationAsCode:
    configurations: []
    secret:
      name: ""
  groovyScripts:
    configurations: []
    secret:
      name: ""
  jenkinsAPISettings:
    authorizationStrategy: createUser
  master:
    disableCSRFProtection: false
    containers:
      - name: jenkins-master
        image: jenkins/jenkins:lts
        imagePullPolicy: Always
        livenessProbe:
          failureThreshold: 12
          httpGet:
            path: /login
            port: http
            scheme: HTTP
          initialDelaySeconds: 100
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          failureThreshold: 10
          httpGet:
            path: /login
            port: http
            scheme: HTTP
          initialDelaySeconds: 80
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          limits:
            cpu: 1500m
            memory: 3Gi
          requests:
            cpu: "1"
            memory: 500Mi
  seedJobs:
    - id: jenkins-operator
      targets: "cicd/jobs/*.jenkins"
      description: "Jenkins Operator repository"
      repositoryBranch: master
      repositoryUrl: https://github.com/jenkinsci/kubernetes-operator.git

        
In this Jenkins instance we are defining some of the configurations of the jenkins instance, like "service" is used to create a loablancer resource for the jenkins instance, configurationAsCode defines configuration of Jenkins customization via Configuration as Code Jenkins plugin, GroovyScripts defines configuration of Jenkins customization via groovy scripts, many more configurations are supported which can be refered from here.

One main configuration from this file is seedJobs, here we can define our jobs that needs to be created by fetching the jenkins file from a remote repository where we store all our jenkins file.

$ kubectl create -f jenkins_instance.yml        

Once the manifest file is applied, we can see our jenkins-master instance is launched.

$ kubectl get pods -n jenkins        

Here we can see that my-jenkins-instance that is our jenkins-master is created, also we can see that a seed-job-agent pod is deployed that will configure our jobs in jenkins.

2.3 Accessing the Jenkins master instance.

To access the jenkins-master instance, we need to use the Loadbalancer service, and credentials to the user can be found in jenkins-operator-credentials-example.

$ kubectl get services -n jenkins
$ kubectl get secret jenkins-operator-credentials-example -n jenkins -o 'jsonpath={.data.user}' | base64 -d
$ kubectl get secret jenkins-operator-credentials-example -n jenkins -o 'jsonpath={.data.password}' | base64 -d        

As you can see our 2 jobs from the remote repository we gave are configured by the seed job that comes from the jenkins-operator.

2.4 Launch a job in jenkins to dynamically provision jenkins agent node.

Now lets launch our dummy job, that will end-to-end test our jenkins instance is working properly or not. Click on the job and select "Build Now" option.

From console output we can see our jenkins instance is working without any issues.

Now lets run our 2nd job that will dynamically provision a jenkens agent node and run our pipeline.

The build was successful, and here you can see the watch output of the where jenkins provision a pod in the kubernetes cluster to run their job and terminate it as soon as it is completed.

This way we can easily run our jenkins pipelines with the help of jenkins-operator on kubernetes.


3. Grafana/Loki-stack for Monitoring Infrastructure Logs and Metrics

3.1 Install grafana/Loki-stack using Helm

First we will create a dedicated namespace called monitoring where we will install our operator:

$ kubectl create namespace monitoring        

Now lets install the operator by first adding the helm repository, then installing it using the following commands.

$ helm repo add grafana https://grafana.github.io/helm-charts        
$ helm upgrade --install loki grafana/loki-stack -n monitoring --set grafana.enabled=true,prometheus.enabled=true,prometheus.alertmanager.persistentVolume.enabled=false,prometheus.server.persistentVolume.enabled=false        

Here we are enabling Grafana and Prometheus, as of now we don't require persistent volume to be created so we are disabling it.

Just with one step our full monitoring stack is deployed, having prometheus, grafana and log management tool loki. This is the power of operator, it becomes very easy to manage and integrate things that usually takes lot of time when we do it manually.

2.3 Access the grafana GUI.

To access the grafana GUI first we have to expose the service, a clusterIP service is already created we will patch the service to make it as LoadBalancer type service.

$ kubectl patch svc loki-grafana -p '{"spec": {"type": "LoadBalancer"}}' -n monitoring        

To get the login-credentials we have can refer the secret created by the operator.

kubectl get secret loki-grafana -n monitoring -o go-template='{{range $k,$v := .data}}{{printf "%s: " $k}}{{if not $v}}{{$v}}{{else}}{{$v | base64decode}}{{end}}{{"\n"}}{{end}}'        

2.4 Monitoring logs on Grafana.

After login, go to explore tab, and select Loki, as the metrics database.

Now apply the following query to monitor our jenkins-instance.

{pod="jenkins-my-jenkins-instance"}        

Now let us try monitoring the memory usage, by our jenkins-master instance, these metrics are come from prometheus, so lets switch the DB for grafana as prometheus.

Now run the following promQL query to get the memory usage.

container_memory_usage_bytes{container="jenkins-master"}        


Conclusion

In this journey through the realm of Kubernetes Operators, we've witnessed the remarkable power they bring to the table for automating and simplifying CI/CD workflows on Amazon EKS. By utilizing Kubernetes Operators, we've achieved:


  • Efficient CI/CD Automation: With Kubernetes Operators, deploying Jenkins CI/CD pipelines on EKS became a seamless and automated process. The Operator pattern allowed us to extend the cluster's capabilities without modifying Kubernetes' core code.
  • Dynamic Resource Provisioning: We harnessed Kubernetes Operators to enable dynamic provisioning of Jenkins agents, optimizing resource allocation and scalability.
  • Streamlined Monitoring: Through Grafana Loki, Promtail, and Prometheus, we effortlessly monitored infrastructure logs and collected valuable metrics, enabling us to make informed decisions and maintain a healthy and performant environment.


This blog post serves as a testament to the transformative power of Kubernetes Operators, unlocking new possibilities for automation, scalability, and efficient resource management within Kubernetes clusters. As you embark on your journey with Kubernetes and CI/CD, remember that Operators are your allies in simplifying complex workflows and ensuring the smooth operation of your applications. We hope this guide has provided you with valuable insights and practical knowledge for your own CI/CD automation adventures on Kubernetes. Happy automating!


CLEANUP: To delete the ekscluster

eksctl delete cluster -n devopscluster        


As we wrap up this exhilarating journey into the realm of CI/CD with EKS Kubernetes, it's essential to take a moment to reflect. You've ventured from the basics of setting up EKS Kubernetes to automating Jenkins CI/CD using Operators, managing pipelines as code, and implementing robust monitoring with Grafana Loki and Prometheus.

But beyond the technical skills and knowledge gained, it's important to acknowledge the journey itself. Each step you took, every challenge you overcame, brought you closer to becoming a master of CI/CD in the Kubernetes universe.

I want to extend my heartfelt gratitude to you, the reader, for embarking on this adventure with me. Your curiosity, dedication, and thirst for knowledge are the driving forces behind your success. Remember that the skills you've acquired here are not just tools in your toolkit; they are the keys to unlocking new horizons in your career.

As you move forward in your CI/CD journey, never stop exploring, experimenting, and learning. The world of DevOps and Kubernetes is ever-evolving, and your expertise is the compass that will guide you through its intricacies.

So, here's to your continued success, your unquenchable thirst for knowledge, and your unwavering dedication to mastering the art of CI/CD. Keep pushing boundaries, keep automating, and keep scaling new heights. The future is yours to conquer! ????


THANKYOU !



Vimal Daga

World Record Holder | 2x TEDx Speaker | Philanthropist | Sr. Principal Consultant | Entrepreneur | Founder LW Informatics | Founder Hash13 pvt ltd | Founder IIEC

1 年

Nice work

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了