วันเสาร์ที่ 22 ธันวาคม พ.ศ. 2561

[Linux Container] - How to Install & Basic

https://linuxcontainers.org/ เว็บที่สามารถไปอ่านได้ควรอ่าน
https://lxd.readthedocs.io/en/stable-3.0/ | ContainerNetwork

แรกเริ่มเดิมทีไม่ได้สนใจเจ้า Linux Container เลยเพราะคิดว่าใช้ยาก หาข้อมูลภาษาไทยหาไม่เจอ เราจะเจอแต่ Docker เยอะเลย
เจ้าตัว Docker พยายามลองใช้หลายครั้งแล้ว ลงเรียนออนไลน์ก็แล้ว ส่วนตัวรู้สึกว่าการ Config ค่าใน Container ทำได้ไม่เหมือนกับที่เราลงกับเครื่องจริงๆ เลยลองหาข้อมูลดูว่ามีตัวเลือกอื่นๆ อีกไหม ซึ่งก็เจออยู่หลายตัว แต่ในนี้จะไม่ขอพูดถึง เพราะไม่ได้อ่านตัวอื่น เลือก Linux Container แล้วก็ศึกษาตัวเดียวนี้เลย และก็พบกับตัวเองว่าคำสั่งหลาย หรือทุกคำสั่งเลยมั้ง เราใช้ใน Container ได้เหมือนลงจริงๆ และก็ลองไปเรื่อยๆ จนคิดว่าเราน่าจะถูกจริตกับตัวนี้ที่สุดแล้วมั้งๆ

การติดตั้ง Linux Container ต้องใช้ Ubuntu นะเพราะเขาค่ายเดียวกันทำมาเพื่อกันและกันก็จะลงง่ายมาก
sudo snap install lxd

เมื่อติดตั้งเสร็จให้เราทำการ Initial ก่อน
sudo lxd init

# เพิ่มเข้ากลุ่ม LXD เวลาใช้งานจะได้ไม่ต้องค่อยพิมพ์ sudo
sudo usermod --append --groups lxd sarankon

คำสั่งมีอะไรบ้างอันนี้แบบไม่จัดกลุ่ม ให้เราพิมพ์
คำสั่ง lxd --help
sudo lxd --help

Description:
  The LXD container manager (daemon)

  This is the LXD daemon command line. It's typically started directly by your
  init system and interacted with through a tool like `lxc`.

  There are however a number of subcommands that let you interact directly with
  the local LXD daemon and which may not be performed through the REST API alone.

Usage:
  lxd [flags]
  lxd [command]

Available Commands:
  activateifneeded   Check if LXD should be started
  help               Help about any command
  import             Import existing containers
  init               Configure the LXD daemon
  shutdown           Tell LXD to shutdown all containers and exit
  version            Show the server version
  waitready          Wait for LXD to be ready to process requests

Flags:
  -d, --debug     Show all debug messages
      --group     The group of users that will be allowed to talk to LXD
  -h, --help      Print help
      --logfile   Path to the log file
      --syslog    Log to syslog
      --trace     Log tracing targets
  -v, --verbose   Show all information messages
      --version   Print version number

Use "lxd [command] --help" for more information about a command.

คำสั่ง lxc --help
sudo lxc --help

Description:
  Command line client for LXD

  All of LXD's features can be driven through the various commands below.
  For help with any of those, simply call them with --help.

Usage:
  lxc [command]

Available Commands:
  alias       Manage command aliases
  cluster     Manage cluster members
  config      Manage container and server configuration options
  console     Attach to container consoles
  copy        Copy containers within or in between LXD instances
  delete      Delete containers and snapshots
  exec        Execute commands in containers
  export      Export container backups
  file        Manage files in containers
  help        Help about any command
  image       Manage images
  import      Import container backups
  info        Show container or server information
  launch      Create and start containers from images
  list        List containers
  move        Move containers within or in between LXD instances
  network     Manage and attach containers to networks
  operation   List, show and delete background operations
  profile     Manage profiles
  project     Manage projects
  publish     Publish containers as images
  remote      Manage the list of remote servers
  rename      Rename containers and snapshots
  restart     Restart containers
  restore     Restore containers from snapshots
  snapshot    Create container snapshots
  start       Start containers
  stop        Stop containers
  storage     Manage storage pools and volumes
  version     Show local and remote versions

Flags:
      --all              Show less common commands
      --debug            Show all debug messages
      --force-local      Force using the local unix socket
  -h, --help             Print help
      --project string   Override the source project
  -q, --quiet            Don't show progress information
  -v, --verbose          Show all information messages
      --version          Print version number

Use "lxc [command] --help" for more information about a command.

ตัวที่น่าจะใช้งานกับ Container ก็เป็นพวก
คำสั่ง สร้าง ลบ ดูรายการ = launch, delete, list
คำสั่ง นำเข้า นำออก (Backup) = import, export
คำสั่ง เริ่มทำงาน หยุดทำงาน = start, stop, restart
คำสั่ง ดูข้อมูล แก้ไขข้อมูล = info, config
คำสั่ง จัดการ Snapshot = snapshot, restore
คำสั่ง จัดการ Image = publish, image list, image import, image export, image delete


คำสั่ง สร้าง launch, ลบ delete, ดูรายการ list
# ดูรายการ Image มีให้เราใช้ หลังจากที่เราใช้คำสั่ง launch จะเป็นการ โหลด Image มาเก็บไว้ที่เครื่องเราเพื่อให้เราสร้างครั้งต่อๆ ไปเร็วขึ้น
lxc image list images: | grep -C1 -i centos

# สร้าง Container และจะเริ่มทำงานเมื่อสร้างเสร็จ lxc launch [remote-name]:image-name container-name
lxc launch images:centos/7 centos7

# ดูรายการ Container
lxc list

# สั่งให้หยุดการทำงานก่อนที่เราจะลบ
lxc stop centos7
lxc delete centos7

คำสั่ง สร้างแบ็คอัพไฟล์ export, โหลดแบ็คอัพไฟล์ import
#lxc export container-name backup-file
lxc export centos-7 centos-7-bakcup

#lxc import backup-file
lxc import centos-7-backup

#ไฟล์ชื่อ Container จะได้เป็นชื่อเดิม ลอง List ดู
lxc list


ตัวอย่างการ Map Storage ระหว่าง Container กับ Host
# ดูการตั้งค่าของ Container centos-7
sudo lxc config device list centos-7
sudo lxc config device show centos-7

# สร้าง Mapping ระหว่าง Host กับ Container share-device เป็นชื่อที่เราตั้งขึ้นเองไว้อ้างอิง source เป็นของ Host ,path เป็นของ Container
lxc config device add centos-7 share-device disk source=/home/share path=/home/share

ตัวอย่างการ Forward Port ผ่าน iptables ก็จะยาวๆ หน่อย
# เพิ่มการ Forward ให้ Port 80 ที่เครื่อง Host จะส่งต่อไปที่ Container IP 10.96.242.212 Port 80
# enp0s3 ให้ดูว่าเป็นช่องไหน ดูได้โดยใช้คำสั่ง ip address
sudo iptables -t nat -A PREROUTING -p tcp -i enp0s3 --dport 80 -j DNAT --to-destination 10.96.242.212:80

# ดูรายการที่เราเคยทำการ Forward ไว้
sudo iptables -t nat -L PREROUTING

# ลบรายการที่เราทำการ Forward ไว้
# `sudo iptables -t nat -D PREROUTING --line-number`
sudo iptables -t nat -D PREROUTING 1




อ้างอิงข้อมูลอื่น
Digital Ocean | Setup and Use LXD on Ubuntu 16.04

ไม่มีความคิดเห็น:

แสดงความคิดเห็น