kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

LVS简介及使用

http://www.cnblogs.com/codebean/archive/2011/07/25/2116043.html

一、LVS是什么?

LVS的英文全称是Linux Virtual Server,即Linux虚拟服务器。它是我们国家的章文嵩博士的一个开源项目。在linux内存2.6中,它已经成为内核的一部分,在此之前的内核版本则需要重新编译内核。

二、LVS能干什么?

LVS主要用于多服务器的负载均衡。它工作在网络层,可以实现高性能,高可用的服务器集群技术。它廉价,可把许多低性能的服务器组合在一起形成一个超级服务器。它易用,配置非常简单,且有多种负载均衡的方法。它稳定可靠,即使在集群的服务器中某台服务器无法正常工作,也不影响整体效果。另外可扩展性也非常好。

三、工作原理

如上图,LVS可分为三部分:

1.Load Balancer:这是LVS的核心部分,它好比我们网站MVC模型的Controller。它负责将客户的请求按照一定的算法分发到下一层不同的服务器进行处理,自己本身不做具体业务的处理。另外该层还可用监控下一层的状态,如果下一层的某台服务器不能正常工作了,它会自动把其剔除,恢复后又可用加上。该层由一台或者几台Director Server组成。

2.Server Array:该层负责具体业务。可有WEB Server、mail Server、FTP Server、DNS Server等组成。注意,其实上层的Director Server也可以当Real server用的。

3.Shared Storage:主要是提高上一层数据和为上一层保持数据一致。

四、负载均衡机制

前面我们说了LVS是工作在网络层。相对于其它负载均衡的解决办法,比如DNS域名轮流解析、应用层负载的调度、客户端的调度等,它的效率是非常高的。LVS的通过控制IP来实现负载均衡。IPVS是其具体的实现模块。IPVS的主要作用:安装在Director Server上面,在Director Server虚拟一个对外访问的IP(VIP)。用户访问VIP,到达Director Server,Director Server根据一定的规则选择一个Real Server,处理完成后然后返回给客户端数据。这些步骤产生了一些具体的问题,比如如何选择具体的Real Server,Real Server如果返回给客户端数据等等。IPVS为此有三种机制:

1.VS/NAT(Virtual Server via Network Address Translation),即网络地址翻转技术实现虚拟服务器。当请求来到时,Diretor server上处理的程序将数据报文中的目标地址(即虚拟IP地址)改成具体的某台Real Server,端口也改成Real Server的端口,然后把报文发给Real Server。Real Server处理完数据后,需要返回给Diretor Server,然后Diretor server将数据包中的源地址和源端口改成VIP的地址和端口,最后把数据发送出去。由此可以看出,用户的请求和返回都要经过Diretor Server,如果数据过多,Diretor Server肯定会不堪重负。

2.VS/TUN(Virtual Server via IP Tunneling),即IP隧道技术实现虚拟服务器。它跟VS/NAT基本一样,但是Real server是直接返回数据给客户端,不需要经过Diretor server,这大大降低了Diretor server的压力。

3.VS/DR(Virtual Server via Direct Routing),即用直接路由技术实现虚拟服务器。跟前面两种方式,它的报文转发方法有所不同,VS/DR通过改写请求报文的MAC地址,将请求发送到Real Server,而Real Server将响应直接返回给客户,免去了VS/TUN中的IP隧道开销。这种方式是三种负载调度机制中性能最高最好的,但是必须要求Director Server与Real Server都有一块网卡连在同一物理网段上。

五、负载调度算法

前面我们都知道Director Server要选择不同的Real server,那么它具体的如果选择Real Server以达到负载均衡的呢,IPVS实现了八种调度方法,具体算法可以查看官网或者百度,这里就不一一列出 了。官网:www.linuxvirtualserver.org。

六、具体配置操作

首先我们这里有三台机子,IP分别是192.168.132.30(Diretor server),192.168.132.64(Real server 1),192.168.132.68(real server 2)。在real server 1和2上面,已经配置好了web服务,并且我们假设还有一个对外访问的虚拟IP是192.168.132.254(VIP)。另外在Diretor server上面已经安装好了ipvsadm。

下面我们VS/DR介绍详细的配置过程。

Diretor server上面的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//首先在Director Server上绑定一个虚拟IP(也叫VIP),此IP用于对外提供服务:
ifconfig eth0:0 192.168.132.254 broadcast 192.168.132.254 netmask 255.255.255.255 up
 
//给设备eth0:0指定一条路由
route add -host 192.168.132.254 dev eth0:0
 
//启用系统的包转发功能
echo "1">/proc/sys/net/ipv4/ip_forward
 
//清楚ipvsadm以前的设置
ipvsadm -C

//添加一个新的虚拟IP记录192.168.132.254,其持续服务之间是120秒 
ipvsadm -A -t 192.168.132.254:80 -s rr -p 120
 
//在新增的虚拟IP记录中新增两天real server记录,-g即为使用VS/DR模式
ipvsadm -a -t 192.168.132.254:80 -r 192.168.132.64:80 -g 
ipvsadm -a -t 192.168.132.254:80 -r 192.168.132.68:80 -g
 
//启用LVS服务
ipvsadm

两台real server上的配置:

1
2
3
4
5
6
7
8
9
/*在回环设备上绑定了一个虚拟IP地址,并设定其子网掩码为255.255.255.255,与Director Server上的虚拟IP保持互通*/
ifconfig lo:0 192.168.132.254 broadcast 192.168.132.254 netmask 255.255.255.255 up
route add -host 192.168.132.254 dev lo:0

//禁用本机的ARP请求
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

之后在其他客户端机子上面,访问 http://192.168.132.254/ 则可以看到结果了。

ethtool命令

http://blog.sina.com.cn/s/blog_71f38909010128yf.html

用途

显示或修改以太网卡的配置信息。

语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ethtool [ -a | -c | -g | -i | -d | -k | -r | -S |] ethX

ethtool [-A] ethX [autoneg on|off] [rx on|off] [tx on|off]

ethtool [-C] ethX [adaptive-rx on|off] [adaptive-tx on|off] [rx-usecs N] [rx-frames N] [rx-usecs-irq N] [rx-frames-irq N] [tx-usecs N] [tx-frames N] [tx-usecs-irq N] [tx-frames-irq N] [stats-block-usecs N][pkt-rate-low N][rx-usecs-low N] [rx-frames-low N] [tx-usecs-low N] [tx-frames-low N] [pkt-rate-high N] [rx-usecs-high N] [rx-frames-high N] [tx-usecs-high N] [tx-frames-high N] [sample-interval N]

ethtool [-G] ethX [rx N] [rx-mini N] [rx-jumbo N] [tx N]

ethtool [-e] ethX [raw on|off] [offset N] [length N]

ethtool [-E] ethX [magic N] [offset N] [value N]

ethtool [-K] ethX [rx on|off] [tx on|off] [sg on|off] [tso on|off]

ethtool [-p] ethX [N]

ethtool [-t] ethX [offline|online]

ethtool [-s] ethX [speed 10|100|1000] [duplex half|full] [autoneg on|off] [port tp|aui|bnc|mii] [phyad N] [xcvr internal|external]

[wol p|u|m|b|a|g|s|d...] [sopass xx:yy:zz:aa:bb:cc] [msglvl N]

描述

Ethtool命令用于获取以太网卡的配置信息,或者修改这些配置。

ethX是以太网卡的名称,Linux系统将检测到的第一块以太网卡命名为eth0, 第二块为eth1,…….。

标志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-a   查看网卡中 接收模块RX、发送模块TX和Autonegotiate模块的状态:启动on 或 停用off
-A    修改网卡中 接收模块RX、发送模块TX和Autonegotiate模块的状态:启动on 或 停用off
-c    display the Coalesce information of the specified ethernet card
-C    Change the Coalesce setting of the specified ethernet card
-g    Display the rx/tx ring parameter information of the specified ethernet card
-G    change the rx/tx ring setting of the specified ethernet card
-i    显示网卡驱动的信息,如驱动的名称、版本等
-d    显示register dump信息, 部分网卡驱动不支持该选项
-e    显示EEPROM dump信息,部分网卡驱动不支持该选项
-E    修改网卡EEPROM byte
-k    显示网卡Offload参数的状态:on 或 off,包括rx-checksumming、tx-checksumming等。
-K    修改网卡Offload参数的状态
-p    用于区别不同ethX对应网卡的物理位置,常用的方法是使网卡port上的led不断的闪;N指示了网卡闪的持续时间,以秒为单位。
-r    如果auto-negotiation模块的状态为on,则restarts auto-negotiation
-S    显示NIC- and driver-specific 的统计参数,如网卡接收/发送的字节数、接收/发送的广播包个数等。
-t    让网卡执行自我检测,有两种模式:offline or online
-s    修改网卡的部分配置,包括网卡速度、单工/全双工模式、mac地址等

示例

查看机器上网卡的速度:百兆还是千兆,请输入:ethool eth0 操作完毕后,输出信息中‘Speed:’ 这一项就指示了网卡的速度。

停止网卡的发送模块TX,请输入:ethtool -A tx off eth0 操作完毕后,可输入:ethtool -a eth0,查看tx模块是否已被停止。

查看网卡eth0采用了何种驱动,请输入:ethtool -i eth0 操作完毕后,显示 driver: bnx2;version: 1.4.30 等信息。

关闭网卡对收到的数据包的校验功能,请输入:ethtool -K eth0 rx off 操作完毕后,可输入:ethtool –k eth0,查看校验功能是否已被停止。

如果机器上安装了两块网卡,那么eth0对应着哪块网卡呢?输入:ethtool -p eth0 10 操作完毕后,看哪块网卡的led灯在闪,eth0就对应着哪块网卡。

查看网卡,在接收/发送数据时,有没有出错?请输入:ethtool -S eth0

将千兆网卡的速度降为百兆,请输入:ethtool -s eth0 speed 100

数据来源

ethtool命令显示的信息来源于网卡驱动层,即TCP/IP协议的链路层。

该命令在Linux内核中实现的逻辑层次为:

最重要的结构体struct ethtool_ops,该结构体成员为用于显示或修改以太网卡配置的一系列函数指针,见下表中的第二列。

网卡驱动负责实现(部分)这些函数,并将其封装入ethtool_ops结构体,为网络核心层提供统一的调用接口。因此,不同的网卡驱动会给应用层返回不同的信息。

ethtool命令选项、struct ethtool_ops成员函数、ethtool命令显示参数的来源,三者间的对应关系如下表所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
命令选项     struct ethtool_ops成员函数      ethtool命令显示参数的来源(以网卡驱动BNX2为例)

-s        get_settingsget_wol             从网卡寄存器中获得网卡速度等信息,可配置
		get_msglevel
		get_link
		set_settings
		set_wol
		set_msglevel

-a -A     get_pauseparam set_pauseparam   从网卡寄存器中获得 Autonegotiate/RX/TX模块的状态:on or off,可配置

-c -C     get_coalesceset_coalesce        从网卡寄存器中获得coalescing参数:TX/RX一个数据包后,推迟发生TX/RX中断的时间(us)/数据包个数。—减小该值可以提高网卡的响应时间。
									  当rx-usecs&rx-frames同时被设为0时,RX中断停止。
									  当tx-usecs&tx-frames同时被设为0时,TX中断停止。
-g -G     get_ringparam set_ringparam     除当前TX/RX ring的值(从网卡寄存器中读取得到,可配置)外,其它为网卡bnx2自己固定的信息。

-k -K get_rx_csumget_tx_csum          显示信息从保存该状态的变量中读取得到,没有对应的寄存器。因此,TX/RX校验等模块一直处于on状态,实际上是无法修改的。
		get_sg
		get_tso
		set_rx_csum
		set_tx_csum
		set_sg
		set_tso

-i        get_drvinfo[self_test_count,    网卡bnx2自己固定的信息,如
		get_stats_coun,t              ——————————————————–
		get_regs_len,                 driver: bnx2
		get_eeprom_len]                   version: 1.4.30
									  firmware-version: 1.8.0.5
									  bus-info: 0000:09:00.0
									  ——————————————————–
-d        get_drvinfoget_regs             不支持,即bnx2中没有实现函数get_regs
-e -E     get_eepromset_eeprom            不支持,即bnx2中没有实现函数get_eeprom
-r        nway_reset                      配置网卡MII_BMCR寄存器,重启Auto negotiation模块
-p        phys_id                         配置网卡BNX2_EMAC_LED寄存器,实现LED闪功能
-t        self_test                       通过配置网卡寄存器,逐一测试网卡的硬件模块:registers,memory,loopback,Link stat,interrupt
-S        get_ethtool_stats               显示信息来源于网卡驱动中的结构体变量stats_blk。(网卡通过DMA方式,将寄存器BNX2_HC_STATISTICS _ADDR_L和
									  BNX2_HC_STATISTICS_ADDR_H中的数据实时地读取到结构体变量struct statistics_block *stats_blk中。)
									  —显示的数据都是从网卡寄存器中统计得到的,各项的含义需查询网卡(芯片)手册。

由上可见,ethtool命令用于显示/配置网卡硬件(寄存器)。

ubuntu安装kvm虚拟机

1
sudo apt-get install qemu-kvm libvirt-bin virt-manager

用 virt-manager

参考 http://nmszh.blog.51cto.com/4609205/1539502


http://www.sysstem.at/category/linux/

问题一:

1
2
3
ERROR internal error: Process exited while reading console log output: char device redirected to /dev/pts/45 (label charserial0)
ioctl(KVM_CREATE_VM) failed: 16 Device or resource busy
failed to initialize KVM: Device or resource busy

This is mostly because you have either VirtualBox or VMware running on the same machine. The reason (at least that’s what I think) is that the kernel module of VirtualBox or VMware and KVM can’t take Advantage of Intel VT-x or AMD-V at the same time.

关闭virtualbox等其他虚拟机就好


http://ask.xmodulo.com/hda-duplex-not-supported-in-this-qemu-binary.html

问题二:

1
Unable to complete install: 'unsupported configuration: hda-duplex not supported in this QEMU library
Solution One: Virt-Manager

On virt-manager, open the VM’s virtual hardware details menu, go to sound device section, and change the device model from default to ac97.

Click on “Apply” button to save the change. See if you can start the VM now.

也就是 最后一步 “勾选安装之前配置“,完成,然后将声卡改成ac97即可

Solution Two: Virsh

If you are using virsh, not virt-manager, you can edit the VM’s XML file accordingly. Look for sound section inside section, and change the sound model to ac97 as follows.

1
2
3
4
5
6
7
  <devices>
	. . .
	<sound model='ac97'>
	  <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
	</sound>
	. . .
  </device>

http://wiki.ubuntu.org.cn/Kvm%E6%95%99%E7%A8%8B