概要
-
サーバにdocker, docker-composeを入れる必要がある場合に、Ansible化してプロビジョニングするための検証例を記載
- 検証手順のまとめなのでAnsibleコード自体は載ってない(別記事で書くかもしれない)
手順
1. 入れたいサーバのOSを特定する
Ansible化するにあたり、どんな手順でインストール出来るかを知る必要があるため、OSを特定する必要がある。
対象サーバに入りOSを確認する。
$ cat /etc/os-release
VERSION="7.6"
ID="ol"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="7.6"
ANSI_COLOR="0;31"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"
ORACLE_BUGZILLA_PRODUCT_VERSION=7.6
ORACLE_SUPPORT_PRODUCT_VERSION=7.6
2. 検証環境を用意する
Virtualboxなどで検証環境を用意する。
IntelMacであればローカルでVirtualboxを使えるのでそれで良いが、AppleSiliconのMacでは使えないためEC2で代用する。
2.1 EC2を用意する
注意点としてまず、EC2上でVirtualboxを使うには、ベアメタル系のインスタンスタイプを選ぶ必要がある。
本日より、5 つの新しい Amazon EC2 ベアメタルインスタンスが利用可能になります。Amazon EC2 ベアメタルインスタンスでは、アプリケーションは基盤となるサーバーの Intel® Xeon® スケーラブルプロセッサとメモリリソースに直接アクセスできます。これらのインスタンスは、ライセンス付与やサポート要件のため、あるいは自前のハイパーバイザーを使用したいと考えているお客様のために非仮想環境で実行する必要のあるアプリケーション、Intel® VT-x などのハードウェアの機能セットへのアクセスが必要なワークロードに最適です。
今回はこの中でもなるべく安価なm5.metal(USD 5.952/h)を利用した。
また、容量がデフォルトの8GBのままにすると、VMを入れていっぱいになってしまうので、EBSボリュームは無料枠MAXの30GBにする。
2.2 EC2(Ubuntu)内で、プロビ対象サーバのOSのVM環境を用意
VirtualboxとVagrantをインストールする
$ sudo apt -y update
...
$ sudo apt install -y virtualbox
...
$ vboxmanage --version
6.1.38_Ubuntur153438
$ sudo apt install -y vagrant
...
$ vagrant --version
Vagrant 2.2.19
user@ip-172-31-4-208:~$ mkdir ol7
user@ip-172-31-4-208:~$ cd ol7
user@ip-172-31-4-208:~/ol7$ vim Vagrantfile
(以下のような中身にする)
Vagrant.configure("2") do |config|
config.vm.box = "generic/oracle7"
config.vm.hostname = "ol7"
config.vm.network "private_network", ip: "192.168.56.115"
config.vm.provider "virtualbox" do |v|
v.name = "ol7"
end
config.vbguest.auto_update = false
end
config.vm.provision "shell" do |s|
s.inline = <<-SHELL
adduser ansible -m -s /bin/bash
mkdir /home/ansible/.ssh
chmod 700 /home/ansible/.ssh
chown -R ansible.ansible /home/ansible/.ssh
echo "%ansible ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/ansible
chmod 440 /etc/sudoers.d/ansible
SHELL
end
end
ホスト名を登録しておく
// 途中聞いてくるやつは全部そのままEnterで
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
...
user@ip-172-31-4-208:~/ol7$ ll ~/.ssh
total 20
drwx------ 2 user user 4096 Nov 22 08:35 ./
drwxr-x--- 5 user user 4096 Nov 22 08:34 ../
-rw------- 1 user user 783 Jan 18 2023 authorized_keys
-rw------- 1 user user 3389 Nov 22 08:35 id_rsa
-rw-r--r-- 1 user user 747 Nov 22 08:35 id_rsa.pub
VM起動
user@ip-172-31-30-86:~/ol7$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'generic/oracle7' version '4.3.6' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH auth method: private key
default: Warning: Connection reset. Retrying...
default:
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 5.2.44
default: VirtualBox Version: 6.1
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Running provisioner: shell...
default: Running: inline script
user@ip-172-31-30-86:~/ol7$ vagrant status
Current machine states:
default running (virtualbox)
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
3. VM上でインストール手順を試し実際に入れられることを確認する
user@ip-172-31-4-208:~/ol7$ ssh ansible@ol7
The authenticity of host 'ol7 (192.168.56.115)' can't be established.
ED25519 key fingerprint is SHA256:
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ol7' (ED25519) to the list of known hosts.
[ansible@ol7 ~]$
3.1 dockerインストール
Oracle公式の手順ページでは以下のようにol7_UEKR5をenableにするとある
対象サーバでyum repolistを確認
$ yum repolist
読み込んだプラグイン:langpacks, ulninfo
ol7_UEKR4/x86_64 Latest Unbreakable Enterprise Kernel Release 4 for Oracle Linux 7Server (x86_64) 63+440
public_ol7_UEKR4_archive/x86_64 Archived Unbreakable Enterprise Kernel Release 4 for Oracle Linux 7Server (x86_64) 67+1,192
repolist: 55,423
UEKR4になっている。
一方で、VMの方を確認
[ansible@ol7 ~]$ yum repolist
Loaded plugins: ulninfo
ol7_UEKR6 | 3.0 kB 00:00:00
ol7_latest | 3.6 kB 00:00:00
(1/5): ol7_latest/x86_64/group_gz | 136 kB 00:00:00
(2/5): ol7_UEKR6/x86_64/updateinfo | 1.0 MB 00:00:00
(3/5): ol7_latest/x86_64/updateinfo | 3.6 MB 00:00:00
(4/5): ol7_latest/x86_64/primary_db | 48 MB 00:00:00
(5/5): ol7_UEKR6/x86_64/primary_db | 66 MB 00:00:01
repo id repo name status
!ol7_UEKR6/x86_64 Latest Unbreakable Enterprise Kernel Release 6 for Oracle Linux 7Server (x86_64) 1082
repolist: 27407
VMではUEKR6になっているので、対象サーバと同じ状況にするためUEKR6をdisable、UEKR4をenableにしておく
[ansible@ol7 ~]$ sudo yum-config-manager --disable ol7_UEKR6
...
[ansible@ol7 ~]$ sudo yum-config-manager --enable ol7_UEKR4
...
手順によるとol7_addonsをenableにするとあるので実行
$ sudo yum-config-manager --enable ol7_addons
...
その後インストールコマンドを実行し、サービススタート & マシン起動時にもスタートするようにしておく
...
[ansible@ol7 ~]$ sudo systemctl enable --now docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[ansible@ol7 ~]$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-11-22 08:52:23 UTC; 3s ago
Docs: https://docs.docker.com
Main PID: 9296 (dockerd)
Tasks: 8
Memory: 27.2M
CGroup: /system.slice/docker.service
└─9296 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...
3.2 ユーザをグループに追加
rootユーザ以外でもdockerを使えるようにユーザをグループに追加する
[ansible@ol7 ~]$ sudo groupadd docker
groupadd: group 'docker' already exists
[ansible@ol7 ~]$ sudo usermod -aG docker $USER
// 新しいセッションにしないと反映されないのでそうする
[ansible@ol7 ~]$ logout
Connection to ol7 closed.
user@ip-172-31-4-208:~/ol7$ ssh ansible@ol7
Last login: Wed Nov 22 08:49:41 2023 from ip-192-168-56-1.ap-northeast-1.compute.internal
[ansible@ol7 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3.3 docker-composeインストール
インストールするバージョンはリリースページから選んでおきOSやCPUアーキに合わせたURLにする
[ansible@ol7 ~]$ sudo -i
[root@ol7 ~]# curl -L https://github.com/docker/compose/releases/download/v2.23.1/docker-compose-linux-x86_64 > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 56.8M 100 56.8M 0 0 59.6M 0 --:--:-- --:--:-- --:--:-- 165M
[root@ol7 ~]# chmod +x /usr/local/bin/docker-compose
[root@ol7 ~]# logout
[ansible@ol7 ~]$ docker-compose --version
Docker Compose version v2.23.1
4. 手順をAnsible化する
4.1 AnsibleをEC2にインストール
実際のansible実行サーバで使っているバージョン確認。
$ ansible --version
ansible 2.8.2
config file = None
configured module search path = ['/home/jen/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/jen/.pyenv/versions/3.8.5/lib/python3.8/site-packages/ansible
executable location = /home/jen/.pyenv/versions/3.8.5/bin/ansible
EC2上でpipを入れてAnsibleインストール
$ sudo apt install python3-pip
$ sudo pip3 install --user ansible\==2.8.2
4.2 手動で検証したインストール手順を元にAnsibleコード作成する
Ansibleコードについては別記事で書くかも。
Ansibleの公式ドキュメントを見てモジュールの使い方を調べながら行う
5. EC2からVMに向けて、実際にAnsibleでdocker, docker-composeをインストール出来るか試す
[server01]
ol7 ansible_user=ansible
プロビ先でroot以外で実行できることを確認するためにansibleユーザをグループに入れるようにしておく
docker_users:
- ansible
playbook実行
// とりあえずdry run(-C)
user@ip-172-31-5-222:~/ol7/ansible$ ansible-playbook -i hosts.dev -C worker.yml
...
// 実際にVMに向けてプロビしてみる
user@ip-172-31-5-222:~/ol7/ansible$ ansible-playbook -i hosts.dev -t docker worker.yml
[DEPRECATION WARNING]: "include" is deprecated, use include_tasks/import_tasks instead. See https://docs.ansible.com/ansible-
core/2.15/user_guide/playbooks_reuse_includes.html for details. This feature will be removed in version 2.16. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
PLAY [all] ***********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
[WARNING]: Platform linux on host ol7 is using the discovered Python interpreter at /usr/libexec/platform-python, but future
installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.15/reference_appendices/interpreter_discovery.html for more information.
ok: [ol7]
[WARNING]: Could not match supplied host pattern, ignoring: worker_for_lgbm
PLAY [server01] ********************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [ol7]
TASK [self/docker : install docker engine] ***************************************************************************************
changed: [ol7]
TASK [self/docker : Start Services] **********************************************************************************************
changed: [ol7]
TASK [self/docker : add users to docker group] ***********************************************************************************
changed: [ol7] => (item=user)
TASK [self/docker : setup docker-compose] ****************************************************************************************
changed: [ol7]
PLAY RECAP ***********************************************************************************************************************
ol7 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
VMに入ってdocker, docker-composeが入れられていることを確認
user@ip-172-31-5-222:~/ol7$ ssh ansible@ol7
Last login: Fri Nov 24 02:59:37 2023 from ip-192-168-56-1.ap-northeast-1.compute.internal
[ansible@ol7 ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[ansible@ol7 ~]$ docker-compose --version
Docker Compose version v2.23.1
6. 実際の対象サーバにプロビする
とりあえずdryrunで問題なさそうなことを確認し、その後dryrun無しで実行。
ジョブが問題なく成功し対象サーバ上で実際にdockerが使えることを確認して完了
$ docker --version
Docker version 19.03.11-ol, build 9bb540d
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ ps aux | grep docker
root 98796 0.0 0.0 1805992 70504 ? Ssl 14:59 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
user 111588 0.0 0.0 112740 2296 pts/3 S+ 15:03 0:00 grep --color=auto docker
$ docker-compose --version
Docker Compose version v2.23.1
(おまけ)
Ansibleの内容は問題なさそうなのに実際のプロビが失敗する場合
$ yum repolist
読み込んだプラグイン:langpacks, ulninfo
ol7_UEKR4/x86_64 Latest Unbreakable Enterprise Kernel Release 4 for Oracle Linux 7Server (x86_64) 63+440
public_ol7_UEKR4_archive/x86_64 Archived Unbreakable Enterprise Kernel Release 4 for Oracle Linux 7Server (x86_64) 67+1,192
repolist: 55,691
そのため、キャッシュとrepoファイルを削除する
[usr@server01 /etc/yum.repos.d]$ sudo rm docker-ce.repo
その後再度プロビ実行で、上手く行った。
VMからインターネットに接続できていなさそうな場合
yum updateなどでHTTPエラー出る場合、インターネットに接続できていない可能性がある
[Errno 14] HTTP Error 404 - Not Found
|
VM上でping google.com など適当にpingを打つと疎通しない場合
→EC2の方でそもそも疎通しているか確認。していない場合はDNS設定を追加