jenkins use dynamic agent wit Kubernetes plugin or oc helm command line - unix1998/technical_notes GitHub Wiki

Both methods for deploying applications to OpenShift, EKS, AKS, GKS, and Kubernetes clusters have their merits and are used in the IT industry. The choice between using the Jenkins Kubernetes plugin and using Helm or kubectl/oc commands depends on the specific needs, preferences, and infrastructure of the organization. Here is a comparison of the two methods, along with their pros and cons:

1. Using Jenkins Kubernetes Plugin

Description

The Jenkins Kubernetes plugin allows Jenkins to dynamically provision and manage agent pods in a Kubernetes cluster. This plugin automates the creation and teardown of pods that act as Jenkins agents, where pipeline steps are executed.

Pros

  • Dynamic Scaling: Automatically scales Jenkins agents up and down based on the workload, optimizing resource utilization.
  • Seamless Integration: Provides seamless integration between Jenkins and Kubernetes, making it easier to run CI/CD pipelines that require Kubernetes resources.
  • Isolation: Each pipeline runs in an isolated pod, ensuring clean environments for builds and tests.
  • Efficient Resource Usage: Resources are only used when needed, reducing costs and avoiding resource contention.

Cons

  • Complexity: Requires additional setup and configuration for the Kubernetes plugin and associated credentials.
  • Maintenance: The plugin and its configurations need to be maintained, which can add overhead.
  • Dependency: Tightly couples the CI/CD process with Jenkins and the Kubernetes plugin, making migrations or changes more challenging.

Example Pipeline

def label = "mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, containers: [
  containerTemplate(name: 'maven', image: 'maven:3.6.3-jdk-11', ttyEnabled: true, command: 'cat'),
  containerTemplate(name: 'node', image: 'node:12-alpine', ttyEnabled: true, command: 'cat')
]) {
    node(label) {
        stage('Build') {
            container('maven') {
                sh 'mvn clean install'
            }
        }
        stage('Deploy') {
            container('node') {
                sh 'helm upgrade --install myapp ./mychart --namespace mynamespace'
            }
        }
    }
}

2. Using Helm or kubectl/oc Commands

Description

Helm is a package manager for Kubernetes that simplifies deployment, while kubectl and oc are command-line tools used to interact with Kubernetes and OpenShift clusters, respectively.

Pros

  • Flexibility: Provides flexibility to use any CI/CD tool, not just Jenkins, for deploying applications.
  • Standardization: Uses industry-standard tools (Helm, kubectl, oc) which are widely adopted and supported.
  • Simplicity: Simpler to set up and maintain compared to using the Jenkins Kubernetes plugin.
  • Granularity: Offers fine-grained control over Kubernetes resources and deployment processes.

Cons

  • Manual Scaling: Requires manual setup and scaling of CI/CD runners or agents.
  • Resource Management: Might need more careful resource management to avoid conflicts or resource exhaustion.
  • Less Integration: Lacks the deep integration that the Jenkins Kubernetes plugin offers, potentially requiring more scripting and custom solutions.

Example Pipeline

pipeline {
    agent any
    environment {
        KUBECONFIG = credentials('kubeconfig-id') // Assuming you have the kubeconfig stored in Jenkins credentials
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean package' // Example for a Maven build
            }
        }
        stage('Deploy') {
            steps {
                withCredentials([file(credentialsId: 'kubeconfig-id', variable: 'KUBECONFIG')]) {
                    sh 'helm repo update'
                    sh 'helm upgrade --install myapp ./mychart --namespace mynamespace'
                }
            }
        }
    }
}

Popularity and Industry Trends

In the North American IT industry, both methods are popular, but their usage often depends on the specific context and requirements of the organization:

  • Jenkins Kubernetes Plugin is popular in environments where Jenkins is heavily used for CI/CD and there is a need for dynamic and scalable build environments.
  • Helm/kubectl/oc Commands are widely used in scenarios where there is a preference for using standard Kubernetes tools, or where multiple CI/CD tools are in use.

Conclusion

  • Jenkins Kubernetes Plugin is great for environments that are heavily invested in Jenkins and need dynamic scaling of build agents. It provides strong integration but can add complexity.
  • Helm/kubectl/oc Commands offer a more flexible and simpler setup that can work with any CI/CD tool, providing greater control and standardization, but may require more manual management of build environments.

Ultimately, the choice depends on the specific needs, existing infrastructure, and preferences of the organization. Both approaches are viable and have their place in modern CI/CD workflows.