Pages

Banner 468 x 60px

 

Thứ Tư, 2 tháng 11, 2011

Hướng dẫn cài đặt và sử dụng OpenVZ trên CentOS 6.0

0 nhận xét

Quản Trị Mạng - OpenVZ là một nhánh mã nguồn mở của Virtuozzo do SWsoft cung cấp. Đây là một giải pháp ảo hóa thương mại được sử dụng bởi nhiều nhà cung cấp máy chủ ảo hóa. Các bản vá nhân OpenVZ được cấp phép theo giấy phép GLP và các công cụ cấp người dùng theo giấy phép QPL. Với OpenVZ bạn có thể tạo ra nhiều máy chủ riêng ảo (VPS) trên cùng một phần cứng tương tự như Xen và các dự án Linux Vserver. Trong bài hướng dẫn này chúng tôi sẽ giúp các bạn chuẩn bị một máy chủ CentOS 6.0 cho OpenVZ.
Ảnh minh họa hệ thống mạng sử dụng công nghệ ảo hóa.

Cài đặt OpenVZ

Để cài đặt OpenVZ, chúng ta cần thêm kho OpenVZ cho yum:
cd /etc/yum.repos.d
wget http://download.openvz.org/openvz.repo
rpm --import http://download.openvz.org/RPM-GPG-Key-OpenVZ
Bây giờ mở openvz.repo...
vi openvz.repo
… và vô hiệu hóa kho lưu trữ [openvz-kernel-rhel5] (đặt enabled=0) và thay vào đó là kích hoạt kho [openvz-kernel-rhel6] (đặt enabled=1):
[...]
[openvz-kernel-rhel5]
name=OpenVZ RHEL5-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel5-2.6.18/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel5-2.6.18
enabled=0
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[...]
[openvz-kernel-rhel6]
name=OpenVZ RHEL6-based kernel
#baseurl=http://download.openvz.org/kernel/branches/rhel6-2.6.32/current/
mirrorlist=http://download.openvz.org/kernel/mirrors-rhel6-2.6.32
enabled=1
gpgcheck=1
gpgkey=http://download.openvz.org/RPM-GPG-Key-OpenVZ
[...]
Nội dung kho lưu trữ các nhân (kernel) của OpenVZ sẽ có một vài sự khác biệt (bạn có thể xem chi tiết tại đây).
Chạy dòng lệnh:
yum search vzkernel
Và đây là một số kernel có sẵn:
[root@server1 yum.repos.d]# yum search vzkernel
[...]
vzkernel.i686 : The Linux kernel
vzkernel.x86_64 : The Linux kernel
vzkernel-debug.i686 : The Linux kernel compiled with extra debugging enabled
vzkernel-debug.x86_64 : The Linux kernel compiled with extra debugging enabled
vzkernel-debug-devel.i686 : Development package for building kernel modules to match the debug kernel
vzkernel-debug-devel.x86_64 : Development package for building kernel modules to match the debug kernel
vzkernel-devel.i686 : Development package for building kernel modules to match the kernel
vzkernel-devel.x86_64 : Development package for building kernel modules to match the kernel
vzkernel-firmware.noarch : Firmware files used by the Linux kernel
vzkernel-headers.i686 : Header files for the Linux kernel for use by glibc
vzkernel-headers.x86_64 : Header files for the Linux kernel for use by glibc
[root@server1 yum.repos.d]#
Chọn một trong số đó và tiến hành cài đặt:
yum install vzkernel
Điều này sẽ tự động update các bộ nạp khởi động GRUB tốt nhất. Chúng ta nên mở /boot/grub/menu.lst; đoạn kernel đầu tiên lúc nên chứa nội dung của kernel OpenVZ mới. Tiêu đề bạn có thể đặt là "CentOS Linux". Ngoài ra hãy chắc chắn rằng giá trị default 0 để các kernel đầu tiên (OpenVZ kernel) sẽ tự động khởi động thay vì CentOS kernel như mặc định.
vi /boot/grub/menu.lst
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/mapper/vg_server1-lv_root
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS Linux OpenVZ (2.6.32-042stab020.1)
root (hd0,0)
kernel /vmlinuz-2.6.32-042stab020.1 ro root=/dev/mapper/vg_server1-lv_root rd_LVM_LV=vg_server1/lv_root rd_LVM_LV=vg_server1/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=de crashkernel=auto rhgb quiet
initrd /initramfs-2.6.32-042stab020.1.img
title CentOS (2.6.32-71.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-71.el6.x86_64 ro root=/dev/mapper/vg_server1-lv_root rd_LVM_LV=vg_server1/lv_root rd_LVM_LV=vg_server1/lv_swap rd_NO_LUKS rd_NO_MD rd_NO_DM LANG=en_US.UTF-8 SYSFONT=latarcyrheb-sun16 KEYBOARDTYPE=pc KEYTABLE=de crashkernel=auto rhgb quiet
initrd /initramfs-2.6.32-71.el6.x86_64.img
Bầy giờ chúng ta tiến hành cài đặt OpenVZ sử dụng công cụ:
yum install vzctl vzquota
Mở /etc/sysctl.conf và chắc chắn rằng bạn có những thiết lập như dưới đây:
vi /etc/sysctl.conf
[...]
net.ipv4.ip_forward = 1
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.all.rp_filter = 1
kernel.sysrq = 1
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.icmp_echo_ignore_broadcasts=1
net.ipv4.conf.default.forwarding=1
[...]
Nếu bạn muốn chỉnh sửa /etc/sysctl.conf, chạy lệnh:
sysctl -p
Lưu ý: Các bước tiếp theo dưới đây rất quan trọng nếu địa chỉ IP của máy ảo là từ một Subnet khác với địa chỉ IP của hệ thống máy chủ. Nếu bạn không thực hiện chính xác theo những bước này, kết nối mạng sẽ không hoạt động trong máy ảo!
Mở /etc/vz/vz.conf và thiết lập NEIGHBOUR_DEVS cho toàn bộ:
vi /etc/vz/vz.conf
[...]
NEIGHBOUR_DEVS=all
[...]
SELinux cần phải vô hiệu hóa nếu bạn muốn sử dụng OpenVZ. Mở /etc/sysconfig/selinux và thiết lập giá trị của SELINUX thành disabled:
vi /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
Cuối cùng khởi động lại hệ thống:
reboot
Nếu hệ thống được reboot lại mà không gặp sự cố nào, chạy tiếp lệnh sau:
uname -r
Và kernel mới của OpenVZ sẽ hiển thị như sau:
[root@server1 ~]# uname -r
2.6.32-042stab020.1
[root@server1 ~]# 

Sử dụng OpenVZ

Trước khi có thể tạo máy ảo với OpenVZ, cần có một template cho distribution mà chúng ta muốn sử dụng trong máy ảo tại đường dẫn /vz/template/cache. Máy ảo sẽ được tạo từ template này. Bạn có thể tìm thấy một danh sách các template của precreated tại đây.
Ví dụ chúng tôi muốn sử dụng Fedora 15 trong máy ảo của mình, do đó tải về một Fedora 15 template:
cd /vz/template/cache
wget http://download.openvz.org/template/precreated/beta/fedora-15-x86.tar.gz
Và sau đây là các lệnh cơ bản để sử dụng OpenVZ:
Để thiết lập một VPS từ Fedora 15 template, chạy lệnh:
vzctl create 101 --ostemplate fedora-15-x86 --config basic
Thông số 101 phải là một unique ID, và mỗi máy ảo cần có một unique ID riêng của nó. Bạn có thể sử dụng phần cuối của địa chỉ IP cho máy ảo, chẳng hạn bạn có địa chỉ là 192.168.0.101, bạn có thể dùng ngay số 101 làm ID.
Nếu bạn muốn VM khởi động ngay khi boot, chạy lệnh:
vzctl set 101 --onboot yes --save
Thiết lập một hostname và địa chỉ IP cho VM:
vzctl set 101 --hostname test.example.com --save
vzctl set 101 --ipadd 192.168.0.101 --save
Tiếp theo, thiết lập số socket lên tới 120 và gán một tên máy chủ cho VM:
vzctl set 101 --numothersock 120 --save
vzctl set 101 --nameserver 8.8.8.8 --nameserver 8.8.4.4 --nameserver 145.253.2.75 --save
(Thay vì sử dụng lệnh vzctl set, bạn có thể chỉnh sửa trực tiếp tập tin cấu hình của VM trong kho lưu trữ tại đường dẫn /etc/vz/conf. Nếu ID của VM là 101, sau đó tiến hành cấu hình lại tập tin /etc/vz/conf/101.conf).
Để khởi động VM, chạy lệnh:
vzctl start 101
Thiết lập một mật khẩu root cho VM, thực thi lệnh:
vzctl exec 101 passwd
Bây giờ bạn có thể thực hiện kết nối tới VM thông qua SSH (chẳng hạn với PuTTY) hoặc nhập vào lệnh sau:
vzctl enter 101
Để thoát khỏi giao diện điều khiển của VM, sử dụng lênh:
exit
Để dừng hẳn một VM, chạy lệnh:
vzctl stop 101
Để restart một VM, chạy lệnh:
vzctl restart 101
Để xóa một VM từ ổ đĩa cứng (bạn phải dừng hẳn lại trước khi làm điều này), chạy lệnh:
vzctl destroy 101
Để xem danh sách các VM của bạn và trạng thái của chúng, dùng lệnh:
vzlist -a
[root@server1 cache]# vzlist -a
      CTID      NPROC STATUS    IP_ADDR         HOSTNAME
       101         14 running   192.168.0.101   test.example.com
[root@server1 cache]#
Để xem nguồn tài nguyên được phân bổ cho một VM, nhập vào dòng lệnh sau:
vzctl exec 101 cat /proc/user_beancounters
[root@server1 cache]# vzctl exec 101 cat /proc/user_beancounters
Version: 2.5
uid resource held maxheld barrier limit failcnt
101: kmemsize 1508202 1661695 11055923 11377049 0
lockedpages 0 0 256 256 0
privvmpages 5430 7102 65536 69632 0
shmpages 381 381 21504 21504 0
dummy 0 0 0 0 0
numproc 19 21 240 240 0
physpages 2489 2775 0 2147483647 0
vmguarpages 0 0 33792 2147483647 0
oomguarpages 2489 2775 26112 2147483647 0
numtcpsock 5 5 360 360 0
numflock 3 4 188 206 0
numpty 0 1 16 16 0
numsiginfo 0 2 256 256 0
tcpsndbuf 44720 0 1720320 2703360 0
tcprcvbuf 81920 0 1720320 2703360 0
othersockbuf 13144 14356 1126080 2097152 0
dgramrcvbuf 0 8380 262144 262144 0
numothersock 11 13 120 120 0
dcachesize 0 0 3409920 3624960 0
numfile 503 531 9312 9312 0
dummy 0 0 0 0 0
dummy 0 0 0 0 0
dummy 0 0 0 0 0
numiptent 10 10 128 128 0
[root@server1 cache]#
Ở đây bạn lưu ý đến cột failcnt bởi nó rất quan trọng, nó chỉ được phép chứa giá trị 0, nếu không có nghĩa là VM đang cần dùng nhiều hơn nguồn tài nguyên đã được phân bổ cho nó. Mở tập tin cấu hình của VM trong /etc/vz/conf và tăng thêm tài nguyên, sau đó khởi động lại VM.
Để tìm hiểu thêm về lệnh vzctl, chạy:
man vzctl

Không có nhận xét nào:

Đăng nhận xét