8
13

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.

Kubernetes 初期化専用コンテナで、ポッド内共有ボリュームにデータを取り込む

Posted at

ポッドを起動する過程で、初期化専用のコンテナを起動して、共有ボリュームに最新データを取り込む事ができます。この初期化専用コンテナの利用方法を確認した備忘録です。

次の initContainers を利用する事で、ポッド起動時に一回だけ実行されるコンテナを利用できる様になります。

  initContainers:
  - image: maho/git_client:v1
    name: tc-3

前半はローカルで、git cloneができるコンテナを作成して、DockerHubへ登録する過程です。後半が、initContainersの検証です。

手作業で git clone できるコンテナを作る

$ docker run -it ubuntu:latest bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
1be7f2b886e8: Pull complete 
6fbc4a21b806: Pull complete 
c71a6f8e1378: Pull complete 
4be3072e5a37: Pull complete 
06c6d2f59700: Pull complete 
Digest: sha256:e27e9d7f7f28d67aa9e2d7540bdc2b33254b452ee8e60f388875e5b7d9b2b696
Status: Downloaded newer image for ubuntu:latest
root@0bcc429a1330:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:3 http://security.ubuntu.com/ubuntu xenial-security/universe Sources [58.3 kB]
...
Get:20 http://archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [5162 B]                                                            
Get:21 http://archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [7179 B]                                                        
Fetched 24.8 MB in 8s (2949 kB/s)                                                                                                                
Reading package lists... Done
root@0bcc429a1330:/# apt-get install curl git -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21) ...
Processing triggers for ca-certificates (20170717~16.04.1) ...
Updating certificates in /etc/ssl/certs...
148 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
root@0bcc429a1330:/# exit
exit

コンテナをイメージにして、DockerHubへ登録する

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
0bcc429a1330        ubuntu:latest       "bash"              2 minutes ago       Exited (0) 5 seconds ago                       laughing_ardinghelli
$ docker commit -m "git & curl client" 0bcc429a1330 git_client:v1
sha256:b3103623e79457357d0e53dd56e81cec20054894d1dbcbbb6cb719e4325db195
$ docker images
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
git_client                                 v1                  b3103623e794        6 seconds ago       242MB
...

DockerHubに登録する

$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (maho): maho
Password: 
Login Succeeded
$ docker tag git_client:v1 maho/git_client:v1
$ docker push maho/git_client:v1
The push refers to repository [docker.io/maho/git_client]
ea165cea7a75: Pushed 
6f4ce6b88849: Mounted from library/ubuntu 
92914665e7f6: Mounted from library/ubuntu 
c98ef191df4b: Mounted from library/ubuntu 
9c7183e0ea88: Mounted from library/ubuntu 
ff986b10a018: Mounted from library/ubuntu 
v1: digest: sha256:23a07d2fa7b5cf161fcc218ce60f40f872e1712051d6cff813cf37b1892d068f size: 1569
$ docker images 
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
git_client                                 v1                  b3103623e794        12 minutes ago      242MB
maho/git_client                            v1                  b3103623e794        12 minutes ago      242MB

初期化のためのコンテナを設定

ポッドの初期化のために、一時的に動作するコンテナを定義できます。
このポッド初期化専用のコンテナを利用して、GitHubからコンテンツをクローンして、コンテンツをローカルに取得して、ポッドを起動します。 クローンする先は、ポッド内の共有ディスク領域で、ウェブサーバーやアプリサーバーのコンテナから、コンテンツをアクセスできます。

以下が、今回の初期化コンテナのコア部分です。

  initContainers:
  - image: maho/git_client:v1
    name: tc-3

次のYAMLは、サービスのためのポッドと初期化のためのポッドを記述したYAML全体です。

t083_host_vol.yml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-3
spec:
  containers:
  # Web-Server
  - image: ubuntu:latest
    name: tc-1
    volumeMounts:
    - mountPath: /var/www1
      name: cache-volume
    command: ["tail",  "-f", "/dev/null"]
  # App-Server
  - image: ubuntu:latest
    name: tc-2
    volumeMounts:
    - mountPath: /var/www2
      name: cache-volume
    command: ["tail",  "-f", "/dev/null"]
  initContainers:
  # Contents-Puller  it is short life container for pulling contents
  - image: maho/git_client:v1
    name: tc-3
    volumeMounts:
    - mountPath: /var/www3
      name: cache-volume
    command: ["git",  "clone", "https://github.com/takara9/k8s_webpage", "/var/www3"]
  volumes:
  - name: cache-volume
    emptyDir: {}

確認

ポッドのコンテナの一つに入って、GitHubから、コンテンツをクローンできている事を確認します。 確かにGitHubからコンテンツがダウンロードされています。

$ kubectl exec -c tc-1 -it test-pod-3 bash
root@test-pod-3:/# df
Filesystem              1K-blocks    Used Available Use% Mounted on
overlay                 103079200 8105640  89714400   9% /
tmpfs                     2048012       0   2048012   0% /dev
tmpfs                     2048012       0   2048012   0% /sys/fs/cgroup
/dev/xvda2               25091960 4804744  20270832  20% /etc/hosts
/dev/mapper/docker_data 103079200 8105640  89714400   9% /etc/hostname
shm                         65536       0     65536   0% /dev/shm
tmpfs                     2048012      12   2048000   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                     2048012       0   2048012   0% /sys/firmware

root@test-pod-3:/# cd /var/www1
root@test-pod-3:/var/www1# ls -al
total 40
drwxrwxrwx 4 root root 4096 Feb  7 01:29 .
drwxr-xr-x 1 root root 4096 Feb  7 01:29 ..
drwxr-xr-x 8 root root 4096 Feb  7 01:29 .git
-rw-r--r-- 1 root root 1068 Feb  7 01:29 LICENSE
-rw-r--r-- 1 root root  924 Feb  7 01:29 README.md
-rwxr-xr-x 1 root root  268 Feb  7 01:29 build_batch_syd.sh
-rwxr-xr-x 1 root root  260 Feb  7 01:29 build_batch_us.sh
drwxr-xr-x 3 root root 4096 Feb  7 01:29 container-web-nginx
-rw-r--r-- 1 root root  841 Feb  7 01:29 nginx-ingress-dal10.yml
-rw-r--r-- 1 root root  822 Feb  7 01:29 nginx-ingress-tok02.yml

参考資料

8
13
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
8
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?