使用 Beaker 测试 Docker 实例

使用 Beaker 测试 Docker 实例

我有一套针对 Docker 主机运行的 Beaker 测试。

我不知道 docker 处理交换文件的复杂程度,但它似乎不喜欢它。

Puppet 代码如下:

exec { 'Create swap file':
  command => "/bin/dd if=/dev/zero of=${swapfile} bs=1M count=${swapfilesize_mb}",
  creates => $swapfile,
}
exec { 'Attach swap file':
  command => "/sbin/mkswap ${swapfile} && /sbin/swapon ${swapfile}",
  require => Exec['Create swap file'],
  unless  => "/sbin/swapon -s | grep ${swapfile}",
}
if $add_mount {
  mount { 'swap':
    ensure  => present,
    fstype  => swap,
    device  => $swapfile,
    dump    => 0,
    pass    => 0,
    require => Exec['Attach swap file'],
  }
}

错误信息如下:

Info: Loading facts
Notice: Compiled catalog for centos-6-x64 in environment production in 0.22 seconds
Info: Applying configuration version '1411345072'
Notice: /Stage[main]/Swap_file/Exec[Create swap file]/returns: executed successfully
Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: mkswap: /tmp/swapfile: warning: don't erase bootbits sectors
Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns:         on whole disk. Use -f to force.
Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: Setting up swapspace version 1, size = 5116 KiB
Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: no label, UUID=ceb75f7d-ae8b-4781-bd1b-4123bec9bcf1
Notice: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: swapon: /tmp/swapfile: swapon failed: Input/output error
Error: /sbin/mkswap /tmp/swapfile && /sbin/swapon /tmp/swapfile returned 255 instead of one of [0]
Error: /Stage[main]/Swap_file/Exec[Attach swap file]/returns: change from notrun to 0 failed: /sbin/mkswap /tmp/swapfile && /sbin/swapon /tmp/swapfile returned 255 instead of one of [0]
Notice: /Stage[main]/Swap_file/Mount[swap]: Dependency Exec[Attach swap file] has failures: true
Warning: /Stage[main]/Swap_file/Mount[swap]: Skipping because of failed dependencies
Notice: Finished catalog run in 0.26 seconds

所以基本上,我该如何设置一个docker容器,让它可以运行swapon而不会出错?

答案1

要在 Docker 中支持交换,首先必须使用以下参数启用内存和 wap 的 cgroup 管理:cgroup_enable=memory swapaccount=1。

如果您使用的是 grub,它应该位于 /etc/default/grub 中,并且要将其添加到的行应该是 GRUB_CMDLINE_LINUX 或 GRUB_CMDLINE_LINUX_DEFAULT。进行此更改后,运行 sudo update-grub 并重新启动。更多详细信息如下: http://docker.readthedocs.org/en/v0.7.3/installation/kernel/

如果这没有帮助,请告诉我,我们可以继续。如果这没有帮助,请尝试通过在 shell 中启动容器来手动运行这些命令。运行此命令以获取交互式 shell 并运行命令:docker run -t -i image_name /bin/bash

答案2

因此我在#dockerfreenode 房间询问,结果发现实际上不可能在 docker 容器内管理交换:

10:44 AM <petems> cnf: How do I make it so that in a docker container for say, centos 6, I can run swap commands? Is that possible?
10:44 AM <cnf> petems: you can NOT swapon for _only_ the container, sorry
10:45 AM <cnf> petems: docker is NOT a VM

相关内容