在Linux环境下配置高可用的容器日志管理系统,需要考虑日志收集、传输、存储和分析等多个环节的可靠性。以下是基于主流开源工具的推荐方案:
# 使用td-agent安装
curl -L https://toolbelt.treasuredata.com/sh/install-ubuntu-focal-td-agent4.sh | sh
<source>
@type forward
port 24224
bind 0.0.0.0
</source>
<match docker.**>
@type kafka2
brokers kafka1:9092,kafka2:9092,kafka3:9092
topic_name docker_logs
<format>
@type json
</format>
</match>
# 安装Fluent Bit
curl https://raw.githubusercontent.com/fluent/fluent-bit/master/install.sh | sh
[INPUT]
Name forward
Listen 0.0.0.0
Port 24224
[OUTPUT]
Name kafka
Match *
Brokers kafka1:9092,kafka2:9092,kafka3:9092
Topics docker_logs
# 示例:使用Docker Compose部署3节点Kafka集群
version: '3'
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- "2181:2181"
kafka1:
image: confluentinc/cp-kafka:latest
depends_on:
- zookeeper
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka1:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 3
kafka2:
image: confluentinc/cp-kafka:latest
depends_on:
- zookeeper
ports:
- "9093:9093"
environment:
KAFKA_BROKER_ID: 2
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka2:9093
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 3
kafka3:
image: confluentinc/cp-kafka:latest
depends_on:
- zookeeper
ports:
- "9094:9094"
environment:
KAFKA_BROKER_ID: 3
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka3:9094
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 3
# 示例:3节点ES集群docker-compose配置
version: '3'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata01:/usr/share/elasticsearch/data
ports:
- 9200:9200
es02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata02:/usr/share/elasticsearch/data
es03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.14.0
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=es01,es02,es03
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms2g -Xmx2g"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- esdata03:/usr/share/elasticsearch/data
volumes:
esdata01:
esdata02:
esdata03:
<source>
@type kafka2
brokers kafka1:9092,kafka2:9092,kafka3:9092
topics docker_logs
format json
</source>
<match **>
@type elasticsearch
host es01
port 9200
logstash_format true
logstash_prefix docker-logs
flush_interval 10s
<buffer>
@type file
path /var/log/td-agent/buffer/elasticsearch
flush_mode interval
retry_type exponential_backoff
retry_wait 1s
retry_max_interval 60s
retry_timeout 60m
chunk_limit_size 8MB
total_limit_size 1GB
overflow_action block
</buffer>
</match>
docker run -d --name kibana --net elasticsearch_net -p 5601:5601 docker.elastic.co/kibana/kibana:7.14.0
# 为关键组件配置持久化存储
volumes:
esdata01:
driver: local
driver_opts:
type: none
device: /data/elasticsearch/node1
o: bind
# 配置logrotate管理节点日志
/var/log/containers/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 644 root root
}
# 修改daemon.json
{
"log-driver": "fluentd",
"log-opts": {
"fluentd-address": "fluentd:24224",
"tag": "docker.{{.Name}}",
"labels": "production_status"
}
}
# 使用DaemonSet部署Fluent Bit
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluent-bit
spec:
selector:
matchLabels:
app: fluent-bit
template:
metadata:
labels:
app: fluent-bit
spec:
containers:
- name: fluent-bit
image: fluent/fluent-bit:1.8.0
volumeMounts:
- name: varlog
mountPath: /var/log
- name: fluent-bit-config
mountPath: /fluent-bit/etc/
volumes:
- name: varlog
hostPath:
path: /var/log
- name: fluent-bit-config
configMap:
name: fluent-bit-config
# 检查Fluentd状态
systemctl status td-agent
# 查看Kafka主题状态
kafka-topics --bootstrap-server kafka1:9092 --list
# 检查ES集群健康状态
curl -X GET "es01:9200/_cluster/health?pretty"
# 查看Kibana日志
docker logs kibana
# 测试日志流水线
echo '{"message":"test log"}' | nc fluentd 24224
通过以上配置,您可以建立一个高可用的容器日志管理系统,能够处理大规模容器化环境的日志需求,并确保在组件故障时系统仍能正常运行。