This is our environment:
- I have a Kubernetes cluster running on Amazon.
- Jenkins CI/CD running on Amazon that connects to a private GitLab and builds our services as Docker images.
- Amazon ECR that stores our Docker images.
My questions:
- How can I auto deploy images from ECR to Kubernetes (as pods) once the Jenkins pipeline pushes newly built images to ECR?
- Can I do that within Jenkins pipeline? I've read lots of material but I couldn't find how should it be done.
There is also a 3rd party tool like Keel but it doesn't support Amazon ECR (Webhook problem with ECR).
Any help would be appreciated.
I have a similar workflow and I hope this helps you get some direction. I am using bitbucket pipeline for CI, but I am sure Jenkins will work fine as well.
This is what I do in my CI flow :
my-cntnr:12
my-cntnr:12
)Here is the script for reference :
note: Since I am using Rancher, I can use Rancher API to update pods and their configuration.
Now for the ECR credentials part for Kubernetes, you have to create a secret ( a Kubernetes only entity), this secret is created using your AWS ECR details. Then you can use this secret in your pod.yml as image-pull-secret. This will tell k8 to use the secret and pull the image from ECR
I have a simple script to quickly do that.
And this is how you can use it in your pod.yml
I've written a detailed article about the process as well. Please find it here.
Yes, it is possible. You can use Amazon ECR for your Kubernetes cluster, but you need to create a secret with credentials. You can develop a pipeline for Jenkins which will automatically deploy updates to your Kubernetes cluster. For this, you need to configure a trigger, for example successful completing of the previous pipeline which builds your image. You may use kubernetes-plugin for developing your pipeline, or you can just call kubectl update with a new image tag. You can update your cluster using default update for deployment or 3rd party tool, for example helm.
*updates
There is a good doc: Using AWS EC2 Container Registry. For Jenkins and Kubernetes I can advise you to use kube2iam, it helps to avoid expiring of the authorization tokens. Create custom roles for Kubernetes nodes and for Jenkins, also don’t forget to add permission to assign the roles for kube2iam.
If you want to see how to automate CI/CD with multiple environments on Kubernetes using GitOps for promotion between environments and Preview Environments on Pull Requests you might wanna check out my recent talk on Jenkins X at DevOxx UK where I do a live demo of this on GKE. Though Jenkins X works on AWS, AKS and GKE and other kubernetes clusters too.
When you merge a change to the master branch, Jenkins X creates a new semantically versioned distribution of your app (pom.xml, jar, docker image, helm chart). The pipeline then automates the generation of Pull Requests to promote your application through all of the Environments via GitOps. You can then decide to rollback versions after the apps release pipeline has completed (as there's an environment pipeline too).