elk

When connecting Docker to the ELK (Elasticsearch, Logstash, Kibana) stack, you enable centralized logging, making it easier to analyze and monitor logs from your Docker containers. Here's a guide on setting up Docker to work with the ELK stack:

Prerequisites:

  • Ensure Docker is installed on your machine or server.

  • Have an ELK stack running. You can either use a managed ELK service, like Elastic Cloud, or set up your ELK stack using Docker Compose.

ELK Stack Setup with Docker Compose:

1. Create a Docker Compose file:

Create a docker-compose.yml file to define the ELK services:

version: "3"
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1
    environment:
      - discovery.type=single-node

  logstash:
    image: docker.elastic.co/logstash/logstash:7.15.1
    volumes:
      - ./logstash/config:/usr/share/logstash/pipeline
    environment:
      LS_JAVA_OPTS: "-Xmx256m -Xms256m"

  kibana:
    image: docker.elastic.co/kibana/kibana:7.15.1
    environment:
      ELASTICSEARCH_URL: http://elasticsearch:9200
    ports:
      - "5601:5601"

Save the file in a directory, for example, elk-stack.

2. Create Logstash Configuration:

Create a Logstash configuration file (logstash.conf) and save it in the logstash/config directory:

input {
  beats {
    port => 5044
  }
}

filter {
  grok {
    match => { "message" => "%{COMBINEDAPACHELOG}" }
  }
}

output {
  elasticsearch {
    hosts => ["elasticsearch:9200"]
    index => "docker-logs-%{+YYYY.MM.dd}"
  }
}

This configuration listens for Beats input on port 5044, applies a grok filter, and sends logs to Elasticsearch.

3. Run ELK Stack:

Run the ELK stack using Docker Compose:

cd elk-stack
docker-compose up -d

This command starts Elasticsearch, Logstash, and Kibana services in the background.

4. Configure Docker Logging Driver:

Update the Docker daemon configuration to use the gelf logging driver. Open or create the Docker daemon configuration file (usually located at /etc/docker/daemon.json):

{
  "log-driver": "gelf",
  "log-opts": {
    "gelf-address": "udp://localhost:12201"
  }
}

Restart the Docker daemon:

sudo systemctl restart docker

Replace localhost:12201 with the address and port where your Logstash instance is running.

5. Run a Docker Container:

Run a Docker container and check if the logs appear in Kibana:

docker run --log-driver=gelf --log-opt gelf-address=udp://localhost:12201 alpine echo "Hello ELK Stack!"

Visit http://localhost:5601 in your browser to access Kibana and visualize the logs.

Notes:

  • The docker-compose.yml file and Logstash configuration are basic examples. Customize them based on your requirements.

  • Ensure that ports 5044 (Logstash input) and 5601 (Kibana) are accessible.

  • Consider securing your ELK stack for production use, including setting up authentication and TLS.

This setup allows Docker containers to send logs to the ELK stack, and Kibana provides a user interface for log visualization and analysis. Adjust configurations based on your specific use case and security requirements.