Service discovery and distributed configuration with Amazon Elastic Kubernetes Service (EKS)
Service discovery and distributed configuration in a Micronaut application with Amazon Elastic Kubernetes Service (EKS)
On this guide
In this section
Getting Started
In this guide, we will deploy three microservices on the Amazon Elastic Kubernetes Service (EKS). We will use Kubernetes service discovery and distributed configuration to wire up our microservices.
You will discover how the Micronaut framework eases Kubernetes integration and deployment to ECR.
What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 17 or greater installed with
JAVA_HOMEconfigured appropriately -
An AWS account with:
-
An existing EKS cluster. You can follow the Getting started with Amazon EKS – AWS Management Console and AWS CLI guide.
Some of the following commands use jq
jq is a lightweight and flexible command-line JSON processor
The Application
Prepare and Deploy Microservices
We will define some environment variables to make deployment easier. In AWS_ACCOUNT_ID, store your AWS account ID. In AWS_REGION, store your AWS region. You can find both by executing commands in the AWS CLI. We will store our EKS cluster name in EKS_CLUSTER_NAME. For this guide, we will use micronaut-k8s.
export AWS_ACCOUNT_ID="$(aws sts get-caller-identity --query \"Account\" --output text)"
export AWS_REGION="$(aws configure get region)"
export EKS_CLUSTER_NAME="micronaut-k8s"Run the next command to log in to Amazon Elastic Container Registry (Amazon ECR):
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.comUsers Microservice
Edit k8s.yml inside users service.
Create a container repository in your AWS account.
export USERS_REPOSITORY=$(aws ecr create-repository --repository-name users --image-scanning-configuration scanOnPush=true --region us-east-1 --output json | jq -r .repository.repositoryUri)Build a Docker image of the users service with the name users.
|
Note
|
Ensure that Docker images are constructed for the correct CPU architecture. For instance, if you’re using Apple Silicon (aarch64), you can modify the DOCKER_DEFAULT_PLATFORM environment variable to the value linux/amd64. Alternatively, you can use ARM (aarch64) instances within your Kubernetes cluster.
|
Tag an existing users microservice image.
docker tag users:latest ${USERS_REPOSITORY}:latestPush the tagged users microservice image to the remote repository.
docker push ${USERS_REPOSITORY}:latestOrders Microservice
Edit k8s.yml inside orders service.
Create a container repository in your AWS account.
export ORDERS_REPOSITORY=$(aws ecr create-repository --repository-name orders --image-scanning-configuration scanOnPush=true --region us-east-1 --output json | jq -r .repository.repositoryUri)Build a Docker image of the orders service with the name orders.
|
Note
|
Ensure that Docker images are constructed for the correct CPU architecture. For instance, if you’re using Apple Silicon (aarch64), you can modify the DOCKER_DEFAULT_PLATFORM environment variable to the value linux/amd64. Alternatively, you can use ARM (aarch64) instances within your Kubernetes cluster.
|
Tag an existing orders microservice image.
docker tag orders:latest ${ORDERS_REPOSITORY}:latestPush the tagged orders microservice image to the remote repository.
docker push ${ORDERS_REPOSITORY}:latestAPI Microservice
Edit k8s.yml inside api service.
Create a container repository in your AWS account.
export API_REPOSITORY=$(aws ecr create-repository --repository-name api --image-scanning-configuration scanOnPush=true --region us-east-1 --output json | jq -r .repository.repositoryUri)Build a Docker image of the api service with the name api.
|
Note
|
Ensure that Docker images are constructed for the correct CPU architecture. For instance, if you’re using Apple Silicon (aarch64), you can modify the DOCKER_DEFAULT_PLATFORM environment variable to the value linux/amd64. Alternatively, you can use ARM (aarch64) instances within your Kubernetes cluster.
|
Tag an existing api microservice image.
docker tag api:latest ${API_REPOSITORY}:latestPush the tagged api microservice image to the remote repository.
docker push ${API_REPOSITORY}:latestDeploy Services to EKS
Create a directory for kubectl configuration.
mkdir -p $HOME/.kubeGenerate kubectl configuration for authentication to EKS.
aws eks update-kubeconfig --region $AWS_REGION --name $EKS_CLUSTER_NAMESet KUBECONFIG to the created config file. This variable is consumed by kubectl.
export KUBECONFIG=$HOME/.kube/configkubectl apply -f auth.ymlRun the next command to deploy the users microservice:
kubectl apply -f users/k8s.ymlRun the next command to deploy the orders microservice:
kubectl apply -f orders/k8s.ymlRun the next command to deploy the api microservice:
kubectl apply -f api/k8s.ymlTest integration between applications deployed on EKS
Run the next command to check status of the pods and make sure that all of them have the status "Running":
kubectl get pods -n=micronaut-k8sNAME READY STATUS RESTARTS AGE
api-6fb4cd949f-kxxx8 1/1 Running 0 2d1h
orders-595887ddd6-6lzp4 1/1 Running 0 2d1h
users-df6f78cd7-lgnzx 1/1 Running 0 2d1hRun the next command to check the status of the microservices:
kubectl get services -n=micronaut-k8sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
api LoadBalancer 10.100.208.154 <redacted> 8080:31171/TCP 30s
orders NodePort 10.100.157.155 <none> 8080:30742/TCP 20m
users NodePort 10.100.126.97 <none> 8080:31580/TCP 20m|
Note
|
If EXTERNAL-IP is in the <pending> state, wait a couple of seconds and then run the command again. AWS will provide you with a hostname instead of an IP address. To access your application, you will have to wait until DNS can resolve the hostname that was created.
|
Run the next command to retrieve the URL of the api microservice:
export API_URL=http://$(kubectl get svc api -n=micronaut-k8s -o json | jq -r '.status.loadBalancer.ingress[0].hostname'):8080Run a cURL command to create a new user via the api microservice:
curl -X "POST" "$API_URL/api/users" -H 'Content-Type: application/json; charset=utf-8' -d '{ "first_name": "Nemanja", "last_name": "Mikic", "username": "nmikic" }'{"id":1,"username":"nmikic","first_name":"Nemanja","last_name":"Mikic"}Run a cURL command to create a new order via the api microservice:
curl -X "POST" "$API_URL/api/orders" -H 'Content-Type: application/json; charset=utf-8' -d '{ "user_id": 1, "item_ids": [1,2] }'{"id":1,"user":{"first_name":"Nemanja","last_name":"Mikic","id":1,"username":"nmikic"},"items":[{"id":1,"name":"Banana","price":1.5},{"id":2,"name":"Kiwi","price":2.5}],"total":4.0}Run a cURL command to list created orders:
curl "$API_URL/api/orders" -H 'Content-Type: application/json; charset=utf-8'[{"id":1,"user":{"first_name":"Nemanja","last_name":"Mikic","id":1,"username":"nmikic"},"items":[{"id":1,"name":"Banana","price":1.5},{"id":2,"name":"Kiwi","price":2.5}],"total":4.0}]We can try to place an order for a user who doesn’t exist (with id 100). Run a cURL command:
curl -X "POST" "$API_URL/api/orders" -H 'Content-Type: application/json; charset=utf-8' -d '{ "user_id": 100, "item_ids": [1,2] }'{"message":"Bad Request","_links":{"self":[{"href":"/api/orders","templated":false}]},"_embedded":{"errors":[{"message":"User with id 100 doesn't exist"}]}}Cleaning Up
To delete all resources that were created in this guide, run the next command.
kubectl delete namespaces micronaut-k8sRun the next command to delete the micronaut-k8s/users artifacts container repository:
aws ecr delete-repository --repository-name users --forceRun the next command to delete the micronaut-k8s/orders artifacts container repository:
aws ecr delete-repository --repository-name orders --forceRun the next command to delete the micronaut-k8s/api artifacts container repository:
aws ecr delete-repository --repository-name api --forceNOTE: If you were following the Getting started with Amazon EKS – AWS Management Console and AWS CLI and you want to clean up everything, don’t forget to follow Step 5: Delete resources chapter.
Next Steps
Explore more features with Micronaut Guides.
Read more about Micronaut Kubernetes module.
Read more about Amazon Elastic Kubernetes Service (EKS)
License
|
Note
|
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |