Introduction: This series of articles, around continuous integration: Jenkins+Docker+K8S related components, to achieve automatic management of source code compilation, packaging, image construction, deployment and other operations; This article describes the use of pipeline integration K8S.

I. Background description

The deployment of distributed services is a complex process, when there are dozens or even hundreds of container applications, it is obviously too difficult to deploy manually. With the help of Kubernetes container choreography engine, it can quickly achieve automatic deployment, expansion, upgrade and a series of complex steps.

Second, process design

In the previous section, the process of source code compilation Jar package and construction of Docker image has been described in the way of pipeline. Next, Docker container is deployed on K8S platform, so that the whole automatic process is basically perfected:

Core steps:

  • Create a YAML file type resource management script;
  • Delete deployed resources.
  • Create a new resource service from the script;

After the Docker image is successfully constructed, the process can first pull the image locally or directly obtain the image in real time from the remote warehouse.

Third, the implementation process

1. Plug-in installation

Kubernetes Plugin for Jenkins integration with K8S.

2. Deployment scripts

The script syntax for deploying docker images in K8S is described in detail in K8S basic module, and the syntax for creating script files is also applicable in docker pipeline module:

Environment {k8s_directory = 'k8s-deploy'} // K8S deploys Docker image stage('K8Sdeploy') {steps {sh "" rm -rf ${k8s_directory} mkdir -p ${k8s_directory} cd ${k8s_directory} cat>k8s-app.yaml<<EOF --- apiVersion: apps/v1 kind: Deployment metadata: name: k8s-app-deployment labels: app: k8s-app spec: replicas: 2 selector: matchLabels: app: k8s-app template: metadata: labels: app: k8s-app spec: containers: - name: k8s-app image: cicada-image/doc-line-app:latest imagePullPolicy: Never ports: - containerPort: 8079 --- apiVersion: v1 kind: Service metadata: name: k8s-app-service labels: app: k8s-app spec: type: NodePort ports: - port: 8080 targetPort: 8079 selector: app: k8s-app EOF cat k8s-app.yaml echo "create k8s-app.yaml success" echo "base Yaml file deploy k8s resource" ls sleep 10 set +e kubectl delete -f k8s-app.yaml sleep 10 set -e kubectl create -f k8s-app.yaml ''' } } }Copy the code

Script description:

  • Go to the working directory of K8S.
  • createk8s-app.yamlDeployed script files;
  • Declare the number of copies of Pod as: 2;
  • Try a resource deletion according to the script;
  • Create resources from scripts;
  • set +e-eIs shell syntax to control whether the script breaks;

After the resource is successfully created, check the console interface of K8S to see whether all components are successfully deployed. Then check whether the environment is smooth by accessing the API interface in the service:

3. Process summary

In the whole process, the following core stages are involved:

  • Source code management, code warehouse acquisition, Hook mode configuration;
  • Local compilation package, generate application Jar package;
  • Docker will build Jar packages into image files, upload warehouse;
  • K8S deploys Docker image on cluster service;

Here to simplify a lot of unnecessary processes, in the actual application, is far more complex than in the case demonstration, can according to the needs of various business, the function of the reference line component documentation, continuously introducing a better way to optimize the process, will eventually form a continuous delivery process automatically, and will not bring reform cost to the code level.


Recommended for the same series:

  • Jenkins management tools in detail
  • Details of Pipeline syntax
  • Introduction to Docker containers
  • Pipeline integrates Docker containers
  • Kubernetes container engine details
  • Secondary shallow encapsulation of microservice components

Source code address

GitEE address https://gitee.com/cicadasmile/butte-auto-parent Wiki, https://gitee.com/cicadasmile/butte-java-noteCopy the code