A Java application usually reaches Kubernetes through a chain of artifacts: source code becomes a build output, the build output becomes a container image, and the image is described by Kubernetes objects that run and expose it. The migration path is less about one command and more about keeping those steps reproducible.

Migration flow from application code to Kubernetes

Kubernetes objects that matter in this path

A Pod is the smallest deployable unit. It can hold one container or a group of containers, and containers in the same Pod share a network namespace. Pods are ephemeral, so they should not be treated as durable hosts.

Controllers sit above Pods and manage them. Deployment handles stateless application rollout; StatefulSet is for stateful workloads; DaemonSet ensures the same Pod runs on every Node; Job runs a one-off task; CronJob runs tasks on a schedule.

A Service gives a stable access point to a set of Pods and defines access policy, preventing callers from losing track of Pods as they are replaced. Labels are attached to resources for association, querying, and filtering. Namespaces provide logical isolation between objects.

Build the project image

Prepare the WAR and database

Unpack the demo archive:

yum install -y unzip && unzip tomcat-java-demo-master.zip

Unpacked demo project

Import the SQL file into MySQL.

SQL file for the demo

On Node1, pull the mysql:5.6 image and start a container:

docker run -p 3306:3306 --name mysql-master \
-v /mydata/mysql/master/log:/var/log/mysql \
-v /mydata/mysql/master/data:/var/lib/mysql \
-v /mydata/mysql/master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.6

MySQL container running

Then import the SQL data. A database tool is the convenient route.

Importing SQL through a tool

Adjust the configuration files in the code so the application points at the database.

Application configuration

Database connection settings

Compile the Java application

Install JDK and Maven:

yum install -y java-1.8.0-openjdk maven

Change the Maven mirror:

vim /etc/maven/settings.xml
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

Maven mirror configuration

Compile the source; this can take a while:

mvn clean package -D maven.test.skip=true

Maven build output

Create the Docker image

Use a Dockerfile to build the image:

Dockerfile for the Java demo

FROM lizhenliang/tomcat
LABEL maintainer www.ctnrs.com
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war

Build it:

docker build -t lizhenliang/java-demo -f Dockerfile .

Docker image build in progress

The build completed successfully.

Successful image build

Deploy the image to Kubernetes

Generate a starting YAML template and then edit it:

kubectl create deployment java-demo --image=lizhenliang/java-demo --dry-run -o yaml
# content
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: java-demo
  name: java-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: java-demo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: java-demo
    spec:
      containers:
      - image: lizhenliang/java-demo
        name: java-demo
        resources: {}
status: {}

Write it to a local file:

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

After editing the replica count, the file can look like this:

[root@master ~]# cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: java-demo
  name: java-demo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: java-demo
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: java-demo
    spec:
      containers:
      - image: lizhenliang/java-demo
        name: java-demo

Apply it:

kubectl apply -f deploy.yaml

Deployment applied

Check the Pods:

kubectl get pods

Pods created by the Deployment

At this point the Java image is running on the Kubernetes platform.

Java application deployed to Kubernetes