Skip to content

Instantly share code, notes, and snippets.

@kern
Last active August 14, 2019 09:19
Show Gist options
  • Save kern/816b86a7f0986aae4305 to your computer and use it in GitHub Desktop.
Save kern/816b86a7f0986aae4305 to your computer and use it in GitHub Desktop.

Kubernetes + AWS ECR = ❤️

Makefile and YAML templates for automating the use of AWS Elastic Container Registry with Kubernetes.

Based off of this awesome Redsaid blog post.

Requirements

  • Amazon ECR, along with your AWS account ID and the region your ECR is in
  • AWS CLI
  • Docker

Usage

The first time you're setting up ECR on the Kube cluster:

$ make install

To build, push, and deploy:

$ make deploy
apiVersion: v1
kind: Secret
type: kubernetes.io/dockerconfigjson
metadata:
name: ecr
data:
.dockerconfigjson: {{CONFIG}}
.PHONY: ecr-login install build push deploy
DOCKER_REPO ?= mycontainer
DOCKER_TAG ?= latest
AWS_ACCOUNT_ID ?= 0123456789
AWS_ECR_REGION ?= us-east-1
AWS_ECR_DOMAIN ?= $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_ECR_REGION).amazonaws.com
ecr-login:
aws ecr get-login --region $(AWS_ECR_REGION) | sh -
install: ecr-login
CONFIG="$(shell base64 < ~/.docker/config.json)"; \
sed -e 's/{{CONFIG}}/'$$CONFIG'/g' ecr.yaml | \
kubectl create -f -
sed -e 's/{{IMAGE}}/$(AWS_ECR_DOMAIN)\/$(DOCKER_REPO):$(DOCKER_TAG)/g' myapp.yaml | \
kubectl create -f -
build:
docker build -t $(DOCKER_TAG) .
push: build ecr-login
docker tag $(DOCKER_REPO):$(DOCKER_TAG) $(AWS_ECR_DOMAIN)/$(DOCKER_REPO):$(DOCKER_TAG)
docker push $(AWS_ECR_DOMAIN)/$(DOCKER_REPO):$(DOCKER_TAG)
deploy: push
CONFIG="$(shell base64 < ~/.docker/config.json)"; \
sed -e 's/{{CONFIG}}/'$$CONFIG'/g' ecr.yaml | \
kubectl replace -f -
kubectl rolling-update myapp --image $(AWS_ECR_DOMAIN)/$(DOCKER_REPO):$(DOCKER_TAG)
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp
spec:
replicas: 2
selector:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: {{IMAGE}}
ports:
- containerPort: 3000
imagePullSecrets:
- name: ecr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment