Production Deployment of OpenTelemetry Demo
Prerequisites
Ensure you have the following installed:
- Docker
- Kubernetes (if deploying to a Kubernetes environment)
- Gradle for building Java components
Step 1: Building the Java Service
The first step involves building the Java-based services, specifically the fraud detection service. The provided Dockerfile will facilitate this process.
Open the Dockerfile located in the root directory of the project.
The Dockerfile contains two stages; the first stage builds the service using Gradle, and the second stage uses Distroless to create a smaller image for production.
FROM --platform=${BUILDPLATFORM} gradle:8-jdk17 AS builder
WORKDIR /usr/src/app/
COPY ./src/frauddetectionservice/ ./
COPY ./pb/ ./src/main/proto/
RUN gradle shadowJar
In this segment, the Gradle image is used to build the Java project. It copies the necessary files and builds the application via the shadowJar
task.
- The second stage utilizes a Distroless image for minimal overhead:
FROM gcr.io/distroless/java17-debian11
ARG OTEL_JAVA_AGENT_VERSION
WORKDIR /usr/src/app/
COPY --from=builder /usr/src/app/build/libs/frauddetectionservice-1.0-all.jar ./
ADD --chmod=644 https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v$OTEL_JAVA_AGENT_VERSION/opentelemetry-javaagent.jar /app/opentelemetry-javaagent.jar
ENV JAVA_TOOL_OPTIONS=-javaagent:/app/opentelemetry-javaagent.jar
ENTRYPOINT [ "java", "-jar", "frauddetectionservice-1.0-all.jar" ]
In this part, it sets up the Java agent for OpenTelemetry by downloading it and configuring it using the JAVA_TOOL_OPTIONS
environment variable.
Step 2: Building the Docker Image
Run the following command to build the Docker image. Ensure you have replaced your_tag
with the desired image name:
docker build -t your_tag .
Step 3: Deploying the Service
If deploying to Kubernetes, create a manifest (YAML file) for the service. Here’s an example of how to define a deployment for the fraud detection service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: fraud-detection-service
spec:
replicas: 3
selector:
matchLabels:
app: fraud-detection
template:
metadata:
labels:
app: fraud-detection
spec:
containers:
- name: fraud-detection
image: your_tag
ports:
- containerPort: 8080
env:
- name: OTEL_JAVA_AGENT_VERSION
value: "1.0.0" # Replace with the correct version
Step 4: Exposing the Service
To make the fraud detection service accessible, create a corresponding service definition as follows:
apiVersion: v1
kind: Service
metadata:
name: fraud-detection-service
spec:
type: LoadBalancer
ports:
- port: 8080
targetPort: 8080
selector:
app: fraud-detection
Step 5: Deploying to Kubernetes
Apply the deployment and service configurations using the kubectl
command:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Step 6: Verifying Deployment
Once the deployment is complete, verify the pods and services are running:
kubectl get pods
kubectl get services
You should see your fraud detection service running and accessible through the LoadBalancer IP provided by Kubernetes.
Step 7: Monitoring with OpenTelemetry
Ensure that your application is correctly instrumented with OpenTelemetry. The configuration in the Dockerfile for the Java agent takes care of this. You can monitor traces and metrics through your OpenTelemetry backend (e.g., Jaeger, Prometheus).
Step 8: CI/CD Integration (Optional)
If utilizing continuous integration/continuous deployment (CI/CD) practices, consider integrating the above Docker build and Kubernetes deployment commands into your build pipeline using your CI/CD tools of choice (e.g., Jenkins, GitHub Actions, GitLab CI).
This enables automatic deployment of your services whenever new changes are pushed to the repository.