7.3 Deploy Spring Cloud application on k8s

Article directory

  • 1. Packaging projects
  • 2. Build the image
    • 2.1 Build image
    • 2.2 Build jdk image
    • 2.3 Build eureka image
    • 2.4 Build item-service image
    • 2.5 Build user-service image
    • 2.6 Build order-service image
    • 2.7 Export the image and import it to other servers
  • 3. Deployment
    • 3.1 Deploy eureka
    • 3.2 Deploy rs, rs will automatically create a container and start the eureka server
    • 3.3 Deploy service and expose eureka access to the outside world
    • 3.4 Execute deployment
    • 3.5 Deploy item-service
    • 3.6 Deploy user-service
    • 3.7 Deploy order-service
    • 3.8 Expose the order service to the outside world for testing

1. Packaging project

We package our Jingtao project and use it as a jar package. Here we only import the first 5 projects:

Modify the yml configuration of projects 2, 3, 4, and 5, add the corresponding configuration, and register using IP

eureka:
  instance:
    prefer-ip-address: true

Execute in project order:

  • Right click – run as – maven build…
  • Goals: install
  • Check Skip Tests.

Create four empty folders and copy the jar files of projects 2, 3, 4, and 5 to the four folders:

  • eureka
  • item
  • user
  • order

Or download and use the prepared file directly:

Upload it to the /root directory of our 191 server:

2. Build image

2.1 Build image

Copy centos7-docker-image.gz to the server and import it

2.2 Build jdk image

  1. Create a new folder jdk, copy jdk-8u212-linux-x64.tar.gz to the jdk directory (skip this step if you use the downloaded file directly)
  2. Create a Dockerfile in the jdk directory (skip this step if you use the downloaded file directly)
cat <<EOF > Dockerfile
FROM centos:7
ADD jdk-8u212-linux-x64.tar.gz /opt/
ENV JAVA_HOME=/opt/jdk1.8.0_212 \
    PATH=$PATH:/opt/jdk1.8.0_212/bin:/usr/tomcat/bin
ENTRYPOINT bash
EOF
  1. Build image
cd ~/
docker build -t centos7-jdk8:v1 jdk/

2.3 Build eureka image

Enter the eureka directory and create the build file (skip this step if you use the downloaded file directly)

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp05-eureka-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp05-eureka-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=eureka1", "--server.port=2001"]
EOF

Build image

cd ~/
docker build -t sp-eureka:v1 eureka/

2.4 Build item-service image

Enter the item directory and create the build file (skip this step if you use the downloaded file directly)

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp02-itemservice-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp02-itemservice-0.0.1-SNAPSHOT.jar"]
EOF

Build image

cd ~/
docker build -t sp-item:v1 item/

2.5 Build user-service image

Enter the user directory and create the build file (skip this step if you use the downloaded file directly)

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp03-userservice-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp03-userservice-0.0.1-SNAPSHOT.jar"]
EOF

Build image

cd ~/
docker build -t sp-user:v1 user/

2.6 Build order-service image

Enter the docker directory and create the build file (skip this step if you use the downloaded file directly)

cat <<EOF > Dockerfile
FROM centos7-jdk8:v1
COPY sp04-orderservice-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp04-orderservice-0.0.1-SNAPSHOT.jar"]
EOF

Build image

cd ~/
docker build -t sp-order:v1 user/

2.7 Export the image and then import it into other servers

Under normal circumstances, our image files need to be placed in the warehouse. When it needs to be started, our server connects to the warehouse to download the image and then starts it; because we have not configured the warehouse, we manually copy the container to the server here;

docker save \
        centos7-jdk8:v1 \
        sp-eureka:v1 \
        sp-item:v1 \
        sp-user:v1 \
        sp-order:v1 \
        | gzip > img.gz

# Copy files to 192 and 193
scp img.gz 192.168.64.192:/root/

scp img.gz 192.168.64.193:/root/
# Execute import docker load -i img.gz at 192 and 193
ssh 192.168.64.192 'docker load -i /root/img.gz'

ssh 192.168.64.193 'docker load -i /root/img.gz'

# View the mirror list of 192 and 193
ssh 192.168.64.192 'docker images'

ssh 192.168.64.193 'docker images'

3. Deployment

3.1 Deploy eureka

To deploy a container with rs, first create the rs deployment description file, and use two controllers to create two Eurekas with different configurations;

cat <<EOF > eureka1-rs.yml
apiVersion: apps/v1 # RS is the resource type provided in apps/v1
kind: ReplicaSet # Resource type
metadata:
  name: eureka1 # RS is named eureka1
spec:
  replicas: 1 # Number of pod replicas
  selector:
    matchLabels: # Use label selector
      app: eureka1 # Select the pod with the label "app=eureka1"
  template:
    metadata:
      labels:
        app: eureka1 # Add label "app=eureka1" to the created pod
    spec:
      containers:
      - name: eureka1 # Container name
        image: sp-eureka:v1 # Mirror
        ports:
        - containerPort: 2001 # The port exposed by the container
          protocol:TCP
EOF
cat <<EOF > eureka2-rs.yml
apiVersion: apps/v1 # RS is the resource type provided in apps/v1
kind: ReplicaSet # Resource type
metadata:
  name: eureka2 # RS is named eureka2
spec:
  replicas: 1 # Number of pod replicas
  selector:
    matchLabels: # Use label selector
      app: eureka2 # Select the pod with the label "app=eureka2"
  template:
    metadata:
      labels:
        app: eureka2 # Add label "app=eureka2" to the created pod
    spec:
      containers:
      - name: eureka2 # Container name
        image: sp-eureka:v1 # Mirror
        args: ["--spring.profiles.active=eureka2", "--server.port=2002"]
        ports:
        - containerPort: 2002 # The port exposed by the container
          protocol:TCP
EOF

3.2 Deploy rs, rs will automatically create a container and start the eureka server

k create -f eureka1-rs.yml
k create -f eureka2-rs.yml

3.3 Deploy service and expose eureka access to the outside world

Here we only expose one eureka server for testing

cat <<EOF > eureka1-svc.yml
apiVersion: v1
Kind: Service
metadata:
  name: eureka1
spec:
  type: NodePort #Open access ports on each node
  ports:
  - port: 2001 # The port used to access the service within the cluster
    targetPort: 2001 # Container port
    nodePort: 30123 # External access port
  selector:
    app: eureka1
EOF
cat <<EOF > eureka2-svc.yml
apiVersion: v1
Kind: Service
metadata:
  name: eureka2
spec:
  type: NodePort #Open access ports on each node
  ports:
  - port: 2002 # The port used to access the service within the cluster
    targetPort: 2002 # Container port
    nodePort: 30124 # External access port
  selector:
    app: eureka2
EOF

3.4 Execute deployment

k create -f eureka1-svc.yml
k create -f eureka2-svc.yml

Post-deployment access testing:

  • http://192.168.64.191:30123/
  • http://192.168.64.191:30124/

3.5 Deploy item-service

cat <<EOF > item-rs.yml
apiVersion: apps/v1 # RS is the resource type provided in apps/v1
kind: ReplicaSet # Resource type
metadata:
  name: item # RS is named item
spec:
  replicas: 1 # Number of pod replicas
  selector:
    matchLabels: # Use label selector
      app: item # Select the pod with the label "app=item"
  template:
    metadata:
      labels:
        app: item # Add label "app=item" to the created pod
    spec:
      containers:
      - name: item # Container name
        image: sp-item:v1 # Mirror
EOF
k create -f item-rs.yml

3.6 Deploy user-service

cat <<EOF > user-rs.yml
apiVersion: apps/v1 # RS is the resource type provided in apps/v1
kind: ReplicaSet # Resource type
metadata:
  name: user # RS is named user
spec:
  replicas: 1 # Number of pod replicas
  selector:
    matchLabels: # Use label selector
      app: user # Select the pod with the label "app=user"
  template:
    metadata:
      labels:
        app: user # Add label "app=user" to the created pod
    spec:
      containers:
      - name: user # Container name
        image: sp-user:v1 # Mirror
EOF
k create -f user-rs.yml

3.7 Deploy order-service

cat <<EOF > order-rs.yml
apiVersion: apps/v1 # RS is the resource type provided in apps/v1
kind: ReplicaSet # Resource type
metadata:
  name: order # RS is named order
spec:
  replicas: 1 # Number of pod replicas
  selector:
    matchLabels: # Use label selector
      app: order # Select the pod with the label "app=order"
  template:
    metadata:
      labels:
        app: order # Add label "app=order" to the created pod
    spec:
      containers:
      - name: order # Container name
        image: sp-order:v1 # Mirror
EOF
k create -f order-rs.yml

At this point we can already implement order access within the cluster:

3.8 Expose the order service to the outside for testing

cat <<EOF > order-svc.yml
apiVersion: v1
Kind: Service
metadata:
  name: order
spec:
  type: NodePort #Open access ports on each node
  ports:
  - port: 8201 # The port used to access the service within the cluster
    targetPort: 8201 # Container port
    nodePort: 30201 # External access port
  selector:
    app: order
EOF
k create -f order-svc.yml

After exposing the service to the outside world, we directly call the order service within the cluster and call goods and other services through the order;