当前位置: 首页 > 工具软件 > kaniko > 使用案例 >

Kaniko push 镜像到私有镜像仓库

龙高歌
2023-12-01

目录

创建ns

获取kaniko镜像

私有镜像仓库需要账号密码

创建docker-registry secret

查看刚刚生成的secret

 对secret进行base64解码

使用Local Directory作为build context

hostpath方式

pvc方式

使用Standard Input作为build context

docker方式

k8s方式

registry不需要账号密码


创建ns

把所有与kaniko相关的全部放在kaniko这个ns下。

kubectl create ns kaniko

获取kaniko镜像

gcr上的镜像不好拉,我已经放在dockerhub上了。这些镜像我自己就在用。

可以在自己的镜像仓库生成这些镜像的(https://blog.csdn.net/u010918487/article/details/108084651)

(在dockerhub上看到明明只有20+M,不知道为啥pull下来有70+M)

// latest
docker pull xkfen/kaniko:v1.0.0

// debug
docker pull xkfen/kaniko:debug

// warmer
docker pull xkfen/kaniko:warmer

私有镜像仓库需要账号密码

创建docker-registry secret

// docker-email可选,可以不要
kubectl create secret docker-registry <secret名称> --docker-server=<镜像仓库地址> --docker-username=<私有镜像仓库登录的用户名> --docker-password=<私有镜像仓库登录的密码> --docker-email=<私有镜像仓库的email> -n kaniko

如:
kubectl create secret docker-registry regcred --docker-server=http://harbor.it.cn/harbor --docker-username=admin --docker-password=Harbor12345 -n kaniko

查看刚刚生成的secret

[root@m1 demo1]# kc get secret regcred -n kaniko -o yaml
apiVersion: v1
data:
  .dockerconfigjson: eyJhdXRocyI6eyJodHRwOi8vaGFyYm9yLxxxxxxxxx
kind: Secret
metadata:
  creationTimestamp: "2020-08-26T12:07:58Z"
  name: regcred
  namespace: kaniko
  resourceVersion: "38285497"
  selfLink: /api/v1/namespaces/kaniko/secrets/regcred
  uid: 453c3148-57d9-4ca6-acae-8edc68a46ede
type: kubernetes.io/dockerconfigjson

 对secret进行base64解码

[root@m1 demo1]# echo xxxxx | base64 -d

私有镜像仓库不需要账号密码的,就不用创建这个secret

在后面的pod.yaml中也不需要挂载kaniko-secret。

使用Local Directory作为build context

1. 由于使用的是Local Directory,因此pod运行的节点一定要有Local Directory。

    比如我的Local Directory在m1.env.lab.io节点,因此kaniko pod运行的时候也要指定在这个节点

2. 如果私有仓库地址不是https的,是http的,那么就要设置insecure参数为true,这个参数默认为false的。设置为true代表使用http把镜像push到镜像仓库。(Push to insecure registry using plain HTTP)

hostpath方式

pod.yaml

[root@m1 demo1]# hostname
m1.env.lab.io

[root@m1 demo1]# pwd
/root/kaniko/demo1

[root@m1 demo1]# cat Dockerfile 
FROM ubuntu
ENTRYPOINT ["/bin/bash", "-c", "echo hello"]

[root@m1 demo1]# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: kaniko-harbor
  namespace: kaniko
spec:
  nodeName: m1.env.lab.io
  containers:
  - name: kaniko
    image: xkfen/kaniko:v1.0.0
    args: ["--verbosity=trace",
           "--log-format=color",
           "--dockerfile=Dockerfile",
           "--context=dir:///workspace/",
           "--destination=harbor.it.cn/it/buildpack-linux:v2.0-ubuntu-test2.1",
           "--skip-tls-verify=true",
           "--insecure=true"]
    volumeMounts:
      - name: dockerfile-storage
        mountPath: /workspace/
      - name: kaniko-secret
        mountPath: /kaniko/.docker
  restartPolicy: Never
  volumes:
    - name: kaniko-secret
      secret:
        secretName: regcred
        items:
          - key: .dockerconfigjson
            path: config.json
    - name: dockerfile-storage
      hostPath:
         path: /root/kaniko/demo1

pvc方式

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: kaniko-pvc
  namespace: kaniko
spec:
  nodeName: m1.env.lab.io
  containers:
  - name: kaniko
    image: xkfen/kaniko:v1.0.0
    args: ["--verbosity=trace",
           "--log-format=color",
           "--dockerfile=Dockerfile",
           "--context=dir:///workspace/",
           "--destination=harbor.it.cn/it/buildpack-linux:v2.0-ubuntu-test2",
           "--skip-tls-verify=true",
           "--insecure=true"]
    volumeMounts:
      - name: dockerfile-storage
        mountPath: /workspace/
      - name: kaniko-secret
        mountPath: /kaniko/.docker
  restartPolicy: Never
  volumes:
    - name: kaniko-secret
      secret:
        secretName: regcred
        items:
          - key: .dockerconfigjson
            path: config.json
    - name: dockerfile-storage
      persistentVolumeClaim:
        claimName: dockerfile-claim

volume.yaml

[root@m1 demo1]# cat volume.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: dockerfile
  labels:
    type: local
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: local-storage
  hostPath:
    path: /root/kaniko/demo1

volume-claim.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: dockerfile-claim
  namespace: kaniko
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi
  storageClassName: local-storage

使用Standard Input作为build context

docker方式

echo -e 'FROM alpine \nRUN echo "created from standard input"' > Dockerfile | tar -cf - Dockerfile | gzip -9 | docker run \
  --interactive -v $(pwd):/workspace xkfen/kaniko:v1.0.0 \
  --context tar://stdin \
  --destination=registry.local/it/buildpack-linuxk:v2.0-alpine

k8s方式

registry不需要账号密码

echo -e 'FROM alpine \nRUN echo "created from standard input"' > Dockerfile | tar -cf - Dockerfile | gzip -9 | kubectl run kaniko-input-k8s \
--rm --stdin=true \
--image=xkfen/kaniko:latest --restart=Never \
--overrides='{
  "apiVersion": "v1",
  "spec": {
    "containers": [
      {
        "name": "kaniko",
        "image": "xkfen/kaniko:v1.0.0",
        "stdin": true,
        "stdinOnce": true,
        "args": [
          "--dockerfile=Dockerfile",
          "--context=tar://stdin",
          "--destination=registry.local/it/buildpack-linux-k8s:v2.0-alpine"
        ]
      }
    ]
  }
}

 

 类似资料: