Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

コンテナ用OS CoreOSを使ってみる


LinuCエヴァンジェリストの鯨井貴博@opensourcetechです。

今回は、コンテナ用途のOSである CoreOSを使ってみます。
f:id:opensourcetech:20200618192522p:plain http://coreos.com/

なお、CoreOSは2020年5月26日にEoL(End of LIife)のアナウンスがされ、
2020年9月1日には終了となります。
http://coreos.com/os/eol/

また、CoreOSはFedora CoreOSへ引き継がれますので、
今後はそちらを使いましょう。
https://getfedora.org/coreos?stream=stable

CoreOSの始め方ですが、以下にあるようにベアメタル・クラウドなど様々な方法があります。
f:id:opensourcetech:20200618192717p:plain http://coreos.com/os/docs/latest/
今回は、ベアメタル「Booting from ISO」をVirtualbox上のVMで使ってみます。

Stable/Beta/Alphaとあり、本日時点でlStable atestは「2512.3.0」となっています。
なお、制限事項(known limitations)には注意が必要です。
- UEFI未サポート
- Ignition Config(JSON形式)をプロビジョニングする方法がない
- 起動には最低2GBのRAMが必須 f:id:opensourcetech:20200618192930p:plain

インストール先となるVirtualbox上の仮想マシン(VM)にISOをセットします。
f:id:opensourcetech:20200618195100p:plain

起動し、以下のようになればOKです。
f:id:opensourcetech:20200618195204p:plain
なお、この状態ではISOイメージから起動したのみで、まだ未インストールで、
これから作成するインストール時に使用する設定ファイルを使い、coreos-installコマンドでインストールします。

作成するインストール用の設定ですが、以下に詳細が記載されています。
https://github.com/coreos/container-linux-config-transpiler/blob/master/doc/examples.md
https://github.com/coreos/container-linux-config-transpiler/blob/master/doc/configuration.md
https://coreos.com/os/docs/latest/cloud-config.html
今回使っているCoreOSでは、ssh-keygenが入っているので、それで秘密鍵・公開鍵のペアを作成します。

core@localhost ~ $ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/core/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/core/.ssh/id_rsa.
Your public key has been saved in /home/core/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:DW0pky/X/Bn625RfDt3QDXWiS2zSQEebTD0noaQW4QM core@localhost
The key's randomart image is:
+---[RSA 2048]----+
|        E.=o=.o.o|
|         = % =+oo|
|        = X @ .+ |
|         O B . o.|
|        S + + o o|
|         o   o =o|
|            . +.=|
|             . =o|
|              o.+|
+----[SHA256]-----+


作成された秘密鍵を、cloud-config.ymlとしてコピーします。

core@localhost ~ $ cp -p /home/core/.ssh/id_rsa.pub cloud-config.yml
core@localhost ~ $ ls
cloud-config.yml


続いて、CoreOSインストール用の設定(cloud-config.yml)を作成します。

core@localhost ~ $ vi cloud-config.yml 
#cloud-config
users:
 - name: "core"
   ssh_authorized_keys:
    - "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfNzPr5sHs8Wo438oxwMEM2fxF9RXGFVVLmyCpr8jBbceoGKTGxZ/gph5W0AsjEs79YBMPy6Gxwsauml2SuaimeKibuEPVrplUvFXXKXOohR4KBTTT4Wn+Uy9Parh7kyRKAUgDuCrFDoysRqXemseFtAfkK+vbDKedcSDbIbxg7+C7FRe4YBHi1/IcBL5FBs3UURzLlet6PAJHnpaamMSR17k/CD9XqEObNUKTelALSVVXUKzBcM7c8xwVTUbNokF8U/YE0SxISi7Qxkw300JJBbUOfr6XpjcOHbbIK6qUqs9/dBn/UIsdjkVWMqD02TIUnkPtyqQyDPRSinTVPaHoNjwN core@locaalhost"



そして、CoreOSのインストール。
インストール先のデバイスを-d、インストールする種類を-C・使用する設定を-cで指定します。

core@localhost ~ $ sudo coreos-install -d /dev/sda -C stable -c ./cloud-config.ymml
2020/06/18 19:22:46 Checking availability of "local-file"
2020/06/18 19:22:46 Fetching user-data from datasource of type "local-file"
Current version of CoreOS Container Linux stable is 2512.3.0
Downloading the signature for https://stable.release.core-os.net/amd64-usr/2512.3.0/coreos_production_image.bin.bz2...
2020-06-18 19:22:47 URL:https://stable.release.core-os.net/amd64-usr/2512.3.0/coreos_production_image.bin.bz2.sig [566/566] -> "/tmp/coreos-install.ACYDXhnQfC/coreos_production_image.bin.bz2.sig" [1]
Downloading, writing and verifying coreos_production_image.bin.bz2...
2020-06-18 19:24:25 URL:https://stable.release.core-os.net/amd64-usr/2512.3.0/coreos_production_image.bin.bz2 [487343704/487343704] -> "-" [1]
gpg: Signature made Fri May 22 20:48:07 2020 UTC
gpg:                using RSA key FD986FB096482F906F55B2EA01C9CAE767B3CA0E
gpg: key 50E0885593D2DCB4 marked as ultimately trusted
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: Good signature from "CoreOS Buildbot (Offical Builds) <buildbot@coreos.com>" [ultimate]
Installing cloud-config...
Success! CoreOS Container Linux stable 2512.3.0 is installed on /dev/sda


「Success!」となれば、インストール成功です。
VMからISOイメージを取り出し、VMを再起動します。
なお、再起動後のSSHログインで使用するので、coreユーザ用の秘密鍵(id_rsa)をscpなどでSSHログインする端末へコピーしておいてください。

core@localhost ~ $ sudo shutdown -h now


起動後は、coreの秘密鍵でログイン。

core@localhost ~ $ cat /etc/os-release 
NAME="Container Linux by CoreOS"
ID=coreos
VERSION=2512.3.0
VERSION_ID=2512.3.0
BUILD_ID=2020-05-22-2004
PRETTY_NAME="Container Linux by CoreOS 2512.3.0 (Oklo)"
ANSI_COLOR="38;5;75"
HOME_URL="https://coreos.com/"
BUG_REPORT_URL="https://issues.coreos.com"
COREOS_BOARD="amd64-usr"


dockerコマンドも入っているので、そのままコンテナ環境として使用することが可能です。

core@localhost ~ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

core@localhost ~ $ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Pulling fs layer 
8d69e59170f7: Pulling fs layer 
3f9f1ec1d262: Pulling fs layer 
d1f5ff4f210d: Pulling fs layer 
1e22bfa8652e: Pulling fs layer
d1f5ff4f210d: Waiting 
1e22bfa8652e: Waiting 
8d69e59170f7: Downloading  277.6kB/26.21MB
8559a31e96f4: Downloading  277.6kB/27.1MB
8d69e59170f7: Downloading  801.9kB/26.21MB
8559a31e96f4: Downloading  1.113MB/27.1MB
3f9f1ec1d262: Downloading     425B/538B
3f9f1ec1d262: Downloading     538B/538B
3f9f1ec1d262: Verifying Checksum 
3f9f1ec1d262: Download complete 
8d69e59170f7: Downloading  1.342MB/26.21MB
8559a31e96f4: Downloading  2.231MB/27.1MB
d1f5ff4f210d: Downloading     425B/902B
d1f5ff4f210d: Verifying Checksum 
d1f5ff4f210d: Download complete 
8559a31e96f4: Downloading   19.8MB/27.1MB
8d69e59170f7: Downloading  11.97MB/26.21MB
1e22bfa8652e: Downloading     672B/672B
1e22bfa8652e: Verifying Checksum 
1e22bfa8652e: Download complete 
8d69e59170f7: Downloading  16.23MB/26.21MB
8559a31e96f4: Downloading  27.05MB/27.1MB
8559a31e96f4: Verifying Checksum 
8559a31e96f4: Download complete 
8559a31e96f4: Extracting  19.46MB/27.1MB
8d69e59170f7: Downloading  24.95MB/26.21MB
8559a31e96f4: Extracting  21.23MB/27.1MB
8d69e59170f7: Verifying Checksum 
8d69e59170f7: Download complete 
8559a31e96f4: Extracting   27.1MB/27.1MB
8559a31e96f4: Pull complete 
8d69e59170f7: Extracting  262.1kB/26.21MB
8d69e59170f7: Extracting  26.21MB/26.21MB
8d69e59170f7: Pull complete 
3f9f1ec1d262: Extracting     538B/538B
3f9f1ec1d262: Pull complete 
d1f5ff4f210d: Extracting     902B/902B
d1f5ff4f210d: Pull complete 
1e22bfa8652e: Extracting     672B/672B
1e22bfa8652e: Pull complete Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest

core@localhost /bin $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              2622e6cca7eb        8 days ago          132MB

core@localhost /bin $ docker co  image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              2622e6cca7eb        8 days ago          132MB

core@localhost /bin $ docker version
Client:
 Version:           18.06.3-ce
 API version:       1.38
 Go version:        go1.10.8
 Git commit:        d7080c1
 Built:             Tue Feb 19 23:07:53 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.3-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       d7080c1
  Built:            Tue Feb 19 23:07:53 2019
  OS/Arch:          linux/amd64
  Experimental:     false

core@localhost /bin $ docker        sudo      cat /etc/coreos/update.conf 
GROUP=stable



CoreOSは自動更新されるので基本的には不要ですが、手動で更新する場合は、以下のようにします。

core@localhost /bin $ sudo update_engine_client -update
I0618 19:33:19.537420  1032 update_engine_client.cc:247] Initiating update check and install.
I0618 19:33:19.542706  1032 update_engine_client.cc:252] Waiting for update to complete.
LAST_CHECKED_TIME=1592490800
PROGRESS=0.000000
CURRENT_OP=UPDATE_STATUS_IDLE
NEW_VERSION=0.0.0
NEW_SIZE=0
E0618 19:33:24.744282  1032 update_engine_client.cc:190] Update failed.
core@localhost /bin $ sudo update_engine_client -update
I0618 19:33:49.686878  1035 update_engine_client.cc:247] Initiating update check and install.
I0618 19:33:49.693328  1035 update_engine_client.cc:252] Waiting for update to complete.
LAST_CHECKED_TIME=1592490830
PROGRESS=0.000000
CURRENT_OP=UPDATE_STATUS_IDLE
NEW_VERSION=0.0.0
NEW_SIZE=0
E0618 19:33:54.744668  1035 update_engine_client.cc:190] Update failed.

core@localhost /bin $ cat /etc/os-release 
NAME="Container Linux by CoreOS"
ID=coreos
VERSION=2512.3.0
VERSION_ID=2512.3.0
BUILD_ID=2020-05-22-2004
PRETTY_NAME="Container Linux by CoreOS 2512.3.0 (Oklo)"
ANSI_COLOR="38;5;75"
HOME_URL="https://coreos.com/"
BUG_REPORT_URL="https://issues.coreos.com"
COREOS_BOARD="amd64-usr"


おまけ
Virtualbox上のNAT設定でポートフォワードする設定。
SSHするときに必要になります。
f:id:opensourcetech:20200621095831p:plain
そして、Teratermなどからアクセスする場合、ユーザ名・秘密鍵の指定(パスワード含む)でアクセスします。
f:id:opensourcetech:20200621100048p:plain f:id:opensourcetech:20200621100140p:plain

Opensourcetech by Takahiro Kujirai