Cloud Native 23 min read

How to Deploy a Spring Cloud Microservice Suite on Kubernetes: Step‑by‑Step Guide

This article provides a comprehensive, step‑by‑step tutorial for building, containerizing, and deploying a Spring Cloud microservice project on a Kubernetes cluster, covering environment preparation, Maven builds, Docker image creation, YAML orchestration, and verification of the frontend portal.

Ops Development Stories
Ops Development Stories
Ops Development Stories
How to Deploy a Spring Cloud Microservice Suite on Kubernetes: Step‑by‑Step Guide

1. Familiarize with Spring Cloud Microservice Project

Code branch description:

dev1 delivers code

dev2 writes Dockerfile to build images

dev3 creates K8s resources

dev4 monitors microservice links

master performs final release

2. Logical Architecture in K8s

Overall logical architecture diagram

Service exposure relationship diagram

3. Prepare Environment

A Kubernetes cluster (single‑master or multi‑master) with the following nodes:

IP

Role

Config

192.168.73.138

master

2C4G

192.168.73.139

node1

2C4G

192.168.73.140

node2

2C4G

192.168.73.137

harbor, mysql

1C2G

CoreDNS

Ingress Controller (refer to previous K8s setup files)

MariaDB database

Install MariaDB on the external host 192.168.73.137:

[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb && systemctl enable mariadb
[root@localhost ~]# mysql_secure_installation   # set password to 123456, press Enter for other prompts

4. Build Source Code

Install JDK 1.8 and Maven, configure a domestic Maven mirror, clone the project, and compile.

[root@k8s-master ~]# yum install java-1.8.0-openjdk maven -y
[root@k8s-master ~]# java -version
openjdk version "1.8.0_242"
[root@k8s-master ~]# mvn -version
Apache Maven 3.0.5 (Red Hat 3.0.5-17)
<mirror>
  <id>nexus-aliyun</id>
  <mirrorOf>*</mirrorOf>
  <name>Nexus aliyun</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
[root@k8s-master ~]# cd /opt
[root@k8s-master opt]# git clone https://github.com/lizhenliang/simple-microservice.git
[root@k8s-master simple-microservice]# mvn clean package -Dmaven.test.skip=true

5. Build Docker Images and Push to Registry

Example Dockerfile for the gateway service:

FROM lizhenliang/java:8-jdk-alpine
LABEL maintainer www.ctnrs.com
ENV JAVA_ARGS="-Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
COPY ./target/gateway-service.jar ./
COPY pinpoint /pinpoint
EXPOSE 9999
CMD java -jar -javaagent:/pinpoint/pinpoint-bootstrap-1.8.3.jar -Dpinpoint.agentId=$(echo $HOSTNAME | awk -F- '{print "gateway-"$NF}') -Dpinpoint.applicationName=ms-gateway $JAVA_ARGS $JAVA_OPTS /gateway-service.jar

Script docker_build.sh builds and pushes all service images to the Harbor registry (modify the registry address to your own):

#!/bin/bash
kubectl create ns ms
docker_registry=192.168.73.137
kubectl create secret docker-registry registry-pull-secret --docker-server=$docker_registry --docker-username=admin --docker-password=Harbor12345 [email protected] -n ms
service_list="eureka-service gateway-service order-service product-service stock-service portal-service"
service_list=${1:-${service_list}}
work_dir=$(dirname $PWD)
current_dir=$PWD
cd $work_dir
mvn clean package -Dmaven.test.skip=true
for service in $service_list; do
   cd $work_dir/$service
   if ls |grep biz &>/dev/null; then
      cd ${service}-biz
   fi
   service=${service%-*}
   image_name=$docker_registry/microservice/${service}:$(date +%F-%H-%M-%S)
   docker build -t ${image_name} .
   docker push ${image_name}
 done

6. K8s Service Orchestration

Gateway service uses a Deployment and is exposed via an Ingress.

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gateway
  namespace: ms
spec:
  rules:
    - host: gateway.ctnrs.com
      http:
        paths:
        - path: /
          backend:
            serviceName: gateway
            servicePort: 9999
---
apiVersion: v1
kind: Service
metadata:
  name: gateway
  namespace: ms
spec:
  ports:
  - port: 9999
    name: gateway
  selector:
    project: ms
    app: gateway
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway
  namespace: ms
spec:
  replicas: 2
  selector:
    matchLabels:
      project: ms
      app: gateway
  template:
    metadata:
      labels:
        project: ms
        app: gateway
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: gateway
        image: 192.168.73.137/microservice/gateway:2020-03-08-16-40-54
        ports:
        - containerPort: 9999
        env:
        - name: JAVA_OPTS
          value: "-Xmx1g"
        resources:
          requests:
            cpu: 0.5
            memory: 256Mi
          limits:
            cpu: 1
            memory: 1Gi
        readinessProbe:
          tcpSocket:
            port: 9999
          initialDelaySeconds: 60
          periodSeconds: 10
        livenessProbe:
          tcpSocket:
            port: 9999
          initialDelaySeconds: 60
          periodSeconds: 10

Product, stock, order, and portal services have similar Deployment/Service/Ingress YAML files (shown below).

# product.yaml (excerpt)
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: portal
  namespace: ms
spec:
  rules:
    - host: portal.ctnrs.com
      http:
        paths:
        - path: /
          backend:
            serviceName: portal
            servicePort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: portal
  namespace: ms
spec:
  ports:
  - port: 8080
    name: portal
  selector:
    project: ms
    app: portal
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: portal
  namespace: ms
spec:
  replicas: 1
  selector:
    matchLabels:
      project: ms
      app: portal
  template:
    metadata:
      labels:
        project: ms
        app: portal
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: portal
        image: 192.168.73.137/microservice/portal:2020-03-08-16-41-31
        ports:
        - containerPort: 8080
        env:
        - name: JAVA_OPTS
          value: "-Xmx1g"
        resources:
          requests:
            cpu: 0.5
            memory: 256Mi
          limits:
            cpu: 1
            memory: 1Gi
        readinessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 10
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 10
# eureka.yaml (excerpt)
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: eureka
  namespace: ms
spec:
  rules:
    - host: eureka.ctnrs.com
      http:
        paths:
        - path: /
          backend:
            serviceName: eureka
            servicePort: 8888
---
apiVersion: v1
kind: Service
metadata:
  name: eureka
  namespace: ms
spec:
  clusterIP: None
  ports:
  - port: 8888
    name: eureka
  selector:
    project: ms
    app: eureka
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: eureka
  namespace: ms
spec:
  replicas: 3
  selector:
    matchLabels:
      project: ms
      app: eureka
  serviceName: "eureka"
  template:
    metadata:
      labels:
        project: ms
        app: eureka
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: eureka
        image: 192.168.73.137/microservice/eureka:2020-03-08-16-40-31
        ports:
        - containerPort: 8888
        env:
        - name: JAVA_OPTS
          value: "-Xmx1g"
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        resources:
          requests:
            cpu: 0.5
            memory: 256Mi
          limits:
            cpu: 1
            memory: 1Gi
        readinessProbe:
          tcpSocket:
            port: 8888
          initialDelaySeconds: 60
          periodSeconds: 10
        livenessProbe:
          tcpSocket:
            port: 8888
          initialDelaySeconds: 60
          periodSeconds: 10

7. Deploy Eureka Cluster

Apply the YAML and verify the pods, services, and ingress:

kubectl apply -f eureka.yaml
# Output shows ingress, service, and statefulset creation
kubectl get pod,svc,ingress -n ms -o wide

8. Deploy Gateway

kubectl create -f gateway.yaml
# Verify pods
kubectl get pod -n ms -o wide

9. Deploy Business Services (product, stock, order)

Update the database URLs and image tags in each service’s application‑fat.yml, then create the resources:

# Example snippet for product-service
spring:
  datasource:
    url: jdbc:mysql://192.168.73.137:3306/tb_product?characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

eureka:
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka-0.eureka.ms:8888/eureka,http://eureka-1.eureka.ms:8888/eureka,http://eureka-2.eureka.ms:8888/eureka
kubectl create -f product.yaml
kubectl create -f stock.yaml
kubectl create -f order.yaml

10. Deploy Frontend Portal

kubectl create -f portal.yaml
# Verify pod status

11. Verify Frontend Functionality

Add host entries for portal.ctnrs.com and eureka.ctnrs.com on the local machine, then open a browser to access the portal. Screenshots show the registration center, frontend UI, product service, and order 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.

Dockerci/cdMicroservicesKubernetesDevOpsSpring Cloud
Ops Development Stories
Written by

Ops Development Stories

Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.

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.