Create Microservices Application
This article explains how to create microservices applications in AAC. Before continuing with this document, you need to read the Create docker application article first.
How Plexus manages microservices
Microservices are applications with several containers. Plexus executes them on Kubernetes clusters, on the same pod; this means every container in the application runs under the same namespace. Therefore, container services can communicate through the internal network by using localhost.
Plexus also exposes to public the service ports configured on the application. See Create application below.
Public endpoints dependence
In most cases, there are services, such as front end, that need to be configured with the public endpoint of any other service running in the same application.
Since the endpoints are dynamically allocated, Plexus offers the possibility to read the service public endpoints from inside the container.
These endpoints are stored as variables in the file with path given by the $PLEXUS_WORKLOAD_ENDPOINTS
environment variable. This variable will be available on the containers once the interactive endpoints are opened, also a message will be shown on the system logs (see image below).
Endpoint variables
$PLEXUS_WORKLOAD_ENDPOINTS
contains the variables that stores the public host name, port and url related to every required port configured on the application. The name of the variables are build by using the port names. In the example:
Port named backend
have the following variables:
PLEXUS_BACKEND_PORT
PLEXUS_BACKEND_URL
Port named ui
has the following variables:
PLEXUS_UI_PORT
PLEXUS_UI_URL
In addition, the public host is always exposed in the same variable name PLEXUS_HOST
.
Create application
After reading the Create Docker Applications article, you will have enough knowledge to directly configure the application to communicate services in the same workload throght the public endpoints.
General application as microservice.
On the general information, configure Service to true and include the ports required for your application.
In the following example, the application configures the backend and ui endpoints. Although the actual endpoint required by users is the ui, we need to expose also the backend endpoint to allow ui communication with it.
Click NEXT, ignore Settings and click again NEXT to be located in containers.
Configure several containers
The system allows to configure several containers with their own ruscript and resources.
Container runscripts
Every container has their own container scripts. There are 3 configurable script: pre-runscript
to include any prior lines before the application command that should be included in runscript
, latest the postrunscript does not apply to service applications (just batch).
The container with services that depends on any public endpoint from the applicatin must have some script to wait for that public endpoint by using the $PLEXUS_WORKLOAD_ENDPOINTS
variable. It should be included on the pre-runscript.
In the following example, the ui
service container waits for the public endpoint of the backend service to become available before proceeding.
cd /workdir
while true
do
source $PLEXUS_WORKLOAD_ENDPOINTS > /dev/null 2>&1
if [[ $? -gt 0 ]]; then
echo "Environment variables not ready"
elif [ -z "${PLEXUS_BACKEND_PORT}" ]; then
echo "Environment variable PLEXUS_BACKEND_PORT is unset or empty."
else
break
fi
sleep 15
done
echo "Environment variables READY"
export CODEGEN_BACKEND_SERVICE_PORT=$PLEXUS_BACKEND_PORT
export EXTERNAL_HOST_IP=$PLEXUS_HOST
export BASIC_URL=http://${EXTERNAL_HOST_IP}:${CODEGEN_BACKEND_SERVICE_PORT}/v1/codegen
export BACKEND_SERVICE_ENDPOINT=$BASIC_URL
Review and create
After adding as many containers in the application as needed, and click NEXT.
Review the application details and click SAVE. Remember you will be able to edit your own applications.