灌溉梦想,记录脚步

通过PXE网络安装OpenBSD 4.2

1. 准备PXE服务

我的PXE服务安装在Ubuntu 7.10 Desktop环境,配置过程如下:

1.1 配置tftp

确保安装了xinetd,因为tftp是借助xinetd监听端口的,/etc/xinetd.d/tftp的内容如下:

service tftp
{
        socket_type = dgram
        protocol = udp
        wait = yes
        user = root
        server = /usr/sbin/in.tftpd
        server_args = -s /tftpboot
        disable = no
}

参数 “-s /tftpboot” 指定tftp的根目录为/tftpboot,这个目录的文件结构如下:

yingyuan@yyresearch:~$ ls -Rl /tftpboot/
/tftpboot/:
总用量 5024
-rw-r--r-- 1 root root 5068775 2007-11-27 18:25 bsd.rd
drwxr-xr-x 2 root root    4096 2007-11-28 10:59 etc
-rw-r--r-- 1 root root   52928 2007-11-27 18:25 pxeboot

/tftpboot/etc:
总用量 4
-rw-r--r-- 1 root root 12 2007-11-28 10:59 boot.conf

bsd.rd是OpenBSD的ram disk文件系统,pxeboot是PXE请求时返回的启动加载文件,这两个文件都可以从安装光盘或者从ftp镜像网站提取。boot.conf是由pxeboot读取的启动配置文件,内容只有一行:

boot bsd.rd

启动tftp服务,

yingyuan@yyresearch:~$ sudo /etc/init.d/xinetd restart

1.2 配置DHCP服务

/etc/dhcp3/dhcpd.conf的配置内容如下,

ddns-update-style none;
option domain-name "example.com";
option domain-name-servers 10.0.0.1;
default-lease-time 600;
max-lease-time 7200;

class "openbsd-pxeboot-class" {
        match pick-first-value (option dhcp-client-identifier, hardware);
        filename "pxeboot";
        next-server 10.0.0.1;
}

subclass "openbsd-pxeboot-class" 1:8:0:27:3B:56:0;

subnet 10.0.0.0 netmask 255.255.255.0 {
        pool {
                allow members of "openbsd-pxeboot-class";
                option routers 10.0.0.1;
                range 10.0.0.200 10.0.0.240;
        }
}

需要安装OpenBSD机器的网卡MAC地址是08:00:27:3B:56:00,协议是以太网(协议号是1),我们把它分到openbsd-pxeboot-class这个类里。

启动dhcp服务,

yingyuan@yyresearch:~$ sudo /etc/init.d/dhcp3-server restart

2. 准备HTTP网络安装文件

我只从OpenBSD的镜像FTP站点拷贝了一些必要的文件,放在Apache的OpenBSD/i386目录下,

yingyuan@yyresearch:~$ ls -l /var/www/OpenBSD/i386
总用量 145976
-rw-r--r-- 1 yingyuan yingyuan 42603450 2007-11-27 17:21 base42.tgz
-rw-r--r-- 1 yingyuan yingyuan  6229740 2007-11-27 17:16 bsd
-rw-r--r-- 1 yingyuan yingyuan  5068775 2007-11-27 17:13 bsd.rd
-rw-r--r-- 1 yingyuan yingyuan  5185536 2007-11-27 17:08 cd42.iso
-rw-r--r-- 1 yingyuan yingyuan 78804092 2007-11-27 17:31 comp42.tgz
-rw-r--r-- 1 yingyuan yingyuan  1240720 2007-11-27 17:34 etc42.tgz
-rw-r--r-- 1 yingyuan yingyuan      266 2007-11-28 12:27 index.txt
-rw-r--r-- 1 yingyuan yingyuan   100845 2007-11-27 17:00 INSTALL.i386
-rw-r--r-- 1 yingyuan yingyuan    22354 2007-11-27 17:02 INSTALL.linux
-rw-r--r-- 1 yingyuan yingyuan  7656850 2007-11-27 17:35 man42.tgz
-rw-r--r-- 1 yingyuan yingyuan     1287 2007-11-27 17:14 MD5
-rw-r--r-- 1 yingyuan yingyuan  2292887 2007-11-27 17:38 misc42.tgz
-rw-r--r-- 1 yingyuan yingyuan    52928 2007-11-27 17:14 pxeboot

启动www服务,

yingyuan@yyresearch:~$ sudo /etc/init.d/apache2 restart

3. 安装OpenBSD 4.2

把机器设置为从网络启动,一按电源按照提示操作就OK了。

分区的时候我选择把整块硬盘分为一个OpenBSD分区,标签分配如下,

标签          容量                                  挂载点
----------------------------
a      512M            /
b      512M            swap
c      整个硬盘
d      1G              /var
e      剩下的空间                    /usr

指定大小的时候以磁盘扇区(512 bytes)为单位,所以需要计算一下。每个标签之间默认间隔一个磁道。

Memcache的备忘

  • 用memcache保存session的例子,非常简单
     
    CODE:

    1. <?php
    2. $session_save_path = "tcp://$host:$port?persistent=1&weight=2&timeout=2&retry_interval=10, ,tcp://$host:$port ";
    3. ini_set(‘session.save_handler’, ‘memcache’);
    4. ini_set(‘session.save_path’, $session_save_path);
    5. ?>

     

  • memcache每一个item上限是1M,注意不要超出上限
  • memcache本身并不支持namespace,但是可以通过一些手段模拟出namespace的效果来,见Memcache 中模拟 namespace
  • 刚接触memcache的时候,可能会写出这样的代码来
    CODE:

    1. $zhang = $memcache->get(‘key1’);
    2. $li = $memcache->get(‘key2’);
    3. $wang = $memcache->get(‘key3’);

    这种写法实际运行效果是

    • get(key1) – 客户端发出请求 – 服务器端查询 – 客户端获取
    • get(key2) – 客户端 – 服务器端 – 客户端
    • get(key3) – 客户端 – 服务器端 – 客户端

    如此一来,会有三次客户端和服务器端交互的过程。但是如果用批量查询的方法,就只有一次交互的过程。比如:

     
    CODE:

    1. $all = $memcache->get(array(‘key1’, ‘key2’, ‘key3’));

    这样性能会有一些提升。对于其它程序语言来说,也封装了类似get_multi这样的方法。

  • 从数据库从查询获得一个列表,放到memcache里面保存起来是一个不错的主意,但是不要忘记memcache有1m限制。如果列表太大,可以考虑把数据分割开来,然后用key序列来保存这个列表数据,比如event_0_500来保存前500行,用event_0_1000保存500-1000行,在获取的时候可以用前面说的批量get来一次性得到这个列表。
  • 经常观察memcached的大小和命中率,方便调整缓存策略

最简便的清空memcache的方法

This is easy right? Can’t you just restart the memcached server? Well yes, but you may cause errors in applications that are already connected to it. You can follow your memcached restart with an application restart, eg for a Ruby on Rails app:

# /etc/init.d/memcached restart && mongrel_rails cluster::restart

Of course if you have more than one application server you have to restart your app on every single one. This would work on an engineyard slice assuming you have the eycap gem installed:

$ cap production memcached:restart
$ cap production mongrel:restart

Restarting your application is not ideal however, you will lose anything cached in memory, cause delays to users trying to access your site, that sort of thing.

So what can be done? The answer is really simple. Assuming a memcached running on the local machine on the default port:

$ echo “flush_all” | nc localhost 11211

Easy!

启用memcached压缩注意事项

在php开发中,开启memcache的数据压缩存储是一件很简单的事情。在多数情况下,压缩数据不仅不会降低程序的执行效率,反倒会因为网络传输的开销降低,带来速度提升。看看最常用的Memcache::set方法:
bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )

在这个方法中,将$flag设置为MEMCACHE_COMPRESSED即可启用memcache压缩存储。

这样做有什么弊端?

如果没有做额外判断,每一次写入memcache都会启用压缩,不管数据的大小。对应的,每次获得数据都需要做一次解压缩的操作,这是典型的一刀切手法。实际上在数据很小的情况下,不需要压缩,在这个基础上压缩省不了多少空间。

更好的压缩策略?

好了,我的想法是在数据超过一定大小(比如2k)的情况下,才开启压缩。这个好办,捋起袖子就干,在调用Memcache::set方法之前,首先判断一下数据的大小,一个strlen就搞定了,再简单不过了。

PHP:

  1. $memcache = new Memcache;
  2. $memcache->connect(‘localhost’, 11211);
  3. $flag = strlen($data)> 2048 ? MEMCACHE_COMPRESSED : 0;
  4. $memcache->set(‘mykey’, $data, $flag);

有人可能会问了,array和object怎么办,这玩意可不能用strlen判断长度。

这还真能难住我一阵子,要知道把array/object写入memcache的时候,php会自动做serialize,再把它当作字符串插入memcache。

PHP:

  1. $flag = strlen(serialize($data))> 2048 ? MEMCACHE_COMPRESSED : 0;

谁会采用这段代码?看起来非常山寨,而且serialize也不快,赔本买卖。

更好的办法!

上面的文字都是废话,直接看这段就好。Memcache::setCompressThreshold方法可以包办之前所有的逻辑。

Memcache::setCompressThreshold – Enable automatic compression of large values

bool Memcache::setCompressThreshold ( int $threshold [, float $min_savings ] )

举个例子,下面这段会自动启用压缩策略,当数据大于2k时,以0.2的压缩比进行zlib。

PHP:

  1. $memcache->setCompressThreshold(2000, 0.2);

根据我的测试结果,setCompressThreshold方法会忽略Memcache::set的flag参数

memcache.php stats like apc.php

For a long time I was looking for a nice web interface like the apc.php (comes with the apc’s source) that displays whole nine yards of stats. The only good tool is memcache-tool from the danga guys. It’s quite complete but I guess I’m too lazy to go on the command line.

Anyways, I decided to rip write my own. Totally based on the original apc.php (I even recycled some functions) and apart from completeness, here is a memcache.php that you can get stats and dump from multiple memcache servers.
Here is a screenshot:


And here is the the source code.

As usual, this piece of software comes with the warnings:
– Don’t expect something complete, might have bugs and security problems etc.
– Do not install on a prod environment unless you’re sure!
– Feel free to add stuff to it, I’ll put it to google code or sf.net soon.
– Feel free to request features. (no I’m not planning a backup tool , there is memcache-tool for that)
– Enjoy!

如何监控MemCached的状态

使用MemCached以后,肯定希望知道cache的效果,对于MemCached的一些运行状态进行监控是必要的。MemCached也提供了stats接口输出一些信息,最简单的方式,就是telnet上去输入stats查看:

telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to memcache_test_host (127.0.0.1 ).
Escape character is '^]'.
stats
STAT pid 7186
STAT uptime 1695
STAT time 1238401344
STAT version 1.2.6
STAT pointer_size 64
STAT rusage_user 0.003999
STAT rusage_system 0.002999
STAT curr_items 1
STAT total_items 54
STAT bytes 135
STAT curr_connections 2
STAT total_connections 111
STAT connection_structures 4
STAT cmd_get 3
STAT cmd_set 54
STAT get_hits 0
STAT get_misses 3
STAT evictions 0
STAT bytes_read 5957
STAT bytes_written 50914
STAT limit_maxbytes 2147483648
STAT threads 1
END

这种方式相当的不方便,所以网上就有各种不同客户端接口写的工具,比如用perl写的这个memcache-tool

./memcached_tool
Usage: memcached-tool  [mode]

       memcached-tool 10.0.0.5:11211 display    # shows slabs
       memcached-tool 10.0.0.5:11211            # same.  (default is display)
       memcached-tool 10.0.0.5:11211 stats      # shows general stats
       memcached-tool 10.0.0.5:11211 move 7 9   # takes 1MB slab from class #7
                                                # to class #9.

You can only move slabs around once memory is totally allocated, and only
once the target class is full.  (So you can't move from #6 to #9 and #7
to #9 at the same itme, since you'd have to wait for #9 to fill from
the first reassigned page)

$ ./memcached_tool 127.0.0.1:11211 stats
  #127.0.0.1:11211 Field       Value
                   bytes         135
              bytes_read        5964
           bytes_written       51394
                 cmd_get           3
                 cmd_set          54
   connection_structures           4
        curr_connections           3
              curr_items           1
               evictions           0
                get_hits           0
              get_misses           3
          limit_maxbytes  2147483648
                     pid        7186
            pointer_size          64
           rusage_system    0.002999
             rusage_user    0.003999
                 threads           1
                    time  1238401521
       total_connections         112
             total_items          54
                  uptime        1872
                 version       1.2.6

命令行的方式,在批处理调用的时候比较方便。但是在展现方面还是web方式更加直观有效,所以就有了php写的memcache.php,是的,用一次就知道这是我想要的。
 

在PHP使用MemCached

在PHP中使用Memcached,有两种方式,一种是安装PHP的memcache扩展(实际上还有另外一个memcached扩展,是基于比较流行的libmemcached库封装的),该扩展是用c写的,效率较高,需要在服务器上安装。另外一种则是直接使用客户端的php-memcached-client类库,但是这个我在网上找了半天也没找到一个官方的网站。所以呢,还是装个扩展吧。假设php安装在/home/admin/php目录:

wget http://pecl.php.net/get/memcache-2.2.5.tgz
gzip -d memcache-2.2.5.tgz
tar xvf memcache-2.2.5.tar
cd memcache-2.2.5
/home/admin/php/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519

./configure --enable-memcache --with-php-config=/home/admin/php/bin/php-config --with-zlib-dir
Installing shared extensions:     /home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/

注意到最后一行返回的信息,将下面两行添加到/home/admin/php/lib/php.ini

extension_dir = "/home/admin/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension=memcache.so

然后重启web服务器即可。如果安装成功,则通过phpinfo()可以获得该扩展的相关信息:

memcache support enabled
Active persistent connections 0
Version 2.2.5
Revision $Revision: 1.111 $
Directive Local Value Master Value
memcache.allow_failover 1 1
memcache.chunk_size 8192 8192
memcache.default_port 11211 11211
memcache.default_timeout_ms 1000 1000
memcache.hash_function crc32 crc32
memcache.hash_strategy standard standard
memcache.max_failover_attempts 20 20

以上参数都可以在php.ini中进行设置。下面是一段官方网站的php测试代码:

<?php
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)\n";
$get_result = $memcache->get('key');
echo "Data from the cache:\n";
var_dump($get_result);
?>

运行后输出如下:

Server's version: 1.2.6
Store data in the cache (data will expire in 10 seconds)
Data from the cache: object(stdClass)#3 (2)
{ ["str_attr"]=>  string(4) "test" ["int_attr"]=>  int(123) }