linux运维:虚拟机封装、CDN
一、虚拟机的封装
1、搭建共享yum源
2、新建虚拟机(rhel6.5)
mem = 512 (默认选择无图形化安装)
GiB=20.0G(虚拟硬盘disk,内存大一点)
name=base
lanuage = English (选择语言)
Time Zone = Asia/Shanghai (时区)
ipv6 (不选)
设置超户密码(westos)
3、设置base虚拟机
vi /etc/yum.repo.d/rhel ##配置好6.5的yum源
yum install vim -y
vim /etc/sysconfig/network-scripts/ifcfg-eth0
/etc/init.d/network restart ##重启网络(/etc/init.d)
vim /etc/sysconfig/network ##永久修改主机名
vim /etc/hosts ##设置本地域名解析
chkconfig iptables off ##关闭火墙开机自启
SELINUX=disabled ##设置selinux
4、物理主机
- virt-sysprep -d base ##清理base虚拟机
如果virt-sysprep命令不存在,则: - yum install libguestfs-tools-c.x86_64 -y
5、关闭封装的base虚拟机
注意:删除base时,不能删除存储的路径/var/lib/libvirt/images/base.qcow2
6、建立子镜像
-
qemu-img create -f qcow2 -b base.qcow2 vm1
qemu-img create -f qcow2 -b base.qcow2 vm2
qemu-img create -f qcow2 -b base.qcow2 vm3 -
安装选择vm子虚拟机信息
选择第四个硬盘导入镜像
path:/var/lib/libvirt/images/vm1
选择linux系统
选择6.5版本的镜像
二、CDN
1、CDN( 多个web解析实现varnish缓存)
server1
-
安装
yum install varnish-3.0.3-1.el6.x86_64.rpm varnish-libs-3.0.3-1.el6.x86_64.rpm -y -
配置后端服务器 (/etc/varnish/default.vcl)
修改端口:80
##定义多个不同域名站点的后端服务器
backend web1 { ##web1指向172.25.254.2
.host = "172.25.254.2";
.port = "80";
}
backend web2 { ##web2指向172.25.254.3
.host = "172.25.254.3";
.port = "80";
}
## 当访问 www.westos.org 域名时从 web1 上取数据,访问 bbs.westos.org 域名时到 web2 取数据,
访问其他页面报错。
sub vcl_recv { ##实质上,域名不同,varnish缓存地址不同
if (req.http.host ~ "^(www.)?westos.org") { ## www.westos.org 和 westos.org 一样,节省空间
set req.http.host = "www.westos.org";
set req.backend = web1;
}
elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";}
}
sub vcl_deliver{ ##第一次访问时MISS,后续访问HIT,varnish默认清楚时间120s
if(obj.hits>0){
set resp.http.X-Cache = "HIT from westos cache";
}
else{
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
- /etc/init.d/varnish reload ## 重启varnish
server2
- vim /var/www/html/index.html ## 写入 www.westos.org.server2
- /etc/init.d/httpd restart
server3
- vim /var/www/html/index.html ## 写入 bbs.westos.org.server3
- /etc/init.d/httpd restart
物理主机
- vim /etc/hosts ## 172.25.254.1 westos.org www.westos.org bbs.westos.org
测试
www.westos.org ----- www.westos.org.service2
westos.org ----- www.westos.org.service2
bbs.westos.org ----- bbs.westos.org.service3
2、varnish实现负载均衡轮询
server1
- vim /etc/varnish/defaults.vcl
backend web1 {
.host = "172.25.254.2";
.port = "80";
}
backend web2 {
.host = "172.25.254.3";
.port = "80";
}
##负载均衡
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
return (pass); ## 为了测试方便,不进行缓存。
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}
- /etc/init.d/varnish reload ##重启varnish
server2
/var/www/html/index.html ##www.westos.org-server2
server3
- vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80 ##打开虚拟主机功能
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName bbs.westos.org
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www
ServerName www.westos.org
</VirtualHost>
- vim /etc/hosts
172.25.254.3 server3 www.westos.org bbs.westos.org - curl www.westos.org
www.westos.org.server3 - curl bbs.westos.org
bbs.westos.org
3、推送管理
server1
- 更改网站信息,使varnish的cache实现同步更新
- 安装httpd、php、unzip
- yum install unzip -y
- 在/var/www/html 下 解压 unzip bansys.zip
- 改配置文件 httpd的端口:8080
- vim /var/www/html/bansys/config.php (修改文件内容)
- vim /etc/varnish/default.vcl
<?php
$var_group1 = array(
'host' => array('172.25.254.1'),
'port' => '80',
);
//varnish 群组定义
//对主机列表进行绑定
$VAR_CLUSTER = array(
'www.westos.org' => $var_group1,
);
//varnish 版本//2.x 和 3.x 推送命令不一样
$VAR_VERSION = "3";
?>
acl westos { ##westos域
"127.0.0.1"; ##内部回环接口
"172.25.254.0"/24;
}
backend web1 {
.host = "172.25.254.2";
.port = "80";
}
backend web2 {
.host = "172.25.254.3";
.port = "80";
}
##实现负载均衡
director lb round-robin {
{ .backend = web1; }
{ .backend = web2; }
}
sub vcl_recv { ##接受数据包的函数
if (req.request == "BAN") {
if (!client.ip ~ westos) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "ban added";
}
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {error 404 "westos cache";
}
}
sub vcl_deliver { ##lookup cache
if (obj.hits > 0) { ##hash中找到
set resp.http.X-Cache = "HIT from westos cache";
}
else { ##hash中找不到
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
}