jenkins-4:部署golang二进制文件到kubernetes
前置阅读:
目录:
(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
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" > ./Dockerfileecho "ADD ./app /app/3rd/" >> ./Dockerfilecat ./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有多个造成困扰,实际生产没有这个deletesh "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
