实战交付一套dubbo微服务到k8s集群之使用Jenkins进行持续构建交付dubo服务的提供者

Posted 枫哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实战交付一套dubbo微服务到k8s集群之使用Jenkins进行持续构建交付dubo服务的提供者相关的知识,希望对你有一定的参考价值。

1.登录到jenkins,新建一个项目

image-20200612154505028

2.新建流水线

image-20200612154754170

3.设置保留的天数及份数

image-20200612154922999

4.添加第一个参数:设置项目的名称

image-20200612155449864

5.添加第二个参数:docker镜像名称

image-20200612155722147

6.添加第三个参数:项目所在的git中央仓库地址

image-20200612155922204

7.添加第四个参数:项目在git仓库的对应分支或版本号

image-20200612160034910

8.添加第五个参数:添加镜像标签

image-20200612160201130

9.添加第六个参数:编译项目的位置

image-20200612160259851

10.添加第七个参数:项目编译完成后,产生jar/war包的位置

image-20200612165449663

11.添加第八个参数:编译所要使用的命令

image-20200612170305822

12.添加第九个参数:使用哪个底包

image-20200612172754115

13.添加第十个参数:使用哪个maven版本编译项目

image-20200612174015026

14.设置流水线构建脚本

脚本代码如下:

pipeline {
  agent any 
    stages {
      stage(\'pull\') { //get project code from repo 
        steps {
          sh "git clone ${params.git_repo} ${params.app_name}/${env.BUILD_NUMBER} && cd ${params.app_name}/${env.BUILD_NUMBER} && git checkout ${params.git_ver}"
        }
      }
      stage(\'build\') { //exec mvn cmd
        steps {
          sh "cd ${params.app_name}/${env.BUILD_NUMBER}  && /var/jenkins_home/maven-${params.maven}/bin/${params.mvn_cmd}"
        }
      }
      stage(\'package\') { //move jar file into project_dir
        steps {
          sh "cd ${params.app_name}/${env.BUILD_NUMBER} && cd ${params.target_dir} && mkdir project_dir && mv *.jar ./project_dir"
        }
      }
      stage(\'image\') { //build image and push to registry
        steps {
          writeFile file: "${params.app_name}/${env.BUILD_NUMBER}/Dockerfile", text: """FROM harbor.od.com/${params.base_image}
ADD ${params.target_dir}/project_dir /opt/project_dir"""
          sh "cd  ${params.app_name}/${env.BUILD_NUMBER} && docker build -t harbor.od.com/${params.image_name}:${params.git_ver}_${params.add_tag} . && docker push harbor.od.com/${params.image_name}:${params.git_ver}_${params.add_tag}"
        }
      }
    }
}

image-20200612171718471

15.最后保存所有设置的

image-20200612170949876

16.在私有仓库创建一个名称为app的项目,属性为私有

image-20200612172044763

17.执行流水线

image-20200612172157153

18.输入相关的参数,执行build

image-20200613192937161

19.构建成功

image-20200613193256391

20.查看私有仓库的app项目是否已经有docker镜像

image-20200612181253309

21.准备dubbo-demo-service的k8s资源配置清单文件

在运维主机mfyxw50.mfyxw.com上操作

[root@mfyxw50 ~]# mkdir -p /data/k8s-yaml/dubbo-demo-service
[root@mfyxw50 ~]# cat > /data/k8s-yaml/dubbo-demo-service/deployment.yaml << EOF
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: dubbo-demo-service
  namespace: app
  labels: 
    name: dubbo-demo-service
spec:
  replicas: 1
  selector:
    matchLabels: 
      name: dubbo-demo-service
  template:
    metadata:
      labels: 
        app: dubbo-demo-service
        name: dubbo-demo-service
    spec:
      containers:
      - name: dubbo-demo-service
        image: harbor.od.com/app/dubbo-demo-service:master_20200613_1929
        ports:
        - containerPort: 20880
          protocol: TCP
        env:
        - name: JAR_BALL
          value: dubbo-server.jar
        imagePullPolicy: IfNotPresent
      imagePullSecrets:
      - name: harbor
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      securityContext: 
        runAsUser: 0
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate: 
      maxUnavailable: 1
      maxSurge: 1
  revisionHistoryLimit: 7
  progressDeadlineSeconds: 600
EOF

在安装有zookeeper的主机(mfyxw10或mfyxw20或mfyxw30)上任意一台执行

查看zookeeper的状态

[root@mfyxw30 bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower

连接进入zookeeper查看

[root@mfyxw30 bin]# ./zkCli.sh -server localhost:2181
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] 

23.应用dubbo-demo-service的k8s的资源配置清单

在master节点(mfyxw30.mfyxw.com或mfyxw40.mfyxw.com)任意一台执行即可

[root@mfyxw30 ~]# kubectl create namespace app
[root@mfyxw30 ~]# kubectl create secret docker-registry harbor --docker-server=harbor.od.com --docker-username=admin --docker-password=Harbor12345 -n app
[root@mfyxw30 ~]# kubectl apply -f http://k8s-yaml.od.com/dubbo-demo-service/deployment.yaml

image-20200612212715671

再次查看zookeeper

[zk: localhost:2181(CONNECTED) 1] ls /
[dubbo, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /dubbo

image-20200613194323898

24.打开 Blue Ocean可以查看到构建的步骤

image-20200613194429319

以上是关于实战交付一套dubbo微服务到k8s集群之使用Jenkins进行持续构建交付dubo服务的提供者的主要内容,如果未能解决你的问题,请参考以下文章

实战交付一套dubbo微服务到k8s集群之使用Jenkins进行持续构建交付dubo服务的提供者

[kubernetes] 交付dubbo之jenkins持续交付dubbo-server

[kubernetes] 交付dubbo之持续交付dubbo-monitor和dubbo-consumer(完结篇)

k8s实战之Service

(2022版)一套教程搞定k8s安装到实战 | K8s集群安装(Kubeadm)

(2022版)一套教程搞定k8s安装到实战 | K8s集群安装(二进制)