DevOps之 基于gitlab-ci的 docker in docker 运行管理体系

Gitlab CI/CD 实现Kind部署运行(类似于github helm/kind action)

Posted by 董江 on Monday, March 18, 2024

Gitlab CI/CD 实现Kind部署运行(类似于github helm/kind action)

背景

需要在docker中运行docker服务, 可能是 docker in docker in docker 模式

因此,有必要的基础image管理服务

基础镜像

ARG GO_VERSION=1.22.1-alpine3.19
ARG CLI_VERSION=20.10.2
ARG ALPINE_VERSION=3.19.0
ARG GOLANGCI_LINT_VERSION=v1.56.2-alpine

FROM golang:${GO_VERSION} AS builder
RUN apk add --no-cache \
    bash \
    git \
    make \
    curl \
    wget \
    docker

####
# golangci-lint
####
FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION} AS lint-base

####
# gotestsum
####
FROM alpine:${ALPINE_VERSION} AS gotestsum
RUN apk add --no-cache \
    tar \
    wget
# install gotestsum
WORKDIR /root
ARG ARCH=amd64
ARG OS=linux
ARG GOTESTSUM_VERSION=1.11.0
RUN wget https://github.com/gotestyourself/gotestsum/releases/download/v${GOTESTSUM_VERSION}/gotestsum_${GOTESTSUM_VERSION}_${OS}_${ARCH}.tar.gz -nv -O - | tar -xz

####
# gosec
####
FROM alpine:${ALPINE_VERSION} AS gosec
RUN apk add --no-cache \
    tar \
    wget
# install gosec
WORKDIR /root
ARG ARCH=amd64
ARG OS=linux
ARG GOSEC_VERSION=2.19.0
RUN wget https://github.com/securego/gosec/releases/download/v${GOSEC_VERSION}/gosec_${GOSEC_VERSION}_${OS}_${ARCH}.tar.gz -nv -O - | tar -xz

FROM builder AS lint
COPY --from=lint-base /usr/bin/golangci-lint /usr/bin/golangci-lint
COPY --from=gotestsum /root/gotestsum /usr/local/bin/gotestsum
COPY --from=gosec /root/gosec /usr/local/bin/gosec
RUN go install github.com/axw/gocov/gocov@latest && mv ${GOPATH}/bin/gocov /usr/local/bin/gocov
RUN go install github.com/AlekSi/gocov-xml@latest && mv ${GOPATH}/bin/gocov-xml /usr/local/bin/gocov-xml
ARG ARCH=amd64
ARG OS=linux
ARG KIND_VERSION=0.19.0
ARG KUBECTL_VERSION=1.29.1
RUN curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v${KUBECTL_VERSION}/bin/${OS}/${ARCH}/kubectl && \
    chmod +x kubectl && \
    mv kubectl /usr/local/bin/ && \
    curl -Lo kind https://github.com/kubernetes-sigs/kind/releases/download/v${KIND_VERSION}/kind-${OS}-${ARCH} && \
    chmod +x kind && \
    mv kind /usr/local/bin/

kind 部署的两种方式

e2e:kind:
  retry: 2
  stage: e2e
  image: ${DEFAULT_IMAGE}
  tags:
    - shell #本地shell模式,使用host os的软件
  before_script:
    - docker info
    - kind create cluster --image artifacts.iflytek.com/docker-repo/kindest/node:v1.25.3 --config .gitlab/kind-config.yml -v 4 --name $CI_PIPELINE_ID --wait 180s
    - kubectl cluster-info --context kind-$CI_PIPELINE_ID
  needs:
    - build:images
  script:
    - kubectl get nodes --insecure-skip-tls-verify=true
  after_script:
    - kind delete cluster --name $CI_PIPELINE_ID

e2e:kind2:
  retry: 2
  stage: e2e
  image: ${DEFAULT_IMAGE}
  tags:
    - docker #本地docker模式,docker in kind in HostOS docker
  services:
    - 
      name: artifacts.iflytek.com/docker-repo/library/docker:25.0.3-dind
      alias: docker
      command: ["dockerd", "-H", "tcp://0.0.0.0:2375", "--tls=false", "--selinux-enabled=false", "--exec-opts", "native.cgroupdriver=systemd"]
  before_script:
    - docker info
    - kind create cluster --image artifacts.iflytek.com/docker-repo/kindest/node:v1.25.3 --config .gitlab/kind-config.yml -v 4 --name $CI_PIPELINE_ID --wait 180s
    - REAL_IP=$(ip route|awk '/default/ { print $3 }')
    - sed -i -e "s/0.0.0.0/$REAL_IP/g" $HOME/.kube/config
  needs:
    - build:images
  script:
    - kubectl get nodes --insecure-skip-tls-verify=true
  after_script:
    - kind delete cluster --name $CI_PIPELINE_ID

方式

「如果这篇文章对你有用,请随意打赏」

Kubeservice博客

如果这篇文章对你有用,请随意打赏

使用微信扫描二维码完成支付