Showing posts with label OpenVZ. Show all posts
Showing posts with label OpenVZ. Show all posts

Saturday, 12 March 2016

VPS Management OpenVZ


CREATE AND MANAGE VPS:
-------------------------------------------

    Before creating VPS you need to download desired pre-created templates (those are in .tar.gz format) and store it under "/vz/template/cache". But don't untar/extract them. You can download templates here - http://download.openvz.org/template/precreated.

DOWNLOAD TEMPLATES
    ]# cd /vz/template/cache
    ]# wget http://download.openvz.org/template/precreated/ubuntu-9.04-x86.tar.gz
    ]# wget http://download.openvz.org/template/precreated/centos-6-x86.tar.gz

CHECK AVAILABLE PRECREATED OSTEMPLATES:

    ]# vzpkgls
       ubuntu-9.04-x86
       centos-6-x86
       
    NOTE: It will get this information from /vz/template/cache directory. Lists all templates without .tar.gz extension.

DETERMINE VPS ID:

    VPS is managed by only ID, hence you need to follow simple guidliness regarding this.The ID is a 32-bit integer number beginning with zero and unique for a given Hardware Node. OpenVZ reserves the IDs ranging from 0 to 100. Though OpenVZ uses only ID 0, different versions might use additional Virtual Private Servers IDs for internal needs.
    The only strict requirement for a VPS ID is to be unique for a particular Hardware Node.For example, on Hardware Node 1 you create
Virtual Private Servers within the range of IDs from 101 to 1000; on Hardware Node 2 you use the range from 1001 to 2000, and so on. This approach makes it easier to remember on which Hardware Node a Virtual Private Server has been created, and eliminates the possibility of VPS
ID conflicts when a Virtual Private Server migrates from one Hardware Node to another.
    Note, Before determining VPS ID make sure that there is no VPS exists having same ID. You may use command -

    ]# vzlist -a 101
       Container(s) Not Found

CREATE VPS AND SET PARAMETERS:

    ]# vzctl create 101 --ostemplate centos-6-x86 --config basic

    Structure of container is, The private area is the directory containing the actual files of the given Container; it is usually residing in /vz/private/CTID/. The private area is mounted to the /vz/root/CTID directory on the Hardware Node and provides Container users with a complete Linux file system tree.
    The option --config sets parameters for VPS from sample configuration files located under "/etc/sysconfig/vz-scripts/". These file names are like, ve-basic.conf-sample, ve-unlimited.conf-sample. You need to specify the file name portion between '-' and '.' with --config option. You can create and use sample configuration files according to your need.
    If you don't specify --ostemplate and --config options, then it will use default settings from globle configuration file "/etc/sysconfig/vz". You may customize this file by editing options CONFIGFILE="basic" and DEF_OSTEMPLATE="centos-5".

    ]# vzctl set 101 --onboot yes --save  OR ]# vzctl set 101 --onboot no --save
   
    ]# vzctl set 101 --ipadd 202.52.134.143 --save

    ]# vzctl set 101 --hostname test.rootuser.in --save

    ]# vzctl set 101 --nameserver 202.52.134.254 --nameserver 8.8.8.8 --save

MEMORY(RAM)LIMIT:

    ]# vzctl set 101 --ram 1G --save AND ]# vzctl set 101 --swap 1G --save (--ram <bytes>[KMG] | --swap <bytes>[KMG])
OR
    ]# vzctl set 101 --vmguarpages 1536M --save (Dedicated)
    ]# vzctl set 101 --oomguarpages 1536M --save
    ]# vzctl set 101 --privvmpages 1536M:2048M --save (Burstable)

HDD(Disk)LIMIT:

    ]# vzctl set 101 --diskspace 45G:50G --save
    ]# vzctl set 101 --diskinodes 471859:524288 --save

    Disk inodes must be at least 1% of actual disk space. Eg, Firstly convert disk space from GB/MB to KB.
    Here, 45 GB = 47185920 KB & 50 GB = 52428800 KB. So disk inodes will be 471859:524288.

CPU/CPU UNITS & CPU LIMIT (CPU OR cupunits+cpulimit)

    Example:
    If you have a 1 Quad Core CPU at 2.0 ghz

    The Max CPU Limit (%) would be 400
    The Max Number of CPU's would 4

    ie. 1 core is max 100% and 4 cores is max 400%
    Case:
    CPU Units = 4
    CPU Limit (%) = 200
    The customer will see 4 cores at 1.0ghz each.

    OR simple use below trick.

    As we have a 1 Quad Core CPU at 2.0 ghz, We can calculate CPU power into Percentage (100%) as below,

    ]# vzcpucheck
    Current CPU utilization: 5000  <-- CPU utilized by HW node including all VPS.
    Power of the node: 479997      <-- HW nodes' max CPU power.

    If I want to assign 25% of my Quad Core CPU to VPS ID 101, then 1% of actual CPU will be [(479997*1)/100] = 4799.97. So 1 cpuunits will be 4799.97. Simillarly 25% of CPU would be around 119999.  In the following example, Container 101 is guaranteed to receive about 25% of the CPU time even if the Hardware Node is fully used. Besides, CT 101 will not receive more than 30% of the CPU time even if the CPU is not fully loaded.

    ]# vzctl set 101 --cpuunits 119999 --cpulimit 30 --save

    In other words, 25% = 1 CPU core. Hence we can also use below command -

    ]# vzctl set 101 --cpus 1 --save

    In above example, We configuring 1 CPU inside container. Option --cpus sets number of CPUs available in the container.

    ]# vzctl start 101
    ]# vzctl set 101 --userpasswd root:CentOS_06 OR ]# vzctl exec 101 passwd 
   
    ]# vzctl set 101 --name computer1 --save (Its an alternate name for CID.)
    ]# vzlist -o name 101

    ]# vzctl set 101 --description "Owner - L Johnson ; Purpose - Email ; Providers - Hostin Services" --save
    ]# vzlist -o description 101

MANAGE CONTAINER STATUS:

    ]# vzctl start 101
    ]# vzctl stop 101
    ]# vzctl restart 101
    ]# vzctl status 101

VIEW CONTAINERS:

    ]# vzlist -a <CID>
    ]# vzlist  [Display live/running/online containers]
    ]# vzlist -a [Display all online/offline containers]

RUN A COMMAND FOR VPS FROM HARDWARE NODE:

    ]# vzctl exec 101 service sshd start
    ]# vzctl exec 101 chkconfig sshd on
    ]# vzctl exec 101 date
    ]# vzctl exec 101 hostname
    ]# vzctl exec 101 ifconfig

ACCESSING VPS FROM HARDWARE NODE:

    ]# vzctl enter 101
 vps~]# exit

    You can access VPS remotely from Linux or windows machine. You may use ssh from Linux and Putty 3rd party tool from windows machine.

DELETE VPS:
   
    ]# vzctl stop 101
    ]# vzctl destroy 101
    ]# cd /etc/sysconfig/vz-scripts/
    ]# ls 101.* [You will find files 101.conf.destroyed, 101.mount.destroyed and 101.umount.destroyed]
    ]# rm -rf 101.*

DETERMINE VPS ID FROM PROCESS:

    ]# top OR ]# ps -auxf
    ]# vzpid <PID>

VALIDATING VIRTUAL PRIVATE SERVER CONFIGURATION:

[Manual Method]

    ]# vzcfgvalidate /etc/sysconfig/vz-scripts/101.conf

    Error: kmemsize.bar should be > 1835008 (currently, 25000)
    Recommendation: dgramrcvbuf.bar should be > 132096 (currently, 65536)
    Recommendation: othersockbuf.bar should be > 132096 (currently,122880)

    ]# vzctl set 101 --kmemsize 2211840:2359296 --save

    ]# vzcfgvalidate /etc/sysconfig/vz-scripts/101.conf

    Recommendation: kmemsize.lim-kmemsize.bar should be > 163840 (currently, 147456)
    Recommendation: dgramrcvbuf.bar should be > 132096 (currently, 65536)
    Recommendation: othersockbuf.bar should ba > 132096 (currently,122880)
    Validation completed: success

[Auto validate Method]

    ]# vzcfgvalidate -r|-i

DISABLE CONTAINER:

    There may appear situations when you wish to forbid Container owners to use their Containers. For example, it may happen in case the Container owner uses it for unallowed purposes: intruding into computers of other users, participating in DoS attacks, etc. In such cases, the OpenVZ software allows you to disable a Container, thus, making it impossible to start the Container once it was stopped. For example, you can execute the following command to disable Container 101 residing on your Hardware Node:

    ]# vzctl set 101 --disabled yes --save  OR     ]# vzctl set 101 --disabled no --save 

    After the Container stopping, the Container user will not be able to start it again until you enable this Container again by passing the --disabled no option to vzctl set. You can also use the --force option to start any disabled Container. For example:

    ]# vzctl start 101 --force

NOTE: I make use of --disabled option, when I migrated vps from one node to another. but one copy of migrated vps was on the source node, so another admin's (or after hw reboot they) can't start automatically.

SUSPEND CONTAINER:

    OpenVZ allows you to suspend any running Container on the Hardware Node by saving its current state to a special dump file. Later on, you can resume the Container and get it in the same state the Container was at the time of its suspending. During the command execution, the /vz/dump/Dump.101 file containing the entire state of Container 101 is created and the Container itself is stopped.

    ]# vzctl chkpnt 101

    At any time, you can resume Container 101 by executing the following command -

    ]# vzctl restore 101

    The Container state is restored from the /vz/dump/Dump.101 file on the Node. Upon the restoration completion, any applications that were running inside Container 101 at the time of its suspending will be running and the information content will be the same as it was when the Container was suspended.

REMOVE VPS IP ADDRESS:

    ]# vzctl set 101 –ipdel 202.52.134.123 --save

IMP FILES:

    /var/log/vzctl.log
    /etc/sysconfig/vz
    /etc/sysconfig/vz-scripts/vpsid.conf
    /etc/sysconfig/vz-scripts/ve-name.conf-sample
    /etc/sysctl.conf
    /etc/cron.d/vpsreboot
    /vz/dump/
    /vz/templates/cache

OTHER IMP PARAMETERS (Remaning to learn):

avnumproc   : The average number of processes and threads.
numproc     : The maximal number of processes and threads the VPS may create.
numtcpsock  : The max number of TCP sockets.
numothersock: The max number of sockets other than TCP ones. Like UDP, etc.

thats it...
   

VPS Migration OpenVZ


LIVE/ONLINE MIGRATION:
------------------------------------

Here,
    I have two HW nodes running OpenVZ as S1.ROOTUSER.IN (192.168.0.10) AND S2.ROOTUSER.IN (192.168.0.100). We will migrate running/live container vps.rootuser.in [1001] from S1.ROOTUSER.IN to S2.ROOTUSER.IN with no downtime.

BEFORE MIGRATION:

    * You have at least one good backup of the virtual machine you intend to migrate.
    * rsync is installed on the target host.
    * In general you cannot migrate from bigger kernel versions to smaller ones.
   
1. ENABLE PASSWORDLESS LOGIN TO S2.ROOTUSER.IN FROM S1.ROOTUSER.IN.
   
    Click Here For Password Less Login With SSH

Now, Login remotely with putty to VPS vps.rootuser.in and continiously ping google.com to make sure that the VPS is live/online while migration.

 vps~]# ssh root@vps.rootuser.in
 vps~]# ping google.com

3. LOGIN TO S1.ROOTUSER.IN(source) AND START MIGRATION:

  S1~]# vzlist
     VEID      NPROC STATUS  IP_ADDR         HOSTNAME
         1001          9 running 202.52.134.143  vps.rootuser.in

  S1~]# vzmigrate --online -r no --keep-dst -v 192.168.0.100 1001

Now you can check status with below command.

  S1~]# ps -aufx | grep vzmigrate
  S1~]# w

4. After migration take a look at -

  S1~]# # vzlist
     VEID      NPROC STATUS  IP_ADDR         HOSTNAME

  S2~]## vzlist
     VEID      NPROC STATUS  IP_ADDR         HOSTNAME
      1001         34 running 202.52.134.143  vps.rootuser.in

PARAMETERS FOR vzmigrate COMMAND:

--r (yes|no)     Whether to remove container on source HN for successfully migrated container. By default 'yes' (remove) enabled.
--online     Perform online (zero-downtime) migration: during the migration the container hangs for a while and after the migration it continues working as though nothing has happened.
-v        Verbose mode. Causes vzmigrate to print debugging messages about its progress (including some time statistics).
--keep-dst     Do not clean synced destination container private area in case of some error. It makes sense to use this option on big container migration to avoid re-syncing container private area in case some error (on container stop for example) occurs during first migration attempt.

NOTE: For simple migration, just remove --online parameter from the above command.





Thats it...

#<A HREF="#top">Top</a>

Vtonf OpenVZ GUI Panel


There are couple of simple web based front-end tools for OpenVZ like Vtonf(Virtuocp) and OVZ web panel:

1. Vtonf (Now virtuocp):

    Download: http://sourceforge.net/projects/vtonf/

    1. Install Packages - vzpkg*, expect*
    2. Enable http support for OpenVZ server.
    3. Extract downloaded tool and refer INSTALL file.

2. OVZ web panel:

    Download and Install : http://code.google.com/p/ovz-web-panel/



http://code.google.com/p/ovz-web-panel/wiki/Installation
Webvz Download Link :- http://sourceforge.net/projects/webvz/files/
Webvz Guide Link :- http://webvz.sourceforge.net/download.html
Software :- http://www.howtogeek.com/howto/8054/how-to-turn-a-physical-computer-into-a-virtual-machine-with-disk2vhd/


VPS Backup On OpenVZ Script


Here, We are going to use a 'vzbackup.sh' bash script to schedule backups. You may customize this script according to you needs. You may schedule backup on remote server or on the same node. For this you need to edit 'vzbackup.sh' script. Steps to schedule backup is are as follows,

1. Make passwordless login enabled to backup server from openvz server.

2. vzdump, scp, rsync and crontab must be running/installed on OpenVZ server.

3. Copy, Edit and set execute permissions to backup script:

    ]# cp -rvp /media/pendrive/vzbackup.sh /vz/backup/
    ]# vi /vz/backup/vzbackup.sh
        [EDIT]
    ]# chmod +x /vz/backup/vzbackup.sh

4. Schedule regular backup through crontab command:

    ]# crontab -e
       41 21 * * * /vz/backup/vzbackup.sh >/dev/null 2>&1

    ]# crontab -l  (Crontab File : /var/spool/cron/root)

    ]# service crond restart


===========================

Script :


#!/bin/sh

#############################################################
# File:         vzbackup.sh
# Description:  A shell script to backup Multiple VPS using vzdump
# Cron Example: 0 2 * * * sh /path/to/vzbackup.sh
#############################################################

####DEFINE THESE#############################################
VPSLIST="4001 4002 3003 3002 3004 4006";
#VPSLIST="3002 3003 3004 3006 4001 4002 4003 4005 4006 4007 4008";
EMAILNOTIFICATION=1;
EMAILADDRESS="audit@hostindia.net";
SCPTOREMOTE=0;
REMOTESERVER="tlv4.hostinservices.com";
REMOTEDIR="/vz/backup/tlv2";
VZDUMP="/usr/sbin/vzdump";
#VZDUMPBASEDIR="/vz/backup/tlv2";
VZDUMPBASEDIR="/backups/tlv1";
VZFLAGS="--suspend --compress";
####END CHANGE HERE###########################################

DATE=`/bin/date "+%Y-%b-%d"`;
VZDUMPDIR="$VZDUMPBASEDIR/$DATE";

if [ ! -d $VZDUMPDIR ];
then
    echo "Creating Directory: ".$VZDUMPDIR;
    mkdir $VZDUMPDIR;
fi

if [ $EMAILNOTIFICATION -eq "0" ];
then
    VZMAILTO="";
else
    VZMAILTO="--mailto $EMAILADDRESS";
fi
   
for VPS in $VPSLIST
do
    echo "VPS $VPS Backup Started";
    $VZDUMP $VZFLAGS --dumpdir $VZDUMPDIR $VZMAILTO $VPS
    echo "VPS $VPS Backup Completed";
done

if [ $SCPTOREMOTE -eq "1" ]
then
    echo "Backing up data to remote server";
    scp -r $VZDUMPDIR $REMOTESERVER:$REMOTEDIR
fi
echo "Done";
exit;

OpenVZ Backup & Restore Of VPS


BACKUP:
-----------   

Command for backup is 'vzdump'. Which having below flags.

]# vzdump <options> <CID>

--exclude <CID>     Assumes --all
--all            Backup all VPSs.
--compress         Compress backup with tar.gz/tgz format.
--dumpdir <DIR>     Stores dump in specified DIR. Default directory is /vz/dump.
--tmpdir <DIR>      Store temporary files in DIR. --suspend and --stop are using this directory to store a copy of the VM.
--stop              Stop/start VM if running.
--suspend           Suspend/resume VM when running. This is used for minimal downtime.
--mailto <EMAIL ID> Send notification mail to EMAIL. You can use this option more than once to specify multiple receivers.

Examples:
   
]# vzdump --suspend --compress --dumpdir /backups/tlv1/2013-Jan-10 --mailto audit@example.net 4002 [at office]
]# vzdump --compress --stop 101
]# vzdump --compress --stop --all
]# vzdump --suspend --exclude 101
]# vzdump --suspend --compress --dumpdir /backups/ --tmpdir /tmp --mailto audit@rootuser.in 101

RESTORE:
-----------


]# vzdump --restore <DUMP DIR> <NEW VPS ID>   
                   
Example 1:

vzrestore /backup/2015-Apr-22/vzdump-openvz-112-2015_04_22-15_58_05.tgz 113

Assume that the VPS (101) is damaged, So I need to restore it on same HW node.

]# vzctl stop 101 [Stop Existing VPS]

]# vzdump --restore /backups/vzdump-openvz-101-2012_11_26.tgz 1001
   
Here, 'vzdump-openvz-101-2012_11_26.tgz' is backup/dump and '1001' is new un-used VPS ID. You may also use CID 101, For this you need to move/delete existing 101 container and its configuration files.

]# vzctl start 1001

Example 2:

Assume that I want to clone VPS 101.

]# vzdump --restore /backups/vzdump-openvz-101-2012_11_26.tgz 1001
   
Here, 'vzdump-openvz-101-2012_11_26.tgz' is backup/dump and '1001' is new un-used VPS ID.

]# vzctl set 1001 --ipdel 10.20.30.10 --save

]# vzctl set 1001 --ipadd 10.20.30.15 --save

]# vzctl set 1001 --hostname new.rootuser.in --save

]# vzctl start 1001

Thats it...

OpenVZ VPS Create Process


vzctl create 199 --ostemplate centos-6-x86
vzctl set 199 --onboot yes --save
vzctl set 199 --ipadd 202.52.134.99 --save
vzctl set 199 --hostname icshost.example.in --save
vzctl set 199 --nameserver 202.52.134.254 --nameserver 8.8.8.8 --save
vzctl set 199 --vmguarpages 1024M --save (Dedicated)
vzctl set 199 --oomguarpages 1024M --save
vzctl set 199 --privvmpages 1024M:2048M --save (Burstable)
vzctl set 199 --diskspace 30G:35G --save
vzctl set 199 --diskinodes 471859:524288 --save
vzctl start 199

INSTALL OPENVZ ON CENTOS 6.4



PARTITIONING:

Assume, I am using 1TB HDD. If you have very less disk space, Then set /boot 300M; / 10G ; swap 4G and rest of the space for /vz.

/boot    500M [Set It Primary Partion.]
/    100G
swap    16G
/vz    Remaning all
xxxx    20G

INSTALLING:

    ]# yum update -y
    ]# yum install wget vim
    ]# cd /etc/yum.repos.d/
    ]# wget http://download.openvz.org/openvz.repo
    ]# rpm --import  http://download.openvz.org/RPM-GPG-Key-OpenVZ
    ]# cd
    ]# mkdir download
    ]# cd download
    ]# wget http://download.openvz.org/kernel/branches/rhel6-2.6.32/042stab072.10/vzkernel-2.6.32-042stab072.10.x86_64.rpm
    ]# rpm -ivh vzkernel-2.6.32-042stab072.10.x86_64.rpm

[Alternet for above 2 steps : yum search vzkernel; yum install vzkernel ]

    ]# cp -rvp /etc/sysctl.conf /etc/sysctl.conf-bak
    ]# vim /etc/sysctl.conf
[Delete all lines and copy/paste contents of sysctl.conf from TLV10 etc.]
:wq

[Alternet for above step.]
---------------------------------------------------------------------------------------------------
]# vim /etc/sysctl.conf [Insure parameters are set as below.]

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


or



# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and
# sysctl.conf(5) for more details.

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

# Controls source route verification
net.ipv4.conf.default.rp_filter = 1

# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.default.proxy_arp = 0
net.ipv4.conf.all.rp_filter = 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
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 1

# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1

# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1

# Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 0

# Controls the default maxmimum size of a mesage queue
kernel.msgmnb = 65536

# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536

# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736

# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296


:wq


# sysctl -p

---------------------------------------------------------------------------------------------------

    NOTE: The following step is important if the IP addresses of your virtual machines are from a different subnet than the host system's IP address. If you don't do this, networking will not work in the virtual machines!

]# vim /etc/sysconfig/selinux
       SELINUX=disabled   
    :wq

]# reboot

Now, make sure openvz kernel is booting by default in "/boot/grub/grub.conf" file.

    ]# uname -a
    ]# yum install vzctl vzquota
    ]# service vz start
    ]# chkconfig vz on
    ]# mkdir downloads


]# vim /etc/vz/vz.conf
       [...]
       NEIGHBOUR_DEVS=all
       [...]
    :wq
       


    ]# cd downloads/
    ]# ls
    ]# rpm -ivh cstream-2.7.4-3.el6.rf.x86_64.rpm
    ]# yum install perl
    ]# rpm -ivh perl-LockFile-Simple-0.207-1.el6.rf.noarch.rpm
    ]# ls
    ]# rpm -ivh vzdump-1.2-5.noarch.rpm
    ]# cd

Download Link:

For cstream:
wget http://pkgs.repoforge.org/cstream/cstream-2.7.4-3.el6.rf.x86_64.rpm
    
For perl-LockFile:
wget http://pkgs.repoforge.org/perl-LockFile-Simple/perl-LockFile-Simple-0.207-1.el6.rf.noarch.rpm

For vzdump:
wget http://download.openvz.org/contrib/utils/vzdump/vzdump-1.2-4.noarch.rpm


cd /vz/template/cache
wget http://download.openvz.org/template/precreated/centos-6-x86_64.tar.gz