~ 5 minutes read
no time? jump straight to the screencast
Continuous Delivery to Kubernetes with jib, skaffold and ktor describes how to continuously deliver every ktor/kotlin code change to Kubernetes.
This write up describes how to continuously deliver ktor/kotlin code changes of the same repository to an EKS–Kubernetes cluster on AWS within seconds.
The Kubernetes infrastructure for this one is AWS based. The canonical way to create an EKS cluster is to use eksctl:
eksctl create cluster ... . [1]
Once this is done, let’s create some ENV variables:
ENDPOINT_URL=$(aws eks describe-cluster --name $CLUSTER_NAME \
--query cluster.endpoint --output text)
CA_CERT=$(aws eks describe-cluster --name $CLUSTER_NAME \
--query cluster.certificateAuthority.data --output text)
AWS_PROFILE=test
AWS_REGION=$(aws configure get region)
Let’s makes sure a local .kube directory exists:
mkdir -p ~/.kube
You can now create a kubectl setup, that points to the EKS cluster. The KUBECONFIG environment variable is used to hold the kubeconfig file.
cat << KBCFG > ~/.kube/config-${CLUSTER_NAME}
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: $CA_CERT
server: $ENDPOINT_URL
name: $CLUSTER_NAME
contexts:
- context:
cluster: $CLUSTER_NAME
user: $CLUSTER_NAME
name: $CLUSTER_NAME
current-context: $CLUSTER_NAME
kind: Config
preferences: {}
users:
- name: $CLUSTER_NAME
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
args:
- --region
- $AWS_REGION
- eks
- get-token
- --cluster-name
- $CLUSTER_NAME
command: aws
env:
- name: AWS_PROFILE
value: $AWS_PROFILE
KBCFG
export KUBECONFIG=$KUBECONFIG:~/.kube/config-${CLUSTER_NAME}
The following AWS cli commands use AWS cli version 2 .
To verify the aws cli version use:
aws --version
Now, let’s login into ECR to be able to fetch docker images from AWS docker registry.
echo $(aws ecr get-login-password)|docker login --password-stdin \
--username AWS https://$(aws sts get-caller-identity \
--query 'Account' --output text).dkr.ecr.${AWS_REGION}.amazonaws.com
In case no ecr repository is in place yet, you can create one e.g. aws ecr create-repository --repository-name ... .
The next step is to identify the ECR repository URI:
export ECRREPO_NAME=[your ecr repo]
export ECRREPO_URI=$(aws ecr describe-repositories \
--repository-names $ECRREPO_NAME \
--query 'repositories[0].repositoryUri' --output text)
Create a kubectl secret to access the ECR repo:
export KUBE_SECRET_LABEL=$(aws sts get-caller-identity \
--query 'Account' --output text)\
--${AWS_REGION}--${AWS_PROFILE}--ecr--registry--secret
export KUBE_SECRET_PASSWORD=$(aws ecr get-authorization-token \
--output text --query 'authorizationData[0].authorizationToken' \
| base64 -d | cut -d: -f2)
KUBE_SECRET_EMAIL=[me@you.com]
# configure kubectl to log into ECR
kubectl delete secret --ignore-not-found $KUBE_SECRET_LABEL
kubectl create secret docker-registry $KUBE_SECRET_LABEL \
--docker-server=https://${ECRREPO_URI} \
--docker-username=AWS \
--docker-password="${KUBE_SECRET_PASSWORD}" \
--docker-email="${KUBE_SECRET_EMAIL}"
Let’s use a seperate Kubernetes namespace:
export KTORJIB_K8S_NAMESPACE=[your namespace]
kubectl create namespace ${KTORJIB_K8S_NAMESPACE}
Afterwards define the skaffold configuration in skaffold.yaml:
cat << SKFLDCFG > skaffold.yaml
apiVersion: skaffold/v2beta2
kind: Config
build:
artifacts:
- image: ${ECRREPO_URI}
jib: {}
SKFLDCFG
Start skaffold with skaffold dev that monitors source code changes and triggers Kubernetes deployments.
Screencast
Deploy Kotlin Application to EKS Kubernetes without Dockerfiles
This setup enables continuous delivery of every ktor/kotlin code change to Kubernetes. You can find the source code in github.com/lotharschulz/ktorjib. Any other kotlin source code that uses jib & skaffold with gradle jib plugin or maven jib plugin can be continuously delivered to Kubernetes in seconds as well.
Links
- https://github.com/stelligent/skaffold_on_aws
- https://github.com/aws-samples/aws-microservices-deploy-options/blob/master/skaffold.md
Leave a Reply