介绍基于HAProxy的高性能缓存服务器nuster

摘要: Nuster是一个基于HAProxy的高性能缓存服务器 介绍 Nuster是一个基于HAProxy的高性能缓存服务器。Nuster完全兼容HAProxy,并且利用HAProxy的ACL功能来提供非常细致的缓存规则,比如 请求地址为某某时缓存 请求参数中的X为Y时缓存 响应头中的X为Y时缓存 请求速率超过多少时缓存 等等 性能 非常快, 单进程模式下是nginx的3倍,多进程下nginx的2倍,varnish的3倍。

Nuster是一个基于HAProxy的高性能缓存服务器

介绍

Nuster是一个基于HAProxy的高性能缓存服务器。Nuster完全兼容HAProxy,并且利用
HAProxy的ACL功能来提供非常细致的缓存规则,比如

  • 请求地址为某某时缓存
  • 请求参数中的X为Y时缓存
  • 响应头中的X为Y时缓存
  • 请求速率超过多少时缓存
  • 等等

性能

非常快, 单进程模式下是nginx的3倍,多进程下nginx的2倍,varnish的3倍。

详见 https://github.com/jiangwenyuan/nuster/wiki/Performance-benchmark:-nuster-vs-nginx-vs-varnish?spm=5176.100239.blogcont307591.18.c776f1eZVmaLX

安装

make TARGET=linux2628
make install

具体参照 https://github.com/jiangwenyuan/nuster/wiki/Performance-benchmark:-nuster-vs-nginx-vs-varnish?spm=5176.100239.blogcont307591.18.c776f1eZVmaLX

使用方法

在global中添加cache on, 然后在backend或listen中添加cache filter和cache rule

指令

cache

syntax: cache on|off [share on|off] [data-size size] [dict-size size]

default: none

context: global

控制是否开启缓存。
可以设置data-size来控制缓存数据的内存使用量。可以使用m, M, g 和 G.
默认是1MB,同时也是最小使用量。只有http内容计算在内,并不包括使用缓存带来的内存开销。

filter cache

syntax: filter cache [on|off]

default: on

context: backend, listen

定义一个cache filter, 另外cache-rule也需要添加。
可以为多个代理添加,并单独设置某个代理的缓存是否开启。
如果定义了多个filter,需要把cache filter放在最后。

cache-rule

syntax: cache-rule name [key KEY] [ttl TTL] [code CODE] [if|unless condition]

default: none

context: backend, listen

定义缓存规则。可以同时定义多个,但是需要注意顺序,匹配则会停止测试。

acl pathA path /a.html
filter cache
cache-rule all ttl 3600
cache-rule path01 ttl 60 if pathA

path01这条规则永远不会执行,因为all会匹配所有的规则。

name

定义一个名字。

key KEY

定义key,由以下关键字组成:

  • method: http method, GET/POST…
  • scheme: http or https
  • host: the host in the request
  • path: the URL path of the request
  • query: the whole query string of the request
  • header_NAME: the value of header NAME
  • cookie_NAME: the value of cookie NAME
  • param_NAME: the value of query NAME
  • body: the body of the request

默认key是method.scheme.host.path.query.body

Example

GET http://www.example.com/q?name=X&type=Y

http header:
GET /q?name=X&type=Y HTTP/1.1
Host: www.example.com
ASDF: Z
Cookie: logged_in=yes; user=nuster;

会得到:

  • method: GET
  • scheme: http
  • host: www.example.com
  • path: /q
  • query: name=X&type=Y
  • header_ASDF: Z
  • cookie_user: nuster
  • param_type: Y
  • body: (empty)

所以默认的key就会得到GEThttpwww.example.com/qname=X&type=Y, 而
key method.scheme.host.path.header_ASDF.cookie_user.param_type则会生成
GEThttpwww.example.com/qZnusterY

一个请求的key能在缓存中找到则返回缓存内容。

ttl TTL

定义key的失效时间,可以使用 d, h, m and s。默认3600秒.
如果不希望失效则设为0

code CODE1,CODE2…

默认只缓存200的响应,如果需要缓存其他的则可以添加,all会缓存任何状态码。

cache-rule only200
cache-rule 200and404 code 200,404
cache-rule all code all

if|unless condition

定义ACL条件
详见 HAProxy configuration 的 7. Using ACLs and fetching samples

FAQ

如何调试?

在global添加debug, 或者带-d启动haproxy

缓存相关的调试信息以[CACHE]开头

如何缓存POST请求?

添加option http-buffer-request

如果自定义了key的话需要使用body关键字

请求body可能不完整,详见HAProxy configuration 的
option http-buffer-request小节

另外可以为post请求单独设置一个后端

Example

global
    cache on data-size 100m
    #daemon
    ## to debug cache
    #debug
defaults
    retries 3
    option redispatch
    timeout client  30s
    timeout connect 30s
    timeout server  30s
frontend web1
    bind *:8080
    mode http
    acl pathPost path /search
    use_backend app1a if pathPost
    default_backend app1b
backend app1a
    balance roundrobin
    # mode must be http
    mode http

    # http-buffer-request must be enabled to cache post request
    option http-buffer-request

    acl pathPost path /search

    # enable cache for this proxy
    filter cache

    # cache /search for 120 seconds. Only works when POST/PUT
    cache-rule rpost ttl 120 if pathPost

    server s1 10.0.0.10:8080
backend app1b
    balance     roundrobin
    mode http

    filter cache on

    # cache /a.jpg, not expire
    acl pathA path /a.jpg
    cache-rule r1 ttl 0 if pathA

    # cache /mypage, key contains cookie[userId], so it will be cached per user
    acl pathB path /mypage
    cache-rule r2 key method.scheme.host.path.query.cookie_userId ttl 60 if pathB

    # cache /a.html if response's header[cache] is yes
    http-request set-var(txn.pathC) path
    acl pathC var(txn.pathC) -m str /a.html
    acl resHdrCache1 res.hdr(cache) yes
    cache-rule r3 if pathC resHdrCache1

    # cache /heavy for 100 seconds if be_conn greater than 10
    acl heavypage path /heavy
    acl tooFast be_conn ge 100
    cache-rule heavy ttl 100 if heavypage tooFast 

    # cache all if response's header[asdf] is fdsa
    acl resHdrCache2 res.hdr(asdf)  fdsa
    cache-rule resCache ttl 0 if resHdrCache1

    server s1 10.0.0.10:8080

frontend web2
    bind *:8081
    mode http
    default_backend app2
backend app2
    balance     roundrobin
    mode http

    # disable cache on this proxy
    filter cache off
    cache-rule all

    server s2 10.0.0.11:8080

listen web3
    bind *:8082
    mode http

    filter cache
    cache-rule everything

    server s3 10.0.0.12:8080

Haproxy 负载均衡配置集群配置

首先安装HAProxy

注:负载均衡建议使用多台服务器,达到更高的性能。

Centos使用

yum install haproxy -y

Debian使用

vi /etc/apt/sources.list

添加如下内容

deb http://ftp.us.debian.org/debian/ wheezy-backports main

然后

apt-get update apt-get install haproxy

接下来设置配置文件

vi /etc/haproxy/haproxy.cfg

清空配置文件后,输入如下内容

  global
    log 127.0.0.1   local0
    log 127.0.0.1   local1 notice
    #log loghost    local0 info
    maxconn 4096
    chroot /var/haproxy
    uid 99
    gid 99
    daemon
    nbproc  1
    #debug
    #quiet

defaults
    log global
    mode    http
    option  httplog
    option  dontlognull
    retries 3
    option redispatch # 自动重定向到健康机器
    maxconn 200000
    timeout check 2000ms # 检查超时
    timeout connect 5000ms # 连接超时
    timeout client 50000ms # 客户端连接超时
    timeout server 50000ms # 服务端连接超时

#配置客户端
listen webinfo :80
       mode http
       balance roundrobin
       stats uri /ha_status
       option httpclose
       option forwardfor
       server web1 127.0.0.1:810 check weight 1 minconn 1 maxconn 200 check inter 40000  #其中一台web服务器
       server web2 127.0.0.1:811 check weight 1 minconn 1 maxconn 200 check inter 40000  #其中一台web服务器
       server web4 127.0.0.1:812 check weight 1 minconn 1 maxconn 200 check inter 40000  #其中一台web服务器

#配置控制台
listen stats :6553
       mode http
       transparent
       stats uri / haproxy-stats
       stats realm Haproxy  statistic
       stats auth admin:admin

haproxy监控页面 和页面详细参数

stats enable是haproxy通过WEB界面进行统计数据展示的功能,通过WEB界面可以查看到haproxy当前运行状态的一些数据,合理配置的情况下,还可以通过WEB界面调整某个后端服务器的状态,权重,启用或暂停一个后端服务器等。

stats enable可以出现在default/listen/backend这几个区块中,stats enable这一条命令就可以开启统计功能,开启后下面参数拥有的默认值:

stats uri : /haproxy?stats
stats realm : “HAProxy Statistics”
stats auth : no authentication
stats scope : no restriction

尽量不要用默认的值,根据自己的情况,明确指定要使用的值。
常用配置:

编辑haproxy.cfg 加上下面参数

listen admin_stats
        stats   enable
        bind    *:8080    //监听的ip端口号
        mode    http    //开关
        option  httplog
        log     global
        maxconn 10
        stats   refresh 30s   //统计页面自动刷新时间
        stats   uri /admin?stats    //访问的uri   ip:8080/admin
        stats   realm haproxy
        stats   auth admin:admin  //认证用户名和密码
        stats   hide-version   //隐藏HAProxy的版本号
        stats   admin if TRUE   //管理界面,如果认证成功了,可通过webui管理节点

保存退出后

重起service haproxy restart

然后访问 http://ip:8080/admin?stats 用户名:admin 密码:admin

注意:访问监控页面是ip+端口号,之前忽略了端口号无法访问

ubuntu 16.04 kdump 使用

1、安装linux-crashdump及kdump

sudo apt-get install linux-crashdump

sudo apt-get install kexec-tool

2、重启电脑

3、下载dbgsym

在图形界面选择一个源

或者添加如下的源

deb http://ddebs.ubuntu.com/ precise main
deb http://ddebs.ubuntu.com/ precise-updates main

4、测试crash产生 echo c >/proc/sysrq-trigger

5、 crash /usr/lib/debug/boot/vmlinux /var/crash/…/vmcore

附:ubuntu 14.40的kdump似乎有问题,无法log和自动重启,这些在16.04中均正常。

Haproxy的安装、配置

未分类

Haproxy的介绍、安装、配置

1、Haproxy简介

Haproxy是一个开源的,高性能的基于HTTP和TCP应用代理的高可用,负载均衡服务软件。它支持双机热备,高可用,负载均衡。有图形界面。
而且拥有很好的对服务器节点的健康检查功能。

单台的haproxy可以满足每天1-3千万的PV。

haproxy为代理,用户的请求和后端的服务器的回应都需要经过haproxy服务器。

2、Haproxy的常用算法

balance leastconn   最少连接数
balance roundrobin   轮循
balance static-rr  权重
balance source   相当于nginx的ip_hash

3、Haproxy的安装

centos6.6默认的源yum安装proxy的版本为1.5.4

yum 安装haproxy

yum install -y haproxy

打开内核转发

vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

让上面配置的/etc/sysctl.conf文件生效

sysctl -p

4、Haproxy配置文件详解

Haproxy配置文件可以分为五部分:

global : 全局配置参数段,主要用来控制haproxy启动前的进程及系统相关设置。

defaults : 配置一些默认参数,如frontend,backend,listen等段末没有设置则使用default段来设置。

listen : 监听

frontend : 用来匹配接收客户所请求的域名,URL等,并针对不同的匹配,做不同的请求处理。

backend : 定义后端服务器集群,以及对后端服务器的一些权重,队列,连接数等的设置

下面为正式环境下的haproxy.cfg配置文件

cat >> /etc/haproxy/haproxy.cfg <<EOF
global
log 127.0.0.1:514 local0 error #一定不能用info,这样会造成很大的磁盘IO
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 20000 #最大连接数
user haproxy
group haproxy
spread-checks 3
nbproc 2
daemon

defaults
mode http
log global
retries 3
option redispatch
#contimeout 5000
#clitimeout 50000
#srvtimeout 50000

listen alex
bind 192.168.0.29:80 #绑定的IP地址。相当于后面real server的VIP
mode http #HTTP模式,也可以作为TCP的反向代理
stats hide-version
stats enable #开启WEB查看proxy的状态
stats uri /admin?stats #查看PROXY状态的URI
stats auth proxy:kobe24 #查看PROXY状态时登录录要用到的用户名和密码
option httpclose
option forwardfor #让后端的real server 的日志可以记录到CLIENT的IP
cookie SERVERID insert indirect
timeout server 15s
timeout connect 15s
timeout client 20s
#option httpchk HEAD /check.html HTTP/1.0
server www01 192.168.0.100:80 cookie A check port 80 inter 5000 fall 5
server www02 192.168.0.101:80 cookie B check port 80 inter 5000 fall 5
EOF

docker里的centos 安装sshd服务

1、 yum install openssh-server

2、启动sshd 报错 需要绝对路径

[root@0463226081ca src]# sshd     
sshd re-exec requires execution with an absolute path

3、用绝对路径 再次报错

[root@0463226081ca src]# /usr/sbin/sshd     
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.

4、执行 少什么生成什么key

ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

5、再次执行 成功

[root@0463226081ca src]# /usr/sbin/sshd
[root@0463226081ca src]# 

6、外部连接还是失败 修改/etc/sshd/sshd_config配置文件 重启再连 成功

UsePAM no

Centos安装SNIProxy

一、序

最近朋友给我介绍了一个黑科技玩法,需要用到sniproxy,这东西我之前就知道,当初拿来代理谷歌配合自建dns用,好久没用都忘记怎么装了……所以做个记录

二、正文

其实吧,作者的GITHUB(传送门)说的很清楚……然而我还是要水一篇233

yum install autoconf automake curl gettext-devel libev-devel pcre-devel perl pkgconfig rpm-build udns-devel
cd /opt
wget -O sniproxy-0.4.0.tar.gz https://github.com/dlundquist/sniproxy/archive/0.4.0.tar.gz
tar xzf sniproxy-0.4.0.tar.gz 
cd sniproxy-0.4.0
./autogen.sh && ./configure && make dist
rpmbuild --define "_sourcedir `pwd`" -ba redhat/sniproxy.spec
rpm -ivh ~/rpmbuild/RPMS/x86_64/sniproxy-0.4.0-1.el6.x86_64.rpm

这是制作RPM包的方法,属于一劳永逸的(要更新除外),对了,在autogen的过程中会报 debchange: command not found 请无视,这是debian的玩意

另外打包必须要yum安装libev-devel和udns-devel,然而make install安装的话我们可以选择编译版本的libev和udns(版本更新,强迫症福利)

cd /opt
wget http://dist.schmorp.de/libev/Attic/libev-4.22.tar.gz
tar xzf libev-4.22.tar.gz
cd libev-4.22
./configure
make
make install
echo -e '/usr/local/libn/usr/local/lib64' > /etc/ld.so.conf.d/local.conf
ldconfig 
cd /opt
wget http://www.corpit.ru/mjt/udns/udns-0.4.tar.gz
tar xzf udns-0.4.tar.gz
cd udns-0.4
./configure
make
cd ..
mv udns-0.4 /usr/local/udns
echo -e '/usr/local/udns' > /etc/ld.so.conf.d/udns.conf
ldconfig

到这里安装基本没啥问题了,至于配置的话,这东西配置相当简单,我给出一份GITHUB上的Example,相信各位一看就懂了

user daemon

pidfile /tmp/sniproxy.pid

error_log {
    syslog daemon
    priority notice
}

listener 127.0.0.1:443 {
    protocol tls
    table TableName

    # Specify a server to use if the initial client request doesn't contain
    # a hostname
    fallback 192.0.2.5:443
}

table TableName {
    # Match exact request hostnames
    example.com 192.0.2.10:4343
    # If port is not specified the listener port will be used
    example.net [2001:DB8::1:10]
    # Or use regular expression to match
    .*\.com    [2001:DB8::1:11]:443
    # Combining regular expression and wildcard will resolve the hostname
    # client requested and proxy to it
    .*\.edu    *:443
}

Centos下改变整个目录及文件大小写

改变指定文件夹下所有目录为小写

function change_path(){
count=`find $1 -type d|wc -l`
echo "开始更改目录,共有${count}个目录"
for i in $(seq 1 $count)
do
    ii=$(find $1 -type d|sed -n "${i}p")
    if [[ $ii == "./" ]]
    then
        continue
    else
        lower_file=$(echo ${ii}|tr 'A-Z' 'a-z')
        if [[ $ii == $lower_file ]]
        then
            continue
        else
            mv $ii $lower_file
        fi
    fi
done
echo "目录修改完成"
}
change_path

代码测试可以运行,但是如果遇到特殊字符,如 { } 或者目录名中有空格或者中文字符会失败,目前没有想到好的解决方法~~ 如有您有什么好的方法, 可以在下方评论~~~

CentOS 7下安装配置proftpd搭建ftp服务器

proftpd全称:Professional FTP daemon,是针对Wu-FTP的弱项而开发的,除了改进的安全性,还具备许多Wu-FTP没有的特点,能以Stand-alone、xinetd模式运行等。ProFTP已经成为继Wu-FTP之后最为流行的FTP服务器软件,越来越多的站点选用它构筑安全高效的FTP站点,ProFTP配置方便,并有MySQL和Quota模块可供选择,利用它们的完美结合可以实现非系统账号的管理和用户磁盘的限制。<摘抄百度百科>

本章通过下载源码的方式安装,可以到官网下载最新版本:http://www.proftpd.org/

1、首先安装lrzsz方便拷贝文件到要搭建服务器的linux路径下

yum install lrzsz

2、上传文件解压

将下载完成的proftpd-1.3.6.tar.gz文件上传到指定路径,使用tar zxvf proftpd-1.3.6.tar.gz解压

3、由于使用源码安装,所以需要安装gcc编译环境

yum install gcc gcc-c++ autoconf automake

3、配置并制定安装和配置文件路径

./configure --prefix=/usr/local/proftpd --sysconfdir=/usr/local/proftpd

4、安装

make&make install

5、修改配置文件,限制匿名用户可以上传下载,但是不能删除(本章暂不详细描述权限控制部分内容)

vi /usr/local/proftpd/proftpd.conf
# This is a basic ProFTPD configuration file (rename it to 
# 'proftpd.conf' for actual use.  It establishes a single server
# and a single anonymous login.  It assumes that you have a user/group
# "nobody" and "ftp" for normal operation and anon.

ServerName                      "Welcome to FTP Server"
ServerType                      standalone
DefaultServer                   on

# Port 21 is the standard FTP port.
Port                            21

# Don't use IPv6 support by default.
UseIPv6                         off

# Umask 022 is a good standard umask to prevent new dirs and files
# from being group and world writable.
Umask                           022

# To prevent DoS attacks, set the maximum number of child processes
# to 30.  If you need to allow more than 30 concurrent connections
# at once, simply increase this value.  Note that this ONLY works
# in standalone mode, in inetd mode you should use an inetd server
# that allows you to limit maximum number of processes per service
# (such as xinetd).
MaxInstances                    30

# Set the user and group under which the server will run.
User                            nobody
Group                           nobody

# To cause every FTP user to be "jailed" (chrooted) into their home
# directory, uncomment this line.
#DefaultRoot ~

# Normally, we want files to be overwriteable.
AllowOverwrite          on

IdentLookups            off
UseReverseDNS           off

# Bar use of SITE CHMOD by default
<Limit SITE_CHMOD>
  AllowAll
</Limit>

# A basic anonymous configuration, no upload directories.  If you do not
# want anonymous users, simply delete this entire <Anonymous> section.
<Anonymous /home>
  User                          ftp
  Group                         ftp

  # We want clients to be able to login with "anonymous" as well as "ftp"
  UserAlias                     anonymous ftp

  # Limit the maximum number of anonymous logins
  MaxClients                    50

  # We want 'welcome.msg' displayed at login, and '.message' displayed
  # in each newly chdired directory.
  DisplayLogin                  welcome.msg
  DisplayChdir                  .message

  # Limit WRITE everywhere in the anonymous chroot
  <Limit DELE RMD>
    DenyAll
  </Limit>
</Anonymous>

6、关闭防火墙

systemctl stop firewalld.service

systemctl disable firewalld.service

7、启动服务器

/usr/local/proftpd/sbin/proftpd

查看进程是否已经启动

[root@localhost ~]# ps -ef |grep proftpd
nobody    1140    1  0 12:13 ?        00:00:00 proftpd: (accepting connections)
root      3182  2270  0 12:50 pts/0    00:00:00 grep --color=auto proftpd  

8、添加到启动项避免每次重启后都需要手动启动

vi /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/usr/local/proftpd/sbin/proftpd

由于在CentOS7中/etc/rc.d/rc.local的权限被降低了,所以需要加上可执行的权限:

chmod +x /etc/rc.d/rc.local

Centos硬盘挂载

查看系统有几块硬盘

fdisk -l

找到需要格式化的硬盘

mkfs.xfs -f /dev/vdb

挂载硬盘

新建一个挂载点

mkdir /data

执行以下命令进行挂载:

mount -t xfs /dev/vdb /data/

最后执行以下命令查看挂载是否成功

df -h
/dev/vda1 50G 18G 30G 38% /
/dev/vdb 50G 33M 50G 1% /data