Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins CD Pipeline to Kubernetes

I'm intending to have a CD Pipeline with Jenkins which takes my application, publishes a docker image to my private docker repository. I think I know how to do that.

What I'm unsure about it the Kubernetes part. I want to take that image and deploy it to my private Kubernetes cluster (currently 1 Master & 1 Slave).

Question: Does that Jenkins Slave which has kubectl and docker installed need to be part of the Kubernetes cluster in order to trigger a deployment? How can I trigger that deployment?

like image 627
Joschi Avatar asked Jul 17 '17 05:07

Joschi


3 Answers

Assuming that you have the following deployment in your cluster:

apiVersion: apps/v1beta1 # for versions before 1.6.0 use 
extensions/v1beta1
kind: Deployment
metadata:
   name: foobar-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: foobar-app
    spec:
      containers:
      - name: foobar
        image: foobar-image:v1
        ports:
        - containerPort: 80

You would have to somehow have Jenkins tell your Kubernetes master the following command:

kubectl set image deployment/foobar-deployment foobar=foobar-image:version

where version is the new verion you just created with Jenkins. This will automatically trigger a redeploy with this version.

As long as you have access to your Kubernetes master that has your cluster on it (via ssh or similar), you can just pass the above command. Don't forget to keep track of version when you pass this command.

like image 97
Lindsay Landry Avatar answered Oct 14 '22 10:10

Lindsay Landry


You can trigger the deployment from Yenkin using kubectl command. for Quick start copy kubernetes cluster admin.conf or $HOME/.kube/config file to Jenkin slave server. Then you can run kubectl like this.

 Kubectl  --kubeconfig=admin.conf create –f <deployment.yml>

Note:

This will give full admin access to cluster, for long tream your can create account with deployment role and use that account for deployment.

like image 29
sfgroups Avatar answered Oct 14 '22 11:10

sfgroups


I'm intending to have a CD Pipeline with Jenkins which takes my application, publishes a docker image to my private docker repository. I think I know how to do that.

That's right, this is the part we are all familiar with.

My advice is that you actually, don't need to do much more in CI.

What I'm unsure about it the Kubernetes part. I want to take that image and deploy it to my private Kubernetes cluster (currently 1 Master & 1 Slave).

It's hard to use CI reliably as a source of truth where you can track what's deployed where. What you can do instead is store app configuration (Deployment+Service YAML files) in a git repository and have git reconciliation operator that connects that repository to the cluster, you can even have multiple cluster setup this way.

Question: Does that Jenkins Slave which has kubectl and docker installed need to be part of the Kubernetes cluster in order to trigger a deployment? How can I trigger that deployment?

Some folks do run CI (such as Jenkins) in their Kubernetes clusters, and it is a legitimate approach, however, this means you have more things to run, and cut your self out from all the hosted CI options out there.

The approach that we've been practising for a while now, is called GitOps and we blogged about various benefits of this approach:

  • GitOps - Operations by Pull Request
  • The GitOps Pipeline - Part 2
  • GitOps Part 3 - Observability

See also:

  • Storing Secure Sealed Secrets using GitOps
  • Technical Overview.
  • Weave Flux: The VCS Reconciliation Operator Source Code on Github

Disclaimer: I am a Kubernetes contributor and Weaveworks employee. We build open-source and commercial tools that help people to get to production with Kubernetes sooner.

like image 25
errordeveloper Avatar answered Oct 14 '22 11:10

errordeveloper