Cloud Native 15 min read

How to Migrate a Project to Kubernetes: Step‑by‑Step Guide

This article walks through the complete process of moving a project to a Kubernetes platform, covering image creation, pod controller management, data persistence, service exposure, external publishing, CI/CD integration, and detailed command‑line examples for building, pushing, and deploying Docker images.

Efficient Ops
Efficient Ops
Efficient Ops
How to Migrate a Project to Kubernetes: Step‑by‑Step Guide

1. Build Image (three steps)

Base image – the underlying OS such as CentOS 7.

Middleware image – e.g., nginx or tomcat service image.

Project image – the application packaged on top of the service image.

Operations staff usually prepare these images in advance so developers can directly use them, ensuring they match the target environment.

2. Controller Manages Pods

Kubernetes deploys the image using controllers; the most common is a Deployment.

Deployment – stateless workloads.

StatefulSet – stateful workloads with stable network ID and storage.

DaemonSet – runs a copy on every node.

Job & CronJob – batch processing.

Stateful pods have identity (network ID, storage) and require ordered start/stop.

3. Pod Data Persistence

Container data can be classified into three categories:

Initial configuration files needed at startup.

Temporary data generated during runtime that must be shared between containers.

Persistent data that must survive pod restarts.

4. Expose Application

A Deployment alone cannot be accessed from outside the cluster; a Service provides a stable ClusterIP and load‑balancing.

Service defines a logical set of Pods and the access policy.

It solves dynamic pod IP changes by offering service discovery and load balancing.

CoreDNS resolves Service names.

5. Publish Application Externally

Ingress complements Service by providing Layer‑7 routing and a unified entry point for multiple services, allowing users to access applications via domain names.

Practical Example: Deploy a Java Tomcat Application

Install OpenJDK and Maven, then build the project and create a Dockerfile that copies the built WAR into a Tomcat image.

yum -y install java-1.8.0-openjdk.x86_64 maven
java -version
# output shows OpenJDK 1.8.0_222
FROM lizhenliang/tomcat
LABEL maintainer zhaochengcheng
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war

Configure Maven mirrors for faster dependency download, then build the WAR package.

vim /etc/maven/settings.xml
<mirror>
  <id>central</id>
  <mirrorOf>central</mirrorOf>
  <name>aliyun maven</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>
mvn clean package -D maven.test.skip=true

Build the Docker image and push it to a private Harbor registry.

docker build -t 192.168.30.24/library/java-demo:latest .
docker login 192.168.30.24
# (error shown – need to add the registry to insecure‑registries on each node)
vim /etc/docker/daemon.json
{
  "registry-mirrors": ["http://f1361db2.m.daocloud.io"],
  "insecure-registries": ["192.168.30.24"]
}
docker push 192.168.30.24/library/java-demo:latest

Create a namespace, a Docker registry secret, and a Deployment that references the image.

apiVersion: v1
kind: Namespace
metadata:
  name: test
---
apiVersion: v1
kind: Secret
metadata:
  name: registry-pull-secret
  namespace: test
type: kubernetes.io/dockerconfigjson
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: tomcat-java-demo
  namespace: test
spec:
  replicas: 3
  selector:
    matchLabels:
      project: www
      app: java-demo
  template:
    metadata:
      labels:
        project: www
        app: java-demo
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: tomcat
        image: 192.168.30.24/tomcat-java-demo/java-demo:latest
        ports:
        - containerPort: 8080

Expose the Deployment with a Service and an Ingress.

apiVersion: v1
kind: Service
metadata:
  name: tomcat-java-demo
  namespace: test
spec:
  selector:
    project: www
    app: java-demo
  ports:
  - name: web
    port: 80
    targetPort: 8080
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-java-demo
  namespace: test
spec:
  rules:
  - host: java.maidikebi.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-java-demo
          servicePort: 80

After creating the resources, the application can be accessed via the Ingress host (or by adding the host entry to /etc/hosts for testing).

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.

JavaDockerci/cdDeploymentKubernetesServiceIngress
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

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.