This is the 28th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

1. Install software on the Node. The specific software content is as follows

1. Download the JDK download the tar packages, uploaded to the node https://pan.baidu.com/s/18IicPYf7W0j-sHBXvfKyyg configuration environment variablesCopy the code

Export JAVA_HOME=/home/jdk1.8.0_161 export JRE_HOME= JAVAHOME/jre exportCLASSPATH=.:{JAVA_HOME}/jre\ export CLASSPATH=.:JAVAHOME/jre exportCLASSPATH=.:{JAVA_HOME}/lib: JREHOME/lib exportPATH=.:{JRE_HOME}/lib\ export PATH=.:JREHOME/lib exportPATH=.:{JAVA_HOME}/bin:$PATH

 
Copy the code

2. Download the tomcat, To the node/home directory wget HTTP: / / https://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.34/bin/apache-tomcat-9.0.34.tar.gzCopy the code

3. Download Jenkins to/home wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war will Jenkins war in tomcat webaap directory mv Jenkins. War /home/tomcat9/webapps Start TomcatCopy the code
Copy the code

4. Download Git and run the apt-get install get command on NodeCopy the code

5. Download maven, To the/home directory wget https://mirror.bit.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz decompression, mv maven... Maven3 configures environment variablesCopy the code

export M2_HOME=/home/maven3

export CLASSPATH=
C L A S S P A T H : CLASSPATH:
M2_HOME/lib

export PATH=
P A T H : PATH:
M2_HOME/bin

Modify the Maven repository, Maven3 /conf/settings. XML <mirror> <id>alimaven</id> <name> Aliyun maven</name> The < url > http://maven.aliyun.com/nexus/content/groups/public/ < / url > < mirrorOf > * < / mirrorOf > < / mirror >Copy the code

 

Ii. Jenkins configuration

1. Enter a value in the browserhttp://192.168.1.104:8080/jenkins/

Then, find the initial password on the server and fill it in

2. Manually install the plug-in

 

 

 

3. Select plug-ins to be installed


ssh/publish over ssh
git/git parameter
maven Integration plugin
Copy the code

Manually add maven plug-ins

Manage –> Plug-in Management –> Search Maven Integration

 

4. In the Jenkins global tool configuration, configure JDK /git/maven

Go to System Configuration > Global Tool Configuration

 

Configure the JDK and Git

 

 

 

 

Iii. Jenkins + K8S release example

Three objectives:

  • The Registry installation is configured and used
  • Jenkins project creation and configuration
  • Jenkins Project Construction

1. Installation and configuration of Docker Registry

On the Node node, get the Registry image

docker pull registry
Copy the code

Start the container

docker run -p 5000:5000 -v /home/registry_images:/var/lib/registry -d registry
Copy the code

Here, registry is mounted to the local directory to avoid image loss after docker restarts

2. Use of Registry

Change the registry container on the master machine to the host ****

/etc/docker/daemon.json
Copy the code
{"insecure-registries":["192.168.1.104:5000"], // Alter registry registry: ["https://w52p8twk.mirror.aliyuncs.com"] }Copy the code

My registry is on node, and the IP of node is 192.168.1.104

Restart the docker

systemctl daemon-reload
systemctl restart docker
Copy the code

 

Download an Nginx and upload it to the Registry repository

Get the nginx mirror docker pull nginx renamed docker tag nginx 192.168.1.104, : 5000 / nginx: testCopy the code

Try to 192.168.1.104, : 5000 / nginx: test to our Registry warehouse

Docker push 192.168.1.104, : 5000 / nginx: testCopy the code

The successful push indicates that our warehouse has been successfully created.

Do the same on Node. Modify the /etc/docker-daemon. json file

{" insecure - registries: "[]" 192.168.1.104, : 5000 ", "registry - mirrors:"/" https://w52p8twk.mirror.aliyuncs.com "}Copy the code

Restart the docker

systemctl daemon-reload
systemctl restart docker
Copy the code

 

3. Build projects on Jenkins

The overall construction process: 1. Set the parameterized construction; 2. Set the address of the code base; 3. After executing the maven build command, a JAR package will be packaged, and the JAR will be packaged into a dynamic image, and pushed to the image repository 4. Copy the yamL file of the application deployment to the master node of k8S, and then run the command to have K8S start the application based on the YAML fileCopy the code

 

  • Create a Maven project

 

 

 

 

 

  • Select discard old builds and keep them for 2 days. The maximum number of builds is 2

 

 

 

  • Select parameterized build, git parameter, name branch, and parameter type branch or tag

The purpose: you can build from branches. Or tag to build

Github.com/solochen84/…

 

  • Let’s start building the project

Project address: github.com/solochen84/…

Git project address: github.com/solochen84/…

This project is public, so the Credentials do not need to be configured

  • Adding maven builds

  • After setting up the build, package the JAR package into a Docker image and push it to Registry

Set the actions to be performed after the build is complete

  

The script content

#! /bin/sh jarName=spring-boot-demo-0.0.1- snapshot. jar jarFolder= pH projectName= pH docker_path=${WORKSPACE} cp ${WORKSPACE}/target/${jarName} ${docker_path} sh /root/docker_dir/deploy_docker.sh ${projectName} ${docker_path} ${jarName}Copy the code
Create the deploy_docker file. Directory: / root/docker_dir/deploy_docker. Sh. The file content is as follows: Set the executable of the fileCopy the code
permissionsCopy the code
chmod 775 deploy_docker.sh
Copy the code

 

Content of deploy_docker.sh file

#! /bin/sh # maven01 $workspace $jarname # ${projectName} ${docker_path} ${jarName} set -e projectName=$1 docker_path=$2 AppName =$3 #user_name= #password= tag=$(date +%s) server_path=192.168.1.104:5000 Target_image =${projectName}:${tag} #${BUILD_NUMBER} echo ${target_image} cd ${docker_path} docker build --build-arg app=${appName} -t ${target_image} . docker tag ${target_image} ${server_path}/${projectName} echo The name of image is "${server_path}/${target_image}"Copy the code
  • Set Jenkins server to K8S master SSH secret free login

Setting up a cryptographic-free login allows Jenkins to run scripts on K8S and execute commands

    • Run it on Jenkins ‘server
Generate the secret key ssh-keygen -t rsa Copy the public key ssh-copy-id -i ~/. SSH /id_rsa.pub [email protected]Copy the code
    • Test encrypted login

SSH [email protected]

  • Set up post-build operations to copy yamL files to K8S Master and run the application

The location of the YAML file is in the project.

Set -e echo ok echo ${WORKSPACE} docker_path=${WORKSPACE} SCP ${WORKSPACE}/*. Yaml 192.168.1.106: /root/ssh 192.168.1.106 '/ usr/bin/kubectl apply - f/root/kube. Yaml' SSH 192.168.1.106 '/ usr/bin/kubectl get SVC | grep maven'Copy the code

Enter the IP address of the master

  • Jenkins Project Construction

 

In the process of building, all kinds of problems arise

1. no matches for kind “Deployment” in version “extensions/v1beta1”

Refer to the article: www.cnblogs.com/nnylee/p/11…

 

Modify the corresponding shell script

Set -e echo ok echo ${WORKSPACE} docker_path=${WORKSPACE} SCP ${WORKSPACE}/*. Yaml 192.168.1.106: /root/ssh 192.168.1.106 'sed -i "s | extensions/v1beta1 | apps/v1 |"/root/kube. Yaml' SSH 192.168.1.106 'sed -i "s | 192.168.0.108 | 192.168.1.104, |" Yaml 'SSH 192.168.1.106 '/usr/bin/kubectl apply -f /root/kube.yaml --validate=false' SSH 192.168.1.106 '/usr/bin/kubectl get svc|grep maven'Copy the code

2. spec.template.metadata.labels: Invalid value: map[string]string{“app”:”maven”}: selector does not match template labels.

  Build step ‘Execute shell’ marked build as failure

Refer to this article: www.cnblogs.com/robinunix/p…

I modified the configuration

apiVersion: v1
kind: Service
metadata:
  name: maven-service
spec:
  type: NodePort
  ports:
  - name: maven
    port: 8080
    nodePort: 31002
    targetPort: 8080
    protocol: TCP
  selector:
    app: maven
---
apiVersion: apps / v1
kind: Deployment
metadata:
   #name: maven-deployment
  name: maven
spec:
  selector:
    matchLabels:
      app: maven
  replicas: 1
  template:
    metadata:
      labels:
        app: maven
    spec:
      containers:
        - name: maven
          image: 192.168.1.104:5000/ maven:latest
          ports:
          - containerPort: 8080
          env:
            - name: key
              value: "value"
Copy the code

Then restart, success!

kubectl get deployments --all-namespaces
Copy the code

The Ready state of the deployment whose name is maven is 0

Check the pod

kubectl get pod --all-namespaces
Copy the code

A pending state was found

View the POD logs

 kubectl describe pod maven-7589958577-5ms68 -n default
Copy the code