Elasticsearch Kibana Setup Example

Elasticsearch is a search engine that can store data, analyze data, search data very fast and accurate, it is distributed and restful, and very easy to scale. Kibana is an open-source graphical user interface that can corporate with Elasticsearch, it can provide visualization of Elasticsearch data, it can also help you to navigate the Elastic Stack. This article will tell you how to install Elasticsearch Kibana in a docker container.

1. Setup Elasticsearch.

  1. Open a terminal and create a new elasticsearch directory to store elasticsearch data.
    mkdir /var/elasticsearch-7.10.1
  2. Create the below files in the above folder.
    ./
    ├── dockerfile
    ├── elasticsearch-7.10.1-amd64.deb
    ├── run.sh
    └── sources.list
  3. Content of dockerfile.
    FROM ubuntu:16.04
    # Edit the sources list.
    ADD sources.list /etc/apt/sources.list
    ADD elasticsearch-7.10.1-amd64.deb ./
    
    # Install jdk and elasticsearch.
    RUN apt-get update && apt-get install -y openjdk-8-jdk --allow-unauthenticated && apt-get clean all && dpkg -i elasticsearch-7.10.1-amd64.deb && rm -rf elasticsearch-7.1.1-amd64.deb
    EXPOSE 9200
    
    # Add startup script.
    ADD run.sh .
    RUN chmod 755 run.sh
    ENTRYPOINT [ "/run.sh"]
  4. Content of sources.list.
    deb https://download.docker.com/linux/ubuntu xenial main restricted
    deb https://download.docker.com/linux/ubuntu xenial-updates main restricted
    deb https://download.docker.com/linux/ubuntu xenial universe
    deb https://download.docker.com/linux/ubuntu xenial-updates universe
    deb https://download.docker.com/linux/ubuntu xenial multiverse
    deb https://download.docker.com/linux/ubuntu xenial-updates multiverse
    deb https://download.docker.com/linux/ubuntu xenial-backports main restricted universe multiverse
    deb https://download.docker.com/linux/ubuntu xenial-security main restricted
    deb https://download.docker.com/linux/ubuntu xenial-security universe
    deb https://download.docker.com/linux/ubuntu xenial-security multiverse
  5. Content of startup script run.sh.
    #!/bin/bash
    set -e
    
    # Add time zone.
    TZ=Asia/Shanghai
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    
    # Override configurtion file.
    cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
    echo "transport.host: localhost
    transport.tcp.port: 9300
    http.port: 9200
    network.host: 0.0.0.0" >> /etc/elasticsearch/elasticsearch.yml
    
    # Modify the startup file and remove the -d parameter to avoid running in the background.
    sed -i 72's@-d -p $PID_FILE@-p $PID_FILE@g' /etc/init.d/elasticsearch
    
    # Start elasticsearch.
    /etc/init.d/elasticsearch start
  6. Generate the elasticserch image.
    docker build -t elasticsearch-7.10.1 .
  7. Start the docker container.
    docker run -d -it --restart=always -p 9200:9200 elasticsearch-7.10.1

2. Setup Kibana.

  1. Open a terminal and run the command mkdir /var/kibana-7.10.1 to create a new directory for kibana.
    mkdir /var/kibana-7.10.1
  2. Create the below files in the above kibana folder.
    ./
    ├── dockerfile
    ├── kibana-7.10.1-amd64.deb
    └── run.sh
  3. The content of the dockerfile in the above folder.
    FROM ubuntu:16.04
    ADD kibana-7.10.1-amd64.deb ./
    
    # Install jdk and elasticsearch.
    RUN dpkg -i kibana-7.10.1-amd64.deb && rm -rf kibana-7.10.1-amd64.deb
    EXPOSE 5601
    
    # Add startup shell script.
    ADD run.sh .
    RUN chmod 755 run.sh
    ENTRYPOINT [ "/run.sh"]
  4. Content of the kibana run.sh script file.
    #!/bin/bash
    
    # Add time zone.
    TZ=Asia/Shanghai
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    
    #elasticsearch="192.168.8.169"
    if [ -z $elasticsearch ];then
        echo "elasticsearch parameter is empty! Please input a valide ip address."
        exit
    fi
    
    # Modify the configuration file.
    # Modify the listening port.
    sed -i '7s@#server.host: "localhost"@server.host: "0.0.0.0"@g' /etc/kibana/kibana.yml
    
    sed -i '28d' /etc/kibana/kibana.yml
    sed -i "N;28 i elasticsearch.hosts: ["http://$elasticsearch:9200"]" /etc/kibana/kibana.yml
    
    # Startup kibana.
    /usr/share/kibana/bin/kibana "-c /etc/kibana/kibana.yml"
  5. Create the kibana image file.
    docker build -t kibana-7.10.1
  6. Start kibana from the above image file.
    docker run -d -it --restart=always -p 5601:5601 -e elasticsearch=192.168.19.19 kibana-7.10.1

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.