4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kanikoによるコンテナビルドをlocal環境で試す

Last updated at Posted at 2019-05-08

Dockerやkubernetesのコンテナをビルドする際、Docker Buildは管理者権限が必要となる。

※ 追記: 19年7月に出たDocker19.03から非管理者権限モードが追加されました。

コンテナのビルドをローカルアカウントで実行可能なkanikoを使ってみる。

kanikoはAPIを使用してkubernetes上のコンテナ内でビルドし、ビルド済みのコンテナをDocker hubなどのレジストリサービスにプッシュするところまで実行してくれる。

Kaniko
https://github.com/GoogleContainerTools/kanikoKaniko
Docker hub
https://cloud.docker.com/

1. Dockerfileの準備

Dockerfileは下記に格納
C:\k\share\kaniko\Dockerfile

1.1 Dockerfile

 FROM ubuntu:18.04
 #sshを入れる
 RUN apt update
 RUN apt install -y ssh

2. Secretの作成

Docker hubのログイン情報をkubernetesのsecretで作る

# kubectl create secret docker-registry regcred --docker-server=your-registry-server --docker-username=your-name --docker-password=your-pword --docker-email=your-email

ここで、
your-registry-server :Docker レジストリのFQDN
Docker hubの場合は
https://index.docker.io/v1/
your-name :Docker hubのユーザーネーム
your-pword :Docker hubのパスワード
your-email :Docker hubに登録したEmailアドレス

例(XXXXXXXはパスワード、@XXX.comはメールのドメインに適宜変更)

# kubectl create secret docker-registry regcred --docker-server=https://index.docker.io/v1/ --docker-username=tetrar124 --docker-password=xxxxxx --docker-email=tetrar124@xxx.com

3. Repositoryの作成

3.1 Docker hubにログインし、上の方にあるRepositoryを選択

image.png

3.2 Create Repositoryを押す

image.png

3.3 名前を入れてCreate(今回のリポジトリ名:ubuntu-ssh1)

image.png

4. kaniko用Persistent Volume、Persistent Claim Volumeの作成

4.1 Persistent Volume

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv004
  labels:
    type: kaniko
spec:
  capacity:
    storage: 1Gi
  # volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: hostpath
  local:
    #Dockerfileを格納したフォルダをマウント
    path: /C/k/share/kaniko
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop   

Persistent Volumeの作成

kubectl apply -f pv.yaml

4.2 Persistent Volume Claim

pvc.yam


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: kaniko-workspace
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi  
  storageClassName: hostpath
  selector:
    matchLabels:
      #PVの指定
      type: kaniko

Persistent Volume Claimの作成

kubectl apply -f pvc.yaml

4. kanikoポッドの作成

kanikoPod.yaml


apiVersion: v1
kind: Pod
metadata:
  name: kaniko
spec:
  containers:
    #KanikoのAPIを指定
    - image: gcr.io/kaniko-project/executor:latest
      args:
      #Dockerfileの場所    
      - --dockerfile=/mnt/Dockerfile
      #Dockerfileの場所 
      - --context= /mnt
      # ビルド後のリポジトリ名
      #- --destination=ユーザーネーム/リポジトリ名
      - --destination=tetrar124/ubuntu-ssh1
      name: kaniko
      volumeMounts:
        - name: mount-volume
          mountPath: /mnt
        - name: kaniko-secret
          mountPath: /root
  restartPolicy: Never
  volumes:
    #kanikoのワークスペース用ボリューム
    - name: mount-volume
      persistentVolumeClaim:
        #PVCの指定
        claimName: kaniko-workspace
      volumes:
    #secretによるDocker レジストリへのログイン
    - name: kaniko-secret
      secret:
        secretName: regcred
        items:
          - key: .dockerconfigjson
            path: .docker/config.json

KanikoのPod作成

# kubectl apply -f kanikoPod.yaml

これでビルド完成。

5. ビルド済みコンテナを使ってみる

ubuntu.yaml


apiVersion: v1
kind: Pod
metadata:
  name: ubuntu18
  labels:
    app: ubuntu18
spec:
  containers:
    #ビルドしたコンテナを指定
    - image: tetrar124/ubuntu-ssh1
      name: ubuntu18
      command:
        - sleep
        - infinity
  restartPolicy: OnFailure
  #secretによるレジストリサービスへのログイン
  imagePullSecrets:
    - name: regcred

ビルド済みコンテナのデプロイ

# kubectl apply -f ubuntu.yaml

  
Kanikoはローカル以外にGCPやAWSなどクラウドでも実行可能。
その辺りは本家に詳しい。
https://github.com/GoogleContainerTools/kanikoKaniko

以上

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?