Giter Club home page Giter Club logo

ecr-agent's Introduction

Kubernetes ECR agent

This agent will update the specified Deployment / StatefulSet when it detects a new version of the image which they use has been pushed to ECR.

Problem

CI process using Codebuild and ECR

  1. The developer pushes their code to GitHub
  2. Github sends a webhook to Codebuild
  3. The Codebuild project builds the Docker image
  4. The Codebuild project pushes the Docker image to an ECR repository
  5. ?? How to tell Kubernetes cluster to update the tags in the Deployment/StatefulSet manifest ??

Solution

CI process using Codebuild and ECR with ECR Agent

  1. The developer pushes their code to GitHub
  2. Github sends a webhook to Codebuild
  3. The Codebuild project builds the Docker image
  4. The Codebuild project pushes the Docker image to an ECR repository
  5. The ECR Agent running in the Kubernetes cluster polls ECR
    1. When a new image is detected, the agent will update the manifest of the Deployment/StatefulSet
    2. Kubernetes will handle deploying the new pods and removing the old ones

How It Works

It works by polling ECR every INTERVAL milliseconds to see if an image has been pushed with a specified tag.

The idea is that your CI pipeline will build the Docker image and tag it with a unique version number and push it to ECR, then it will tag it with "production" (or whatever IMAGE_TAG is set to) and pushing that tag to ECR too.

The agent will then read the "production" image, find the other tag (for example "v1") and then look at the current image name for the K8s resource by making a request to the management API. If the version differs (for example, if the Deployment is using "v0.1") then the agent will update the Deployment manifest to use tag "v1" instead. Kubernetes will then manage replacing the pods.

Limitations

  • Currently one agent per ECR image / Kubernetes resource. (PRs welcome!)
  • Only Deployments and StatefulSets are supported
  • ECR images should only have two tags - any more will be ignored
  • Must pass in a Base64-encoded Kubeconfig as the "read from pod" code isn't working (PRs welcome!)

Usage

Local

First build the image:

docker build . -t ecr-agent:latest

Next run the image

docker run -it \
--env AWS_ACCESS_KEY_ID=<REDACTED> \
--env AWS_SECRET_ACCESS_KEY=<REDACTED> \
--env AWS_REGION=eu-west-1 \
--env K8S_CLUSTER_NAME=<REDACTED> \
--env K8S_KUBECONFIG=<REDACTED> \
--env INTERVAL=30000 \
--env IMAGE_TAG=latest \
--env ECR_REPOSITORY=sample-app \
--env RESOURCE_TYPE=deployment \
--env RESOURCE_NAME=sample-app \
ecr-agent:latest \
node bundle.js

On Cluster

Edit the environment variable values in ./ecr-agent.replicaset.yaml. Optionally specify your own hosted image.

Then create the ReplicaSet resource:

kubectl apply -f ecr-agent.replicaset.yaml

ecr-agent's People

Contributors

carlevans719 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.