2
4

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.

LXD をCentOS7に導入し、コンテナにSSH接続する手順

Posted at

LXD をCentOS7に導入する

CentOS7にLXDを導入する手順の概要

  • 事前準備
    • Kernelのアップグレード
  • snapd&LXDのインストール
  • LXDのセットアップ

事前準備

セキュリティ設定の無効化

sed -i -e "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
Kernel アップグレード

Kernel3.13以降でないとダメなようです。実際、CentOS7のKernel3.10系のままで進めると、LXDのインストール時に確実に止まってしまいます。このため、少々しんどいですが、Kernelのアップグレードを実施します。

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

GPG-KeyとELRepoの登録を実施。
現在インストールされているKernelのバージョンを確認

# yum list installed | grep kernel
kernel.x86_64                         3.10.0-957.el7                   @anaconda
kernel-tools.x86_64                   3.10.0-957.el7                   @anaconda
kernel-tools-libs.x86_64              3.10.0-957.el7                   @anaconda
# 

Kernel アップグレード

yum --enablerepo=elrepo-kernel install kernel-ml

最新Kernelのインストール
menuentryを確認
初期起動Kernelに4.18.12-1.el7.elrepo.x86_64を選択
grub.cfgへの反映
再起動

# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (4.18.12-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-862.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-626b869879714a9cbe128e5b6f85dd89) 7 (Core)

# grub2-set-default 0
# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.18.12-1.el7.elrepo.x86_64
Found initrd image: /boot/initramfs-4.18.12-1.el7.elrepo.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-626b869879714a9cbe128e5b6f85dd89
Found initrd image: /boot/initramfs-0-rescue-626b869879714a9cbe128e5b6f85dd89.img
done
# reboot

snapd & LXD install

snapd install

epelとyum-plugin-coprのインストール
coprの有効化とsnapdのインストール
snapdサービスの自動起動設定


yum -y install epel-release && yum -y install yum-plugin-copr
yum -y copr enable ngompa/snapcore-el7 && yum -y install snapd
systemctl enable --now snapd.socket

grubby の設定

grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
grubby --args="namespace.unpriv_enable=1" --update-kernel="$(grubby --default-kernel)"
sh -c 'echo "user.max_user_namespaces=3883" > /etc/sysctl.d/99-userns.conf'
reboot

grubby ?

snap にプロキシの設定をする(必要な場合)

プロキシサーバーの確認

# vi /etc/profile
     78 PROXY='http://proxy.esl.xxxxx.com:8080'
     79 export http_proxy=$PROXY
     80 export HTTP_PROXY=$PROXY
     81 export https_proxy=$PROXY
     82 export HTTPS_PROXY=$PROXY

snap のコンフィグにプロキシを設定

# vi /etc/sysconfig/snapd 
      1 SNAP_REEXEC=0
      2 
      3 http_proxy='http://proxy.esl.xxxxx.com:8080'
      4 https_proxy='http://proxy.esl.xxxxx.com:8080'

lxd install

snap上でlxdが参照できています。

# snap search lxd
Name             Version        Developer       Notes  Summary
lxd-demo-server  0+git.f3532e3  stgraber        -      Online software demo sessions using LXD
lxd              3.6            canonical       -      System container manager and API
satellite        0.1.2          alanzanattadev  -      Advanced scalable Open source intelligence platform

snapのシンボリックリンクを張る
lxdのインストール

ln -s /var/lib/snapd/snap /snap
snap install lxd

# snap install lxd
Ensure prerequisites for "lxd" are available                            
Ensure prerequisites for "lxd" are available                            
~省略~
Download snap "core" (5662) from channel "stable"                       
Download snap "core" (5662) from channel "stable"      3% 23.2MB/s 3.87s
~省略~
Download snap "core" (5662) from channel "stable"    100% 11.2MB/s 0.0ns
Download snap "lxd" (9239) from channel "stable"                        
Download snap "lxd" (9239) from channel "stable"       2% 15.0MB/s 4.57s
~省略~
Download snap "lxd" (9239) from channel "stable"     100% 10.4MB/s 0.0ns

以下コマンドでlxd がインストールされていればOK

# snap list
Name  Version    Rev   Tracking  Publisher   Notes
core  16-2.37.1  6350  stable    canonical✓  core
lxd   3.9        9919  stable    canonical✓  -
# 

LXDの設定

lxd の初期化
# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: 
Do you want to configure a new storage pool? (yes/no) [default=yes]: 
Name of the new storage pool [default=default]: 
Name of the storage backend to use (btrfs, ceph, dir, lvm) [default=btrfs]: 
Create a new BTRFS pool? (yes/no) [default=yes]: 
Would you like to use an existing block device? (yes/no) [default=no]: 
Size in GB of the new loop device (1GB minimum) [default=15GB]: 50GB
Would you like to connect to a MAAS server? (yes/no) [default=no]: 
Would you like to create a new local network bridge? (yes/no) [default=yes]: 
What should the new bridge be called? [default=lxdbr0]: 
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
Would you like LXD to be available over the network? (yes/no) [default=no]: yes
Address to bind LXD to (not including port) [default=all]: 
Port to bind LXD to [default=8443]: 
Trust password for new clients: 
Again: 
Would you like stale cached images to be updated automatically? (yes/no) [default=yes] 
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: 
# 

利用可能なコンテナイメージファイルの確認

Linux Containersにアップロードされているコンテナイメージを参照することができます。
膨大な数がありますので、ここではgrepしてcentosに絞って参照してみます。

# lxc image list images: | grep Centos  -i
| centos/6 (3 more)             | 951d3d506c86 | yes    | Centos 6 amd64 (20190203_08:13)          | x86_64  | 109.74MB | Feb 3, 2019 at 12:00am (UTC)  |
| centos/6/i386 (1 more)        | 0962d37a535b | yes    | Centos 6 i386 (20190203_08:13)           | i686    | 109.94MB | Feb 3, 2019 at 12:00am (UTC)  |
| centos/7 (3 more)             | 1aa1269c02e6 | yes    | Centos 7 amd64 (20190203_08:13)          | x86_64  | 124.86MB | Feb 3, 2019 at 12:00am (UTC)  |
| centos/7/arm64 (1 more)       | f68215a646f2 | yes    | Centos 7 arm64 (20190203_08:15)          | aarch64 | 125.11MB | Feb 3, 2019 at 12:00am (UTC)  |
| centos/7/armhf (1 more)       | ed128baca1e2 | yes    | Centos 7 armhf (20190203_08:16)          | armv7l  | 122.54MB | Feb 3, 2019 at 12:00am (UTC)  |
| centos/7/i386 (1 more)        | 9d6be1b686b0 | yes    | Centos 7 i386 (20190203_08:20)           | i686    | 125.30MB | Feb 3, 2019 at 12:00am (UTC)  |
| centos/7/ppc64el (1 more)     | f4c0e318c17f | yes    | Centos 7 ppc64el (20190203_08:14)        | ppc64le | 126.70MB | Feb 3, 2019 at 12:00am (UTC)  |
|                               | 048289cefef9 | yes    | Centos 7 amd64 (20190201_07:09)          | x86_64  | 124.66MB | Feb 1, 2019 at 12:00am (UTC)  |
|                               | 1f8d10793687 | yes    | Centos 7 armhf (20190202_07:10)          | armv7l  | 122.54MB | Feb 2, 2019 at 12:00am (UTC)  |
|                               | 1fa54a3d65fa | yes    | Centos 7 i386 (20190202_07:16)           | i686    | 125.30MB | Feb 2, 2019 at 12:00am (UTC)  |
|                               | 4267c3e171bf | yes    | Centos 7 arm64 (20190201_07:10)          | aarch64 | 124.91MB | Feb 1, 2019 at 12:00am (UTC)  |
|                               | 613a796be3e1 | yes    | Centos 6 i386 (20190202_07:09)           | i686    | 109.94MB | Feb 2, 2019 at 12:00am (UTC)  |
|                               | 681e3bc8cb26 | yes    | Centos 6 i386 (20190201_07:09)           | i686    | 109.94MB | Feb 1, 2019 at 12:00am (UTC)  |
|                               | 6c29f94c5ae2 | yes    | Centos 7 ppc64el (20190201_07:10)        | ppc64le | 126.49MB | Feb 1, 2019 at 12:00am (UTC)  |
|                               | a11079b4b4b1 | yes    | Centos 7 armhf (20190201_07:11)          | armv7l  | 122.49MB | Feb 1, 2019 at 12:00am (UTC)  |
|                               | aef5c2f47867 | yes    | Centos 7 i386 (20190201_07:09)           | i686    | 125.07MB | Feb 1, 2019 at 12:00am (UTC)  |
|                               | b44de52b5918 | yes    | Centos 7 amd64 (20190202_07:09)          | x86_64  | 124.86MB | Feb 2, 2019 at 12:00am (UTC)  |
|                               | b8740111c6c8 | yes    | Centos 6 amd64 (20190202_07:09)          | x86_64  | 109.74MB | Feb 2, 2019 at 12:00am (UTC)  |
|                               | ba62e743461d | yes    | Centos 7 arm64 (20190202_07:36)          | aarch64 | 125.11MB | Feb 2, 2019 at 12:00am (UTC)  |
|                               | c579368412fd | yes    | Centos 7 ppc64el (20190202_07:10)        | ppc64le | 126.70MB | Feb 2, 2019 at 12:00am (UTC)  |
|                               | f4189737512a | yes    | Centos 6 amd64 (20190201_07:09)          | x86_64  | 109.74MB | Feb 1, 2019 at 12:00am (UTC)  |
# 

(必要な場合)LXCにプロキシ設定

LXDデーモンはリモートサーバーからイメージを取得する。

外部のネットワークにHTTP(S)プロキシ経由で接続する必要がある場合、以下の設定を行うか、もしくはデーモンの環境において標準的なPROXY環境変数を設定する必要があります。

# lxc config set core.proxy_http 'http://proxy.esl.xxxxx.com:8080'
# lxc config set core.proxy_https 'http://proxy.esl.xxxxx.com:8080'
// lxc config set core.proxy_ignore_hosts image-server.local

上記の設定により、LXDから始まる通信はすべて'http://proxy.esl.xxxxx.com:8080'というHTTPプロキシを経由します。
(ただしimage-server.localとの通信のみは例外的にプロキシを経由しないという設定になっています。)

コンテナのインストール
# lxc launch images:centos/7/amd64 test-centos7
Creating lxc751
Retrieving image: metadata: 100% (3.20GB/s)                                            
Retrieving image: rootfs: 1% (490.01kB/s)                                            
Retrieving image: rootfs: 2% (687.27kB/s)                                            
~省略~
Retrieving image: rootfs: 100% (3.14MB/s)                                            
Starting test-centos7

上記コマンドによりcentos7のコンテナインストールが完了します。
公開サーバの「images:centos/7/amd64」からイメージをDLします。
初回はDLするのに2~3分程度かかりますが、2回目以降はローカルに保存されたコンテナイメージから展開されるため早いです。
また上記の例ではコンテナに「test-centos」という名前付与しています。

起動したコンテナは以下のように確認できます。

# lxc list
+--------------+---------+----------------------+-----------------------------------------------+------------+-----------+
|     NAME     |  STATE  |         IPV4         |                     IPV6                      |    TYPE    | SNAPSHOTS |
+--------------+---------+----------------------+-----------------------------------------------+------------+-----------+
| test-centos7 | RUNNING | 10.24.185.246 (eth0) | fd42:1c6e:a050:3ce7:216:3eff:fed0:9bbf (eth0) | PERSISTENT |           |
+--------------+---------+----------------------+-----------------------------------------------+------------+-----------+
# 

コンテナのbashへの移動

ホストOSからコンテナのbashへ移動できます
IPアドレスを確認し、先ほどのlist で表示されたIPアドレスと同じであることが確認できます

# lxc exec test-centos7 bash
[root@test-centos7 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
5: eth0@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:d0:9b:bf brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.24.185.246/24 brd 10.24.185.255 scope global dynamic eth0
       valid_lft 3481sec preferred_lft 3481sec
    inet6 fd42:1c6e:a050:3ce7:216:3eff:fed0:9bbf/64 scope global mngtmpaddr dynamic 
       valid_lft 3551sec preferred_lft 3551sec
    inet6 fe80::216:3eff:fed0:9bbf/64 scope link 
       valid_lft forever preferred_lft forever
[root@test-centos7 ~]# 
Windowsからアクセスしたい

Windows側のルーティング設定

C:\WINDOWS\system32>route add 10.24.185.0 mask 255.255.255.0 192.168.56.105 -p
 OK!

追加するコンテナのネットワーク帯域 : 10.24.185.0/24
経由するルータ(というか今回はVMのCentoOS) : 192.168.56.105
-p : 永続設定化。オプションなしの場合再起動後設定は保存されない

これでping が通るはず

コンテナのSSh設定の変更.
各コンテナにはopenssh-server をインストールしておく

lxc exec container-name — vi /etc/ssh/sshd_config

変更箇所

PermitRootLogin yes
PubkeyAuthentication yes
PasswordAuthentication yes

rootパスワード設定

lxc exec container-name — passwd
Enter new UNIX password:
Retype new UNIX password:

コンテナの再起動

lxc restart container-name

ジャンプサーバー経由のサーバー上のCentOS上のコンテナへのSSH接続

ジャンプ元の設定

編集 > サーバー > プロトコル > ポートフォワード > 新規

- Local Socks
- Host : localhost 
- Port : 10023(任意)
コンテナ側の設定

編集 > サーバー > プロキシ設定 > 

- Select Proxy Protocol : SOCKS5
- Over SSL : 使用しない
- Server Option > Proxy Server Address : localhost
Socket Port : 10023
WinSCP からコンテナにアクセスしたい

2段階の踏み台を利用することになるので Rlogin を利用する前提でWinSCPを用いたコンテナへの接続方法を記す

  1. RLogin でジャンプサーバーを経由して接続するサーバーへアクセスする。
    上 "ジャンプサーバー経由のサーバー上のCentOS上のコンテナへのSSH接続" ポートフォワード設定を行っているものとする
  2. WinSCP
    a. セション情報にはコンテナの情報を入力
    b. 設定 > 接続 > プロキシ
    プロキシ形式:SOCKS5
    プロキシホスト名:localhost
    ポート番号:10023
    ユーザ名、パスワード:コンテナに設定した値を入力
2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?