環境準備#
-
硬體環境
操作系統:Centos Stream 9
CPU:4 顆
記憶體:8GB
硬碟:50GB
-
軟體環境
docker 版本:26.1.0
docker compose 版本:v2.26.1
部署過程#
準備基礎環境,安裝 Docker#
1、安裝 yum-utils 軟體包(提供 yum-config-manager 實用程式)並設置儲存庫,命令如下。
# 安裝 yum-utils 軟體包
yum install -y yum-utils
# 設置 docker-ce 儲存庫
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
2、安裝最新版 docker 及其組件,命令如下。
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
3、啟動 Docker 服務,並設置為開機自啟動,命令如下。
# 啟動 Docker 服務
systemctl start docker
# 設置 Docker 為開機自啟動
systemctl enable docker
# 查看 Docker 服務狀態
systemctl status docker
4、修改 Docker 的資料目錄為 “/data/dockerData”,並重啟 docker 服務使配置生效。
# 創建 Docker 守護進程配置文件
# 編輯配置文件 /etc/docker/daemon.json,配置 docker 資料目錄
vi /etc/docker/daemon.json
**************************daemon.json**************************
{
"data-root": "/data/dockerData"
}
**************************daemon.json**************************
# 重啟 docker 服務,使配置生效
systemctl restart docker
5、設置與記憶體映射相關的內核參數為 262144,查看應用到系統的內核參數。
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p
6、配置防火牆策略
firewall-cmd --add-port=9200/tcp --permanent
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --add-port=5601/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all
部署 elk 集群#
1、創建構建容器所需要的文件目錄,並賦予相應的訪問權限。
cd /data
mkdir es-node1 es-node2 es-node3 elk-kibana
chmod g+rwx es-node1 es-node2 es-node3 elk-kibana
chgrp 0 es-node1 es-node2 es-node3 elk-kibana
2、在 /data/script 目錄下,創建並編輯 .env 文件,用來配置 elk 的環境變量。
mkdir /data/script
vi /data/script/.env
**************************.env**************************
# Elasticsearch 的密碼
ELASTIC_PASSWORD=elk#bd@123
# Kibana 的密碼
KIBANA_PASSWORD=elk#bd@123
# Elastic Stack 的版本號
STACK_VERSION=8.13.3
# Elastic Stack 的集群名稱
CLUSTER_NAME=ELK-docker-Cluster
# 指定 Elastic Stack 的許可證類型
LICENSE=basic
# 指定 Elasticsearch 的端口號
ES_PORT=9200
# 指定 Kibana 的端口號
KIBANA_PORT=5601
# 指定記憶體限制
MEM_LIMIT=2147483648
# 指定 Docker Compose 專案的名稱
COMPOSE_PROJECT_NAME=elk-docker-project
**************************.env**************************
3、創建 logstash 容器所需的目錄和配置文件。
mkdir -p logstash/config logstash/pipeline
echo 'http.host: "0.0.0.0"' > /data/logstash/config/logstash.yml
vi /data/logstash/pipeline/logstash.conf
**************************logstash.conf**************************
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["https://es-node1:9200"]
index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
user => "elastic"
password => "elk#bd@123"
cacert=> "/usr/share/logstash/config/certs/ca/ca.crt"
}
}
**************************logstash.conf**************************
4、創建 docker compose 文件,編排創建 es 初始化節點、es 三節點、Kibana、logstash 容器。
vi /data/script/elk.yml
**************************elk.yml**************************
services:
es-setup:
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
container_name: es-setup
volumes:
- /data/elk-certs:/usr/share/elasticsearch/config/certs
user: "0"
networks:
net:
ipv4_address: 172.20.100.10
command: >
bash -c '
if [ x${ELASTIC_PASSWORD} == x ]; then
echo "Set the ELASTIC_PASSWORD environment variable in the .env file";
exit 1;
elif [ x${KIBANA_PASSWORD} == x ]; then
echo "Set the KIBANA_PASSWORD environment variable in the .env file";
exit 1;
fi;
if [ ! -f config/certs/ca.zip ]; then
echo "Creating CA";
bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
unzip config/certs/ca.zip -d config/certs;
fi;
if [ ! -f config/certs/certs.zip ]; then
echo "Creating certs";
echo -ne \
"instances:\n"\
" - name: es-node1\n"\
" dns:\n"\
" - es-node1\n"\
" - localhost\n"\
" ip:\n"\
" - 127.0.0.1\n"\
" - name: es-node2\n"\
" dns:\n"\
" - es-node2\n"\
" - localhost\n"\
" ip:\n"\
" - 127.0.0.1\n"\
" - name: es-node3\n"\
" dns:\n"\
" - es-node3\n"\
" - localhost\n"\
" ip:\n"\
" - 127.0.0.1\n"\
> config/certs/instances.yml;
bin/elasticsearch-certutil cert --silent --pem -out config/certs/certs.zip --in config/certs/instances.yml --ca-cert config/certs/ca/ca.crt --ca-key config/certs/ca/ca.key;
unzip config/certs/certs.zip -d config/certs;
fi;
echo "Setting file permissions";
chown -R root:root config/certs;
find . -type d -exec chmod 750 \{\} \;;
find . -type f -exec chmod 640 \{\} \;;
echo "Waiting for Elasticsearch availability";
until curl -s --cacert config/certs/ca/ca.crt https://es-node1:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
echo "Setting kibana_system password";
until curl -s -X POST --cacert config/certs/ca/ca.crt -u "elastic:${ELASTIC_PASSWORD}" -H "Content-Type: application/json" https://es-node1:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 10; done;
echo "All done!";
'
healthcheck:
test: ["CMD-", "[ -f config/certs/es-node1/es-node1.crt ]"]
interval: 1s
timeout: 5s
retries: 120
es-node1:
depends_on:
es-setup:
condition: service_healthy
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
container_name: es-node1
restart: always
networks:
net:
ipv4_address: 172.20.100.11
volumes:
- /data/elk-certs:/usr/share/elasticsearch/config/certs
- /data/es-node1:/usr/share/elasticsearch/data
ports:
- ${ES_PORT}:9200
environment:
- node.name=es-node1
- cluster.name=${CLUSTER_NAME}
- cluster.initial_master_nodes=es-node1,es-node2,es-node3
- discovery.seed_hosts=es-node2,es-node3
- ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.key=certs/es-node1/es-node1.key
- xpack.security.http.ssl.certificate=certs/es-node1/es-node1.crt
- xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.enabled=true
- xpack.security.transport.ssl.key=certs/es-node1/es-node1.key
- xpack.security.transport.ssl.certificate=certs/es-node1/es-node1.crt
- xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.verification_mode=certificate
- xpack.license.self_generated.type=${LICENSE}
mem_limit: ${MEM_LIMIT}
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test:
[
"CMD-",
"curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
]
interval: 10s
timeout: 10s
retries: 120
extra_hosts:
- "es-node1:172.20.100.11"
- "es-node2:172.20.100.12"
- "es-node3:172.20.100.13"
es-node2:
depends_on:
- es-node1
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
container_name: es-node2
restart: always
networks:
net:
ipv4_address: 172.20.100.12
volumes:
- /data/elk-certs:/usr/share/elasticsearch/config/certs
- /data/es-node2:/usr/share/elasticsearch/data
environment:
- node.name=es-node2
- cluster.name=${CLUSTER_NAME}
- cluster.initial_master_nodes=es-node1,es-node2,es-node3
- discovery.seed_hosts=es-node1,es-node3
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.key=certs/es-node2/es-node2.key
- xpack.security.http.ssl.certificate=certs/es-node2/es-node2.crt
- xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.enabled=true
- xpack.security.transport.ssl.key=certs/es-node2/es-node2.key
- xpack.security.transport.ssl.certificate=certs/es-node2/es-node2.crt
- xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.verification_mode=certificate
- xpack.license.self_generated.type=${LICENSE}
mem_limit: ${MEM_LIMIT}
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test:
[
"CMD-",
"curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
]
interval: 10s
timeout: 10s
retries: 120
extra_hosts:
- "es-node1:172.20.100.11"
- "es-node2:172.20.100.12"
- "es-node3:172.20.100.13"
es-node3:
depends_on:
- es-node2
image: docker.elastic.co/elasticsearch/elasticsearch:${STACK_VERSION}
container_name: es-node3
restart: always
networks:
net:
ipv4_address: 172.20.100.13
volumes:
- /data/elk-certs:/usr/share/elasticsearch/config/certs
- /data/es-node3:/usr/share/elasticsearch/data
environment:
- node.name=es-node3
- cluster.name=${CLUSTER_NAME}
- cluster.initial_master_nodes=es-node1,es-node2,es-node3
- discovery.seed_hosts=es-node1,es-node2
- bootstrap.memory_lock=true
- xpack.security.enabled=true
- xpack.security.http.ssl.enabled=true
- xpack.security.http.ssl.key=certs/es-node3/es-node3.key
- xpack.security.http.ssl.certificate=certs/es-node3/es-node3.crt
- xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.enabled=true
- xpack.security.transport.ssl.key=certs/es-node3/es-node3.key
- xpack.security.transport.ssl.certificate=certs/es-node3/es-node3.crt
- xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
- xpack.security.transport.ssl.verification_mode=certificate
- xpack.license.self_generated.type=${LICENSE}
mem_limit: ${MEM_LIMIT}
ulimits:
memlock:
soft: -1
hard: -1
healthcheck:
test:
[
"CMD-",
"curl -s --cacert config/certs/ca/ca.crt https://localhost:9200 | grep -q 'missing authentication credentials'",
]
interval: 10s
timeout: 10s
retries: 120
extra_hosts:
- "es-node1:172.20.100.11"
- "es-node2:172.20.100.12"
- "es-node3:172.20.100.13"
kibana:
depends_on:
es-node1:
condition: service_healthy
es-node2:
condition: service_healthy
es-node3:
condition: service_healthy
image: docker.elastic.co/kibana/kibana:${STACK_VERSION}
container_name: kibana
restart: always
networks:
net:
ipv4_address: 172.20.100.14
volumes:
- /data/elk-certs:/usr/share/kibana/config/certs
- /data/kibana:/usr/share/kibana/data
ports:
- ${KIBANA_PORT}:5601
environment:
- SERVERNAME=kibana
- ELASTICSEARCH_HOSTS=https://es-node1:9200
- ELASTICSEARCH_USERNAME=kibana_system
- ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD}
- ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES=config/certs/ca/ca.crt
mem_limit: ${MEM_LIMIT}
healthcheck:
test:
[
"CMD-",
"curl -s -I http://localhost:5601 | grep -q 'HTTP/1.1 302 Found'",
]
interval: 10s
timeout: 10s
retries: 120
logstash:
image: docker.elastic.co/logstash/logstash:${STACK_VERSION}
container_name: logstash
volumes:
- /data/elk-certs:/usr/share/logstash/config/certs
- /data/logstash/pipeline:/usr/share/logstash/pipeline
- /data/logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml
user: "0"
restart: always
ports:
- 5044:5044
networks:
net:
ipv4_address: 172.20.100.15
extra_hosts:
- "es-node1:172.20.100.11"
- "es-node2:172.20.100.12"
- "es-node3:172.20.100.13"
networks:
net:
driver: bridge
ipam:
config:
- subnet: 172.20.100.0/24
**************************elk.yml**************************
執行命令,創建容器
docker compose -f /data/script/elk.yml up -d
[+] Running 42/16
✔ es-node3 Pulled 35.6s
✔ es-setup Pulled 35.6s
✔ logstash Pulled 96.0s
✔ kibana Pulled 99.3s
✔ es-node2 Pulled 35.6s
✔ es-node1 Pulled 35.6s[+] Running 7/7
✔ Network elk-docker-project_net Crea… 0.6s
✔ Container es-setup Healthy 21.6s
✔ Container logstash Started 12.5s
✔ Container es-node1 Healthy 72.5s
✔ Container es-node2 Healthy 72.5s
✔ Container es-node3 Healthy 73.0s
✔ Container kibana Started 73.4s
容器創建完成後,查看各容器狀態是否正常
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5b591d4ade82 docker.elastic.co/kibana/kibana:8.13.3 “/bin/tini — /usr/l…” 5 minutes ago Up 4 minutes (healthy) 0.0.0.0:5601->5601/tcp, :::5601->5601/tcp kibana
e2e75a54149e docker.elastic.co/elasticsearch/elasticsearch:8.13.3 “/bin/tini — /usr/l…” 5 minutes ago Up 5 minutes (healthy) 9200/tcp, 9300/tcp es-node3
b8c8357f3634 docker.elastic.co/elasticsearch/elasticsearch:8.13.3 “/bin/tini — /usr/l…” 5 minutes ago Up 5 minutes (healthy) 9200/tcp, 9300/tcp es-node2
c552940bd559 docker.elastic.co/elasticsearch/elasticsearch:8.13.3 “/bin/tini — /usr/l…” 5 minutes ago Up 5 minutes (healthy) 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 9300/tcp es-node1
0366974065d8 docker.elastic.co/logstash/logstash:8.13.3 “/usr/local/bin/dock…” 5 minutes ago Up 5 minutes 0.0.0.0:5044->5044/tcp, :::5044->5044/tcp, 9600/tcp logstash
5、修改 kibana 的配置文件,將其語言轉化為中文。
docker cp kibana:/usr/share/kibana/config/kibana.yml .
vi kibana.yml
**************************kibana.yml**************************
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN"
server.publicBaseUrl: "http://localhost:5601/"
**************************kibana.yml**************************
docker cp kibana.yml kibana:/usr/share/kibana/config/kibana.yml
docker restart kibana
6、在瀏覽器中分別訪問 https://10.10.2.103:9200/
和 http://10.10.2.103:5601/
,輸入賬戶和密碼,進行查看,如下圖所示。
推送 Linux 日誌,驗證部署#
1、安裝 Filebeat 日誌採集器
(1)通過 rpm 方式安裝 Filebeat 日誌採集器。
#下載 rpm 包
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-8.13.3-x86_64.rpm
#執行安裝
rpm -vi filebeat-8.13.3-x86_64.rpm
(2)配置 Filebeat
#啟動 Filebeat
systemctl start filebeat
#查看 Filebeat 運行狀態
systemctl status filebeat
#設置 Filebeat 為開機自啟
systemctl enable filebeat
2、配置 Filebeat 收集 Linux 日誌
(1)配置 filebeat 文件,將日誌發送到 Logstash 服務。
# 修改 filebeat 配置文件
mv /etc/filebeat/filebeat.yml /etc/filebeat/filebeat.yml.bak
# 新建 filebeat 配置文件
vi /etc/filebeat/filebeat.yml
**************************filebeat-dns.yml**************************
# 配置文件輸入,監視日誌文件
filebeat.inputs:
- type: log
enabled: true
# 指定 DNS 日誌文件路徑
paths:
- /var/log/messages*
# 標識日誌類型為 elk-linux
fields:
type: elk-linux
# 將額外的字段添加到根級別
fields_under_root: true
# 配置輸出到 Logstash
# 指定 Logstash 服務的地址和端口
output.logstash:
hosts: ["10.10.2.103:5044"]
**************************filebeat-dns.yml**************************
(2)重啟 filebeat 服務,驗證配置是否生效
# 重啟 filebeat
systemctl restart filebeat
# 刪除鎖文件後,重新啟動 Filebeat 服務
rm -rf /var/lib/filebeat/filebeat.lock
# 運行並查看 Filebeat 的運行日誌
filebeat -e -c /etc/filebeat/filebeat.yml
3、在瀏覽器中登錄 Kibana,點擊左側菜單中的 “Stack Management”,管理創建數據視圖。如下圖所示。
4、在 “Stack Management” 界面中,點擊 “數據視圖”,選擇索引模式,創建 “elk-linux” 視圖。如下圖所示。
5、數據視圖創建完成後,選擇左側菜單中的 “Discover”,查看 Linux 日誌詳細內容。如下圖所示。由此,elk 集群搭建完成。