K8s 集成 Jenkins 部署Go Web项目

时间:2022-11-29 07:53:40

风离不摆烂学习日志 Day9 K8s 集成 Jenkins 部署Go Web项目

K8s 集成 Jenkins 部署Go Web项目

Dockerfile

FROM golang:alpine as builder
# 需要go环境
MAINTAINER fl1906

WORKDIR /work
# 源
RUN go env -w GOPROXY=https://goproxy.cn && go env -w CGO_ENABLED=0 && go env -w GO111MODULE=on
COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN go build -o main main.go


FROM alpine:latest
# 设置时区
RUN apk add --no-cache tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" >  /etc/timezone

WORKDIR /
# 复制到工作区
COPY --from=builder /work/ ./
# COPY --from=builder /work/config ./config
# 对外端口
EXPOSE 8080
# 执行
CMD ["./main"]

踩坑 build的时候走到下载mod包会报错:“dial tcp: lookup goproxy.cn on 223.6.6.6:53: read udp 172.17.0.2:41803->223.6.6.6:53: i/o timeout”。

go语言 Dockerfile builddial tcp: lookup 报错解决方案

build的时候走到下载mod包会报错:“dial tcp: lookup goproxy.cn on 223.6.6.6:53: read udp 172.17.0.2:41803->223.6.6.6:53: i/o timeout”。

开始以为是DNS问题,然后怀疑是代理不对?最终排除了这两个部分,于是大胆的考虑容器里是不是不通外网?

还真是如此。

因为之前的build指令没有特殊指定网络,所以修正指令后如下:

docker build --network host ...

即可正常build镜像。

Jenkinsfile

pipeline {
  agent {
    node {
      label 'go16'
    }

  }
  stages {
    stage('从仓库拉取代码') {
      agent none
      steps {
        container('base') {
          git(credentialsId: 'gitee-id', url: 'https://gitee.com/fl1906249647/learning-gin.git', branch: 'master', changelog: true, poll: false)
        }

      }
    }

    stage('构建推送镜像到阿里云仓库 ') {
      agent none
      steps {
        container('go') {
          sh 'docker build --network host -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/go-admin:SNAPSHOT-$BUILD_NUMBER .'
          withCredentials([usernamePassword(credentialsId : 'aliyun-docker-registry' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
            sh 'docker login  -u "$DOCKER_USER_VAR" -p "$DOCKER_PWD_VAR" $REGISTRY'
            sh 'docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/go-admin:SNAPSHOT-$BUILD_NUMBER '
          }

        }

      }
    }

         stage('部署到线上') {
                     steps {
                         container ('go') {
                              withCredentials([
                                  kubeconfigFile(
                                  credentialsId: env.KUBECONFIG_CREDENTIAL_ID,
                                  variable: 'KUBECONFIG')
                                  ]) {
                                  sh 'envsubst < deploy/go-admin.yaml | kubectl apply -f -'
                              }
                         }
                     }
        }

  }
  environment {
    KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
    REGISTRY = 'registry.cn-hangzhou.aliyuncs.com'
    DOCKERHUB_NAMESPACE = 'choose-course-system'
    APP_NAME = 'learning-gin'
  }
}

go-admin.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: go-admin
  name: go-admin
  namespace: go-admin   #一定要写名称空间
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  selector:
    matchLabels:
      app: go-admin
  strategy:
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: go-admin
    spec:
      imagePullSecrets:
        - name: aliyun-docker-registry  #提前在项目下配置访问阿里云的账号密码
      containers:
        - image: $REGISTRY/$DOCKERHUB_NAMESPACE/go-admin:SNAPSHOT-$BUILD_NUMBER
          imagePullPolicy: Always
          name: app
          ports:
            - name: http-admin
              containerPort: 8080
              protocol: TCP
          resources:
            limits:
              cpu: '3'
              memory: 3000Mi
            requests:
              cpu: 60m
              memory: 50Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: go-admin
  name: go-admin
  namespace: go-admin
spec:
  ports:
    - name: http
      port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    app: go-admin
  sessionAffinity: None
  type: ClusterIP

代码推送至仓库 自动触发流水线

在Gitee仓库配置 WebHook 提交代码

K8s 集成 Jenkins 部署Go Web项目

项目代码结构

K8s 集成 Jenkins 部署Go Web项目

成功

K8s 集成 Jenkins 部署Go Web项目