Logstash supports cluster deployment, which can improve processing capacity and availability by forming a cluster with multiple Logstash instances. Here is content related to Logstash cluster deployment and management.
Cluster Architecture
Single-node Deployment
shellData Sources → Logstash → Elasticsearch
Suitable for small-scale scenarios, a single Logstash instance processes all data.
Cluster Deployment
shellData Sources → Load Balancer → Logstash Cluster → Elasticsearch ├── Logstash Node 1 ├── Logstash Node 2 └── Logstash Node 3
Suitable for large-scale scenarios, multiple Logstash instances share the load.
Load Balancing Strategies
1. Using Beats Load Balancing
conf# Filebeat configuration output.logstash: hosts: ["logstash1:5044", "logstash2:5044", "logstash3:5044"] loadbalance: true worker: 2
2. Using Message Queues
shellData Sources → Kafka → Logstash Cluster → Elasticsearch ├── Logstash 1 ├── Logstash 2 └── Logstash 3
3. Using Load Balancer
shellData Sources → Nginx/HAProxy → Logstash Cluster → Elasticsearch
Persistent Queue
Logstash supports persistent queues, which can retain data during restarts and prevent data loss.
Enable Persistent Queue
conf# logstash.yml queue.type: persisted path.queue: /path/to/queue/data queue.page_capacity: 250mb queue.max_events: 0 queue.max_bytes: 1gb queue.drain: true
Memory Queue
conf# logstash.yml queue.type: memory queue.max_events: 10000
Configuration Management
1. Configuration File Synchronization
Use configuration management tools (such as Ansible, Puppet, Chef) to synchronize configuration files to all nodes.
2. Configuration Center
Use configuration centers (such as Consul, etcd) to manage configurations.
3. Configuration Version Control
Include configuration files in version control systems (Git).
Monitoring and Alerting
1. Logstash Monitoring API
bash# View node information curl -XGET 'localhost:9600/_node' # View pipeline statistics curl -XGET 'localhost:9600/_node/stats/pipelines?pretty' # View plugin statistics curl -XGET 'localhost:9600/_node/stats/plugins?pretty'
2. Prometheus Integration
conf# logstash.yml http.host: "0.0.0.0" http.port: 9600 monitoring.enabled: true monitoring.elasticsearch.hosts: ["http://es:9200"]
3. Key Metrics
- Events per second (EPS)
- Pipeline latency
- Queue size
- JVM memory usage
- CPU usage
High Availability
1. Multi-instance Deployment
Deploy multiple Logstash instances to avoid single points of failure.
2. Persistent Queue
Enable persistent queue to prevent data loss.
3. Health Checks
Configure health checks to automatically restart failed instances.
4. Auto-scaling
Automatically adjust the number of instances based on load.
Performance Tuning
1. Pipeline Workers
conf# logstash.yml pipeline.workers: 4
Set to 1-2 times the number of CPU cores.
2. Batch Size
conf# logstash.yml pipeline.batch.size: 500
Increasing batch size can improve throughput.
3. JVM Memory
bash# config/jvm.options -Xms4g -Xmx4g
4. Garbage Collector
bash# config/jvm.options -XX:+UseG1GC
Troubleshooting
1. View Logs
bashtail -f /var/log/logstash/logstash-plain.log
2. Check Configuration
bashbin/logstash --config.test_and_exit -f /path/to/config.conf
3. Debug Mode
bashbin/logstash --config.debug -f /path/to/config.conf
4. View Pipeline Status
bashcurl -XGET 'localhost:9600/_node/stats/pipelines?pretty'
Real-world Deployment Example
Docker Compose Deployment
yamlversion: '3' services: logstash1: image: docker.elastic.co/logstash/logstash:8.0.0 volumes: - ./config/logstash1.conf:/usr/share/logstash/pipeline/logstash.conf - ./config/logstash.yml:/usr/share/logstash/config/logstash.yml ports: - "5044:5044" - "9600:9600" environment: - "LS_JAVA_OPTS=-Xms2g -Xmx2g" logstash2: image: docker.elastic.co/logstash/logstash:8.0.0 volumes: - ./config/logstash2.conf:/usr/share/logstash/pipeline/logstash.conf - ./config/logstash.yml:/usr/share/logstash/config/logstash.yml ports: - "5045:5044" - "9601:9600" environment: - "LS_JAVA_OPTS=-Xms2g -Xmx2g"
Kubernetes Deployment
yamlapiVersion: apps/v1 kind: Deployment metadata: name: logstash spec: replicas: 3 selector: matchLabels: app: logstash template: metadata: labels: app: logstash spec: containers: - name: logstash image: docker.elastic.co/logstash/logstash:8.0.0 ports: - containerPort: 5044 - containerPort: 9600 resources: limits: memory: "4Gi" cpu: "2" requests: memory: "2Gi" cpu: "1" volumeMounts: - name: config mountPath: /usr/share/logstash/pipeline volumes: - name: config configMap: name: logstash-config
Best Practices
- Capacity Planning: Plan cluster scale based on data volume
- Monitoring and Alerting: Establish comprehensive monitoring and alerting mechanisms
- Configuration Management: Use configuration management tools to manage configurations uniformly
- Data Backup: Regularly backup configurations and data
- Security Hardening: Enable SSL/TLS, configure access control
- Performance Testing: Conduct thorough performance testing before going live
- Documentation: Record deployment and operations documentation