Prerequisites
Ensure you have Docker and Docker Compose installed on your production server to facilitate containerized deployments.
Docker Compose Configuration
Create a docker-compose.yml
file to configure the services necessary for the application. This file outlines the services you need, including Zipkin for tracing, RabbitMQ for message brokering, a Web API, and a Worker Service.
Sample docker-compose.yml
version: '3.8'
services:
zipkin:
image: openzipkin/zipkin
ports:
- 9411:9411
rabbitmq:
image: rabbitmq:3-management-alpine
ports:
- 5672:5672
- 15672:15672
webapi:
build:
context: ../..
dockerfile: ./examples/MicroserviceExample/WebApi/Dockerfile
image: opentelemetry-example-webapi
environment:
- ASPNETCORE_ENVIRONMENT=Production
- RABBITMQ_HOSTNAME=rabbitmq
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest
- ZIPKIN_HOSTNAME=zipkin
ports:
- 5000:5000
depends_on:
- rabbitmq
- zipkin
workerservice:
build:
context: ../..
dockerfile: ./examples/MicroserviceExample/WorkerService/Dockerfile
image: opentelemetry-example-workerservice
environment:
- DOTNET_ENVIRONMENT=Production
- RABBITMQ_HOSTNAME=rabbitmq
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=guest
- ZIPKIN_HOSTNAME=zipkin
restart: on-failure
depends_on:
- rabbitmq
- zipkin
In this docker-compose.yml
, you can see configuration for Zipkin, RabbitMQ, your web API, and your worker service. It is crucial to set the environment variables accordingly for a production deployment.
Dockerfile Configuration
Each microservice should have a corresponding Dockerfile
that outlines how to build the application.
Sample Dockerfile
for Worker Service
ARG SDK_VERSION=8.0
FROM mcr.microsoft.com/dotnet/sdk:${SDK_VERSION} AS build
ARG PUBLISH_CONFIGURATION=Release
ARG PUBLISH_FRAMEWORK=net8.0
WORKDIR /app
COPY . ./
RUN dotnet publish ./examples/MicroserviceExample/WorkerService -c "${PUBLISH_CONFIGURATION}" -f "${PUBLISH_FRAMEWORK}" -o /out -p:IntegrationBuild=true
FROM mcr.microsoft.com/dotnet/aspnet:${SDK_VERSION} AS runtime
WORKDIR /app
COPY --from=build /out ./
ENTRYPOINT ["dotnet", "WorkerService.dll"]
This Dockerfile handles the building of your Worker Service.
- Build Stage: This stage uses the .NET SDK to publish your application in Release mode.
- Runtime Stage: This stage switches to the ASP.NET base image and sets the entry point for your application.
Deploying the Application
Make sure that the appropriate networking configurations and firewall rules are in place for your production environment.
Run the following command to start the services defined in your
docker-compose.yml
:docker-compose up -d
This command will build images for your web API and worker service and start all containers defined in the Docker Compose file. The
-d
flag runs the containers in detached mode.Confirm that all services are running smoothly:
docker-compose ps
Access the Zipkin UI at
http://<server-ip>:9411
to confirm trace collection.Access RabbitMQ Management Console at
http://<server-ip>:15672
using the default credentials (guest/guest).Monitor logs for each service by using:
docker-compose logs -f
This setup supports a production deployment of an OpenTelemetry .NET application, allowing you to trace and monitor microservices effectively.
References: docker-compose.yml
, Dockerfile
.