最近入了一台独立服务器,如果直接拿来跑项目的话就太浪费资源了。于是打算使用Proxmox VE这款虚拟化管理软件进行VPS管理。

Proxmox VE是一款套开源的虚拟化管理软件,用户可通过网页的方式来管理服务器上使用 kvm 以及 lxc 技术运行的虚拟机。同时提供了一些先进功能的支持,如集群、HA等。

0x00 安装

Proxmox VE是基于Debian进行开发的,主要有两种安装方式。
其一是通过官方提供的iso作为一个全新的系统安装
另一种方式是在已有的Debian系统上安装
手动安装时请务必保证网卡配置正确,若出错的话在不带IPMI的机子上很难处理。

安装完成后即可通过https://ip:8006/访问管理页面

另外,这里记录一下版本升级的方法。由于Proxmox VE是一家商业公司在运营,所以一些功能是需要购买订阅才能使用的,例如说版本更新功能。但是可以通过一些方法绕过限制。注意这些更新方法请勿用于生产环境中。

将软件源更改为测试源

修改/etc/apt/sources.list.d/pve-install-repo.list, 将 pve-no-subscription 修改为pvetest
然后apt三连即可更新为新版本。

apt-get update
apt-get upgrade
apt-get dist-upgrade

0x01 相关设定

对于kvm虚拟化的虚拟机,若想上传需要用到的iso文件,可以直接通过网页端上传,也可以直接将文件放入/var/lib/vz/template/iso/
如果想对kvm虚拟机的启动参数进行调整,官方提供了api:qm set,具体可参照官方文档
对于lxc虚拟化的虚拟机,可以直接从系统中下载对应发行版的模板,无需自行下载。
可以直接使用LXC自带的api对lxc虚拟机进行管理,注意-n为虚拟机的id。

0x02 网络配置

对于多ip的服务器,本身官方就是按照桥接的方式做好网络配置的,直接在虚拟机中填写分配的ip即可。
对于单ip服务器,可以采用NAT的方法让虚拟机连上外部网络。这里介绍俩种方式。

采用QEMU自带的NAT

对于KVM虚拟机,可以直接在创建虚拟机的时候勾上NAT,这时候就会自动为虚拟机分配一个虚拟的子网并且虚拟机可以通过nat连接到外部网络,基本上是开箱即用。同时也支持端口映射,具体可参考官方wiki下的QEMU port redirection。但之前在使用的过程中,发现这个端口映射并不是很稳定。同时虽然这种方法很简单,但是虚拟机之间是隔离的,无法互通数据,这样就非常不灵活。
同时,LXC虚拟机是没有这种开箱即用的NAT的。

配置iptables创建子网以实现nat

主要思路是创建一个虚拟桥接设备并创建一个子网,然后将所有虚拟机包括宿主机都连接到这个子网内,再开启iptables的NAT功能。
编辑配置文件/etc/interfaces,以下是参考配置

auto vmbr2
iface vmbr2 inet static
    address 10.0.0.254
    netmask 255.255.255.0
    bridge_ports none
    bridge_stp off
    bridge_fd 0
    post-up echo 1 > /proc/sys/net/ipv4/ip_forward
    post-up iptables -t nat -A POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE
    post-down iptables -t nat -D POSTROUTING -s '10.0.0.0/24' -o vmbr0 -j MASQUERADE

以上配置创建了vmbr2并且分配了一个子网10.0.0.0/24,同时宿主机(同时亦为网关)在这个子网内的ip为10.0.0.254。然后开启了内核的转发功能与iptables的NAT功能(其中vmbr0为通向外部网络的设备)。
若想添加端口转发直接在iptables中增加相关条目即可。
例如想要将宿主机vmbr0的80端口的tcp连接转发到10.0.0.102的80端口上:
iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80
如果想保存转发规则,使之重启后依然有效,则需要在/etc/interfaces相应位置加入

post-up iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80
post-down iptables -t nat -D PREROUTING -i vmbr0 -p tcp --dport 80 -j DNAT --to 10.0.0.102:80

通过以上方法就能组建一个灵活的子网了,kvm虚拟机和lxc虚拟机都可接入,并且都可以有端口转发。由于没有DHCP服务器所以要自行分配ip。注意创建虚拟机的时候将其挂载到vmbr2端口下。
我的服务器只有一个ip,所以内部组网就只能采取这种这种的方法了hhhh。为了充分利用资源,我将80,443端口转发到内部一台虚拟机上,这台虚拟机再使用nginx反代到内网的其它虚拟机,以充分利用单个ip。

启用BBR优化网络

目前的Proxmox VE版本的linux内核版本比较新,已经包含了bbr模块了。

修改sysctl.conf

echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

保存生效

sysctl -p

检测是否已启用bbr模块

lsmod | grep bbr

如果含有bbr即说明内核内已启用bbr模块

转:http://www.senra.me/add-internal-adapter-for-proxmox-to-secure-intranet-communication/

一.锲子

最近在配置hadoop过程中为了让各节点使用内网通信,研究了下怎么在proxmox下添加内网地址,一开始觉得挺容易,然后发现有点小坑,记录下。

二.Here We Go

首先需要在宿主机上添加个内网网卡,可以使用Proxmox添加,也可以直接改配置文件,效果一样,面板如下,vlan aware随意

编辑文件的话需要修改/etc/network/interfaces ,添加如下,同样vlan aware 随意

之后我们便为宿主机添加了一个内网网卡,地址为10.10.10.254

之后我们在创建的容器或是虚拟机上同样添加一个网卡,mac地址会自动生成,网关就写宿主机的,桥接写我们添加的那个网卡,然后就可以了

PS.需要注意的有一点,先加内网地址后加外网地址,否则默认路由会是内网,这个是个坑,我一开始没在意然后发现怎么也访问不了

Hbase hbck

[plain] view plain copy

static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="18" height="18" align="middle" name="ZeroClipboardMovie_1"/>

在CODE上查看代码片派生到我的代码片

  1. 新版本的 hbck 可以修复各种错误,修复选项是:
  2. (1)-fix,向下兼容用,被-fixAssignments替代
  3. (2)-fixAssignments,用于修复region assignments错误
  4. (3)-fixMeta,用于修复meta表的问题,前提是HDFS上面的region info信息有并且正确。
  5. (4)-fixHdfsHoles,修复region holes(空洞,某个区间没有region)问题
  6. (5)-fixHdfsOrphans,修复Orphan region(hdfs上面没有.regioninfo的region)
  7. (6)-fixHdfsOverlaps,修复region overlaps(区间重叠)问题
  8. (7)-fixVersionFile,修复缺失hbase.version文件的问题
  9. (8)-maxMerge <n> (n默认是5),当region有重叠是,需要合并region,一次合并的region数最大不超过这个值。
  10. (9)-sidelineBigOverlaps ,当修复region overlaps问题时,允许跟其他region重叠次数最多的一些region不参与(修复后,可以把没有参与的数据通过bulk load加载到相应的region)
  11. (10)-maxOverlapsToSideline <n> (n默认是2),当修复region overlaps问题时,一组里最多允许多少个region不参与
  12. 由于选项较多,所以有两个简写的选项
  13. (11) -repair,相当于-fixAssignments -fixMeta -fixHdfsHoles -fixHdfsOrphans -fixHdfsOverlaps -fixVersionFile -sidelineBigOverlaps
  14. (12)-repairHoles,相当于-fixAssignments -fixMeta -fixHdfsHoles -fixHdfsOrphans
  15. 新版本的 hbck
  16. (1)缺失hbase.version文件
  17.  加上选项 -fixVersionFile 解决
  18. (2)如果一个region即不在META表中,又不在hdfs上面,但是在regionserver的online region集合中
  19.  加上选项 -fixAssignments 解决
  20. (3)如果一个region在META表中,并且在regionserver的online region集合中,但是在hdfs上面没有
  21.  加上选项 -fixAssignments -fixMeta 解决,( -fixAssignments告诉regionserver close region),( -fixMeta删除META表中region的记录)
  22. (4)如果一个region在META表中没有记录,没有被regionserver服务,但是在hdfs上面有
  23. 加上选项 -fixMeta -fixAssignments 解决,( -fixAssignments 用于assign region),( -fixMeta用于在META表中添加region的记录)
  24. (5)如果一个region在META表中没有记录,在hdfs上面有,被regionserver服务了
  25. 加上选项 -fixMeta 解决,在META表中添加这个region的记录,先undeploy region,后assign
  26. (6)如果一个region在META表中有记录,但是在hdfs上面没有,并且没有被regionserver服务
  27. 加上选项 -fixMeta 解决,删除META表中的记录
  28. (7)如果一个region在META表中有记录,在hdfs上面也有,table不是disabled的,但是这个region没有被服务
  29. 加上选项 -fixAssignments 解决,assign这个region
  30. (8)如果一个region在META表中有记录,在hdfs上面也有,table是disabled的,但是这个region被某个regionserver服务了
  31. 加上选项 -fixAssignments 解决,undeploy这个region
  32. (9)如果一个region在META表中有记录,在hdfs上面也有,table不是disabled的,但是这个region被多个regionserver服务了
  33. 加上选项 -fixAssignments 解决,通知所有regionserver close region,然后assign region
  34. (10)如果一个region在META表中,在hdfs上面也有,也应该被服务,但是META表中记录的regionserver和实际所在的regionserver不相符
  35. 加上选项 -fixAssignments 解决
  36. (11)region holes
  37. 需要加上 -fixHdfsHoles ,创建一个新的空region,填补空洞,但是不assign 这个 region,也不在META表中添加这个region的相关信息
  38. (12)region在hdfs上面没有.regioninfo文件
  39. -fixHdfsOrphans 解决
  40. (13)region overlaps
  41. 需要加上 -fixHdfsOverlaps
  42. 说明:
  43. (1)修复region holes时,-fixHdfsHoles 选项只是创建了一个新的空region,填补上了这个区间,还需要加上-fixAssignments -fixMeta 来解决问题,( -fixAssignments 用于assign region),( -fixMeta用于在META表中添加region的记录),所以有了组合拳 -repairHoles 修复region holes,相当于-fixAssignments -fixMeta -fixHdfsHoles -fixHdfsOrphans
  44. (2) -fixAssignments,用于修复region没有assign、不应该assign、assign了多次的问题
  45. (3)-fixMeta,如果hdfs上面没有,那么从META表中删除相应的记录,如果hdfs上面有,在META表中添加上相应的记录信息
  46. (4)-repair 打开所有的修复选项,相当于-fixAssignments -fixMeta -fixHdfsHoles -fixHdfsOrphans -fixHdfsOverlaps -fixVersionFile -sidelineBigOverlaps
  47. 新版本的hbck从(1)hdfs目录(2)META(3)RegionServer这三处获得region的Table和Region的相关信息,根据这些信息判断并repair

示例:

[plain] view plain copy

static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="18" height="18" align="middle" name="ZeroClipboardMovie_2"/>

在CODE上查看代码片派生到我的代码片

  1. 查看hbasemeta情况
  2. hbase hbck
  3. 1.重新修复hbase meta表(根据hdfs上的regioninfo文件,生成meta表)
  4. hbase hbck -fixMeta
  5. 2.重新将hbase meta表分给regionserver(根据meta表,将meta表上的region分给regionservere)
  6. hbase hbck -fixAssignments

默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案:

1.检查当前安装的PHP包
yum list installed | grep php
如果有安装的PHP包,先删除他们
 yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_64 php-mysql.x86_64 php-pdo.x86_64
2.Centos 5.X
  rpm -Uvh http://mirror.webtatic.com/yum/el5/latest.rpm
CentOs 6.x
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
CentOs 7.X
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
如果想删除上面安装的包,重新安装
rpm -qa | grep webstatic
rpm -e  上面搜索到的包即可

3.运行yum install

  yum install php55w.x86_64 php55w-cli.x86_64 php55w-common.x86_64 php55w-gd.x86_64 php55w-ldap.x86_64 php55w-mbstring.x86_64 php55w-mcrypt.x86_64 php55w-mysql.x86_64 php55w-pdo.x86_64

yum install php56w.x86_64 php56w-cli.x86_64 php56w-common.x86_64 php56w-gd.x86_64 php56w-ldap.x86_64 php56w-mbstring.x86_64 php56w-mcrypt.x86_64 php56w-mysql.x86_64 php56w-pdo.x86_64

注:如果想升级到5.6把上面的55w换成56w就可以了。

yum install php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64
4.安装PHP FPM

yum install php55w-fpm
yum install php56w-fpm
yum install php70w-fpm
注:如果想升级到5.6把上面的55w换成56w就可以了。

转自:http://blog.csdn.net/mapboo/article/details/49536297

CSDN博主Mapboo原创,感谢博主!

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

前言:

纵观国内的广告平台,比如多盟,万普,百度,腾讯,或者别的一些广告平台,真是应接不暇,但是具体接入那就自己判断了,经过一番折腾,最终还是选择了谷歌平台,理应是稳定,可靠,安全,收入颇高,但是登录网络有点问题,应该大家都懂吧,这里就不再赘述了,各自想办法吧;

准备工作:

1,首先你是Android开发者,或者至少会点Java编程基础;

2,熟悉使用Android Studio(以下简称:AS),即官方Android应用开发工具;

3,注册了http://www.google.cn/admob/ 应用,并且有个banner_ad_unit_id吧;

4,待加入横幅广告条的app;

5,可参考官方接入指南:https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start

开发过程:

1,创建或打开AS 工程,本示例是新建工程;

2,打开Android SDK Manager安装Google Repository;

3,关联我们的广告SDK,这里有两种方式:

A  点击Project Structure 直接选取,如下图:

com.google.android.gms:play-services:7.8.0;

B 打开build-gradle 加入一行代码,如下图:

compile 'com.google.android.gms:play-services:6.+'

4,程序会开始自动编译,然后打开AndroidManifest.xml 文件,添加如下代码:

[java] view plain copy

static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="27" height="15" align="middle" name="ZeroClipboardMovie_1"/>

print?

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.mapboo.adviewdemo" >
  4.    <span style="color:#ff0000;"> <uses-permission android:name="android.permission.INTERNET" />
  5.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/></span>
  6.     <application
  7.         android:allowBackup="true"
  8.         android:icon="@mipmap/ic_launcher"
  9.         android:label="@string/app_name"
  10.         android:theme="@style/AppTheme" >
  11.        <span style="color:#ff0000;"> <meta-data android:name="com.google.android.gms.version"
  12.             android:value="@integer/google_play_services_version" /></span>
  13.         <activity
  14.             android:name=".MainActivity"
  15.             android:label="@string/app_name" >
  16.             <intent-filter>
  17.                 <action android:name="android.intent.action.MAIN" />
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.        <span style="color:#ff0000;"> <activity
  22.             android:name="com.google.android.gms.ads.AdActivity"
  23.             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
  24.             android:theme="@android:style/Theme.Translucent" /></span>
  25.     </application>
  26. </manifest>

 

5,打开activity_main.xml 文件,输入如下代码:

[java] view plain copy

static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="27" height="15" align="middle" name="ZeroClipboardMovie_2"/>

print?

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     xmlns:ads="http://schemas.android.com/apk/res-auto"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:paddingBottom="@dimen/activity_vertical_margin"
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"
  8.     android:paddingRight="@dimen/activity_horizontal_margin"
  9.     android:paddingTop="@dimen/activity_vertical_margin"
  10.     tools:context=".MainActivity"
  11.     android:gravity="center_vertical">
  12.     <TextView
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content"
  15.         android:text="@string/demoName"
  16.         android:gravity="center"
  17.         android:textSize="30dp"
  18.         android:layout_marginTop="150dp"
  19.         />
  20.     <com.google.android.gms.ads.AdView
  21.         android:id="@+id/adView_bottom"
  22.         android:layout_width="match_parent"
  23.         android:layout_height="wrap_content"
  24.         android:layout_alignParentBottom="true"
  25.         android:layout_centerHorizontal="true"
  26.         android:layout_gravity="bottom"
  27.         ads:adSize="BANNER"
  28.         ads:adUnitId="@string/banner_ad_unit_id" />
  29. </RelativeLayout>

 

[java] view plain copy

static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="27" height="15" align="middle" name="ZeroClipboardMovie_3"/>

print?

  1. <resources>
  2.     <string name="app_name">AdViewDemo</string>
  3.     <string name="demoName">谷歌Admob广告条示例</string>
  4.     <string name="banner_ad_unit_id">ca-app-pub-8514769117499822/6386262194</string>
  5. </resources>

 

6,打开MainActivity文件,输入如下代码:

[java] view plain copy

static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="27" height="15" align="middle" name="ZeroClipboardMovie_4"/>

print?

  1. package com.mapboo.adviewdemo;
  2. import android.support.v7.app.ActionBarActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import com.google.android.gms.ads.AdListener;
  8. import com.google.android.gms.ads.AdRequest;
  9. import com.google.android.gms.ads.AdView;
  10. public class MainActivity extends ActionBarActivity {
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         /**
  16.          * 创建广告条对象
  17.          */
  18.         AdView mAdView = (AdView) findViewById(R.id.adView_bottom);
  19.         AdRequest adRequest = new AdRequest.Builder().build();
  20.         /**
  21.          * 创建一个监听,比如判断用户是不是已经点击了广告条,以此来给用户增加积分等操作;
  22.          */
  23.         AdListener listener = new AdListener() {
  24.             @Override
  25.             public void onAdClosed() {
  26.                 super.onAdClosed();
  27.                 Log.i("TAG""onAdClosed");
  28.             }
  29.             @Override
  30.             public void onAdFailedToLoad(int errorCode) {
  31.                 super.onAdFailedToLoad(errorCode);
  32.                 Log.i("TAG""onAdFailedToLoad");
  33.             }
  34.             @Override
  35.             public void onAdLeftApplication() {
  36.                 super.onAdLeftApplication();
  37.                 Log.i("TAG""onAdLeftApplication");
  38.             }
  39.             @Override
  40.             public void onAdLoaded() {
  41.                 super.onAdLoaded();
  42.                 Log.i("TAG""onAdLoaded");
  43.             }
  44.             @Override
  45.             public void onAdOpened() {
  46.                 super.onAdOpened();
  47.                 Log.i("TAG""onAdOpened");
  48.             }
  49.         };
  50.         /**
  51.          * 设置监听
  52.          */
  53.         mAdView.setAdListener(listener);
  54.         /**
  55.          * 开始载入广告条
  56.          */
  57.         mAdView.loadAd(adRequest);
  58.     }
  59.     /**
  60.      *
  61.      官方示例:https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start
  62.      
  63.      分享:安卓发布平台 http://blog.csdn.net/mapboo/article/details/49531043
  64.      应用汇:http://www.appchina.com/
  65.      安智市场:http://www.anzhi.com/
  66.      华为市场:http://appstore.huawei.com/
  67.      百度市场:http://app.baidu.com/pass/center
  68.      蒲公英:http://www.pgyer.com
  69.      N多网 :http://www.nduoa.com/developer
  70.      豌豆荚:http://developer.wandoujia.com/
  71.      360:http://dev.360.cn
  72.      QQ:http://op.open.qq.com
  73.      FIR:http://fir.im
  74.      Android中国开发者 交流QQ群:361871827 欢迎加入一起讨论技术...
  75.      */
  76. }

 

编译运行:

运行代码,就会出现Admob广告条,只要替换自己的banner_ad_unit_id就能开始赚钱了;

效果图:

本实例源代码下载地址:http://pan.baidu.com/s/1pJ3UEcr

Android中国开发者 QQ群:361871827 欢迎一起讨论技术..

优化博客网页内容时,将js和css指定到static.151051.cn域名下,没想到其中css使用的字体文件无法加载,提示错误信息如下:

Font from origin 'http://static.151051.cn' has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.151051.cn' is therefore not allowed access.

好吧,字面上的意思是,字体被禁止调用,原因头信息中并不包含Access-Control-Allow-Origin,没有授权给www.151051.cn使用,原谅我蹩脚的英文。随手百度了一下Access-Control-Allow-Origin这货,原来这货是用来定义允许哪个域使用资源,可以有效解决字体远程调用的问题。一不做二不休,开搞。

如果是php文件,可以在php文件中定义<?php header("Access-Control-Allow-Origin: http://www.151051.cn");?> 注意,这里网上有很多文章是用*代替域名,这样做比较"呵呵",所以还是指定域名比较好,安全性高一些。

像我现在这种需求,在static.151051.cn授权给www.151051.cn使用,而且static并不想支持php,那肿么办呢?

呵呵,好办,直接在nginx里加入到头信息中,一劳永逸。如下:
location ~ .*\.(js|css|woff|ttf|svg|eot|oft)?$
{
add_header Access-Control-Allow-Origin http://www.151051.cn;
expires 2h;
}

好了,解决!

告别node-forever,拥抱PM2
http://www.oschina.net/translate/goodbye-node-forever-hello-pm2?cmp

static.oschina.net/uploads/img/201307/29151744_a70t.png" alt="pm2 logo" />devo.ps团队对JavaScript的迷恋已经不是什么秘密了;node.js作为服务器端,AngularJS作为客户端,某种程度上说,我们的堆栈是用它建成的.我们构建静态客户端和RESTful JSON API的方法意味着我们跑了很多的node.js,我必须承认尽管node.js的一切都令人敬畏,但当我们在生产环境中运行它时它仍然会让我们感到头疼.相比一些更加成熟的语言,它的工具和最佳实践仍然缺乏(试想一下:监控,日志,错误处理).

到目前为止,我们仍然依赖漂亮俏皮的node-forever模块.它是非常伟大的模块,不过依然缺失一些功能:

  • 有限的监控和日志功能,
  • 进程管理配置的支持差,
  • 不支持集群,
  • 代码库老化(意味着在升级node.js时频繁的失败).

这就是为什么我们要在过去的几个月里去写PM2模块.在我们即将发布针对生产环境的正式版之前我们想先让您看一眼.

PM2到底是什么个东西呢?

首先第一件事,你需要先通过 npm 来安装它:

npm install -g pm2

让我们通过表格来对比下:

Feature Forever PM2
Keep Alive
Coffeescript
Log aggregation
API
Terminal monitoring
Clustering
JSON configuration

现在让我来介绍一点点主要特性...

原生的集群化支持

Node v0.6引入了集群特性,允许你在多个Node应用中共享socket.问题在于,它不能在容器外运行而且需要一些额外的配置来处理主进程和子进程.

PM2原生支持处理这个问题,而且不需要额外的代码:PM2本身作为主进程,然后它将你的代码封装到一个特殊的集群进程里,就像node.js一样,为你的代码文件添加一些全局变量.想要启动一个使用所有CPU核心的集群,你只需要键入如下的指令:

$ pm2 start app.js -i max

然后;

$ pm2 list

然后就会显示类似下面的东西(ASCII UI FTW);

static.oschina.net/uploads/img/201307/29151744_iiYh.png" alt="pm2 list" />

就像你看到的,现在你的应用有多少个进程就取决于你的CPU核心数了.

按照termcaps-HTOP(Linux下的系统监控与进程管理软件)的方式管理

通过pm2 list命令来观察所有运行的进程以及它们的状态已经足够好了.但是怎么来追踪它们的资源消耗呢?别担心,用这个命令:

$ pm2 monit

你可以得到进程(以及集群)的CPU的使用率和内存占用.

static.oschina.net/uploads/img/201307/29151746_LbVd.png" alt="pm2 monit" />

声明: node-usage 到目前为止还不支持MacOS(随便什么性能要求),不过它在Linux下运行良好.

现在,让我们来核实一下我们的集群,还有对内存堆栈的垃圾回收,我们假设你已经有一个HTTP基准测试工具(如果没有,你一定要使用 WRK):

$ express bufallo // Create an express app $ cd bufallo
$ npm install
$ pm2 start app.js -i max
$ wrk -c 100 -d 100 http://localhost:3000/

在另一个终端,运行监控选项:

$ pm2 monit

耶~

实时集中log处理

现在你不得不管理多个集群进程:一个爬取数据,一个处理数据,等等...这就意味着大量log,你可以按照老式的方法处理:

$ tail -f /path/to/log1 /path/to/log2 ...

但我们想的很周到,我们增加了logs功能:

$ pm2 logs

static.oschina.net/uploads/img/201307/29151747_jrUM.png" alt="pm2 monit" />

快速恢复

现在事情一切顺利,你的进程嗡嗡的运行着,你需要做一次硬重启(hard restart).现在吗?是的,首先,dump掉:

$ pm2 dump

然后,你可以从文件中恢复它:

$ pm2 kill // 让我们假设一个PM2停掉了
$ pm2 resurect // 我所有的进程又满血满状态复活了

强健的API

比方说,你想要监控所有被PM2管理的进程,而且同时还想监控运行这些进程的机器的状态(甚至希望创建一个Angular应用来调用这些API...):

$ pm2 web

打开浏览器输入 http://localhost:9615 ,我嘞个去!!

对了,还有很多特性...

  • 全部测试通过,
  • 新一代的update-rc.d(pm2 startup),当然它还是alpha版,
  • 开发模式下更改文件自动重启(pm2 dev),也同样还是草稿,
  • 自动刷新log,
  • 快捷的通过JSON文件管理你的应用,
  • 在error log里记录未捕获的异常,
  • 记录重启的次数和时间,
  • 退出时自动杀死进程.

 

下一步计划?

首先,你可以去Github上粉我们(我们喜欢stars): https://github.com/Unitech/pm2.

我们开发的PM2提供了先进完整的Node进程管理解决方案.我们希望能有更多的人来帮助我们:更多的pull requests.一些还停留在开发路线图上面的功能我们会尽快完成,下面这些就是:

  • 远程管理/状态校验,
  • 嵌入式跨进程通信通道(消息总线),
  • V8垃圾回收的内存泄漏检查,
  • Web界面,
  • 监控数据持久化,
  • 邮件通知.

特别感谢 Makara Wang 的观点和工具,还有 Alex Kocharin 提的建议和提交的代码.

转:http://zhaohe162.blog.163.com/blog/static/38216797201402234212981/

本文演示在Linux上安装NodeJS及Express开发框架
 
软件环境:
VMware 9
CentOS 6.5
NodeJS v0.10.24

 
安装过程:

Step 1、确认服务器有nodejs编译及依赖相关软件,如果没有可通过运行以下命令安装。

[root@BobServerStation local]# yum -y install gcc gcc-c++ openssl-devel

Step 2、下载NodeJS源码包并解压。

[root@BobServerStation local]# wget http://nodejs.org/dist/v0.10.24/node-v0.10.24.tar.gz

[root@BobServerStation local]# tar zxvf node-v0.10.24.tar.gz
[root@BobServerStation local]# cd node-v0.10.24

转:http://www.uplook.cn/kbase-Index-show-view10393.html

#!/bin/bash
# BY kerryhu
# MAIL:king_819@163.com
# BLOG:http://kerry.blog.51cto.com
# Please manual operation yum of before Operation.....
#============================ 更新系统时间 ============================
yum install -y ntp
ntpdate time.nist.gov
echo "00 01 * * * ntpdate time.nist.gov" >> /etc/crontab

#============================ Varnish安装 =============================

如果是RedHat/CentOS系统,在安装varnish的时候首先要安装以下软件包
automake
autoconf
libtool
ncurses-devel
libxslt
groff
pcre-devel
pkgconfig

groupadd www
useradd www -g www -s /sbin/nologin
mkdir -p /data/varnish/{cache,logs}
chmod +w /data/varnish/{cache,logs}
chown -R www:www /data/varnish/{cache,logs}
cd /opt
yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
wget http://sourceforge.net/projects/varnish/files/varnish/2.1.3/varnish-2.1.3.tar.gz/download
tar -zxvf varnish-2.1.3.tar.gz
cd varnish-2.1.3
./configure --prefix=/usr/local/varnish
make;make install

yum -y install wget --noplugins
wget freevps.us/downloads/nginx-centos-6.sh -O - | bash

注意安装完成后 php-fpm 默认跑在 apache 用户上。

安装完成后,如果你需要的 PHP 扩展未安装,可输入如下命令查询:

yum list | grep ^php*

找到了扩展的名字,就可以直接安装了:

# 以 php memcache 扩展为例
yum install -y php-pecl-memcached

此外还遇到的问题是 DOMDocument 找不到,直接 yum 安装 php-xml:

yum install -y php-xml

所有扩展 yum 安装完成后都需要重新启动 / 载入 php-fpm。
贴下内容来看看(非最新版本,请直接下载 .sh 文件):

#!/bin/bash

##################
# disable apache #
##################
service httpd stop 
chkconfig httpd off
service xinetd stop
chkconfig xinetd off
service saslauthd stop
chkconfig saslauthd off
service sendmail stop
chkconfig sendmail off
service postfix stop
chkconfig postfix off

#Optimize yum on OpenVZ
if [ -e "/proc/user_beancounters" ]
then
  sed -i 's/plugins=1/plugins=0/' /etc/yum.conf
fi

#remove all current PHP and MySQL, will reinstall later. Also, remove apache 
yum -y remove httpd php mysql rsyslog sendmail postfix

###################
# Add a few repos #
###################
# install the Atomic repo for php and nginx (may use epel for nginx depending on version)
wget -q -O - http://www.atomicorp.com/installers/atomic | sh

# RPMForge for nginx dependencies
rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm

#EPEL for syslog-ng and nginx (may use atomic for nginx depending on version)
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm

################################
# Install PHP, NGINX and MySQL #
################################
#yum install GeoIP libGeoIP.so.1 --enablerepo=repoforge
yum -y install mysql-server php-fpm php-mysql php-gd nginx nano exim syslog-ng

#################
# install MySQL #
#################
#yum -y install mysql-server
cat > /etc/my.cnf <<END
[mysqld]
default-storage-engine = myisam
key_buffer = 8M
query_cache_size = 8M
query_cache_limit = 4M
max_connections=25
thread_cache=1
skip-innodb 
query_cache_min_res_unit=0
tmp_table_size = 4M
max_heap_table_size = 4M
table_cache=256
concurrent_insert=2 
END
echo  Do not worry if you see a error stopping MySQL or NGINX
/etc/init.d/mysqld stop
/etc/init.d/mysqld start

####################
# Set up NGINX PHP #
####################
cat > /etc/nginx/php <<END
index index.php index.html index.htm;

location ~ \.php$ {

   include fastcgi_params;
    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
  try_files \$uri =404;
    fastcgi_pass 127.0.0.1:9000;
    error_page 404 /404page.html; #makes nginx return it's default 404 
#	page instead of a blank page

} 
END

cat > /etc/nginx/nginx.conf <<END
user              nginx nginx;
worker_processes  2;

error_log         logs/error.log;

pid               logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 64M;
    sendfile        on;
    tcp_nopush      on;

    keepalive_timeout  3;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server_tokens off;

    include /etc/nginx/conf.d/*;
} 
END
rm /etc/nginx/conf.d/*
cat > /etc/nginx/conf.d/default.conf <<END
server {
    listen 80 default;
    server_name _;
    root /var/www/html;
    include php;

  } 
END
mkdir /usr/share/nginx/logs/
service nginx restart
chkconfig nginx on
chkconfig mysqld on

cat > /etc/php-fpm.d/www.conf <<END
; Start a new pool named 'www'.
[www]

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000

; Set listen(2) backlog. A value of '-1' means unlimited.
; Default Value: -1
;listen.backlog = -1

; List of ipv4 addresses of FastCGI clients which are allowed to connect.
; Equivalent to the FCGI_WEB_SERVER_ADDRS environment variable in the original
; PHP FCGI (5.2.2+). Makes sense only with a tcp listening socket. Each address
; must be separated by a comma. If this value is left blank, connections will be
; accepted from any ip address.
; Default Value: any
listen.allowed_clients = 127.0.0.1

; Set permissions for unix socket, if one is used. In Linux, read/write
; permissions must be set in order to allow connections from a web server. Many
; BSD-derived systems allow connections regardless of permissions. 
; Default Values: user and group are set as the running user
;                 mode is set to 0666
;listen.owner = nobody
;listen.group = nobody
;listen.mode = 0666

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives:
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes to be created when pm is set to 'dynamic'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI.
; Note: Used when pm is set to either 'static' or 'dynamic'
; Note: This value is mandatory.
pm.max_children = 5

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 1

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 1

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 3

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 500

; The URI to view the FPM status page. If this value is not set, no URI will be
; recognized as a status page. By default, the status page shows the following
; information:
;   accepted conn    - the number of request accepted by the pool;
;   pool             - the name of the pool;
;   process manager  - static or dynamic;
;   idle processes   - the number of idle processes;
;   active processes - the number of active processes;
;   total processes  - the number of idle + active processes.
; The values of 'idle processes', 'active processes' and 'total processes' are
; updated each second. The value of 'accepted conn' is updated in real time.
; Example output:
;   accepted conn:   12073
;   pool:             www
;   process manager:  static
;   idle processes:   35
;   active processes: 65
;   total processes:  100
; By default the status page output is formatted as text/plain. Passing either
; 'html' or 'json' as a query string will return the corresponding output
; syntax. Example:
;   http://www.foo.bar/status
;   http://www.foo.bar/status?json
;   http://www.foo.bar/status?html
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set 
;pm.status_path = /status

; The ping URI to call the monitoring page of FPM. If this value is not set, no
; URI will be recognized as a ping page. This could be used to test from outside
; that FPM is alive and responding, or to
; - create a graph of FPM availability (rrd or such);
; - remove a server from a group if it is not responding (load balancing);
; - trigger alerts for the operating team (24/7).
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set
;ping.path = /ping

; This directive may be used to customize the response of a ping request. The
; response is formatted as text/plain with a 200 response code.
; Default Value: pong
;ping.response = pong

; The timeout for serving a single request after which the worker process will
; be killed. This option should be used when the 'max_execution_time' ini option
; does not stop script execution for some reason. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_terminate_timeout = 0

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
;request_slowlog_timeout = 0

; The log file for slow requests
; Default Value: /var/log/php-fpm.log.slow
;slowlog = /var/log/php-fpm.log.slow

; Set open file descriptor rlimit.
; Default Value: system defined value
;rlimit_files = 1024

; Set max core size rlimit.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
;rlimit_core = 0

; Chroot to this directory at the start. This value must be defined as an
; absolute path. When this value is not set, chroot is not used.
; Note: chrooting is a great security feature and should be used whenever 
;       possible. However, all PHP paths will be relative to the chroot
;       (error_log, sessions.save_path, ...).
; Default Value: not set
;chroot = 

; Chdir to this directory at the start. This value must be an absolute path.
; Default Value: current directory or / when chroot
;chdir = /var/www

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Default Value: no
;catch_workers_output = yes

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

; Additional php.ini defines, specific to this pool of workers. These settings
; overwrite the values previously defined in the php.ini. The directives are the
; same as the PHP SAPI:
;   php_value/php_flag             - you can set classic ini defines which can
;                                    be overwritten from PHP call 'ini_set'. 
;   php_admin_value/php_admin_flag - these directives won't be overwritten by
;                                     PHP call 'ini_set'
; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.

; Defining 'extension' will load the corresponding shared extension from
; extension_dir. Defining 'disable_functions' or 'disable_classes' will not
; overwrite previously defined php.ini values, but will append the new value
; instead.

; Default Value: nothing is defined by default except the values in php.ini and
;                specified at startup with the -d argument
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com
;php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M
php_admin_value[upload_max_filesize] = 32M
END

mkdir /var/www
mkdir /var/www/html/
useradd apache
service php-fpm start
chkconfig php-fpm on
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save

wget freevps.us/downloads/setup-vhost.sh -O /bin/setup-vhost
chmod 755 /bin/setup-vhost
echo "alias nano='nano -w'" >> ~/.bashrc
clear
echo Installation done.
echo to create a vhost, run
echo setup-vhost example.com
echo do not include the www. subdomain.