2022-05-15に更新

DockerとKubernetes

読了目安:12分

0 前提

Oracle LinuxとWindows

1 環境構築

準備

1-1 Dockerオフラインインストール

参考にしたURLはhttps://qiita.com/kod314/items/e574ac12c23598e0d903
オンラインインストールの場合は、
https://qiita.com/kichise/items/f8e56c6d2d08eaf4a6a0
の手順に従えば可能。

今回はオフラインインストールを実行しようと思う。社内規則によりDockerをインストールするサーバがインターネット接続できない場合がある。その場合、インターネット接続できる自席端末からネットに接続してDockerのインストーラーをダウンロード。それをDockerをインストールしたいサーバに転送して実行させる。

1-1-1 OS確認

まずはOSのversionを確認。

$ uname -a
Linux publicpc1 5.4.17-2136.305.5.3.el8uek.x86_64 #2 SMP Thu Mar 17 10:45:33 PDT 2022 x86_64 x86_64 x86_64 GNU/Linux

より詳細

#cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.5"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.5"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:5:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.5
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.5

Linuxカーネルの確認

$ hostnamectl
Static hostname: publicpc1
Icon name: computer-vm
Chassis: vm
Machine ID: 4c3dc5bfa43b47b8b66789ce778a0711
Boot ID: 226a196eedc746e2848a6f15e9afeb0b
Virtualization: kvm
Operating System: Oracle Linux Server 8.5
CPE OS Name: cpe:/o:oracle:linux:8:5:server
Kernel: Linux 5.4.17-2136.305.5.3.el8uek.x86_64
Architecture: x86-64

x86_64と記載されているので、Intel(AMD)の64bitオペレーティングシステムであることがわかる。

1-1-2 Docker engineをインストール

Docker本体であるDocker Engineと、同時に複数のコンテナを操作するツールであるDocker Composeをダウンロードする。
参考にした公式:https://docs.docker.com/engine/install/binaries/

まずはdocker本体
https://download.docker.com/linux/static/stable/x86_64/
→バージョンがいろいろあるので、最新のdocker-20.10.9.tgz をダウンロードしてみた。
自分の端末(クライアント側にダウンロードしたファイルをLinuxサーバに転送してから解凍する。

$ ls -al /home/opc/docker-20.10.9.tgz
-rw-r--r--. 1 opc opc 63350495 May  4 04:23 /home/opc/docker-20.10.9.tgz

展開

$ tar zxvf docker-20.10.9.tgz

すると、カレントディレクトリにdockerディレクトリが作成される。
dockerディレクトリの中身を/usr/bin/配下にコピーする。

$ sudo cp docker/* /usr/bin/

試しに起動するには

$ sudo dockerd &

1-1-3 Docker Composeをインストール

続いてはdocker-compose
インストール手順は以下に記載されているので以降この指示に従う。
https://github.com/docker/compose/blob/v2/README.md

さきほど(1-1-1節)でOSとCPUを確認したので、
それに従ってdocker-compose-linux-x86_64をダウンロードする。
https://github.com/docker/compose/releases

自分のwindows端末(クライアント側)にダウンロードしたdocker-compose-linux-x86_64をdockerをインストールしたLinuxサーバに転送。
転送したら、バイナリファイルの名前をdocker-composeにリネーム。

$ mv docker-compose-linux-x86_64 docker-compose

docker-composeをいどうする。移動する先は目的に応じて移動先が異なる。
今回は

$ sudo mv docker-compose /usr/local/bin/
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose -v
Docker Compose version v2.5.0

1-1-4 Docker イメージのオフラインで利用する

$ tar zxvf helloworld_img.tar.gz
$ docker load < helloworld_img # dockerイメージをload
$ docker images # hello-worldイメージが存在することを確認

1-1-5 Dockerグループ作成

Docker daemonはrootユーザで実行しなければならず、毎回sudoを使うのは面倒である。
その場合、dockerグループを作成してそこに任意のユーザを所属させてあげる。

groupadd docker
usermod -aG docker opc

1-1-6 トラブルシュート

docklerをサービスでenebleにしようと思ったができなかった。

[root@publicpc1 system]# systemctl enable docker
Failed to enable unit: Unit file docker.service does not exist.

以下のサイトを参考に対応してみた。
https://jhooq.com/docker-daemon-centos/

/usr/lib/systemd/system配下にdocker.socketファイルを作成し以下の内容を記載した。

[Unit]
Description=Docker Socket for the API

[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker

[Install]
WantedBy=sockets.target 

続いて、/usr/lib/systemd/system/配下にdocker.serviceを作成する。

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket containerd.service

[Service]
Type=notify
#the default is not to use systemd for cgroups because the delegate issues still
#exists and systemd currently does not support the cgroup feature set required
#for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

#Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
#Both the old, and new location are accepted by systemd 229 and up, so using the old location
#to make them work for either version of systemd.
StartLimitBurst=3

#Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
#Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
#this option work for either version of systemd.
StartLimitInterval=60s

#Having non-zero Limit*s causes performance problems due to accounting overhead
#in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

#Comment TasksMax if your systemd version does not support it.
#Only systemd 226 and above support this option.
TasksMax=infinity

#set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

#kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500

[Install]
WantedBy=multi-user.target

この中身をみるとRequires=docker.socket containerd.serviceと記載されている通り、containerd.serviceが必要。なので/usr/lib/systemd/system配下にcontainerd.serviceサービスを作成する必要がある。

#Copyright The containerd Authors.
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#http://www.apache.org/licenses/LICENSE-2.0
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
#Having non-zero Limit*s causes performance problems due to accounting overhead
#in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
#Comment TasksMax if your systemd version does not supports it.
#Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target

上記を作成する。

ちゃんとリロードしとく。

systemctl daemon-reload

1-2.Kubernetesの準備

1-2-1. インストール

オフライン環境でKubernetesを構築する手順。
手順の参考にしたのは以下
https://docs.genesys.com/Documentation/GCXI/latest/Dep/DockerOffline

まずはネットにつながる端末から

$ pwd
/etc/yum.repos.d
$ cat kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

以下のコマンドを実行してファイルをダウンロード。
<your_rpm_dir>の部分は自分で適当なディレクトリを作成して
そこのパスを指定。

yumdownloader --assumeyes --destdir= --resolve yum-utils kubeadm-1.21.* kubelet-1.21.* kubectl-1.21.* ebtables

続いて、ダウンロードしたファイルをインストールしていく。

yum install -y --cacheonly --disablerepo=* /*.rpm

インストールする際、バージョンが異なるファイルが存在してインストールエラーになるかもしれない。
その場合は、必要のないバージョンのファイルは削除してから
再度インストールするとうまくいく。
うまくいくと以下のようになる。

Complete!

必要なイメージが揃っているか確認してみよう。

$ kubeadm config images list
I0504 23:15:36.977910  309762 version.go:254] remote version is much newer: v1.24.0; falling back to: stable-1.21
k8s.gcr.io/kube-apiserver:v1.21.12
k8s.gcr.io/kube-controller-manager:v1.21.12
k8s.gcr.io/kube-scheduler:v1.21.12
k8s.gcr.io/kube-proxy:v1.21.12
k8s.gcr.io/pause:3.4.1
k8s.gcr.io/etcd:3.4.13-0
k8s.gcr.io/coredns/coredns:v1.8.0

1-2-2. kubectlの概要

参考URL:https://kubernetes.io/ja/docs/reference/kubectl/_print/
Kubernetesクラスタを操作するコマンド

2.Docker入門

ツイッターでシェア
みんなに共有、忘れないようにメモ

kawai_mizugorou

社会人2年目.自分用のメモとして使ってます.

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント