使用kaniko构建镜像

发表于 Kubernetes 分类,标签:

创建拉取镜像的secrets,使用场景为pods创建的过程中,kaniko在私有仓库中,需要设置

kubectl create secret docker-registry harbor-regsecret-pull \
  --docker-server=harbor.h.idx.ee \
  --docker-username=user-pull \
  --docker-password=a123456


创建推送镜像的secrets,使用场景是将此权限通过挂载到kaniko容器内,当构建完进项之后,用此权限推送至目标仓库

kubectl create secret docker-registry harbor-regsecret-push \
  --docker-server=harbor.h.idx.ee \
  --docker-username=user-push \
  --docker-password=a123456


创建命令格式

kubectl create secret docker-registry regcred \
  --docker-server=<你的镜像仓库服务器> \
  --docker-username=<你的用户名> \
  --docker-password=<你的密码> \
  --docker-email=<你的邮箱地址>


其他备用创建方案,仅做参考

kubectl create secret generic harborsecret --from-file=.dockerconfigjson=/root/.docker/config.json --type=kubernetes.io/dockerconfigjson



因为是验证,所有这里的Dockerfile使用configMap来做挂载,正常应该是程序生成出来,或者通过接口获取,或者提前准备,为凸显其它一些特性,此configMap文件有部分和此实验无关的信息,最终用到的只有名称,以及data里面的dockerile,文件如下

kubectl apply -f dockerfile.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: dockerfile
data:
  # 类属性键;每一个键都映射到一个简单的值
  player_initial_lives: "3"
  ui_properties_file_name: "user-interface.properties"
  # 类文件键
  game.properties: |
    enemy.types=aliens,monsters
    player.maximum-lives=5
  dockerfile: |
    FROM nginx:1.21.1-alpine
    RUN apk add busybox-extras curl \
        && date > /root/date.log



创建pod,文件如下

kubectl apply -f kanilo-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: kaniko
spec:
  imagePullSecrets:
    - name: harbor-regsecret-pull
  restartPolicy: Never
  containers:
  - name: kaniko
    image: harbor.h.idx.ee/kaniko/kaniko:v1.6.0
    args: ["--dockerfile=/workspace/Dockerfile",
          "--context=/workspace/",
          "--destination=harbor.h.idx.ee/kanikotest/kaniko-nginx-test:v1"]
    volumeMounts:
      - name: kaniko-secret
        mountPath: /kaniko/.docker/config.json
        readOnly: true
        subPath: config.json
      - name: dockerfile
        mountPath: /workspace/Dockerfile
        readOnly: true
        subPath: dockerfile
  
  volumes:
    - name: kaniko-secret
      secret:
        secretName: harbor-regsecret-push
        items:
          - key: .dockerconfigjson
            path: config.json
    - name: dockerfile
      configMap: 
        defaultMode: 420
        name: dockerfile
        items: 
          - key: dockerfile
            path: dockerfile


kaniko执行过程日志如下

image.png


harbor镜像仓库

image.png

image.png



注意:

1、推送的权限还可以也是用configMap,也可以用普通secrets

2、权限必须挂载到 容器里面的   /kaniko/.docker/config    ,应该是默认位置,也许指定参数改变,暂未验证

3、考虑到kaniko构建特性,为提高构建效率,应该还要添加共享挂载盘做为cache、还需要看一下kaniko的使用说明,参数用法

4、暂未涉及到拉取代码、sonar扫描、编译出成品二进制包(或者其它) 等步骤,有时间之后会进一步研究。


参考文档

https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/

https://github.com/GoogleContainerTools/kaniko/releases/tag/v1.6.0

https://segmentfault.com/a/1190000039713484?utm_source=sf-similar-article

https://github.com/GoogleContainerTools/kaniko#running-kaniko-in-a-kubernetes-cluster





0 篇评论

发表我的评论