diff --git a/.github/workflows/buildstack.yml b/.github/workflows/buildstack.yml new file mode 100644 index 0000000..98aad64 --- /dev/null +++ b/.github/workflows/buildstack.yml @@ -0,0 +1,32 @@ +name: Build Elastic Stack + +on: + push: + branches: [master] + pull_request: + branches: [master] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: Configure VM for Elasticsearch + run: | + sudo swapoff -a + sudo sysctl -w vm.swappiness=1 + sudo sysctl -w fs.file-max=262144 + sudo sysctl -w vm.max_map_count=262144 + - name: Run docker-compose + run: docker-compose up -d elasticsearch kibana + - name: Wait + run: sleep 30 + - name: Elasticsearch Health + run: curl http://localhost:9200/_cat/health + - name: Kibana status + run: curl http://localhost:5601/api/status + - name: PHP - Composer install + run: cd app/php && composer install + - name: PHP - Test app + run: php app/php/app.php -a fo1 -b bar diff --git a/.gitignore b/.gitignore index a37f50b..5797d25 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .idea -app/vendor +app/php/vendor /var/ \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 7c19e9a..0000000 --- a/.travis.yml +++ /dev/null @@ -1,21 +0,0 @@ -language: php -sudo: required - -php: - - '7.3' - -notifications: - email: - on_success: never - on_failure: never - -services: - - docker - -before_script: - - cd app && composer install && cd .. - - docker-compose up -d - -script: - - sudo chmod -R 777 var/logs - - cd app/php && php app.php -a fo1 -b bar diff --git a/README.md b/README.md index 1cf3982..13a688e 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,24 @@ -[![Build Status](https://travis-ci.org/CodelyTV/elastic-stack-docker-example.svg?branch=master)](https://travis-ci.org/CodelyTV/elastic-stack-docker-example) +# Elastic Stack with Docker +![Build Elastic Stack](https://github.com/CodelyTV/elastic-stack-example/workflows/Build%20Elastic%20Stack/badge.svg) -# Elastic Stack with Docker (PHP sample App) +## How to run the Stack -Simple PHP Application using Elastic Stack running with Docker. +To initialize all the needed services. -- ``docker-compose up -d `` to initialize all the needed services. +``` +docker-compose up -d +``` -- ```cd app && composer install``` to install needed PHP dependencies +## How to run PHP sample app -- ```php app/app.php -a fo1 -b bar``` to execute the PHP sample application +Install needed PHP dependencies + +``` +cd app && composer install +``` + +Execute the PHP sample application + +``` +php app/php/app.php -a fo1 -b bar +``` diff --git a/app/php/app.php b/app/php/app.php index 6e06c2f..c4c68df 100644 --- a/app/php/app.php +++ b/app/php/app.php @@ -3,45 +3,50 @@ require __DIR__ . '/vendor/autoload.php'; use Monolog\Logger; +use Elastica\Client; +use Monolog\Formatter\JsonFormatter; +use Monolog\Handler\ElasticSearchHandler; +use Monolog\Handler\ErrorLogHandler; +use Monolog\Handler\RotatingFileHandler; // create a log channel $log = new Logger('logger'); //Log to stdout -$stdoutHandler = new \Monolog\Handler\ErrorLogHandler(); -$formatter = new \Monolog\Formatter\JsonFormatter(); +$stdoutHandler = new ErrorLogHandler(); +$formatter = new JsonFormatter(); $stdoutHandler->setFormatter($formatter); $log->pushHandler($stdoutHandler); // File Handler -$fileHandler = new \Monolog\Handler\RotatingFileHandler('../var/logs/app.log', 0, Logger::DEBUG); -$formatter = new \Monolog\Formatter\JsonFormatter(); +$fileHandler = new RotatingFileHandler('../var/logs/app.log', 0, Logger::DEBUG); +$formatter = new JsonFormatter(); $fileHandler->setFormatter($formatter); $log->pushHandler($fileHandler); // Elasticsearch Handler -$elasticaClient = new \Elastica\Client( +$elasticaClient = new Client( [ 'host' => 'localhost', 'port' => 9200 ] ); -$elasticsearchHandler = new \Monolog\Handler\ElasticSearchHandler($elasticaClient); +$elasticsearchHandler = new ElasticSearchHandler($elasticaClient); $log->pushHandler($elasticsearchHandler); // My Application $options = getopt('a:b:'); # App Servidor A -if ($options['a'] == 'warning') { +if ($options['a'] === 'warning') { $log->warn('Esto es un Warning', ['Servidor' => 'Servidor A']); } else { $log->info('Esto es un Info', ['Servidor' => 'Servidor A']); } # App Servidor B -if ($options['b'] == 'error') { +if ($options['b'] === 'error') { $log->error('Esto es un Error', ['Servidor' => 'Servidor B']); } else { $log->info('Esto es un Info', ['Servidor' => 'Servidor B']); diff --git a/docker-compose.yml b/docker-compose.yml index 5d61d6f..984246a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,17 +1,29 @@ -version: '3.3' +version: '3.7' services: elasticsearch: - image: docker.elastic.co/elasticsearch/elasticsearch:6.4.2 + image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0 container_name: elasticsearch ports: ['9200:9200'] + environment: + - "ELASTIC_PASSWORD=elastic" + - "bootstrap.memory_lock=true" + - "ES_JAVA_OPTS=-Xms1g -Xmx1g" + - "discovery.type=single-node" + - "TAKE_FILE_OWNERSHIP=1" + ulimits: + memlock: + soft: -1 + hard: -1 volumes: - './var/elasticsearch:/usr/share/elasticsearch/data' logstash: - image: docker.elastic.co/logstash/logstash:6.4.2 + image: docker.elastic.co/logstash/logstash:7.5.0 container_name: logstash + environment: + - "ELASTIC_PASSWORD=elastic" ports: ['9600:9600'] depends_on: ['elasticsearch'] volumes: @@ -19,8 +31,10 @@ services: - './var/logs:/logs' filebeat: - image: docker.elastic.co/beats/filebeat:6.4.2 + image: docker.elastic.co/beats/filebeat:7.5.0 container_name: filebeat + environment: + - "ELASTIC_PASSWORD=elastic" user: root volumes: - './var/filebeat/log/:/var/log/:ro' @@ -30,9 +44,11 @@ services: command: ["--strict.perms=false"] kibana: - image: docker.elastic.co/kibana/kibana:6.4.2 + image: docker.elastic.co/kibana/kibana:7.5.0 container_name: kibana + environment: + - "ELASTIC_PASSWORD=elastic" ports: ['5601:5601'] depends_on: - - 'elasticsearch' + - elasticsearch diff --git a/etc/filebeat/filebeat.yml b/etc/filebeat/filebeat.yml index 37642b5..faa496b 100644 --- a/etc/filebeat/filebeat.yml +++ b/etc/filebeat/filebeat.yml @@ -1,7 +1,6 @@ logging.level: info logging.to_files: false -#------------------------------ Log input -------------------------------- filebeat.inputs: - type: log enabled: true @@ -14,16 +13,7 @@ filebeat.inputs: fields: ['message'] target: json -#========================== Elasticsearch output =============================== output.elasticsearch: hosts: ["elasticsearch:9200"] username: elastic password: changeme - -#============================== Dashboards ===================================== -#setup.dashboards: -# enabled: true -#setup.kibana: -# host: "kibana:80" -# username: elastic -# password: changeme \ No newline at end of file diff --git a/etc/filebeat/meta.json b/etc/filebeat/meta.json deleted file mode 100644 index 20e96c9..0000000 --- a/etc/filebeat/meta.json +++ /dev/null @@ -1 +0,0 @@ -{"uuid":"42866224-c96f-477e-b259-349b38b05fc1"} diff --git a/etc/filebeat/registry b/etc/filebeat/registry deleted file mode 100644 index fe51488..0000000 --- a/etc/filebeat/registry +++ /dev/null @@ -1 +0,0 @@ -[] diff --git a/var/elasticsearch/.gitkeep b/var/elasticsearch/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/var/filebeat/.gitkeep b/var/filebeat/.gitkeep deleted file mode 100644 index e69de29..0000000