docker搭建ELK集群

13次阅读

共计 3553 个字符,预计需要花费 9 分钟才能阅读完成。

首先安装 docker,需要 centos7 的机子,centos6 的百度自己折腾

# 安装 docker
yum install -y docker-io 
systemctl start docker

进入正题,这里搭建的是 ELK7.2 版本

  1. 搭建 elasticsearch
  • 修改内核参数
    vim /etc/sysctl.conf
vm.max_map_count = 655360
vm.swappiness = 1
  • 进入 data 创建目录 elk
    cd /data && mkdir elk
  • 进入 elk,创建目录 data logs
    cd elk
    mkdir data
    mkdir logs
  • 回到 data 授予 elk1000:1000 权限(es 默认权限是 1000)
    cd /data
    chown 1000:1000 elk -R
  • 这里搭建的是 2 个节点的 es,在 2 台机子分别建目录,然后分别执行以下命令
docker run --name ES   \
        -d --net=host \
        --restart=always \
        --privileged=true \
        --ulimit nofile=655350 \
        --ulimit memlock=-1 \
        --memory=16G \
        --memory-swap=-1 \
        --volume /data:/data \
        --volume /etc/localtime:/etc/localtime \
        -e TERM=dumb \
        -e ELASTIC_PASSWORD='changeme' \
        -e ES_JAVA_OPTS="-Xms8g -Xmx8g" \
        -e cluster.name="es" \
        -e node.name="node-1" \
        -e node.master=true \
        -e node.data=true \
        -e node.ingest=false \
        -e node.attr.rack="0402-K03" \
        -e discovery.zen.ping.unicast.hosts="ip1,ip2" \
        -e xpack.security.enabled=true  \
        -e xpack.monitoring.collection.enabled=true  \
        -e xpack.monitoring.exporters.my_local.type=local \
        -e xpack.monitoring.exporters.my_local.use_ingest=false \
        -e discovery.zen.minimum_master_nodes=1 \
        -e gateway.recover_after_nodes=1 \
        -e cluster.initial_master_nodes="node-1" \
        -e network.host=0.0.0.0 \
        -e http.port=9200 \
        -e path.data=/data/elk/data \
        -e path.logs=/data/elk/logs \
        -e bootstrap.memory_lock=true \
        -e bootstrap.system_call_filter=false \
        -e indices.fielddata.cache.size="25%" \
        elasticsearch:7.2.0

其中需要注意的几个参数,

    ELASTIC_PASSWORD 这个是设置你 elastic 这个用户对应的密码
    memory 是你服务器的内存
    ES_JAVA_OPTS 这个数值一般是内存的一半
    discovery.zen.ping.unicast.hosts 这个参数是你集群的 ip

在第二个节点修改以下参数

    node.name, node.master=false, 去除 cluster.initial_master_nodes 参数
    
  • 完成以后就可以执行以下命令看一下日志了

docker logs ES -f

  • 检测一下结果
curl --user elastic:changeme -XGET http://ip1:9200/_cat/indices

看到这张图就代表你搭建成功了,number_of_nodes 代表的节点数 最后一个参数代表加载了多少

2. 搭建 kibana

  • 搭建 kibana 比较简单,先拉一下镜像
docker pull kibana
  • 执行命令开启服务
docker run --name kibana \
        --restart=always \
        -d --net=host \
        -v /data:/data \
        -v /etc/localtime:/etc/localtime \
        --privileged \
        -e TERM=dumb \
        -e SERVER_HOST=0.0.0.0 \
        -e SERVER_PORT=5601 \
        -e SERVER_NAME=Kibana-100 \
        -e ELASTICSEARCH_HOSTS=http://localhost:9200 \
        -e ELASTICSEARCH_USERNAME=elastic \
        -e ELASTICSEARCH_PASSWORD=changeme \
        -e XPACK_MONITORING_UI_CONTAINER_ELASTICSEARCH_ENABLED=true \
        -e LOG_FILE=/data/elasticsearch/logs/kibana.log \
        kibana:7.2.0
  • kibana 搭建比较简单,接下来就做个 nginx 设置一下就好了,给个简单的配置
server {
        listen       80;
        #listen       [::]:80 default_server;
        #server_name  ;
        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host  $http_host;
        proxy_set_header X-Nginx-Proxy true;
        proxy_set_header Connection "";
        proxy_pass      http://127.0.0.1:5601;

        }

        error_page 404 /404.html;
            location = /40x.html { }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {}}
  1. logstash 安装
  • 创建目录授权
cd /data
mkdir config
cd config
mkdir pipeline
cd /data
chown 1000:1000 config
  • 在 config 中创建以下文件

vim log4j2.properties


logger.elasticsearchoutput.name = logstash.outputs.elasticsearch
logger.elasticsearchoutput.level = debug

vim logstash.yml
内容直接放空,wq 出来就好

vim pipelines.yml

- pipeline.id: my-logstash
  path.config: "/usr/share/logstash/config/pipeline/*.conf"
  pipeline.workers: 3

紧接着进入 pipeline 目录,创建要

cd pipeline
vim redis2es.conf
input {
    redis {
        data_type => "list"
        codec => "json"
        key => "xxx"
        host => "xxxxxxx"
        port => 6379
        password => "xxxxxx"
        threads => 1
        batch_count => 100
    }
}
output {
    elasticsearch {hosts => ["ip1:9200"]
        index => "%{srv}-%{type}-%{+yyyy.MM.dd}"
        document_type => "%{type}"
        workers => 1
        user => "elastic"
        password => "changme"
    }
    file {path => "/usr/share/logstash/%{srv}-%{type}-%{+yyyyMMdd}.log"
        gzip => true
        flush_interval => 10
        workers => 1
    }
}

以上 xxx 请自行脑补


额外扩展,推荐一个工具——dremio
简单的命令解决

docker pull dremio/dremio-oss
docker run -p 9047:9047 -p 31010:31010 -p 45678:45678 dremio/dremio-oss

正文完
 0