Deploy Kotlin code changes to Kubernetes in seconds

Deploy Kotlin code change to Kubernetes in seconds

~ 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 EKSKubernetes 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 --output text)
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
- cluster:
    certificate-authority-data: $CA_CERT
    server: $ENDPOINT_URL
- context:
    cluster: $CLUSTER_NAME
    user: $CLUSTER_NAME
current-context: $CLUSTER_NAME
kind: Config
preferences: {}
      - --region
      - $AWS_REGION
      - eks
      - get-token
      - --cluster-name
      command: aws
      - name: AWS_PROFILE
        value: $AWS_PROFILE

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}

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)\

export KUBE_SECRET_PASSWORD=$(aws ecr get-authorization-token \ 
--output text --query 'authorizationData[0].authorizationToken' \
| base64 -d | cut -d: -f2)


# 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}" \

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
    - image: ${ECRREPO_URI}
      jib: {}

Start skaffold with skaffold dev that monitors source code changes and triggers Kubernetes deployments.


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 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.



[1]: I used a similar AWS EKS setup as in ALB INGRESS Controller CrashLoopBackOffs in AWS EKS on FARGATE.

Be the first to comment

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.