docker水系~微服务部署运维

Docker和ELK搭建日志平台

2017-01-16  本文已影响7026人  灼灼2015

Elasticsearch, Logstash 和 Kibana 简称ELK,是日志平台。
用途:Bug定位,历史统计。
自从有了Dokcer 什么都想往里面放来着。

1. 规划

从N台应用服务器上收集localhost_access的日志,通过Logstash做数据加工处理(filter),使用Elasticsearch保存数据建立索引,最后通过Kibana 图形界面-来使用日志信息。 ELK.jpg

2. 基础镜像

因ELK的启动需要用到jdk,不想每个docker都重复配置jdk,提前准备elk的基础镜像

FROM centos:latest
MAINTAINER chances-lixr
VOLUME [ "/opt/product/data/" ]
RUN  /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN /bin/echo -e "ZONE="Asia/Shanghai"\nUTC=false\nRTC=false" > /etc/sysconfig/clock
RUN mkdir /opt/product/tools/
ADD ./tools /opt/product/tools/
ENV JAVA_HOME /opt/product/tools/jdk1.8.0_51
CMD ["/usr/sbin/init"]

3. Elasticsearch 配置

ELK的官方网站 https://www.elastic.co

FROM elkbase:v1.0
MAINTAINER chances-lixr
VOLUME [ "/opt/product/data/" ]
ADD ./elk /opt/product/
RUN useradd elk && chown -R elk:elk /opt/product/elasticsearch-5.1.2
ADD all.sh /root/
RUN chmod +x /root/all.sh
EXPOSE 9200
EXPOSE 9300
ENTRYPOINT ["/root/all.sh"]
CMD ["/usr/sbin/init"]
#!/bin/bash
echo "* soft nofile 65536" > /etc/security/limits.conf 
echo "* hard nofile 131072" > /etc/security/limits.conf
echo "* soft nproc 2048" > /etc/security/limits.conf
echo "* hard nproc 4096" > /etc/security/limits.conf
echo "vm.max_map_count=655360" > /etc/sysctl.conf 
sysctl -p
cd /opt/product/elasticsearch-5.1.2/config/
rm -rf elasticsearch.yml
cp /opt/product/data/elk/elasticsearch.yml .
chown -R elk:elk /opt/product/elasticsearch-5.1.2
chown -R elk:elk /opt/product/data/elk/elasticsearchdata
su - elk <<!
export JAVA_HOME=/opt/product/tools/jdk1.8.0_51
export PATH=$JAVA_HOME/bin:$PATH
/opt/product/elasticsearch-5.1.2/bin/elasticsearch 
docker run --privileged --restart=always -d -ti -v /opt/product/data:/opt/product/data -p 9200:9200 -p 9300:9300 elasticsearch:v1.0 /bin/bash

4. Logstash 配置

FROM elkbase:v1.0
MAINTAINER chances-lixr
VOLUME [ "/opt/product/data/" ]
ADD ./elk /opt/product/
ADD all.sh /root/
RUN chmod +x /root/all.sh
EXPOSE 5044
EXPOSE 4560
EXPOSE 8080
ENTRYPOINT ["/root/all.sh"]
CMD ["/usr/sbin/init"]
#!/bin/bash
export JAVA_HOME=/opt/product/tools/jdk1.8.0_51
export PATH=$JAVA_HOME/bin:$PATH
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8  -Duser.timezone=GMT+08"
cd /opt/product/logstash-5.1.2/config/
rm -rf logstash.yml
cp /opt/product/data/elk/logstash.yml logstash.yml
/opt/product/logstash-5.1.2/bin/logstash -f /opt/product/data/elk/logstash.conf
input {     
   beats {
    port => "5044"
   }
}
output {
   elasticsearch {
       hosts => ["elasticsearch的ip:9200"]
       index => "logstash-tomcat-accesslog-%{+YYYY.MM.dd}"
    }       
}

logstash.yml

将logstash本身的logstash.yml 拷贝到/opt/product/data/elk目录下
在/opt/product/data/elk目录下建立 logstashdata目录

docker run  --restart=always -d -ti -v /opt/product/data:/opt/product/data -p 5044:5044 -p 4560:4560 -p 18080:8080 logstash:v1.0 /bin/bash

5. kibana 配置

FROM elkbase:v1.0
MAINTAINER chances-lixr
VOLUME [ "/opt/product/data/" ]
ADD ./elk /opt/product/
ADD all.sh /root/
RUN chmod +x /root/all.sh
EXPOSE 5601
ENTRYPOINT ["/root/all.sh"]
CMD ["/usr/sbin/init"]
#!/bin/bash 
export JAVA_HOME=/opt/product/tools/jdk1.8.0_51
export PATH=$JAVA_HOME/bin:$PATH
cd /opt/product/kibana-5.1.2/config/
rm -rf kibana.yml
ln -s /opt/product/data/elk/kibana.yml . 
cd /opt/product/kibana-5.1.2/
rm -rf data
ln -s  /opt/product/data/elk/kibanadata /opt/product/kibana-5.1.2/data
/opt/product/kibana-5.1.2/bin/kibana 
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.url: "http://ip:9200"
docker run  --restart=always -d -ti -v /opt/product/data:/opt/product/data -p 5601:5601  kibana:v1.0 /bin/bash

6. FileBeat配置

FROM elkbase:v1.0
MAINTAINER chances-lixr
VOLUME [ "/opt/product/data/" ]
ADD ./elk /opt/product/
ADD all.sh /root/
RUN chmod +x /root/all.sh
ENTRYPOINT ["/root/all.sh"]
CMD ["/usr/sbin/init"]
#!/bin/bash
export JAVA_HOME=/opt/product/tools/jdk1.8.0_51
export PATH=$JAVA_HOME/bin:$PATH
cd /opt/product/filebeat-5.1.2/
rm -rf filebeat.yml
ln -s /opt/product/data/elk/filebeat.yml . 
rm -rf data 
ln -s /opt/product/data/elk/filebeatdata /opt/product/filebeat-5.1.2/data
/opt/product/filebeat-5.1.2/filebeat -e -c filebeat.yml
filebeat.prospectors:
  - input_type: log
    document_type: tomcataccess
  paths:
     - /opt/product/data/logs/tomcat/localhost_access_log*.txt
     - /opt/product/data/epg2logs/tomcat/localhost_access_log*.txt
output.logstash:
  # The Logstash hosts
  hosts: ["ip:5044"]

在/opt/product/data/elk 目录下创建filebeatdata

docker run -d -ti -v /opt/product/data:/opt/product/data  filebeat:v1.0 /bin/bash

7. UI界面

8. 遇到错误

上一篇下一篇

猜你喜欢

热点阅读