monit 在 centos 7 和debian 8, debian 9下的使用

在老的centos 6 和 debian 7 中,monit 是通过对 pid 的监控来判断程序是否die 或者有问题的, 但是在 centos 7 和 debian 8 和debian 9下,只要service 不是以forking 的形式启动,systemd 就不会让service 创建 pid file,即使你在service 的配置文件中创建了 PidFile 的命令,这个命令是会被忽略的

因为在由systemd 控制的linux 系统中,monit 无法通过pid 的形式来监控程序,在这里我们就需要用到monit 的的match 命令,比如说nginx 来可以match ‘nginx’, 来完成对nginx 的监控

Unix 守护进程

守护进程是在后台运行不受终端控制的进程(如输入、输出等),一般的网络服务都是以守护进程的方式运行。守护进程脱离终端的主要原因有两点:(1)用来启动守护进程的终端在启动守护进程之后,需要执行其他任务。(2)(如其他用户登录该终端后,以前的守护进程的错误信息不应出现)由终端上的一些键所产生的信号(如中断信号),不应对以前从该终端上启动的任何守护进程造成影响。要注意守护进程与后台运行程序(即加&启动的程序)的区别。

更多

service在centos6,debian7 和centos7,debian8 的不同

我是一个很老套的人,一直在坚持使用centos 6 和 debian 7,但是无奈很多软件已经取消了对centos 6 和debian 8 的支持,现在只能加班加点开始适应debian 8 和 centos 7.

在centos 6 和debian 7中,脚本一直使用的是/etc/init.d/xxx, 在系统升级到Centos 7 和 Debian 8后,虽然以前的启动脚本仍然可以使用,但是在centos 7 和 debian8中, 系统的service 已经由sysvinit 转向了systemd,进而也有了很多的变化

CentOS 7 集成了 RHEL 7 的新特性,systemd的使用也大幅提高了系统服务的运行效率,同时也变的简单而易用了许多.

Unit 的文件位置,一般主要有三个目录:

/lib/systemd/system
/run/systemd/system
/etc/systemd/system

这三个目录的配置文件优先级一次从低到高,如果同意选项三个地方都配置了,优先级高的会覆盖优先级低的. 

对于第三方或者自己定义的service 来说,我们一般放在/etc/systemd/service下面。

更多

Linode 1GB Benchmark

这是新开的一台DNS 的服务器,正好拿来测试比较一下

 

Benchmark started on Mon Jul 17 13:19:13 UTC 2017
Full benchmark log: /root/bench.log

System Info
-----------
Processor : Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz
CPU Cores : 1
Frequency : 2799.988 MHz
Memory : 988 MB
Swap : MB
Uptime : 7 min,

OS : \S
Arch : x86_64 (64 Bit)
Kernel : 4.9.15-x86_64-linode81
Hostname : xx.xxxx.xxx



Speedtest (IPv4 only)
---------------------
Your public IPv4 is 162.xx.xx.xxx

Location Provider Speed
CDN Cachefly 105MB/s

Atlanta, GA, US Coloat 49.0MB/s
Dallas, TX, US Softlayer 27.7MB/s
Seattle, WA, US Softlayer 18.8MB/s
San Jose, CA, US Softlayer 12.4MB/s
Washington, DC, US Softlayer 50.1MB/s

Tokyo, Japan Linode 10.9MB/s
Singapore Softlayer 3.64MB/s

Rotterdam, Netherlands id3.net 6.50MB/s
Haarlem, Netherlands Leaseweb 73.5MB/s



Disk Speed
----------
I/O (1st run) : 210 MB/s
I/O (2nd run) : 611 MB/s
I/O (3rd run) : 607 MB/s
Average I/O : 476 MB/s

CentOS 上使用 NFS 的教程以及介绍

NFS 可以把NFS server 上的文件及文件共享给NFS client 服务器

两个概念,NFS Server(Master) 和 NFS Client

NFS Server 可以把本服务器上的文件和文件夹共享给NFS Client, NFS Client 可以在共享的文件和文件夹上进行读写操作,并且实时返回给NFS Master

NFS Server 一般作为存储服务器,常见为使用RAID 5 和 RAID 10 的存储服务器,容量比较大. 对于一些图片站和网盘站点,网站所在的服务器一般为NFS Client,NFS Client 把图片和文件直接存储在NFS Server上

为了方便对NFS进行user id 和 group id 进行统一管理,我们一般需要NFS Server 和 NFS Client 使用同一种操作系统, 比如说centos 和 centos. 当然了,分别使用不同的操作系统,比如说centos 和 debian 也是可以的,就是有点麻烦而已.

下面进行详细的安装步骤:

无论是CentOS 6 和 CentOS 7, 安装步骤基本都一致,这里推荐CentOS 7. RHEL系的操作系统越新越好用

服务器配置:

NFS Server Hostname: server.unixmen.local
NFS Server IP Address: 192.168.1.101/24

 

NFS Client Hostname: client.unixmen.local
NFS Client IP Address: 192.168.1.102/24

NFS服务器端配置:

CentOS 6:

yum install nfs-utils nfs-utils-lib

chkconfig nfs on 
service rpcbind start
service nfs start

CentOS 7:

yum install nfs-utils nfs-utils-lib

systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap

systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap

因为CentOS 7 在系统上改了好多,所以安装稍微有点不同,其实都是同一个库

下面配置共享目录,假设我们需要共享/home 目录给client

vi /etc/exports

然后添加下面:

/home  192.168.0.102 (rw,sync,no_root_squash,no_subtree_check)

参数的解释:

rw: 这个选项允许client server 在共享目录上进行读和写的操作

ro: 只读

sync: 将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;

no_root_squash: 登入 NFS 主机使用分享目录的使用者,如果是 root 的话,那么对于这个分享的目录来说,他就具有 root 的权限!

root_squash: 在登入 NFS 主机使用分享之目录的使用者如果是 root 时,那么这个使用者的权限将被压缩成为匿名使用者,通常他的 UID 与 GID 都会变成 nobody 那个系统账号的身份

all_squash: 将远程访问的所有普通用户及所属组都映射为匿名用户或用户组 (nfsnobody)

no_all_squash: 与all_squash 相反, 默认配置

anonuid=xxx: 将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);
anongid=xxx: 将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

这里需要注意的, NFS 进行映射的时候,用的是uid 和 gid, 而非是用户名和group 的名字. 所以通常的做法是, 在NFS server 和 NFS client 上使用相同的用户名和用户组,以及相同UID和 GID

相同的用户名和用户组这个好解决, UID 和 GID不同的话,需要update uid 和 gid以使他们相同. 在centos 上我们经常使用军哥的lnmp,用户名和用户组都为www,加入uid 为501, gid 为501, 则我们可以用

(rw, sync, no_subtree_check, all_squash, anonuid=501, anongid=501)

async: 将数据先保存在内存缓冲区中,必要时才写入磁盘;

subtree: 若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

配置完成以后,我们需要export这个配置来时他生效:

exportfs -a

下面我们需要配置NFS Client

安装NFS:

yum install nfs-utils nfs-utils-lib

mount 共享目录给 /home

mount -t nfs 192.168.1.101:/home /home

然后我们就可以通过df -h 看到mount 的目录或者mount 来查看

如果需要开机启动时自动mount,我们需要编辑/etc/fstab

192.168.1.101:/home /home nfs rw,sync,hard,intr 0 0

移除NFS mount

umount /home

MySql 的 ONLY_FULL_GROUP_BY

最近把mysql 升级到了5.7.18 然后就发现了一个很明显的变化, ONLY_FULL_GROUP_BY 这个mysql mode 被加了进来, 按照sql 99 的标准这是好事.

但是实际上, mysql 的这个版本只能说almost identical or very close to, 不能说完全相等

简单的说,就是

That is, MySQL 5.7.5m15 will by default reject only those GROUP 
BY-queries that include non-aggregated expressions in the SELECT-list that 
are not functionally dependent upon the GROUP BY-list.

This not only means that you cannot mess up your GROUP BY-queries anymore 
(as MySQL will now reject an improper GROUP BY query), 
it will also not require you to write non-sensical "dummy" aggregates
 over expressions that can only have one value per aggregated 
result row. Hurrah!

详细的解释请看这篇文章:

http://rpbouman.blogspot.nl/2014/09/mysql-575-group-by-respects-functional.html

对于iptables 的一点理解

这是无意中在stackoverflow上看到的,以前还真没有注意到.

Question:

I was trying to block an ip of ( network stresser tool – a ddos websites selling ddos bots by seconds online ) the first one i was trying to block with iptables :

iptables -A INPUT -d 173.193.26.73 -j DROP
iptables -A INPUT -s 173.193.26.73 -j DROP
however i still see the ip and bandwidth still up on iftop. also still see the ip going on tcpdump.

is that normal? what’s the problem in here?

 

Answer: 

Yes, it’s normal. iptables doesn’t stop inbound traffic turning up on your physical interface, thus notching up your iftop counts, and it doesn’t stop the kernel seeing that it’s there, hence the tcpdump output. It does, however, stop the kernel passing the traffic on to anything that might be listening for it.

If you see any output traffic in response to those input packets, something’s wrong. But otherwise, no, that’s normal.

iptables 工作在5个链上, prerouting, input, forward, output, postrouting

prerouting 和 postrouting 一般是在NAT上使用,所以traffic 会流经CPU,也即是说iptables 无法阻止真实网卡的流入流量, 因此itfop 和 tcpdump 都能看到流入的流量,但是在用iptables block了这个IP的情况下,是不会有流出的流量的.

 

简单的防DDos和CC attack 的一点补救措施

其实这些措施也基本没有什么大用,在代理IP 足够多的情况下只能依靠硬件去防守,但是聊胜于无

首先可以看下hivelocity 的一篇文章

https://www.hivelocity.net/kb/how-to-check-if-your-linux-server-is-under-ddos-attack/

1 安装CSF, 这个应该在我的所有服务器上都有安装

2 check the number of active connections sorted by asc

netstat -n | grep ':80' | awk -F' ' '{print $5}' | awk -F':' '{print$1}' | sort | uniq -c | sort -n

By Desc:

netstat -n | grep ':80' | awk -F' ' '{print $5}' | awk -F':' '{print$1}' | sort | uniq -c | sort -n -r

3 install iftop

yum install epel-release -y

yum install iftop -y

iftop -n

 

4 fail2ban 在一定情况下还是有作用的

5 Check average single php-fpm instance memory usage:

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

Online.net Xenserver 使用RPN-SAN的诸多问题

Online.net 的系统本身就有很多问题,加上XenServer 以后问题就更多了,再加上RPN – SAN 以后,那问题就更更更多了。。。

为什么要是用RPN-SAN? 

因为online.net 的服务器很多都是纯SSD 服务器,如果我们需要放置大量的图片的话,显然是不够的,这个时候RPN-SAN 就派上用场了, 在纯dedicated server上使用SAN,那是很容易的事情,直接挂载就可以了,在XenServer上挂载iSCSI 的 SAN,则稍微麻烦点, 有几点注意事项:

  1. XenServer 7 是建立在CentOS 7 上面的, iSCSI用的port 是3260, 所以需要在防火墙上打开3260
  2. Online.net 的RPN-SAN 用的是内网,位于 10.88.0.0/14. 如果你的online.net 的服务器是用的IPMI 安装的系统,那么你需要把这台服务器自带的Private IP 配置上. 关于private ip 的 netmask,和gateway 可以先用DHCP 激活,然后自己记录下来,稍后你可以一直使用DHCP,或者按照自己的意愿使用static
  3. 在购买完了Online.net 的 RPN-SAN以后,你需要在此RPN-SAN的管理页面,给使用此RPN-SAN 的dedicated server授权,这是从security 的角度考虑
  4. 于此同时,你还需要在RPN Group里面,把RPN-SAN 和使用此SAN 的服务器放置于同一个Group里面
  5. 一般来说,RPN-SAN 和 服务器的Private IP 需要在一个网段,我们这里可以使用10.88.0.0/14 或者10.90.0.0/15
  6. XenServer 需要手动添加一条面向private network 10.88.0.0/14 或者10.90.0.0/15的 route,这样是他们互相通信,为了方便,此文下面假设我们使用的是10.90.0.0/15这个网段
  7. XenServer 通过xapi 添加static route 的办法:
xe network-list
xe network-param-set uuid=<UUID> other-config:static-routes=10.90.0.0/15/10.90.23.1

在这命令里面,我们的网卡为xenbr2,IP 为10.90.23.44, Gateway 为10.90.23.1

运行完这条命令, 如果我们使用route -n 来查看route, 会发现没有任何变化, 这是因为我们需要重启系统或者运行xe-toolstack-restart 来使新的配置生效

添加static route 很简单,但是删除就比较麻烦了. 删除的话,需要一次性删除所有的static route,然后再添加我们认为必须的route.

删除所有static route:

xe network-param-remove uuid={network UUID} param-name=other-config param-key=static-routes

If there were some needed static route, add them back, for example:

xe network-param-set uuid={network UUID} other-config:static-routes=192.168.98.0/24/192.168.1.1,192.168.99.0/24/192.168.1.1

 

After that, reboot to make them take effect.

对于storage来说,我们经常会把NIC 的MTU 改为9000, steps to modify MTU in Senserver:

  1. shutdown guests
  2. reconfigure network

xe network-param-set uuid=<network-uuid> MTU=9000

      3. reboot hosts

       4. verify proper MTU sizing