Shipwright是一个构建和发布工具,它是一个Tekton的扩展,可以用来构建镜像并发布到dockerhub。

大多数人可能对Jenkins比较熟悉,而Tekton是一个类似的工具,但它是一个云原生的工具,它的配置是通过yaml文件来定义的,这样可以很好的和gitops结合起来。

如果deployment的规模比较大,可能使用Tekton会更好点,因为它的初始设计就是为了大规模的CI/CD。

以下的内容是对github上的一个repo hello-python项目,构建镜像,并发布到dockerhub上。。

前提条件

  • 本地已经有个k8s集群
  • 已经有一个dockerhub账号

安装shipwright

先安装Tekton pipeline

kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/previous/v0.50.5/release.yaml

安装Shipwright

kubectl apply --filename https://github.com/shipwright-io/build/releases/download/v0.13.0/release.yaml --server-side
curl --silent --location https://raw.githubusercontent.com/shipwright-io/build/v0.13.0/hack/setup-webhook-cert.sh | bash
curl --silent --location https://raw.githubusercontent.com/shipwright-io/build/main/hack/storage-version-migration.sh | bash

安装 Shipwright build strategies

kubectl apply -f \
https://github.com/shipwright-io/build/releases/download/v0.13.0/sample-strategies.yaml

查看安装情况

ClusterBuildStrategy

构建镜像

将dockerhub的credentials保存到secret中

dockerhub-token

REGISTRY_SERVER=https://index.docker.io/v1/ REGISTRY_USER=meirongdev REGISTRY_PASSWORD=Dockerhub上的Personal Access Token
kubectl create secret docker-registry push-secret \
    --docker-server=$REGISTRY_SERVER \
    --docker-username=$REGISTRY_USER \
    --docker-password=$REGISTRY_PASSWORD  \
    --docker-email=[email protected]

可以通过kubectl get secret查看secret是否创建成功。

创建构建任务

在shipwright中可以使用以下工具来创建容器镜像

  • kaniko
  • Cloud Native Buildpacks
  • BuildKit
  • Buildah

这里我们使用kaniko来构建镜像。

apiVersion: shipwright.io/v1beta1
kind: Build
metadata:
  name: kaniko-py-build
spec:
  source:
    type: Git
    git:
      url: https://github.com/meirongdev/hello_python
    contextDir: ./
  strategy:
    name: kaniko
    kind: ClusterBuildStrategy
  output:
    image: docker.io/meirongdev/hello-py:latest
    pushSecret: push-secret
kubectl apply -f ./build-kaniko.yaml
kubectl get builds

kubectl get builds

可以看到该Build任务是注册成功了,接下来就需要执行这个任务。需要BuildRun来执行Build任务。

apiVersion: shipwright.io/v1beta1
kind: BuildRun
metadata:
  generateName: kaniko-py-buildrun-
spec:
  build:
    name: kaniko-py-build
kubectl create -f ./buildrun-kaniko.yaml

kubectl get buildruns

此时可以在dockerhub上看到镜像已经构建成功了。

相关代码