# Deploy Jenkins in the Kubernetes cluster

## Deploy Jenkins in the Kubernetes cluster

Deploying **Jenkins** on a Kubernetes cluster can be quite beneficial for managing CI/CD pipelines efficiently. Here's a general guide to get you started:

## **Prerequisites**:

* You should have a running Kubernetes cluster.
* **kubectl** should be configured to connect to your cluster.

### Check Kubernetes cluster and nodes

```
kubectl get nodes
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fr7afva8KvP86hOVs0KA4%2Fimage.png?alt=media&#x26;token=fd8ddbab-fe3e-4b2e-99c6-502ca44b29b9" alt=""><figcaption></figcaption></figure>

## Setup Jenkins deployment

### Create the Jenkins directory and navigate the directory

```
mkdir jenkins
cd jenkins
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FGOlRjOK8vUTvtWIaf013%2Fimage.png?alt=media&#x26;token=350a535b-d7b6-4958-97f6-7be6e5c5a398" alt=""><figcaption></figcaption></figure>

### Create a Jenkins deployment YAML file

Create a YAML file called jenkins-deployment.yaml with the following content

```
vi  jenkins-deployment.yaml 
```

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        ports:
        - name: http-port
          containerPort: 8080
        - name: jnlp-port
          containerPort: 50000
        volumeMounts:
        - name: jenkins-vol
          mountPath: /var/jenkins_home
      volumes:
      - name: jenkins-vol
        emptyDir: {}
```

### Create a Jenkins service YAML file

Expose the Jenkins deployment using a **NodePort** service:

```
vi jenkins-service.yaml
```

```
apiVersion: v1
kind: Service
metadata:
  name: jenkins
spec:
  selector:
    app: jenkins
  ports:
  - name: http-port
    port: 8080
    targetPort: 8080
    nodePort: 30000
  type: NodePort
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FzS5iyPGcvjFFTjESQzx5%2Fimage.png?alt=media&#x26;token=28dec054-b410-46a6-8c59-d0a9ebe1e64f" alt=""><figcaption></figcaption></figure>

### Apply the YAML file to create the Jenkins service:

```
kubectl apply -f jenkins-deployment.yaml 
```

```
kubectl apply -f jenkins-service.yaml
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F5E8xlsdsItqw8qGB0ELN%2Fimage.png?alt=media&#x26;token=0694db27-a248-451a-9b10-bad1f579bcae" alt=""><figcaption></figcaption></figure>

Check the service's&#x20;

```
kubectl get all
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FZOzx4LWJ5NIZBi0EXEW6%2Fimage.png?alt=media&#x26;token=52d5b8e6-3b51-4e8f-bed2-af93cdb288d6" alt=""><figcaption></figcaption></figure>

To verify if the Jenkins service is running from your terminal, execute the following command:

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FN7Vt9UAFTxjEkqWVNOK8%2Fimage.png?alt=media&#x26;token=416d4c2e-e30e-4b7a-a151-288667764c30" alt=""><figcaption></figcaption></figure>

### **Access Jenkins:**

Access Jenkins using the external IP and NodePort:

**Navigate to the NeevCloud panel > Server > Select your node > copy Public\_IP**&#x20;

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FeWmwwL9X3GbPIK4uSwe0%2Fimage.png?alt=media&#x26;token=aa80a878-b46a-4053-99db-a7fc0403e923" alt=""><figcaption></figcaption></figure>

Check Node Port

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FENSeklWGjTIyrgik96bJ%2Fimage.png?alt=media&#x26;token=a4ac0671-a567-4105-a3d8-a2d5d2bbe2f4" alt=""><figcaption></figcaption></figure>

**<http://Node\\_IP>: Nodeport**

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FFw3ATR2TDrjaT8fQMUVn%2Fimage.png?alt=media&#x26;token=d6a34d52-693f-4b56-a893-bd2f62446715" alt=""><figcaption></figcaption></figure>

Run this path on your terminal using the sudo (root user)

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FyGl5n1CGG9dV4j8buHsO%2Fimage.png?alt=media&#x26;token=52c41dc1-192e-4445-a22b-f8a220087249" alt=""><figcaption></figcaption></figure>
