Welcome to my GitHub

Github.com/zq2599/blog…

Content: all original article classification summary and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;

background

  1. For production environments, we typically use the CI&&CD tool for the entire build and deployment, so this article is not suitable for production environments;
  2. This article is for learning and development environments where we change code frequently and want to see results quickly;

Content abstract

  1. If you are developing a SpringBoot application and the application is deployed in a K8S environment, you can refer to this section to quickly deploy the application to a K8S environment.
  2. The version of SpringBoot in this paper is 2.3.0.release. Since this version, SpringBoot officially recommended a new docker image construction scheme. If you are the previous version, please modify the part of the image construction in this paper.

Environmental information

The actual combat has two environments: development environment and operation environment. The development environment information is as follows:

  1. Operating system: Ubuntu 20.04 LTS Desktop (also verified: MacBook Pro 13 inch, macOS Catalina 10.15.4)
  2. CPU: 2.30GHz x 4, Memory: 32 GB, hard disk: 1 TB NVMe
  3. The JDK: 1.8.0 comes with _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. OS: CentOS Linux release 7.8.2003
  2. Kubernetes: 1.15

Also, install sshPass in your development environment;

SpringBoot project source code

  1. This practice used a common SpringBoot project, source code can be downloaded on GitHub, address and link information as shown in the following table (github.com/zq2599/blog…
The name of the link note
Project home page Github.com/zq2599/blog… The project’s home page on GitHub
Git repository address (HTTPS) Github.com/zq2599/blog… The project source warehouse address, HTTPS protocol
Git repository address (SSH) [email protected]:zq2599/blog_demos.git The project source warehouse address, SSH protocol
  1. This Git project has multiple folders. The application of this chapter is in the Probedemo folder, as shown in the red box below:

Essential content

  1. The whole project is a common SpringBoot project, in addition to the common POM.xml and source code, but also the following three files:

2. The Dockerfile file is used to build a Docker image. If your SpringBoot version is 2.3.0.release or higher, you are advised to use the image building method officially recommended by SpringBoot. Yaml file and probedemo.yaml file is related to 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.

Detailed auto_deploy. Sh

The auto_deploy.sh function is as follows:

  1. Call maven command to edit build project;
  2. Make the constructed 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. Through sshpass remote execution command, the docker image into the K8S server’s local Docker warehouse;
  7. Remotely execute commands to deploy deployment and service through sshpass;
  8. The full contents of auto_deploy.sh are as follows:
#! /bin/bash

#Check whether sshPass is installedif ! [ -x "$(command -v sshpass)" ]; Then echo 'please install sshpass before using this script! ' exit 1 fi
#The mirror of
IMAGE_NAME='bolingcavalry/probedemo'

#The TAG nameTAG_NAME = '0.0.1'
#Yaml file names for Deployment and Service are configured
DEPLOY_SERVICE_YAML='probedemo.yaml'

#IP address of the K8S environmentK8S_IP_ADDRESS = '192.168.50.135'
#SSH account of the K8S environment
K8S_SSH_ACCOUNT='root'

#SSH password in the 8S environment
K8S_SSH_PSWD='888888'

#The location of tar and YAML files on K8S
K8S_FILE_PATH='~/deploy_temp'

#Current name directoryCURRENT_DIR= 'PWD' echo 'to start automatic build and deployment, the current directory is: '${CURRENT_DIR}
#Run maven to build the projectMVN clean package -u-dskiptests echo" ${IMAGE_NAME}/${TAG_NAME} docker build -t ${IMAGE_NAME}/${TAG_NAME}. Echo ${TAG_NAME} docker save ${IMAGE_NAME}/${TAG_NAME} > ${CURRENT_DIR}/image.tar echo "${K8S_FILE_PATH} sshpass -p ${K8S_SSH_PSWD} ssh ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS} "mkdir -p ${K8S_FILE_PATH}" echo "To send yamL file to K8S server: "${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" "${IMAGE_NAME}/${TAG_NAME} sshpass -p ${K8S_SSH_PSWD} scp ${CURRENT_DIR}/image.tar ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS}:${k8s_path}/ echo "${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 "wait 10 seconds for" sleep 10 echo "to clear images loaded to local repository before: "${IMAGE_NAME}/${TAG_NAME} sshpass -p ${K8S_SSH_PSWD} ssh ${K8S_SSH_ACCOUNT}@${K8S_IP_ADDRESS} "docker rmi ${IMAGE_NAME}/${TAG_NAME}" echo" "${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 "Delete tar files: ${CURRENT_DIR}/image.tar rm -rf ${CURRENT_DIR}/image.tar echo "${IMAGE_NAME}/${TAG_NAME} docker rmi ${IMAGE_NAME}/${TAG_NAME}Copy the code

The experiment

  1. Change the values of variables in the auto_deploy.sh file, such as the K8S environment address and account password, based on your actual situation.
  2. In the development stage, the Modification of Java code is completed;
  3. Run the auto_deploy.sh script.
  4. The console prompts as follows:

5. The deployment is complete and the functionality can be verified.

Matters needing attention

This practice is to update the image of the local export to tar, and then to the K8S environment to import, when the K8S environment has more than one machine is not suitable for this, it is recommended to use a private image warehouse, image push to the warehouse, and then K8S in the image warehouse pull image;

At this point, SpringBoot application rapid deployment to K8S combat is completed, this is my development process commonly used remote deployment means, I hope to provide you with some reference;

You are not alone, Xinchen original accompany all the way

  1. Java series
  2. Spring series
  3. The Docker series
  4. Kubernetes series
  5. Database + middleware series
  6. The conversation series

Welcome to pay attention to the public number: programmer Xin Chen

Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…

Github.com/zq2599/blog…