前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jenkins-4:部署golang二进制文件到kubernetes

jenkins-4:部署golang二进制文件到kubernetes

作者头像
千里行走
发布2022-04-28 16:25:12
8340
发布2022-04-28 16:25:12
举报
文章被收录于专栏:千里行走千里行走

目录:

(1).制作Go服务镜像

(2).制作jenkins-jnlp-golang镜像

1.制作golang镜像

2.制作docker镜像

(3).golang-demo

(4).使用PipelineScript发布golang-demo到kubernetes

(5).使用Jenkenisfile发布golang-demo到kubernetes

(6).不足

(7).参考资料

(1).制作Go服务镜像

分两层:alpine-golang-basic和alpine-golang-common。basic是打基础软件包,和业务无相关性,common可能会根据不同的业务的要求有定制,如启动参数等,因为第一次你不太可能把这个规范做的很好,所以把基础包打到一个image,修改规范改common即可。

alpine-golang-basic:

https://gitee.com/future-cicd/jenkins-kubernetes-golang-demo/tree/master/alpine/alpine-golang-basic

这里边有坑。alpine是ubuntu linux,增加用户是adduser而不是useradd,而且要加-D参数(表示创建的用户没有密码)。

/app/3rd和/app/logs是一个规范定义,前者放golang可执行文件,后者放输出日志。

执行build.sh构建并推送镜像:是推送到我自己的harbor仓库。

alpine-golang-common:基于alpine-golang-basic构建。

https://gitee.com/future-cicd/jenkins-kubernetes-golang-demo/blob/master/alpine/alpine-golang-common/Dockerfile

执行build.sh完成构建并推送到harbor。

看一下image的大小:只有不到13MB,这就是为什么用alpine的原因。

(2).制作jenkins-jnlp-golang镜像

1.制作golang镜像

用于编译与构建golang应用。

docker pull golang:1.18

docker tag 4df7abb7452e harbor-core.qianlixinzou.com:31600/devops/jenkins-jnlp-golang:1.18

docker push harbor-core.qianlixinzou.com:31600/devops/jenkins-jnlp-golang:1.18

也可以直接执行:要换成你自己的harbor仓库。

https://gitee.com/future-cicd/jenkins-jnlp-slave/blob/master/jenkins-jnlp-golang/build.sh

2.制作docker镜像

准备了一个镜像。

https://gitee.com/future-cicd/jenkins-jnlp-slave/tree/master/jenkins-jnlp-docker/jenkins-jnlp-docker-19.03

内容:

将/root/.docker/config.json文件拷贝到Dockerfile同目录下。

构建镜像:

docker build . -t harbor-core.qianlixinzou.com:31600/devops/jenkins-jnlp-docker:19.03

或者直接执行脚本:

sh ./build.sh

推送到镜像仓库:

docker push harbor-core.qianlixinzou.com:31600/devops/jenkins-jnlp-docker:19.03

(3).golang-demo

golang-demo:

https://gitee.com/future-cicd/jenkins-kubernetes-golang-demo

很简单,就是一个web的helloworld。

(4).使用PipelineScript发布golang-demo到kubernetes

创建流水线任务:jenkins-kubernetes-golang-demo

选择pipeline script:

要填写的jenkins脚本内容:

https://gitee.com/future-cicd/jenkins-kubernetes-golang-demo/blob/master/jenkinsfile

代码语言:javascript
复制
podTemplate(cloud: 'kubernetes',nodeSelector: 'jenkins-jnlp=yes',
    containers: [
        containerTemplate(
            name: 'build-go', 
            //设置go代理,否则有些依赖包无法下载。
            envVars: [
                envVar(key: 'GO111MODULE', value: 'on'), 
                envVar(key: 'GOPROXY', value: 'https://goproxy.io'),
                envVar(key: 'CGO_ENABLED', value: 'p0'),
                envVar(key: 'GOOS', value: 'linux')
            ],
            image: 'harbor-core.qianlixinzou.com:31600/devops/jenkins-jnlp-golang:1.18', 
            ttyEnabled: true,
            command: 'cat'
        ),
        containerTemplate(
            name: 'build-image',
            ttyEnabled: true,
            image: 'harbor-core.qianlixinzou.com:31600/devops/jenkins-jnlp-docker:19.03'
        ),
    ],
    //需要将docker和kubectl挂在到pod中这样才可以在pod中与k8s进行联通操作。
    volumes: [
        hostPathVolume(hostPath: '/run/docker.sock', mountPath: '/run/docker.sock'),
        hostPathVolume(hostPath: '/etc/docker', mountPath: '/etc/docker'),
        hostPathVolume(hostPath: '/root/.kube', mountPath: '/root/.kube'),
        hostPathVolume(hostPath: '/usr/bin/kubectl', mountPath: '/usr/bin/kubectl')
    ]
 )
 {
    node(POD_LABEL) {
        stage('build-code') {
            // 从git仓库拉取代码
            checkout([
                $class: 'GitSCM', 
                branches: [
                    [name: "master"]
                ],
                browser: [$class: 'GitLab', repoUrl: 'https://gitee.com/future-cicd/jenkins-kubernetes-golang-demo.git'], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [], 
                submoduleCfg: [], 
                userRemoteConfigs: [
                    [url: "https://gitee.com/future-cicd/jenkins-kubernetes-golang-demo.git"]
                ]
            ])
            container('build-go') {
                stage('Build a go project') {
                    // 将go应用构建名为app的可执行二进制文件
                    sh "go mod tidy"
 //必须加CGO_ENABLED=0,因为alpine缺少很多go程序依赖的lib库,不加会提示你app not found,你需要ldd app才能看到需要依赖的lib库。
                    sh "CGO_ENABLED=0 go build -o app "
                }
            }
        }
        stage('build-image') {
            container('build-image') {
                stage('build docker iamge'){
                    sh """
                        echo "FROM harbor-core.qianlixinzou.com:31600/devops/alpine-golang-common:0.1" > ./Dockerfile
                        echo "ADD ./app /app/3rd/" >> ./Dockerfile
                        cat ./Dockerfile
                    """
                    sh "docker build ./ -t harbor-core.qianlixinzou.com:31600/devops/jenkins-kubernetes-golang-demo:0.1 && docker push harbor-core.qianlixinzou.com:31600/devops/jenkins-kubernetes-golang-demo:0.1"
                }
            }
        }
        stage('deploy'){
            container('build-image') {
                stage('deploy'){
                    //我这里delete是因为我的这台机器配置不够,防止一些异常如replicset有多个造成困扰,实际生产没有这个delete
                    sh "kubectl delete -f jenkins-kubernetes-golang-demo.yaml"
                    sh "kubectl apply -f jenkins-kubernetes-golang-demo.yaml"
                }
            }
        }
    }
}

然后执行构建即可。

进入容器验证OK:

(5).使用Jenkenisfile发布golang-demo到kubernetes

保存后执行构建,验证OK。

(6).不足

这个demo仅仅是走通,实际上并不优雅,与生产有很大差距,不过这个差距仅仅在于工程合理规划方面。后续会整理。

(7).参考资料

1.golang交付kubernetes

https://www.cnblogs.com/jasonminghao/p/12769072.html

2.Jenkins的pipeline实践之GitSCM参数配置项详解

https://wiki.eryajf.net/pages/ff3332/

3.git-Parameter插件在pipeline共享库中的实践详解

https://wiki.eryajf.net/pages/5328.html#%E9%87%8D%E8%A6%81%E5%8F%82%E6%95%B0

4.Pipeline: SCM Step

https://www.jenkins.io/doc/pipeline/steps/workflow-scm-step/#-checkout-%20check%20out%20from%20version%20control

5.基于 Jenkins 的 CI/CD (二)

https://www.qikqiak.com/k8s-book/docs/37.Jenkins%20Pipeline.html

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-04-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 千里行走 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档