如何在 Linux 启动时自动启动 LXD 容器

如何在 Linux 启动时自动启动 LXD 容器

Q:我正在使用基于 LXD(“Linux 容器”)的虚拟机。如何在 Linux 系统中启动时自动启动 LXD 容器?

当 LXD 在启动时运行,你就可以随时启动容器。你需要将 boot.autostart 设置为 true。你可以使用 boot.autostart.priority(默认值为 0)选项来定义启动容器的顺序(从最高开始)。你也可以使用 boot.autostart.delay(默认值 0)选项定义在启动一个容器后等待几秒后启动另一个容器。

语法

上面讨论的关键字可以使用 lxc 工具用下面的语法来设置:

  1. $ lxc config set{vm-name}{key}{value}
  2. $ lxc config set{vm-name} boot.autostart {true|false}
  3. $ lxc config set{vm-name} boot.autostart.priority integer
  4. $ lxc config set{vm-name} boot.autostart.delay integer

如何在 Linux 16.10 中让 LXD 容器在启动时启动?

输入以下命令:

  1. $ lxc config set{vm-name} boot.autostart true

设置一个 LXD 容器名称 “nginx-vm” 以在启动时启动

  1. $ lxc config set nginx-vm boot.autostart true

你可以使用以下语法验证设置:

  1. $ lxc config get{vm-name} boot.autostart
  2. $ lxc config get nginx-vm boot.autostart

示例输出:

  1. true

你可以使用下面的语法在启动容器后等待 10 秒钟后启动另一个容器:

  1. $ lxc config set nginx-vm boot.autostart.delay 10

最后,通过设置最高值来定义启动容器的顺序。确保 dbvm 容器首先启动,然后再启动 nginxvm。

  1. $ lxc config set db_vm boot.autostart.priority 100
  2. $ lxc config set nginx_vm boot.autostart.priority 99

使用下面的 bash 循环在 Linux 上查看所有配置值:

  1. #!/bin/bash
  2. echo'The current values of each vm boot parameters:'
  3. for c in db_vm nginx_vm memcache_vm
  4. do
  5. echo"*** VM: $c ***"
  6. for v in boot.autostart boot.autostart.priority boot.autostart.delay
  7. do
  8. echo"Key: $v => $(lxc config get $c $v) "
  9. done
  10. echo""
  11. done

示例输出:

Fig.01: Get autostarting LXD containers values using a bash shell script

Fig.01: Get autostarting LXD containers values using a bash shell script


via: https://www.cyberciti.biz/faq/how-to-auto-start-lxd-containers-at-boot-time-in-linux/

作者:Vivek Gite 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

猜你喜欢

转载自www.linuxidc.com/Linux/2017-12/149868.htm