Cloud Native 8 min read

Deploy a Java Application on Kubernetes: From Image Build to Service Exposure

This guide walks through Kubernetes fundamentals—including Pods, Controllers, Services, Labels, and Namespaces—and provides a step‑by‑step tutorial for migrating a Java project to a Kubernetes cluster, building Docker images, pushing to Docker Hub, creating deployments, and exposing the service.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Deploy a Java Application on Kubernetes: From Image Build to Service Exposure

Kubernetes Basic Concepts

Pod :

1. Smallest deployment unit,
2. A group of containers,
3. Containers in a pod share network namespace,
4. Lifecycle is short‑lived,

Controllers :

1. Deployment: deploy stateless apps
2. StatefulSet: deploy stateful apps
3. ReplicaSet: ensure desired pod replica count
4. DaemonSet: ensure a pod runs on each node
5. Job: one‑time tasks
6. CronJob: scheduled tasks
Higher‑level objects for deploying and managing pods

Service :

Prevents pod isolation, finds required pod
Defines load‑balancing access policy for a set of pods

Label :

Tag attached to a resource for association, query, and filtering

Namespaces :

Logical or resource isolation for objects

Project Migration to Kubernetes Process

Base image → runtime image (Python/PHP/Go/Java) → project image (packaged project)

Practical Project Steps

1. Import Java source package to the master server

192.168.106.102 – k8s-master

192.168.106.103 – k8s-node01

192.168.106.104 – k8s-node02

192.168.106.103 – MySQL database

2. Import database files into MySQL (node01)

# Execute on 192.168.106.103
source /root/tables_ly_tomcat.sql
granted all on test.* to 'test'@'%' identified by "Zhangfan@123";

Modify the application configuration to use the new database user and IP.

vim src/main/resources/application.yml

3. Build the Java Docker image on the master

cat Dockerfile
FROM centos:7
MAINTAINER zhangfan
ENV VERSION=8.5.61
RUN yum install -y java-1.8.0-openjdk wget curl unzip iproute net-tools && yum clean all && rm -rf /var/cache/yum/*
RUN wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz && \
    tar zxf apache-tomcat-${VERSION}.tar.gz && \
    mv apache-tomcat-${VERSION} /usr/local/tomcat && \
    rm -rf apache-tomcat-${VERSION}.tar.gz && \
    rm -rf /usr/local/tomcat/webapps/* && \
    mkdir /usr/local/tomcat/webapps/test && \
    echo "ok" > /usr/local/tomcat/webapps/test/status.html && \
    sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/tomcat/bin
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]

4. Install build tools and compile the project

yum -y install java-1.8.0-openjdk maven
cd /root/tomcat-java-demo-master
mvn clean package -Dmaven.test.skip=true

The build produces a WAR file.

5. Create a new Dockerfile for the project image

cat Dockerfile
FROM tomcat:v1
LABEL test www.zhangsan.com
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
# Build and tag the image
docker build -t zhangfan5391621/java-demo .

6. Push the image to Docker Hub

docker push zhangfan5391621/java-demo   # Upload to Docker Hub

7. Generate a Kubernetes Deployment YAML

kubectl create deployment java-demo --image=zhangfan5391621/java-demo --dry-run -o yaml > deploy.yaml

Sample deploy.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: java-demo
  name: java-demo
spec:
  replicas: 3
  selector:
    matchLabels:
      app: java-demo
  template:
    metadata:
      labels:
        app: java-demo
    spec:
      containers:
      - image: zhangfan5391621/java-demo
        name: java-demo
kubectl apply -f deploy.yaml   # Create the pods
kubectl get pods               # Verify all pods are Running

8. Expose the application via a Service

kubectl expose deployment java-demo --port=80 --target-port=8080 --type=NodePort -o yaml --dry-run > svc.yaml
kubectl apply -f svc.yaml
kubectl get pods,svc

Access the service at http://<master-ip>:<NodePort> (e.g., http://192.168.106.102:30187/).

The complete deployment order is: build environment image → build project image → push to Docker Hub → create Deployment → expose Service.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaDeploymentKubernetesDevOps
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.