拆车五十铃FTR54发动(Gitlab+Jenkins Pipeline+k8s在生产环境中的应用)

Posted

篇首语:认识到自己无知是知识进步的重要阶段。本文由小常识网(cha138.com)小编为大家整理,主要介绍了拆车五十铃FTR54发动(Gitlab+Jenkins Pipeline+k8s在生产环境中的应用)相关的知识,希望对你有一定的参考价值。

拆车五十铃FTR54发动(Gitlab+Jenkins Pipeline+k8s在生产环境中的应用)



系统环境说明

  • GitLab Community Edition 11.9.8
  • Jenkins ver. 2.190.3
  • 仓库使用阿里的镜像仓库
  • Kubernetes v1.14.2

gitlab和jenkins-master可以选择自建或者部署到k8s中,当前场景是部署在k8s集群之外;

镜像仓库可以选择使用harbor或者阿里镜像仓库,当前场景使用的是阿里的镜像仓库;

编译发布流程

流程很简单,提交代码到不同的分支,触发通知到jenkins,jenkins pipeline会根据Jenkinsfile文件中定义k8s环境,动态生成一个jenkins slave在不同k8s环境中构建镜像,推送镜像到仓库,然后在部署到对应k8s环境,部署结束后jenkins slave会自动终止

集成配置过程

因为都是现有的环境,所以部署过程就省略了,直接开始做集成配置;如果你是全新安装的Jenkins,选择安装推荐插件+kubernetes插件;现在假如插件都已经安装完成了,jenkins登陆默认账户admin,密码查看/var/jenkins_home/secrets/initialAdminPassword文件,下面开始配置

点击左侧系统管理,打开系统配置,我们这里要实现部署发布到2个k8s环境,所以我们配置两个云,先新增一个云


名称:随便填,后面Jenkinfile会用到

Kuberneters地址:填写apiserver地址

Kuberneters服务证书key:需要拿k8s的crt和key做格式转换,下面会介绍

Kubernetes 命名空间:填写jenkins slave要生成的命名空间,这个自己看着填吧

凭据:需要拿k8s的crt和key做格式转换,然后生成jenkins全局凭据

Jenkins地址:填写jenkins master地址,也就是当前jenkins地址

Jenkins通道:填写jenkins master与jenkins slave通信地址,jenkins通信端口默认是50000

配置第一个k8s相关信息,过程中会用到认证k8s apiserver的key和凭证,所以我们先获取下需要的key和凭证(在要配置的k8s master机器操作)

$ cat /root/.kube/config apiVersion: v1clusters:- cluster: certificate-authority-data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ROanAwK2swaGdhV2xxCjBDZmtkbjM0MkY2bUhSZFNyeGg3eURleGNtNEtGck5OVnNPY0h5MEJhQXhwZ0JOUmErMG5oZ1dDbHh1M0F4OWgKaVRELzAyczRCcWZKTFZjZXk0Q2VnWW8zUDVDYWVjTTZSaTg4TVFKYlZ1OWx6RVZPUzlBRGNKZ0VkWkdHUFUyVwpEcHBhZXdZNjhSVVg1MG9ObXN2S2h1RGNCSWxyeHJ4T2J4Wk1wYm5hYldUMlFaKzM4ampEaDNwVmxhcz0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= server: https://192.168.0.54:6443 name: kubernetescontexts:- context: cluster: kubernetes user: kubernetes-admin name: kubernetes-admin@kubernetescurrent-context: kubernetes-admin@kuberneteskind: Configpreferences: users:- name: kubernetes-admin user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJSzZSZWRVQ2NNS0V3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RBMk1EWXhNREkyTURsYUZ3MHlNREEyTURVeE1ESTJNVEZhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQTBrcW1jci9DNHBaSTgwUTAKTzBOaklRV1hPelQ0TmVPVFJQd0N1UW1CaDlZcUJ0QTBkZnJvVnRzT2JRTEdlWE5GUVlhRzM5V2ZsN292YURCNwp3Wm11WVo3alZKUm9BZmpldEg3d0lIN3pNYUdCbk1hN3RoTUNqWXBmdXRpQyszQm51cjFLRmFJR2FKcDNlK0NQCjAyVjdSVDJjUXFPbHVOVUtFSEl2UG15YTdKM0pHWUpLNFVBVEhQOVhIMVZNemVoc3N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lNOHBxRHBqVU5DRnlKemJqajNvN1ZsTGRSc0FybnpFZnVNaUo5YjZ2dVpaWXQzRwp2bEZFMDBiYnNQWmRVbzArenJvSEZMK2JqQ1QrODl1UUFJRHFHcGtyYUdIZ0IxelZyMHF2L0NXajlnbzkrdUxhCjJKQk5ud1RSczlsbzE1ZzV1YnJPRWFBNzlBaExEeEFuZFA3Z0RpMENnWUFyODBpa2NmN2RNUDRBRlpndEVYTm8KQWZUVGI4WFJSZmsweHZNQUt6RW5SSENwT2hocWJlTW5yV2Z6V0JlSDRiSUZlenNtWDg3d0pxQ2VERzFWeFQyVwpiZHVxb0NONHg0R1lIWENFSm4yV2ZYS3gyKzIzaEY0MGRzQk9pdlpBSDhhaG5qWTBuSGZMaTh1MFVtOHk1UXFDClc0Z1g3UWU1emNIZlJQdXZWL013OVFLQmdRQy8rKzFYd2cxU2thQkZNTkRKWWZjZWNtUUlibUwzeHEvUjNQVkIKSjJhQ1FDdTgxbGdZaURUS0I0c2kzcmlNWHpKRVdad3NPOENueDhvWVhYRjU3YjlpUjEzd1RoZFpjcFpZU2lNawpmWTBhRGpEa3hpVEc0UGJWMSt0UDhOdWVPK2hwT2FaME1TaEhVZElJVjlXdmY5b3NXTlVmbTFQY29pdktUSStMCnpYQTRzUUtCZ0ZsUnpoeDlYczJ2L1h1VGl0NE1mNUpMU2ppNk93c3pZVXNmTzVMQ1BVbGQrcjA5dmFsS0tPQlcKU1FUVDhTdnBpMzZhQVFTV0xCUUdVYkh2VlJ0QkJHOWxiY2JSSHlla1RrYksxTEwxVk0zKzdtRXZCUDFZZU9qZQpwWkRlTWcrZWQyY3F5MDJhZWwxbGhBK3dJRmlnUzV5bGY0Z25paFBnMVZ2WmJIUGIzREdGCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

我们用到的Kubernetes 服务证书转换 keycertificate-authority-data,凭据转换client-certificate-data和client-key-data

获取/root/.kube/config中certificate-authority-data的内容并转化成base64 encoded文件,将生成的ca.cr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7Z9fi4eAe0I8vtvqjjYizG1gPP2KuxwHxFkIDLByMFdSHnrTEYyj65QgmKBYZDgEd2viVpSxs8cwBex/Om5Vq+ftNjp0+k0hgaWlq0Cfkdn342F6mHRdSrxh7yDexcm4KFrNNVsOcHy0BaAxpgBNRa+0nhgWClxu3Ax9hiTD/02s4BqfJLVcey4CegYo3P5CaecM6Ri88MQJbVu9lzEVOS9ADcJgEdZGGPU2WDppaewY68RUX50oNmsvKhuDcBIlrxrxObxZMpbnabWT2QZ+38jjDh3pVlas=-----END CERTIFICATE-----

获取/root/.kube/config中client-certificate-data和client-key-data的内容并转化成base64 encoded文件

$ echo LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJSzZSZWRVQ2NNS0V3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RBMk1EWXhNREkyTURsYUZ3MHlNREEyTURVeE1ESTJNVEZhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQTBrcW1jci9DNHBaSTgwUTAKTzBOaklRV1hPelQ0TmVPVFJQd0N1UW1CaDlZcUJ0QTBkZnJvVnRzT2JRTEdlWE5GUVlhRzM5V2ZsN292YURCNwp3Wm11WVo3alZKUm9BZmpldEg3d0lIN3pNYUdCbk1hN3RoTUNqWXBmdXRpQyszQm51cjFLRmFJR2FKcDNlK0NQCjAyVjdSVDJjUXFPbHVOVUtFSEl2UG15YTdKM0pHWUpLNFVBVEhQOVhIMVZNemVoc3N5ZjF5UTZZb1BxTTR1b08KVGVNN3g0NFcvS2hrWjBMWkhncnB5RDJQNlN2NGVoaGl6YTQrdUk1Si80OFhhVFJwMUxINmdGODV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mRVbzArenJvSEZMK2JqQ1QrODl1UUFJRHFHcGtyYUdIZ0IxelZyMHF2L0NXajlnbzkrdUxhCjJKQk5ud1RSczlsbzE1ZzV1YnJPRWFBNzlBaExEeEFuZFA3Z0RpMENnWUFyODBpa2NmN2RNUDRBRlpndEVYTm8KQWZUVGI4WFJSZmsweHZNQUt6RW5SSENwT2hocWJlTW5yV2Z6V0JlSDRiSUZlenNtWDg3d0pxQ2VERzFWeFQyVwpiZHVxb0NONHg0R1lIWENFSm4yV2ZYS3gyKzIzaEY0MGRzQk9pdlpBSDhhaG5qWTBuSGZMaTh1MFVtOHk1UXFDClc0Z1g3UWU1emNIZlJQdXZWL013OVFLQmdRQy8rKzFYd2cxU2thQkZNTkRKWWZjZWNtUUlibUwzeHEvUjNQVkIKSjJhQ1FDdTgxbGdZaURUS0I0c2kzcmlNWHpKRVdad3NPOENueDhvWVhYRjU3YjlpUjEzd1RoZFpjcFpZU2lNawpmWTBhRGpEa3hpVEc0UGJWMSt0UDhOdWVPK2hwT2FaME1TaEhVZElJVjlXdmY5b3NXTlVmbTFQY29pdktUSStMCnpYQTRzUUtCZ0ZsUnpoeDlYczJ2L1h1VGl0NE1mNUpMU2ppNk93c3pZVXNmTzVMQ1BVbGQrcjA5dmFsS0tPQlcKU1FUVDhTdnBpMzZhQVFTV0xCUUdVYkh2VlJ0QkJHOWxiY2JSSHlla1RrYksxTEwxVk0zKzdtRXZCUDFZZU9qZQpwWkRlTWcrZWQyY3F5MDJhZWwxbGhBK3dJRmlnUzV5bGY0Z25paFBnMVZ2WmJIUGIzREdGCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg== | base64 -d > /tmp/client.key

将上面生成的文件转换为P12认证文件cert.pfx,并下载至本地;生成过程中设置的密码要记住,后面有用

$ openssl pkcs12 -export -out /tmp/cert.pfx -inkey /tmp/client.key -in /tmp/client.crt -certfile /tmp/ca.crtEnter Export Password:Verifying - Enter Export Password: $ sz /tmp/cert.pfx

然后回到jenkins配置全局凭据

最终新增kubernetes云配置,点击连接测试,提示成功即可

配置第二个k8s相关信息,过程和配置第一个k8s一样,不再过多说明了


创建流水线项目


打开Bule Ocean,请创建你的第一个流水线,选择代码仓库为Git

输入要创建流水线的Git项目仓库地址,输入后jenkins会自动生成公钥,把生成的公钥配置在gitlab的ssh key中,然后点击创建流水线

创建流水线时候,Jenkins会自动检测git项目各个分支的根目录是否存在文件“Jenkinsfile”,如果存在就生成一个分支流水线,下图中生成了分支master和分支docker流水线


到目前为止流水线已经配置完成,但是还无法实现自动触发构建,需要配置扫描多分支流水线触发器;设置1分钟检测一次

触发构建之前我们先看下流水线执行构建部署的Jenkinsfile文件内容,文件保存在git项目的各个分支

def label = "slave-$UUID.randomUUID().toString()"podTemplate(cloud: 'kubernetes', label: label, containers: [ containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true), containerTemplate(name: 'kubectl', image: 'bitnami/kubectl', command: 'cat', ttyEnabled: true),], volumes: [ hostPathVolume(mountPath: '/root/.kube', hostPath: '/root/.kube'), hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock')])  node(label)  def myRepo = checkout scm def gitCommit = myRepo.GIT_COMMIT def gitBranch = myRepo.GIT_BRANCH def imageTag = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim() def dockerRegistryUrl = "registry.cn-beijing.aliyuncs.com" def imageEndpoint = "addnewer-dsc/approval-fe" def image = "$dockerRegistryUrl/$imageEndpoint" stage('构建 Docker 镜像')  withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'DockerRegistry', usernameVariable: 'DOCKER_HUB_USER', passwordVariable: 'DOCKER_HUB_PASSWORD']])  container('docker')  echo "3. 构建 Docker 镜像阶段" sh """ docker login $dockerRegistryUrl -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASSWORD docker build -t $image:$imageTag . docker push $image:$imageTag """    stage('Run kubectl')  container('kubectl')  sh """ sed -i "s#<IMAGE>#$image#g" *.yaml sed -i "s#<IMAGE_TAG>#$imageTag#g" *.yaml kubectl apply -f . """   

podTemplate(cloud: 'kubernetes' 我们要实现不同分支部署到不同k8s环境,所以我们不同分支中Jenkinsfile中pod模版要指定不同的cloud,这个很重要;默认名称为kubernetes,这个名称对应我们在jenkins中新增k8s云的名称containerTemplate(name: 'docker' 指定我们需要构建镜像需要用到的docker镜像containerTemplate(name: 'kubectl' 指定我们部署容器到k8s需要用到的kubectl镜像hostPathVolume(mountPath: 上面指定镜像依赖的映射文件def imageTag 生成镜像tag名称def dockerRegistryUrl 定义docker仓库地址stage('构建 Docker 镜像') 定义流水线构建docker镜像执行步骤 credentialsId: 'DockerRegistry', 从jenkins全局凭据获取docker仓库id usernameVariable: 'DOCKER_HUB_USER',从jenkins全局凭据获取docker仓库用户名 passwordVariable: 'DOCKER_HUB_PASSWORD'从jenkins全局凭据获取docker仓库密码stage('Run kubectl') 定义流水线部署应用到k8s执行步骤,部署应用的yaml文件也是在git项目中后缀为yaml的文件

了解了上面的Jenkinsfile流水线文件的执行流程后,可以看到我们还没有在jenkins中添加docker仓库的全局凭据,下面我们添加


流水线构建测试


提交任何变更到git项目,1分钟后jenkins会自动检测到变更,开始执行流水线;这里我随便提交下测试代码,就可以看到流水开始执行了




相关参考

拆车五十铃6WA1发动机(一代经典日系车,老一辈卡车人都认识,五十铃810系列卡车历史回顾)

有读者想看五十铃810系列卡车,安排↓想必很多老一辈的卡车人都认识这款车,虽然车上的标识都是像CXZ、CVR这一类英文,但是它们仍然有一个统一的名字叫五十铃810。至于这个命名的由来,笔者Google了一下,其解释是810是一个...

拆车五十铃C190发动机(这辆车和奥迪一样做了局部包梁加强,你有了解过吗?)

...奥迪一样做了局部包梁加强,你有了解过吗?这就是庆铃五十铃700P五吨平板清障车。侧面图首先底盘车架做局部包梁加强有什么好处呢?它主要是增强前后底横梁与底盘的连接强度,加强车架底盘的整体刚性,减小离心力与车身扭...

拆车捷豹零件发动机(凯迪拉克、捷豹和沃尔沃,买时很轻松,后期维护费用受不了?)

作为二线豪华品牌,凯迪拉克、捷豹和沃尔沃也算是一个档次。捷豹在英国曾经是贵族品牌。它的质量并不逊色于路虎。自从与奇瑞合资后,身价和销量直线下降!美系代表车型凯迪拉克,曾经是总统座驾,也是网红座驾。风靡...

拆车内饰板专用工具(五十铃mu-X牧游侠:纵置四驱+采埃孚8AT带大梁还有后锁)

...得看它!种族优势作为一款非承载式车身的硬派SUV,江西五十铃-mu-X牧游侠(下文简称mu-X牧游侠)的悬架结构为前双叉臂,后悬架为整体桥式非独立悬架。后桥的整体桥设计,越野时一边车轮假若被抬高,整体桥会把差速器等车...

拆车助力泵(以为换个刹车助力泵就行了,结果自己傻眼了,问题一个接一个)

...步判断了刹车助力泵损坏,需更换新的。维修的这辆车是五十铃的勾臂车,刹车助力泵的安装位置也真的是难拆,需要拆完仪表盘,收音机等附件,才能拆出助力泵,一拆一装将近一天,哎这速度是不是有些慢了,反正挺

拆车波罗发动机(POLO发动机抖动怎么解决?)

最近有网友咨询POLO发动机抖动怎么解决?发动机的正常运行是一个非常复杂并且还是闭环控制的系统。在很多方面如果出现了问题,都有可能会引起发动机的抖动。根据我个人的维修经验,在以下2个最容易出现的方面跟大家分...

拆车奔驰560发动机(奔驰宝马发动机,少花几十万到手,超值)

发动机作为汽车的心脏几乎每个品牌都有自己的发动机像本田的地球梦福特的Ecoboost等等都是比较有名的然而在汽车界也有一些特例今天我们聊一聊那些共用发动机的品牌(车型)奔驰的慷慨?M274发动机奔驰E级、C级、GLC&英...

拆车绅宝发动机(北汽绅宝X35发动机共振问题难解决)

近日,专业的汽车质量投诉平台汽车门网收到车主投诉,称其购买的绅宝X35车型,发动机转速在1500-1800转之间时发动机震动明显,而且噪音明显加大,虽然厂商答应索赔更换部件,但是实际去4S店进行问题解决时,得到的结果是...

拆车本田24发动机(买发动机送车的本田,真有这么强?)

关于本田,车圈有这么一句话,叫做买发动机送车。光这句话就能看出本田发动机有多好。实际上,发动机虽然是汽车最为核心的部分,但它受时代框架和政策影响是比较大的,尤其是现在的环保政策越来越严格,全世界都在搞...

拆车发动机(为什么说日系发动机不能修,一拆开相当于被毁了)

消费者在选择车的时候,往往都非常关注所谓的三大件,发动机就是其中一个,可是发动机质量再好,也不能确保始终处于万全的状态,用久了以后都难免会有些小问题。一般来说,如果发动机有毛病的话,修车工采取的方式都...