snap學習之搭建lxc/lxd容器驗證snap包

  lxc是Linux Container的簡寫,它是一種內核虛擬化技術,可以提供輕量級的虛擬化,以便隔離進程和資源;它不需要提供指令解釋機制,沒有全虛擬化的複雜性,相當於C++中的NameSpace。lxc容器能有效地把操作系統管理的資源劃分到不同的組中,並能在不同的組之間平衡有衝突的資源使用需求,因此它可以在單一的主機節點上同時執行多個相互隔離的容器。lxd是基於lxc構築的容器管理進程,提供鏡像、網絡、存儲、以及容器等能力。
  大家可能有個疑問,爲什麼不用docker容器呢?docker容器原先也是我的首選,但實際操作過程中發現snap包安裝所需要的squashfs文件系統在docker中無法mount,會出現如下錯誤:

  system does not fully support snapd: cannot mount squashfs imag  

  所以大家不要再嘗試用docker容器了。

  下面我們開始在Ubuntu 16.04上搭建lxc容器來驗證nginx snap包。

1.軟件安裝
  lxc/lxd容器需要安裝lxd軟件。

  sudo apt install lxd

  lxd安裝好之後,再進行lxd初始化。

  $ sudo lxd init
  Name of the storage backend to use (dir or zfs): zfs
  Create a new ZFS pool (yes/no)? yes
  Name of the new ZFS pool: pool
  Would you like to use an existing block device (yes/no)? no
  Size in GB of the new loop device (1GB minimum): 30
  Would you like LXD to be available over the network (yes/no)? no
  Do you want to configure the LXD bridge (yes/no)? yes 
  ......

  lxd初始化的參數都採用默認值即可,並根據自己的網絡情況配置網橋的ipv4和ipv6地址。如果安裝時,出現缺失zfs文件系統錯誤,安裝一下即可。

  sudo apt install zfsutils-linux

  lxd安裝完成後,用ifconfig命令可以看到網絡中多了一個名稱爲lxdbr0的網橋設備,後續創建的所有的lxc容器,ip地址都需要配置在網橋的這個網段中,在本文中創建容器時,我會詳細說明。

  我們再確認一下lxd是否已經啓動。

  pp@pp-ThinkPad-S2-2nd-Gen:~$ ps -A | grep "lxd"
   3179 ?        00:00:10 lxd
   3243 ?        00:00:00 lxd

2.配置ubuntu容器
2.1 下載鏡像文件
  和docker類似,lxc配置容器前也需要下載對應的image鏡像文件。
  lxc的image鏡像下載比較慢,我們可以添加清華的remote鏈接,加速下載。

  lxc remote add tuna-images https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ --protocol=simplestreams --public

  添加後,我們看一下remote鏈接。

  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc remote list
  +-----------------+--------------------------------------------------+---------------+--------+--------+
  |      NAME       |                       URL                        |   PROTOCOL    | PUBLIC | STATIC |
  +-----------------+--------------------------------------------------+---------------+--------+--------+
  | local (default) | unix://                                          | lxd           | NO     | YES    |
  +-----------------+--------------------------------------------------+---------------+--------+--------+
  | tuna-images     | https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ | simplestreams | YES    | NO     |
  +-----------------+--------------------------------------------------+---------------+--------+--------+
  | ubuntu          | https://cloud-images.ubuntu.com/releases         | simplestreams | YES    | YES    |
  +-----------------+--------------------------------------------------+---------------+--------+--------+
  | ubuntu-daily    | https://cloud-images.ubuntu.com/daily            | simplestreams | YES    | YES    |
  +-----------------+--------------------------------------------------+---------------+--------+--------+

  添加好之後,我們在清華的remote鏈接上找找ubuntu的鏡像。

  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc image list tuna-images: | grep "ubuntu"
  ......
  | ubuntu/14.04 (7 more)                | 556ec728021d | yes    | Ubuntu trusty amd64 (20200203_07:42)         | x86_64  | 75.20MB  | Feb 3, 2020 at 12:00am (UTC)  |
  ......
  | ubuntu/16.04 (7 more)                | 52aeaeb9a861 | yes    | Ubuntu xenial amd64 (20200203_07:42)         | x86_64  | 80.65MB  | Feb 3, 2020 at 12:00am (UTC)  |
  ......
  | ubuntu/18.04 (7 more)                | c1347eac69d7 | yes    | Ubuntu bionic amd64 (20200203_07:42)         | x86_64  | 94.22MB  | Feb 3, 2020 at 12:00am (UTC)  |
  ......

  可以看到目前常用的ubuntu版本鏡像都有。
  我們下載ubuntu 18.04版本的遠程鏡像到本地,鏡像別名爲ubuntu-snaptest。

  sudo lxc image copy ubuntu:18.04 local: --alias ubuntu-snaptest

  下載好之後,我們看看本地的image鏡像。

  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc image list
  +-----------------+--------------+--------    +---------------------------------------------+--------+----------  +------------------------------+
  |      ALIAS      | FINGERPRINT  | PUBLIC |                 DESCRIPTION                 |  ARCH  |   SIZE   |         UPLOAD DATE          |
  +-----------------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
  | ubuntu-snaptest | 979ff60086ca | no     | ubuntu 18.04 LTS amd64 (release) (20200107) | x86_64 | 178.70MB | Jan 10, 2020 at 9:22am (UTC) |
  +-----------------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+

2.2 創建ubuntu容器實例
  我們使用下載好的ubuntu 18.04鏡像來創建ubuntu容器實例。

  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc launch ubuntu-snaptest snapts
  Creating snapts
  Starting snapts
  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc list
  +---------+---------+--------------------+------+------------+-----------+
  |  NAME   |  STATE  |        IPV4        | IPV6 |    TYPE    | SNAPSHOTS |
  +---------+---------+--------------------+------+------------+-----------+
  | snapts  | RUNNING |                    |      | PERSISTENT | 0         |
  +---------+---------+--------------------+------+------------+-----------+

  可以看到ubuntu 18.04容器實例已經創建成功,但沒有IP地址,還需要進入ubuntu 18.04容器實例配置IP地址,否則容器實例將無法訪問訪問網絡。容器實例的IP地址需要配置在前文所述的lxdbr0網橋IP地址的網段內。

  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc exec snapts bash
  root@snapts:~# cd /etc/netplan/
  root@snapts:/etc/netplan# ls
  50-cloud-init.yaml

  ubuntu 18.04容器實例配置網絡需要修改這個yaml文件,編輯輸入一下內容。

  # This file is generated from information provided by the datasource.  Changes
  # to it will not persist across an instance reboot.  To disable cloud-init's
  # network configuration capabilities, write a file
  # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the       following:
  # network: {config: disabled}
  network:
      version: 2
      ethernets:
          eth0:
               dhcp4: no
               addresses: [10.10.8.100/8]
               gateway4: 10.10.8.1
               nameservers:
                     addresses: [8.8.8.8]

  我電腦上配置網橋ip地址爲10.10.8.1,所以容器實例的ip地址配置爲同一個網段的10.10.8.100。配置好ip地址之後,重啓容器實例。

  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc stop snapts
  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc start snapts
  pp@pp-ThinkPad-S2-2nd-Gen:~$ lxc list
  +---------+---------+--------------------+------+------------+-----------+
  |  NAME   |  STATE  |        IPV4        | IPV6 |    TYPE    | SNAPSHOTS |
  +---------+---------+--------------------+------+------------+-----------+
  | snapts  | RUNNING | 10.10.8.100 (eth0) |      | PERSISTENT | 0         |
  +---------+---------+--------------------+------+------------+-----------+  

  容器實例配置的IP地址已經生效。
  ubuntu 18.04配置IP地址和ubuntu 16.04是有差異的,這裏也附上ubuntu 16.04配置IP地址的方法,供使用ubuntu 16.04容器參考。

  vim /etc/network/interface
      # The loopback network interface
      auto lo
      iface lo inet loopback
      # The primary network interface
      auto eth0
      iface eth0 inet static
      address 10.10.8.101
      netmask 255.0.0.0
      gateway 192.168.8.1
  
  vim /etc/resolvconf/resolv.conf.d/base
      nameserver 8.8.8.8
      nameserver 8.8.4.4
  
  resolvconf -u
  
  /etc/init.d/networking restart

3.驗證snap包
  ubuntu 18.04容器實例創建好了,我們把nginx的snap包拷貝到容器中安裝驗證。

  lxc file push nginx_1.17.7_amd64.snap snapts/home/ubuntu/

  我們把nginx的snap包拷貝到容器後,再進入容器安裝,第一次安裝nginx snap包時,需要下載snap core,可能會比較慢。

  root@snapts1:/home/ubuntu# snap install nginx_1.17.7_amd64.snap --dangerous --devmode
  Download snap ""core" (8268) from  channel "stable"     
  nginx 1.17.7 installed  

  nginx snap包安裝成功,再執行一下nginx命令。

  root@snapts1:/home/ubuntu# nginx
  root@snapts1:/home/ubuntu# ps -A | grep "nginx"
   1508 ?        00:00:00 nginx
   1509 ?        00:00:00 nginx
  root@snapts1:/home/ubuntu# cd /var/snap/nginx/common/logs/
  root@snapts1:/var/snap/nginx/common/logs# ls
  access.log  error.log
  root@snapts1:/var/snap/nginx/common/logs# cd ../run
  root@snapts1:/var/snap/nginx/common/run# ls
  nginx.pid

  nginx命令運行成功,並正確生成了log文件和pid文件。

4.後續工作
  lxc/lxd是一套完整的虛擬化技術,支持的命令有很多,這裏不一一描述,請看參考資料。
  到這裏snap devmode模式的打包驗證工作基本完成了,後續我們將驗證classic/strict模式的snap包,請大家持續關注。

5.參考資料:
  https://blog.51cto.com/johjoh/2153408?source=dra

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章