Centos8 Docker部署ElasticSearch集群
阅读原文时间:2021年11月19日阅读:1

ELK部署

部署ElasticSearch集群

1.拉取镜像及批量生成配置文件
# 拉取镜像
[root@VM-24-9-centos ~]# docker pull elasticsearch:7.14.2

# 生成配置文件及目录
for port in $(seq 1 4); \
do \
mkdir -p /data/elk/es/node-${port}/conf
mkdir -p /data/elk/es/node-${port}/data
mkdir -p /data/elk/es/node-${port}/plugins
chmod 777 /data/elk/es/node-${port}/data
touch /data/elk/es/node-${port}/conf/es.yml
cat << EOF >>/data/elk/es/node-${port}/conf/es.yml
cluster.name: jinx
node.name: node${port}
node.master: true
node.data: true
bootstrap.memory_lock: false
network.host: 0.0.0.0
http.port: 920${port}
transport.tcp.port: 930${port}
discovery.seed_hosts: ["81.70.118.191:9301","81.70.118.191:9302","81.70.118.191:9303","81.70.118.191:9304"]
cluster.initial_master_nodes: ["node1","node2","node3","node4"]
cluster.routing.allocation.cluster_concurrent_rebalance: 32
cluster.routing.allocation.node_concurrent_recoveries: 32
cluster.routing.allocation.node_initial_primaries_recoveries: 32
http.cors.enabled: true
http.cors.allow-origin: "*"
discovery.zen.minimum_master_nodes: 2
EOF
done
2.批量创建容器及查看集群信息
# 批量创建容器
for port in $(seq 1 4); \
do \
docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-d -p 920${port}:920${port} -p 930${port}:930${port} \
-e ES_MIN_MEM=128m \
-e ES_MAX_MEM=2048m \
-v /data/elk/es/node-${port}/conf/es.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /data/elk/es/node-${port}/data/:/usr/share/elasticsearch/data/ \
-v /data/elk/es/node-${port}/plugins/:/usr/share/elasticsearch/plugins  \
--name es-${port} \
elasticsearch:7.14.2
done

# 查看单个节点信息
[root@VM-24-9-centos ~]# curl http://81.70.118.191:9201/
{
  "name" : "node1",
  "cluster_name" : "jinx",
  "cluster_uuid" : "Vjb7cu6fQ6y2-ZWk0YGIiQ",
  "version" : {
    "number" : "7.2.0",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "508c38a",
    "build_date" : "2019-06-20T15:54:18.811730Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

# 查看集群信息
[root@VM-24-9-centos ~]# curl http://81.70.118.191:9201/_cat/nodes?pretty
172.17.0.2 37 97 0 0.00 0.00 0.08 mdi * node1
172.17.0.4 35 97 0 0.00 0.00 0.08 mdi - node3
172.17.0.3 39 97 1 0.00 0.00 0.08 mdi - node2
172.17.0.6 34 97 1 0.00 0.00 0.08 mdi - node4

使用Nginx做集群负载均衡

1.获取镜像
# 拉取镜像 此处我们拉取的是官方最新镜像,其它版本可以去DockerHub查询
[root@VM-24-9-centos ~]# docker pull nginx
2.创建容器
# 创建容器 第一个nginx是容器名,第二个nginx是镜像名
[root@VM-24-9-centos ~]# docker run -d -p 9200:9200 --name nginx nginx
3.把容器内的配置文件等复制到容器外用于挂载
# nginx的配置文件日志文件及默认的页面分别放于容器内的 /etc/nginx /usr/share/nginx/html /var/log/nginx 中,我们需要将其挂载到容器外部

# 创建三个文件夹 conf html logs
[root@VM-24-9-centos data]# mkdir -p /data/nginx/{conf.d,html,logs}

# 将容器内的 nginx.conf配置文件和default.conf配置文件复制出来
[root@VM-24-9-centos data]# docker cp nginx:/usr/share/nginx/html /data/nginx
[root@VM-24-9-centos data]# docker cp nginx:/etc/nginx/nginx.conf /data/nginx
[root@VM-24-9-centos data]# docker cp nginx:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/default.conf

# 查看目录结构
[root@VM-24-9-centos nginx]# cd /data/nginx
[root@VM-24-9-centos nginx]# ll
total 16
drwxr-xr-x 2 root root 4096 Nov 16 10:48 conf.d
drwxr-xr-x 2 root root 4096 Nov 16 10:48 html
drwxr-xr-x 2 root root 4096 Nov 16 10:48 logs
-rw-r--r-- 1 root root  648 Nov  2 23:01 nginx.conf

# 在 conf.d 目录下再建一个 es.conf 配置文件用于做负载均衡
[root@VM-24-9-centos conf]# vim /data/nginx/conf.d/es.conf
upstream es{
    server 81.70.118.191:9201 weight=1;
    server 81.70.118.191:9202 weight=1;
    server 81.70.118.191:9203 weight=1;
}
server {
    listen       9200;
    server_name  localhost;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        proxy_pass http://es;
    #   root   html;
    #  index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
4.删除之前的容器,然后创建新的容器把目录挂载上去
# 停止容器
[root@VM-24-9-centos nginx]# docker stop  nginx
nginx
# 删除容器
[root@VM-24-9-centos nginx]# docker rm  nginx
nginx
# 创建新的容器 --privileged=true 容器内部对挂载的目录拥有读写等特权
docker run -d -p 9200:9200 --name nginx_9200 \
-v /data/nginx/html:/usr/share/nginx/html \
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
--privileged=true \
nginx
5.访问负载均衡配置的地址查看是否成功
[root@VM-24-9-centos conf]# curl http://81.70.118.191:9200/
{
  "name" : "node3",
  "cluster_name" : "jinx",
  "cluster_uuid" : "5aRGIwI0T-qHks6vXzRNQQ",
  "version" : {
    "number" : "7.14.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "6bc13727ce758c0e943c3c21653b3da82f627f75",
    "build_date" : "2021-09-15T10:18:09.722761972Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

部署ElasticSearch-Head

​ ElasticSearch-Head是一个管理界面,可以查看ElasticSearch相关信息

1.拉取ElasticSearch-Head镜像
[root@VM-24-9-centos ~]#  docker pull mobz/elasticsearch-head:5
2.运行ElasticSearch-Head容器
# 创建容器
[root@VM-24-9-centos ~]# docker run -d --name es_admin -p 9100:9100 mobz/elasticsearch-head:5

# pc端访问 IP:9100 即可用管理工具查看集群信息了

部署Kibana

1.拉取镜像
# 拉取镜像
[root@VM-24-9-centos conf.d]# docker pull kibana:7.14.2
2.创建挂载目录
# 创建挂载目录
[root@VM-24-9-centos conf]# mkdir -p /data/elk/kibana/

# 创建配置文件
[root@VM-24-9-centos conf]# vim /data/elk/kibana/conf/kibana.yml

server.name: kibana
# kibana的主机地址 0.0.0.0可表示监听所有IP
server.host: "0.0.0.0"
# kibana访问es的URL
elasticsearch.hosts: [ "http://81.70.118.191:9200" ]
elasticsearch.username: 'kibana'
elasticsearch.password: '123456'
# 显示登陆页面
xpack.monitoring.ui.container.elasticsearch.enabled: true
# 语言
i18n.locale: "zh-CN"
server.publicBaseUrl: "http://81.70.118.191:9200"
3.运行容器
[root@VM-24-9-centos conf]# docker run -d -p 5601:5601 --privileged=true --name=kibana -v/data/elk/kibana/conf/kibana.yml:/usr/share/kibana/config/kibana.yml kibana:7.2.0