Key takeaway
- Deploy a Java microservice on Kubernetes: build the image, write Deployment and Service manifests, roll out safely, verify, troubleshoot and monitor it.
Deploying a Java microservice on Kubernetes means packaging the service as a container image, publishing it to a registry, and describing the desired state in a Deployment and a Service so the cluster runs and updates it for you. The steps are the same for any Java service: build, publish, declare, roll out, verify and monitor.
This guide is for Java developers and platform engineers who are moving an existing application into containers and want a clear path from source code to a running, observable service. It also flags two habits from older tutorials: manifests written for outdated API versions and storage tied to one product.
What do you need before you deploy?
Container platforms such as Docker Swarm, Mesos and AWS ECS exist, but this guide uses Kubernetes, which automates the deployment and management of containerised applications. Gather the following first.
| Requirement | Why you need it |
|---|---|
| A Kubernetes cluster (Minikube for testing) | Runs your pods and keeps them at the desired state |
| kubectl | The command line tool that manages a cluster, locally or remotely |
| Java application source and a Dockerfile | Describes how the service is built into an image |
| A container registry | Stores the image so cluster nodes can pull it; options include Docker Hub, AWS ECR and Google Artifact Registry |
| Persistent storage (only if the service writes files) | Keeps data after a pod is replaced; skip it for stateless services |
Most microservices should be stateless and keep their data in a database. Attach storage only when a service must keep documents, images or similar files. For the delivery pipeline around this, see our guide to continuous integration and continuous delivery.
How do you containerise a Java service?
Write a Dockerfile that builds the application with Maven and runs the resulting jar. The sketch below is deliberately small: pick base images that your security team approves and a Java version that is still supported.
FROM maven:3-eclipse-temurin-21 AS build
WORKDIR /usr/src/app
COPY . .
RUN mvn clean package
FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=build /usr/src/app/target/your-app.jar app.jar
EXPOSE 7102
CMD ["java", "-jar", "app.jar"]
Build the image with docker build -t your-app:1.0 ., log in to your registry with docker login, then tag and push it with docker tag and docker push, using your registry account and repository name. Use a version tag for every build so a rollout and a rollback always point at a known image.
How do you write the Deployment and Service manifests?
The Kubernetes documentation says a Deployment manages a set of pods to run an application workload and provides declarative updates for pods and ReplicaSets. You describe the desired state, and the Deployment controller changes the actual state to match it at a controlled rate. The documentation example uses apiVersion: apps/v1. Older guides that use extensions/v1beta1 should be updated to apps/v1.
apiVersion: apps/v1
kind: Deployment
metadata:
name: your-app
labels:
app: your-app
spec:
replicas: 2
selector:
matchLabels:
app: your-app
template:
metadata:
labels:
app: your-app
spec:
containers:
- name: your-app
image: your-registry/your-app:1.0
ports:
- containerPort: 7102
---
apiVersion: v1
kind: Service
metadata:
name: your-app
spec:
type: NodePort
selector:
app: your-app
ports:
- port: 7102
The documentation explains that the selector tells the ReplicaSet which pods to manage, and warns against overlapping labels or selectors with other controllers, because overlapping controllers can conflict and behave unexpectedly. Apply both objects with kubectl apply -f. A NodePort Service suits a test cluster; production clusters usually put an ingress or load balancer in front.
How do rolling updates and storage behave?
| Topic | What the Kubernetes documentation says | What it means for a Java service |
|---|---|---|
| Rollout trigger | A rollout starts only when the pod template changes; scaling does not trigger one | Change the image tag in the template to release a new build |
| Rolling update defaults | By default at least 75% of desired pods stay up (25% max unavailable) and at most 125% run (25% max surge) | Capacity dips and rises slightly during a release; size replicas with that in mind |
| Rollback | You can roll back to an earlier revision if the current state is not stable | Keep versioned image tags so a rollback has a clear target |
| Volumes | Ephemeral volume types have a lifetime tied to a pod, while persistent volumes exist beyond any single pod; data in an emptyDir is deleted when the pod is removed | Use a persistent volume claim for files that must survive a pod being replaced |
Many older tutorials attach a GlusterFS volume. It does not appear in the current Kubernetes list of volume types, so choose a storage class or CSI-backed volume offered by your platform instead.
How do you verify and troubleshoot the deployment?
Run kubectl get pods in your namespace to see each pod and its status, and kubectl get svc to find the Service port. Then open the service address in a browser or call its health endpoint. If something is wrong, work through the same short list every time:
- Check the pod status and recent events with
kubectl describe pod. - Read the container logs with
kubectl logs. - Confirm the Service selector matches the pod labels and the port matches the container port.
- Check that the application has the configuration, secrets and downstream services it expects.
How do you monitor Java microservices on Kubernetes?
Prometheus is an open source monitoring system that scrapes metrics from services over HTTP and stores them as time series. Grafana turns those series into dashboards and alert rules that notify people when a state changes. For Java services, expose metrics from the application, scrape them with Prometheus, and build dashboards per service. Our use case on monitoring Java microservices covers this in more depth, and Java microservices for scalable applications covers the architecture side. For a Python equivalent, read microservices with Python on Kubernetes.
How do AI agents change Kubernetes deployments?
Agents can take over the repetitive parts of this workflow: drafting manifests from a service description, running the verification checks above, reading logs when a rollout stalls, and proposing a rollback. The risk is that an agent with cluster access can change production, so each action needs a clear policy, a human approval point for risky steps, and a record of what it did. ElixirClaw, the AI agents computing platform, is where XenonStack builds and runs agents for DevOps and automation work. Our DevOps consulting services and the AWS Jenkins pipeline use case show how the pipeline underneath fits together.
What should you do first?
- Containerise one stateless service and push a versioned image to your registry.
- Write a Deployment and Service with
apps/v1and an explicit selector, and apply them to a test cluster. - Practise a release and a rollback by changing the image tag.
- Add metrics scraping and one dashboard before adding a second service.
- Decide who approves production changes, whether made by a person or an agent.
To map one deployment decision your agents make, and the context and policy it depends on, book a Context Workshop. See what happens in the workshop.
Frequently asked questions
What is a Kubernetes Deployment?
A Deployment manages a set of pods to run an application workload and provides declarative updates for pods and ReplicaSets. You describe the desired state and the controller moves the cluster towards it.
Which apiVersion should a Deployment use?
Use apps/v1, as in the current Kubernetes documentation. Older guides that show extensions/v1beta1 should be updated.
What triggers a rolling update?
A rollout starts only when the pod template changes, for example a new image tag. Scaling the Deployment does not trigger one.
Do Java microservices need persistent volumes?
Only if the service must keep files after a pod is replaced. Stateless services should keep their data in a database instead.
How do you check a deployment worked?
List the pods and the Service with kubectl, confirm the pods are running, then call the service address. If it fails, check pod events, container logs, selectors and ports.
Follow XenonStack on LinkedIn: XenonStack on LinkedIn for new guides on agentic AI, data and platform engineering.
Ready to put AI agents to work? Explore ElixirClaw, the agentic OS platform to build, orchestrate and govern AI agents.
Explore the XenonStack platform family: ElixirData · ElixirClaw · ElixirHub · NexaStack · MetaSecure · Neural AI · AgentGRC · AgentSRE · AgentAnalyst · AgentInstruct · AgentRAI · AgentScrum · AgentSearch · AgentForce · AI Aviator · SAIF Aviator · CohortIQ · XenonStack AI · XenonStack UAE