原文来自:http://www.cnblogs.com/shantu/p/4589822.html
因公司数据安全和分析的需要,故调研了一下 GlusterFS + lagstash + elasticsearch + kibana 3 + redis 整合在一起的日志管理应用:
安装,配置过程,使用情况等续
一,glusterfs分布式文件系统部署: 说明: 公司想做网站业务日志及系统日志统一收集和管理,经过对mfs, fastdfs 等分布式文件系统的调研,最后选择了 glusterfs,因为Gluster具有高扩展性、高性能、高可用性、可横向扩展的弹性特点,无元数据服务器设计使glusterfs没有单点故障隐 患,官网:www.gluster.org
1. 系统环境准备:
Centos 6.4 服务端: 192.168.10.101 192.168.10.102 192.168.10.188 192.168.10.189 客户端: 192.168.10.103 EPEL源和GlusterFS源 增加EPEL源和GLusterFS源,EPEL源中包含GLusterFS,版本比较旧,相对稳定,本次测试采用最新的3.5.0版本。
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget -P /etc/yum.repos.dhttp://download.gluster.org/pub/gluster/glusterfs/LATEST/CentOS/glusterfs-epel.repo
2. 部署过程
服务端安装:
yum -y install glusterfs glusterfs-fuseglusterfs-server
chkconfig glusterd on
service glusterd start
服务端配置:
将4个存储节点组成一集群,本文在第一个节点执行,只需要在任意节点执行就ok。
[root@db1 ~]# gluster peer probe192.168.10.102
probe successful
[root@db1 ~]# gluster peer probe192.168.10.188
probe successful
[root@db1 ~]# gluster peer probe 192.168.10.189
probe successful
查看集群的节点信息:
[root@db1 ~]# gluster peer status
number of peers: 3
hostname: 192.168.10.102
uuid:b9437089-b2a1-4848-af2a-395f702adce8
state: peer in cluster (connected)
hostname: 192.168.10.188
uuid: ce51e66f-7509-4995-9531-4c1a7dbc2893
state: peer in cluster (connected)
hostname: 192.168.10.189
uuid:66d7fd67-e667-4f9b-a456-4f37bcecab29
state: peer in cluster (connected)
以/data/gluster为共享目录,创建名为test-volume的卷,副本数为2:
sh cmd.sh "mkdir /data/gluster"
[root@db1 ~]# gluster volume create test-volume replica 2192.168.10.101:/data/gluster 192.168.10.102:/data/gluster192.168.10.188:/data/gluster 192.168.10.189:/data/gluster
creation of volume test-volume has beensuccessful. please start the volume to access data.
启动卷:
[root@db1 ~]# gluster volume starttest-volume
starting volume test-volume has beensuccessful
查看卷状态:
[root@db1 ~]# gluster volume info
volume name: test-volume
type: distributed-replicate
status: started
number of bricks: 2 x 2 = 4
transport-type: tcp
bricks:
brick1: 192.168.10.101:/data/gluster
brick2: 192.168.10.102:/data/gluster
brick3: 192.168.10.188:/data/gluster
brick4: 192.168.10.189:/data/gluster
3. 客户端安装配置:
安装:
yum -y installglusterfs glusterfs-fuse
挂载:
mount -t glusterfs 192.168.10.102:/test-volume/mnt/ (挂载任意一个节点即可)推荐用这种方式。
mount -t nfs -o mountproto=tcp,vers=3192.168.10.102:/test-volume /log/mnt/ (使用nfs挂载,注意远端的rpcbind服务必须开启)
echo "192.168.10.102:/test-volume/mnt/ glusterfs defaults,_netdev 0 0" >> /etc/fstab (开机自动挂载)
4. 测试
检查文件正确性
dd if=/dev/zero of=/mnt/1.img bs=1Mcount=1000 # 在挂载客户端生成测试文件
cp /data/navy /mnt/ # 文件拷贝到存储上
宕机测试。
使用glusterfs-fuse挂载,即使目标服务器故障,也完全不影响使用。用NFS则要注意挂载选项,否则服务端故障容易导致文件系统halt住而影响服务!
# 将其中一个节点停止存储服务service glusterd stop
service glusterfsd stop# 在挂载客户端删除测试文件
rm -fv /mnt/navy# 此时在服务端查看,服务被停止的节点上navy并未被删除。此时启动服务:serviceglusterd start# 数秒后,navy就被自动删除了。新增文件效果相同!
5. 运维常用命令:
删除卷
gluster volume stop test-volume
gluster volume delete test-volume
将机器移出集群
gluster peer detach 192.168.10.102
只允许172.28.0.0的网络访问glusterfs
gluster volume set test-volumeauth.allow 192.168.10.*
加入新的机器并添加到卷里(由于副本数设置为2,至少要添加2(4、6、8..)台机器)
gluster peer probe 192.168.10.105
gluster peer probe 192.168.10.106
gluster volume add-brick test-volume192.168.10.105:/data/gluster 192.168.10.106:/data/gluster
收缩卷
# 收缩卷前gluster需要先移动数据到其他位置
gluster volume remove-brick test-volume192.168.10.101:/data/gluster/test-volume192.168.10.102:/data/gluster/test-volume start
# 查看迁移状态
gluster volume remove-brick test-volume192.168.10.101:/data/gluster/test-volume192.168.10.102:/data/gluster/test-volume status
# 迁移完成后提交
gluster volume remove-brick test-volume192.168.10.101:/data/gluster/test-volume192.168.10.102:/data/gluster/test-volume commit
迁移卷
# 将192.168.10.101的数据迁移到,先将192.168.10.107加入集群
gluster peer probe 192.168.10.107
gluster volume replace-bricktest-volume 192.168.10.101:/data/gluster/test-volume192.168.10.107:/data/gluster/test-volume start
# 查看迁移状态gluster volume replace-brick test-volume192.168.10.101:/data/gluster/test-volume192.168.10.107:/data/gluster/test-volume status
# 数据迁移完毕后提交gluster volume replace-brick test-volume192.168.10.101:/data/gluster/test-volume192.168.10.107:/data/gluster/test-volume commit
# 如果机器192.168.10.101出现故障已经不能运行,执行强制提交然后要求gluster马上执行一次同步
gluster volume replace-bricktest-volume 192.168.10.101:/data/gluster/test-volume192.168.10.102:/data/gluster/test-volume commit -force
gluster volume heal test-volumes full
24007
二.日志收集系统部署
说明简解:
系统各部分应用介绍
Logstash:做系统log收集,转载的工具。同时集成各类日志插件,对日志查询和分析的效率有很大的帮助.一般使用shipper作为log收集、indexer作为log转载.
Logstash shipper收集log 并将log转发给redis 存储
Logstash indexer从redis中读取数据并转发给elasticsearch
redis:是一个db,logstash shipper将log转发到redis数据库中存储。Logstash indexer从redis中读取数据并转发给elasticsearch。
kafka:这里我们把redis换作用kafka,主要用于处理活跃的流式数据,高吞吐率,显式分布式,支持数据并行加载
Elasticsearch:开源的搜索引擎框架,前期部署简单,使用也简单,但后期需要做必要的优化具体请参照博客http://chenlinux.com/categories.html#logstash-ref 中logstash部分.可进行多数据集群,提高效率。从redis中读取数据,并转发到kibana中
Kibana: 开源web展现。
日志收集系统架构图:
虚拟服务器准备:
192.168.10.143 logstash shipper 日志数据生产端
192.168.10.144 logstash indexer kafka 日志消费端将日志写入elasticsearch集群
192.168.10.145 elasticsearch-node1 kibana3 kibana展显elasticsearch中的数据
192.168.10.146 elasticsearch-node2
1.三台主机都要安装jdk 1.7 推荐oracle jdk 1.7+版本 java -version 设置java的环境变量,比如
vim ~/.bashrc
>>
JAVA_HOME=/usr/java/jdk1.7.0_55
PATH=$PATH:/$JAVA_HOME/bin
CLASSPATH=.:$JAVA_HOME/lib
JRE_HOME=$JAVA_HOME/jre
export JAVA_HOME PATH CLASSPATH JRE_HOME
>>
source ~/.bashrc
2.安装kafka(192.168.10.144) wget http://mirrors.hust.edu.cn/apache/kafka/0.8.1.1/kafka2.9.2-0.8.1.1.tgz tar zxvf kafka2.9.2-0.8.1.1.tgz ln –s kafka_2.9.2-0.8.1.1 /usr/local/kafka vim /usr/local/kafka/config/server.properties broker.id=10-144 host.name=kafka-10-144 echo “192.168.10.144 kafka-10-144” >> /etc/hosts 注意kafka启动依懒于zookeeper,需要安装一下zookeeper-server cdh5.2源上有,配置好yum源即可 yum install zookeeper-server –y vim /etc/zookeeper/conf/zoo.cfg dataDir=/data/zookeeper 配置一下zookeeper 数据存放路径 启动zookeeper和kafka /etc/init.d/zookeeper-server start nohup /usr/local/kafka/bin/kafka-server-start.sh /usr/local/kafka/config/server.properties &
3.安装Elasticsearch(192.168.10.145)
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.1.tar.gz
elasticsearch解压即可使用非常方便,接下来我们看一下效果,首先启动ES服务,切换到elasticsearch目录,运行bin下的elasticsearch
tar zxvf elasticsearch-1.4.1.tar.gz
ln –s elasticsearch-1.4.1 /usr/local/es
cd /usr/local/es/
vim config/elasticsearch.yml 添加如下配置,要不然kibana调es时会报错(es 1.4 和kibana 3.1.2版本的问题)
cluster.name: elasticsearch 打开前面注释
node.name: "G1-logs-es02" 打开前面注释,根据主机名配置,做集群
http.cors.enabled: true
http.cors.allow-origin: "*"
nohup bin/elasticsearch &
访问默认的9200端口
curl -X GET http://localhost:9200
Elasticsearch(192.168.10.146)的安装同上:
4.安装logstash (192.168.10.143 ,192.168.10.144)生产端和消费端都要安装 这里参考了 http://blog.csdn.net/xzwdev/article/details/41278033 https://github.com/joekiller/logstash-kafka git clone https://github.com/joekiller/logstash-kafka
cd /usr/local/src/logstash-kafka
make tarball 编译kakfa支持logstash 时间比较长,大概两个小时
会在/usr/local/src/logstash-kafka/build/ 目录生成logstash-1.4.2.tar.gz 文件,
logstash-1.4.2.tar.gz文件后面会在日志生产端和消费日志传输用,
生产日志端配置并启动(192.168.10.143)
配置收集haproxy日志示例:
tar zxvf logstash-1.4.2.tar.gz
ln –s logstash-1.4.2 /usr/local/logstash
vim /usr/local/logstash/conf/ logstash_shipper_haproxy.conf
input{
file{
path => "/data/application/haproxy-1.4.18/logs/haproxy.log" 指定所收集的日志文件路径
type => "haproxylog" 日志所属业务服务名称
}
}
output{
kafka{
broker_list => "192.168.10.144:9092" kafka服务地址
topic_id => "logstash-haproxylog" 标识消费端取日志的id
}
}
启动生产端日志收集服务
nohup /usr/local/logstash/bin/logstash –f /usr/local/logstash/conf/ logstash_shipper_haproxy.conf &
消费端服务器配置:
tar zxvf logstash-1.4.2.tar.gz
ln –s logstash-1.4.2 /usr/local/logstash
vim /usr/local/logstash/consumer_conf/logstash_haproxylog_es.conf
input{
kafka{
zk_connect => "192.168.10.144:2181"
group_id => 'logstash-haproxylog'
topic_id => 'logstash-haproxylog'
}
}
output{
elasticsearch{
host => "192.168.10.145"
port => "9300"
index => "haproxy-5-13-%{+YYYY.MM.dd}"
}
}
启动消费端服务
nohup /usr/local/logstash/bin/logstash –f /usr/local/logstash/consumer_conf/logstash_haproxylog_es.conf &
优化补充:
1,es中建立不同业务的索引,需要做if判断,这里是在logstash消费端配置的
如:
input{
kafka{
zk_connect => "192.168.35.130:2181"
group_id => "g1.api.test.com"
topic_id => 'g1.api.test.com'
}
kafka{
zk_connect => "192.168.35.130:2181"
group_id => "go.clientinner.test.com"
topic_id => "go.clientinner.test.com"
}
kafka{
zk_connect => "192.168.35.130:2181"
group_id => "api7.mobile.test.com_app"
topic_id => "api7.mobile.test.com_app"
}
}
filter {
ruby {
init => "@kname = ['time','uid','ip','uname','stime','etime','exec_time','url','ua','module','response_status','http_status','query_string']"
code => "event.append(Hash[@kname.zip(event['message'].split('|'))])"
}
mutate {
convert => ["exec_time", "float"]
}
geoip {
database => "/data/application/logstash/patterns/GeoLiteCity.dat"
source => "ip"
fields => ["country_name","city_name"]
}
useragent {
source => "ua"
target => "useragent"
}
}
output{
if [type] == "go.clientinner.test.com"{
elasticsearch{
template => "/usr/local/logstash/conf/logstash.json"
template_overwrite => true #修改url 不分词的属性,
host => "192.168.35.131"
port => "9300"
index => "go.clientinner.test.com-%{+YYYY.MM.dd}"
}
} else if [type] == "g1.api.test.com"{
elasticsearch{
template => "/usr/local/logstash/conf/logstash.json"
template_overwrite => true
host => "192.168.35.131"
port => "9300"
index => "g1.api.test.com-%{+YYYY.MM.dd}"
}
}else if [type] == "api7.mobile.test.com_app"{
elasticsearch{
template => "/usr/local/logstash/conf/logstash.json"
template_overwrite => true
host => "192.168.35.131"
port => "9300"
index => "api7.mobile.test.com_app-%{+YYYY.MM.dd}"
}
}
}
2,logstash 往es存数据,每天建立时间索引,默认会用utc时间,是早上8点才建立,造成当前的日志数据存入昨天的索引中,需要做如下修改:
修改logstash/lib/logstash/event.rb 可以解决这个问题
第226行
.withZone(org.joda.time.DateTimeZone::UTC)
修改为
.withZone(org.joda.time.DateTimeZone.getDefault())
5 安装kibana(192.168.10.145)
logstash的最新版已经内置kibana,你也可以单独部署kibana。kibana3是纯粹JavaScript+html的客户端,所以可以部署到任意http服务器上。
wget http://download.elasticsearch.org/kibana/kibana/kibana-latest.zip
unzip kibana-latest.zip
cp -r kibana-latest /var/www/html
可以修改config.js来配置elasticsearch的地址和索引。
修改以下行。
elasticsearch: "http://192.168.10.145:9200",
6,最终实现如下图:
7,日志系统维护:
1,elsasticsearch 集群扩展
这里主要说给新添加一下es新节点
Es节点的elsasticsearch安装参考上面,
添加新节点前,需要执行以下命令
1) 先暂停集群的shard自动均衡:
在主节点上
curl -XPUT http://192.168.35.131:9200/_cluster/settings -d '{"transient" : {"cluster.routing.allocation.enable" : "none"}}'
关闭其它节点和主节点:如下:
curl -XPOST http://192.168.35.132:9200/_cluster/nodes/_local/_shutdown
curl -XPOST http://192.168.35.131:9200/_cluster/nodes/_local/_shutdown
2) 启动主节点,和其它从节点,
3) 添加新节点,启动和配置参考其它从节点
2,kafka+zookeeper集群扩展:
安装包:kafka_2.9.2-0.8.1.1.tgz elasticsearch-1.4.1.tar.gz
Kafka+zookeeper配置如下:
Cat kafka/config/server.properties 主要配置
broker.id=35125
host.name=192.168.35.125
advertised.host.name=192.168.35.125
log.dirs=/data/kafka-logs
zookeeper.connect=192.168.35.130:2181,192.168.35.124:2181,192.168.35.125:2181
cat /etc/zookeeper/conf/zoo.cfg
dataDir=/data/zookeeper 节点信息存放目录
clientPort=2181
# zookeeper cluster
server.35130=G1-logs-kafka:2888:3888
server.35124=bj03-bi-pro-tom01:2888:3888
server.35125=bj03-bi-pro-tom02:2888:3888
启动服务
Chown zookeeper.zookeeper /data/zookeeper -R
/etc/init.d/zookeeper-server init 第一次启动时需要初始化/data/zookeeper目录
Echo “35130” > /data/zookeeper/myid
Chown zookeeper.zookeeper /data/zookeeper -R
/etc/init.d/zookeeper-server start 先启动zookeeper
nohup ./bin/kafka-server-start.sh config/server.properties > /data/kafka-logs/kafka.log & 再启动kafka
8,kibana登录认证安装配置:
应用说明:
Nginx: 记录日志,做es的反向代理
Nodejs: 跑 kibana-authentication-proxy
Kibana: 把原来kibana目录链接到kibana-authentication-proxy 下
kibana-authentication-proxy:用户认证和代理请求es,
8.1,nginx安装配置 #wget http://nginx.org/download/nginx-1.2.9.tar.gz # yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel #tar zxvf nginx-1.2.9.tar.gz # cd nginx-1.2.9 # ./configure --prefix=/usr/local/nginx #make && make install
配置如下:
#cat /usr/local/nginx/conf/nginx.conf
user web;
worker_processes 4;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
upstream kianaca {
server 192.168.35.131:9200 fail_timeout=30s;
server 192.168.35.132:9200 fail_timeout=30s;
server 192.168.35.125:9200 fail_timeout=30s;
}
server {
listen 8080;
server_name 192.168.35.131;
location / {
root /var/www/html/kibana-proxy;
index index.html index.htm;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_pass http://kianaca;
proxy_set_header Host lashou.log.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html/kibana-proxy;
}
}
}
# /usr/local/nginx/sbin/nginx -t
#/usr/local/nginx/sbin/nginx
8.2 安装kibana-authentication-proxy
#cd /var/www/html/
#git clone https://github.com/wangganyu188/kibana-authentication-proxy.git
#mv kibana-authentication-proxy kibana-proxy
# cd kibana-proxy
#yum install npm
#npm install express
#git submodule init
#npm install
#node app.js
配置 kibana-proxy/config.js
可能有如下参数需要调整:
es_host #这里是nginx地址
es_port #nginx的8080
listen_port #node的监听端口, 9201
listen_host #node的绑定IP, 可以0.0.0.0
cas_server_url
8.3请求路径
node(9201) <=> nginx(8080) <=> es(9200)