简体   繁体   中英

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?

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.

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.

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:

See also:


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM