During the development phase, quickly deploy SpringBoot applications to K8S

background

  1. For the production environment, we generally use CI&&CD tools to complete the entire build and deployment, so this article is not suitable for the production environment;
  2. For the learning and development environment, we frequently modify the code and want to see the effect quickly. This article is aimed at this scenario;

brief introduction

  1. If you are developing a SpringBoot application and the application is deployed in the K8S environment, you can refer to this article to quickly deploy the application to the K8S environment;
  2. The version of SpringBoot in the article is 2.3.0.RELEASE. Starting from this version, SpringBoot officially recommends a new docker image construction solution. If you are a previous version, please modify the image construction part in the article yourself;

Environmental information

There are two environments for this actual combat: development and operation environment, the development environment information is as follows:

  1. Operating system: Ubuntu 20.04 LTS desktop version (also verified: MacBook pro 13-inch, macOS Catalina 10.15.4)
  2. CPU: 2.30GHz × 4, memory: 32G, hard disk: 1T NVMe
  3. JDK:1.8.0_231
  4. MAVEN:3.6.3
  5. SpringBoot:2.3.0.RELEASE
  6. Docker:19.03.10
  7. Development tools: IDEA 2020.1.1 (Ultimate Edition)

The operating environment information is as follows:

  1. Operating system: CentOS Linux release 7.8.2003
  2. Governors : 1.15

In addition, please install sshpass in the development environment;

SpringBoot project source code

  1. An ordinary SpringBoot project was used in this actual combat. The source code can be downloaded on GitHub. The address and link information are shown in the following table (https://github.com/zq2599/blog_demos):
name link Remarks
Project homepage https://github.com/zq2599/blog_demos The project's homepage on GitHub
git warehouse address (https) https://github.com/zq2599/blog_demos.git The warehouse address of the project source code, https protocol
git warehouse address (ssh) [email protected]:zq2599/blog_demos.git The warehouse address of the source code of the project, ssh protocol
  1. There are multiple folders in this git project. The application of this chapter is under the probeemo folder, as shown in the red box below:
    Insert picture description here

Necessary content

  1. The whole project is a normal SpringBoot project. In addition to the common pom.xml and source code, there are three more files:
    Insert picture description here
  2. The Dockerfile file is used to build the docker image. If your SpringBoot version is 2.3.0.RELEASE or higher, it is recommended to use the image building method officially recommended by SpringBoot. This Dockerfile can be used for reference;
  3. It is related to the probedemo.yaml file and your specific project. Please modify it yourself to ensure that it can be used to deploy deployment and service;
  4. auto_deploy.sh is the key to rapid deployment, which will be explained in detail next;

auto_deploy.sh detailed

The role of auto_deploy.sh is as follows:

  1. Invoke the maven command to edit the build project;
  2. Make the built jar file into a docker image;
  3. Export the docker image as a tar file;
  4. Upload the tar file to the K8S server;
  5. Send the probedemo.yaml file to the K8S server;
  6. Execute commands remotely through sshpass to import the docker image into the local docker warehouse of the K8S server;
  7. Execute commands remotely through sshpass to deploy deployment and service;
  8. The complete content of auto_deploy.sh is as follows:
#!/bin/bash

# 判断是否安装了sshpass
if ! [ -x "$(command -v sshpass)" ]; then
  echo '请安装sshpass后再使用此脚本!'
  exit 1
fi

# 镜像名
IMAGE_NAME='bolingcavalry/probedemo'

# TAG名
TAG_NAME='0.0.1'

# 配置了deployment和service的yaml文件名
DEPLOY_SERVICE_YAML='probedemo.yaml'

# K8S环境的IP地址
K8S_IP_ADDRESS='192.168.50.135'

# K8S环境的SSH账号
K8S_SSH_ACCOUNT='root'

# 8S环境的SSH密码
K8S_SSH_PSWD='888888'

# K8S上存放tar和yaml文件的位置
K8S_FILE_PATH='~/deploy_temp'

# 当前名目录
CURRENT_DIR=`pwd`

echo '开始自动构建和部署,当前目录是:'${CURRENT_DIR}

# 执行maven命令构建项目
mvn clean package -U -DskipTests

echo "构建镜像文件:"${IMAGE_NAME}/${TAG_NAME}
docker build -t ${IMAGE_NAME}/${TAG_NAME} .

echo "将镜像导出为tar文件:"${IMAGE_NAME}/${TAG_NAME}
docker save ${IMAGE_NAME}/${TAG_NAME} > ${CURRENT_DIR}/image.tar

echo "在K8S服务器创建存放文件的目录:"${K8S_FILE_PATH}
sshpass -p ${K8S_SSH_PSWD} ssh ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS} "mkdir -p ${K8S_FILE_PATH}"

echo "将yaml文件发送到K8S服务器:"${IMAGE_NAME}/${TAG_NAME}
sshpass -p ${K8S_SSH_PSWD} scp ${CURRENT_DIR}/${DEPLOY_SERVICE_YAML} ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS}:${K8S_FILE_PATH}/

echo "将镜像tar文件发送到K8S服务器:"${IMAGE_NAME}/${TAG_NAME}
sshpass -p ${K8S_SSH_PSWD} scp ${CURRENT_DIR}/image.tar ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS}:${K8S_FILE_PATH}/

echo "如果K8S环境之前已经部署过,就先清理:"${IMAGE_NAME}/${TAG_NAME}
sshpass -p ${K8S_SSH_PSWD} ssh ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS} "kubectl delete -f ${K8S_FILE_PATH}/${DEPLOY_SERVICE_YAML}"

echo "等待10秒"
sleep 10

echo "清理之前加载到本地仓库的镜像:"${IMAGE_NAME}/${TAG_NAME}
sshpass -p ${K8S_SSH_PSWD} ssh ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS} "docker rmi ${IMAGE_NAME}/${TAG_NAME}"

echo "从tar文件加载镜像:"${IMAGE_NAME}/${TAG_NAME}
sshpass -p ${K8S_SSH_PSWD} ssh ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS} "docker load < ${K8S_FILE_PATH}/image.tar"

echo "部署:"${IMAGE_NAME}/${TAG_NAME}
sshpass -p ${K8S_SSH_PSWD} ssh ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS} "kubectl apply -f ${K8S_FILE_PATH}/${DEPLOY_SERVICE_YAML}"

echo "删除tar文件:"${CURRENT_DIR}/image.tar
rm -rf ${CURRENT_DIR}/image.tar

echo "删镜像:"${IMAGE_NAME}/${TAG_NAME}
docker rmi ${IMAGE_NAME}/${TAG_NAME}

experiment

  1. Please modify the values ​​of variables in auto_deploy.sh according to your actual situation, such as K8S environment address, account password, etc.;
  2. In the development stage, modify the java code to complete;
  3. Execute the script auto_deploy.sh;
  4. The console prompt is as follows:
    Insert picture description here
  5. The deployment has been completed and the function can be verified;

Precautions

The method to update the image in this actual combat is to export it locally as tar, and then transfer it to the K8S environment to import. This is not suitable when there are multiple machines in the K8S environment. It is recommended to use a private mirror warehouse, push the image to the warehouse, and then K8S Pull mirror in mirror warehouse;

At this point, the actual combat of the rapid deployment of SpringBoot applications to K8S is complete. This is a commonly used remote deployment method in my development process. I hope to provide you with some reference;

Welcome to my GitHub

Welcome to follow my public account: programmer Xin Chen

Insert picture description here

Guess you like

Origin blog.csdn.net/boling_cavalry/article/details/106594392