debian openvz vps 优化

比较喜欢 debian vps ,系统比较简洁,而且很省内存,但大部分的 vps 商都使用的标准模版,带了不少垃圾东西。

首先进行系统更新

apt-get update && apt-get upgrade

安装sysv-rc-conf做为开机管理项

apt-get install sysv-rc-conf

先删除 apache 相关,使用 nginx 代替

apt-get remove --purge apache*

删除sendmail 相关

apt-get remove --purge sendmail* sasl2-bin

删除bind9相关

apt-get remove --purge bind9-*

删除xinetd 相关

apt-get remove --purge xinetd 

删除samba相关

apt-get remove --purge samba-*

删除rpcbind相关

apt-get remove --purge rpcbind

debian 下安装 lxc

本文安装环境 debian 6.0

安装 lxc debootstrap 工具

aptitude install lxc bridge-utils debootstrap

设置网络

auto eth1
iface eth1 inet manual

auto br0
iface br0 inet static
  bridge_ports eth1  #根据自己实际网卡设置
  bridge_fd 0
  address 10.0.0.100
  netmask 255.255.255.0
  gateway 10.100.0.1
  dns-nameservers 10.20.0.2

加载 cgroup 编辑 /etc/fstab

cgroup  /sys/fs/cgroup  cgroup  defaults  0   0

立即加载

mount /sys/fs/cgroup

接下来创建一下虚拟机

#下载 squeeze  64位
debootstrap --verbose --variant=minbase --arch=amd64 --include ifupdown,locales,libui-dialog-perl,\
dialog,netbase,net-tools,iproute,openssh-server,vim,apt-utils,proxychains \
squeeze /var/cache/lxc/debian/rootfs-amd64 http://mirrors.163.com/debian/


mkdir -p /lxc/vm0     #存放目录

/usr/lib/lxc/templates/lxc-debian -p /lxc/vm0/   # 创建虚拟机

配置虚拟机网络,在 /lxc/vm0/config 文件填加些文本

lxc.utsname = vm0
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
# lxc.network.name = eth0
lxc.network.hwaddr = 00:FF:12:34:56:78
lxc.network.ipv4 = 10.0.0.110/24

到些整个虚拟机创建完成,接下来我们启动他

lxc-start -n vm0 -f /lxc/vm0/config

一些常用命令

lxc-info -n vm0 # 查看状态
lxc-stop -n vm0 # 关闭虚拟机

后边陆续添加如何对 lxc虚拟机 进行资源限制

debian 下 lxc 限制内存出错

起动 vm 的时候出现

lxc-start: open /cgroup/vm0/memory.limit_in_bytes : No such file or directory
lxc-start: failed to setup the cgroups for ‘vm0’
lxc-start: failed to setup the container
lxc-start: invalid sequence number 1. expected 2
lxc-start: failed to spawn ‘vm0’

在 /etc/default/grub 文件填加

GRUB_CMDLINE_LINUX="cgroup_enable=memory"
0# lxc-checkconfig 
Kernel config /proc/config.gz not found, looking in other places...
Found kernel config file /boot/config-2.6.32-5-amd64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled
Multiple /dev/pts instances: enabled

--- Control groups ---
Cgroup: enabled
Cgroup namespace: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: missing
Cgroup cpuset: enabled

--- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
File capabilities: enabled

debian bridge 网卡参数说明

bridge_fd is the bridge forward delay time, in seconds, default 15.

bridge_hello is the bridge hello time, in seconds, default 2.

bridge_maxage is the bridge’s maximum message time, in seconds, default is 20.

bridge_stp controls the spanning tree protocol, on or off. Default is off, and is recommended to stay that way.

lxc guest 报错 respawning too disabled minutes

INIT: Id "c1" respawning too fast: disabled for 5 minutes
INIT: Id "c4" respawning too fast: disabled for 5 minutes
INIT: Id "c1" respawning too fast: disabled for 5 minutes
INIT: Id "c2" respawning too fast: disabled for 5 minutes
INIT: Id "c3" respawning too fast: disabled for 5 minutes
INIT: Id "c4" respawning too fast: disabled for 5 minutes
INIT: Id "c1" respawning too fast: disabled for 5 minutes
INIT: Id "c2" respawning too fast: disabled for 5 minutes
INIT: Id "c3" respawning too fast: disabled for 5 minutes
INIT: Id "c4" respawning too fast: disabled for 5 minutes

解决方法 编辑 /etc/inittab

#c1:12345:respawn:/sbin/getty 38400 tty1 linux
#c2:12345:respawn:/sbin/getty 38400 tty2 linux
#c3:12345:respawn:/sbin/getty 38400 tty3 linux
#c4:12345:respawn:/sbin/getty 38400 tty4 linux