Jenkins pipeline integrates k8s to realize automatic code integration and deployment

1. Preconditions

1. Install the k8s cluster

Here we need to build a K8s cluster first. The author here uses a k8s cluster with one master and one cluster. The version of the k8s cluster uses version 1.19.5. Server configuration: 2 cores 4G, operating system: CentOS Linux release 7.9 .2009 (Core)

CPU name         ip
k8smaster 192.168.19.8
k8sworker         192.168.19.9

The specific installation steps can be carried out according to the document: Use kubeadm to install kubernetes_v1.19.x | Kuboard

2. Install Kuboard to manage the k8s cluster

After the installation is complete, install Kuboard v3 - kubernetes, follow the instructions:

kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3.yaml
# You can also use the following command, the only difference is that this command uses Huawei Cloud's mirror warehouse instead of docker hub to distribute Kuboard Mirror image
# kubectl apply -f https://addons.kuboard.cn/kuboard/kuboard-v3-swr.yaml

 After Kuboard installation is complete,

  • http://Open the link 192.168.19.8 in your browser :30080

  • Enter the initial username and password, and log in

    • username: admin
    • password: Kuboard123

3. Install IngressClass for service exposure

Install the ingressController in the cluster's IngressClass management

 4. Create the test namespace

In the future, the services we deploy through jenkinszhong are placed in this namespace, and the following instructions are executed on the master node of the k8s cluster:

[root@localhost ~]# kubectl create ns test
namespace/test created
[root@localhost ~]# kubectl get ns 
NAME              STATUS   AGE
default           Active   13h
ingress-nginx     Active   30m
kube-node-lease   Active   13h
kube-public       Active   13h
kube-system       Active   13h
kuboard           Active   13h
test              Active   2m20s

5. Configure the relevant configuration information of docker private server on Kuboard

6. Configure the private server address of docker on the master and worker nodes of k8s

 Add in the /etc/docker/daemon.json file

Then restart the docker service, both master and worker need to execute

 systemctl restart docker

To test whether docker can log in, perform the following naming:

docker login 192.168.19.7:80 -u admin -p Harbor12345

found to be able to log in normally 

2. Add the k8s deployment file to the project code

1. Add the pipe-line.yml file in the docker folder of the project code

The content of the file is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: test
  name: pipeline
  labels:
    app: pipeline
spec:
  replicas: 2
  selector:
    matchLabels:
      app: pipeline
  template:
    metadata:
      labels:
        app: pipeline
    spec:
      imagePullSecrets:
        - name: harbor
      containers:
        - name: pipeline
          image: 192.168.19.7:80/repo/mytest:v1.0.10
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  labels:
    app: pipeline
  name: pipeline
spec:
  selector:
    app: pipeline
  ports:
    - port: 8081
      targetPort: 8080
  type: NodePort
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: test
  name: pipeline
spec:
  ingressClassName: ingress
  rules:
    - host: zhang.pipeline.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: pipeline
                port:
                  number: 8081

2. Add domain name resolution

Add the following domain name resolution to the hosts file of the window:

C:\Windows\System32\drivers\etc\hosts file

192.168.19.8  zhang.pipeline.com

3. Configure the jenkins pipeline

3-1. Now create a k8smaster directory under the /usr/local/ directory of k8smaster

3.2. Add a publish over ssh on jenkins

 

3.3. Add a file transfer task to the task of the project, and perform pod deployment and rolling update

sshPublisher(publishers: [sshPublisherDesc(configName: 'k8smaster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''kubectl apply -f /usr/local/k8smaster/docker/pipe-line.yml
kubectl rollout restart deployment pipeline -n test''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'docker/pipe-line.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])

 4. Test the pipeline construction after deployment

 It can be seen that the execution has been successful, so far we have completed the pipeline construction of CICD based on jenkins and integrated k8s cluster

Guess you like

Origin blog.csdn.net/zhangshenglu1/article/details/130759582