{"id":11116,"date":"2021-12-12T01:36:20","date_gmt":"2021-12-11T22:36:20","guid":{"rendered":"https:\/\/kifarunix.com\/?p=11116"},"modified":"2024-03-18T07:45:56","modified_gmt":"2024-03-18T04:45:56","slug":"ship-system-logs-to-elk-stack-using-elastic-agents","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/ship-system-logs-to-elk-stack-using-elastic-agents\/","title":{"rendered":"Ship System Logs to ELK Stack using Elastic Agents"},"content":{"rendered":"\n
This tutorial will take you through how to ship system logs to ELK stack using Elastic Agents. You might be so used to using Elastic beats such as Filebeat, metricsbeat, Winlogbeat etc. to ship log from your end points to ELK for visualization. However, Elastic has announced the general availability Elastic Agents. Elastic Agent<\/a> is a single, unified agent that you deploy to hosts or containers to collect data and send it to the Elastic Stack. Behind the scenes, Elastic Agent runs the Beats shippers or Elastic Endpoint required for your configuration<\/em>.<\/p>\n\n\n\n Read more about the capabilities of both the Elastic Beats and Elastic Agents<\/a>.<\/p>\n\n\n\n In order to collect and forward system logs to ELK stack using Elastic Agents, you need to deploy the Elastic agents.<\/p>\n\n\n\n There are multiple ways in which Elastic agents can be deployed;<\/p>\n\n\n\n So, in this setup, we will deploy Elastic agents using the recommended method.<\/p>\n\n\n\n To begin with, you need to be having a running Elastic stack. You can follow the tutorials below to install Elastic stack;<\/p>\n\n\n\n Install ELK Stack on Debian 11<\/a><\/p>\n\n\n\n Install ELK Stack on Rocky Linux 8<\/a><\/p>\n\n\n\n Install ELK Stack on Ubuntu 20.04<\/a><\/p>\n\n\n\n In our setup, we are running ELK stack v 7.16.<\/p>\n\n\n\n Next, you need to configure a secure and encrypted connections in Elastic stack.<\/p>\n\n\n\n Next, start Elasticsearch;<\/p>\n\n\n\n Use either command, See how to use the command by passing To Uses randomly generated passwords, run the command below;<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n For now, we are only interested in passwords for the users: Be sure to save all these passwords as they are needed to be used later.<\/p>\n\n\n\n When Elastic security features are enabled, Kibana has to connect to Elasticsearch using valid credentials.<\/p>\n\n\n\n In this case, it uses the username Add the username password to Kibana keystore instead of plain text in the configuration file;<\/p>\n\n\n\n When prompted, enter the password for Next, define the Kibana username by uncommenting the line, This commands generates a number of encryption keys. We only need xpack.encryptedSavedObjects.encryptionKey<\/span><\/p>\n\n\n\n Add the line into kibana.yml config;<\/p>\n\n\n\n Kibana now prompts to username\/password to allow you login. Use the Once logged into Kibana, navigate to menu > Management > Fleet.<\/p>\n\n\n\n When you click on Fleet, the Fleet configuration interface opens up. It takes a while to load fully, for the first time.<\/p>\n\n\n\n Ensure that your Kibana server has internet connection<\/strong> as it requires to download and install integration packages from the Elastic Package Registry.<\/p>\n\n\n\n Once it loads, this is how it looks;<\/p>\n\n\n\n Click on Fleet Settings<\/strong> at the top right corner and;<\/p>\n\n\n\n Next, add a Fleet Server. Fleet server is required before you can enroll agents with Fleet<\/em>.<\/p>\n\n\n\n To add a Fleet server;<\/p>\n\n\n\n By Clicking on the download link provided, you are taken to a web page where you can download the Fleet server Elastic agent installer.<\/p>\n\n\n\n In my current setup, ELK stack is running on Debian 11. Hence, we use the DEB binary installer;<\/p>\n\n\n\n If you have Elastic repos in place, you should be able to install straight from those repos;<\/p>\n\n\n\n The method of installation will determine how you start the fleet server.<\/strong> If you used the TAR file, you can start the Fleet server from the archive with the command In this setup, we will use production<\/strong> setup. Hence, we need to generate the TLS certs.<\/p>\n\n\n\n Generate a CA.<\/p>\n\n\n\n A zip file with CA is placed under Navigate to this directory and extract the CA;<\/p>\n\n\n\n You now have a directory called ca, with your CA cert and key;<\/p>\n\n\n\n Next, generate certificates using the CA above. The command prompts to enter the name of the zip file to stop certs. Press enter to go with default.<\/em><\/p>\n\n\n\n Extract the certs;<\/p>\n\n\n\n Next, enable Elasticsearch HTTPS connection;<\/p>\n\n\n\n Restart Elasticsearch;<\/p>\n\n\n\n Enable Kibana Elasticsearch HTTPS connection as well;<\/p>\n\n\n\n Update the path to the CA cert.<\/p>\n\n\n\n Set the Elasticsearch URL to https;<\/p>\n\n\n\n Start and Stop Kibana;<\/p>\n\n\n\n Once Elasticsearch and Kibana are up, login back to Kibana and define the IP address of the Fleet server host. NOTE<\/strong> if you already added the Fleet server above, skip this step.<\/em><\/p>\n\n\n\n Next, click Fleet Settings<\/strong> and add the TLS certificates generated above under Elasticsearch output configuration (YAML)<\/strong>. See screenshot below;<\/p>\n\n\n\n Sample key;<\/p>\n\n\n\n If you downloaded the TAR of the Elastic Agent, extract and run the command below from within the Elastic agent archive folder;<\/p>\n\n\n\n If you installed the Elastic agent already using the DEB\/RPM or from the Elastic repo using the package manager, start the Fleet server using the command;<\/p>\n\n\n\n Sample Agent enrollment output;<\/p>\n\n\n\n Click on Data streams to view data collected.<\/p>\n\n\n\n You can view dashboards associated with each data set by clicking actions > view dashboards.<\/p>\n\n\n\n In our next tutorial, we will learn how to install and enroll remote Elastic agents on Fleet manager.<\/p>\n\n\n\n Install and Enroll Elastic Agents to Fleet Manager in Linux<\/a><\/p>\n\n\n\n Reference<\/p>\n\n\n\n Setup Fleet Manager on ELK<\/a><\/p>\n\n\n\n Detect Changes to Critical Files in Linux using Auditbeat and ELK<\/a><\/p>\n\n\n\n Configure ELK Stack Alerting with ElastAlert<\/a><\/p>\n\n\n\nShipping System Logs to ELK Stack with Elastic Agents<\/h2>\n\n\n\n
\n
Install and manage Elastic Agents using Kibana Fleet UI<\/h3>\n\n\n\n
1. Install and Setup Elastic Stack<\/h4>\n\n\n\n
2. Configure Basic Security for Elastic Stack<\/h4>\n\n\n\n
\n
systemctl stop elasticsearch kibana<\/code><\/pre>\n\n\n\n
\n
echo -e \"xpack.security.enabled: true\\nxpack.security.authc.api_key.enabled: true\" >> \/etc\/elasticsearch\/elasticsearch.yml<\/code><\/pre>\n\n\n\n
systemctl start elasticsearch<\/code><\/pre>\n\n\n\n
\n
\/usr\/share\/elasticsearch\/bin\/elasticsearch-setup-passwords<\/strong><\/code>, to generate the password.<\/p>\n\n\n\n
-h<\/code> option.<\/p>\n\n\n\n
\/usr\/share\/elasticsearch\/bin\/elasticsearch-setup-passwords -h<\/code><\/pre>\n\n\n\n
echo \"y\" | \/usr\/share\/elasticsearch\/bin\/elasticsearch-setup-passwords auto<\/code><\/pre>\n\n\n\n
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.\nThe passwords will be randomly generated and printed to the console.\n\n\nChanged password for user apm_system\nPASSWORD apm_system = 0ULOnDiadDDpLehYPDI3\n\nChanged password for user kibana_system\nPASSWORD kibana_system = 1LaDy3SBeDRKhvXpZG1A\n\nChanged password for user kibana\nPASSWORD kibana = 1LaDy3SBeDRKhvXpZG1A\n\nChanged password for user logstash_system\nPASSWORD logstash_system = YHvf3EtvaIrcX4xSrIoK\n\nChanged password for user beats_system\nPASSWORD beats_system = mw4LN3BurdA3qwrDMk4P\n\nChanged password for user remote_monitoring_user\nPASSWORD remote_monitoring_user = XpRuM8iFlXJWMj3HP97J\n\nChanged password for user elastic\nPASSWORD elastic = WSZdCjtgn9c8Pphd4St4\n<\/code><\/pre>\n\n\n\n
elastic<\/code><\/strong> and
kibana_system<\/code><\/strong>.<\/p>\n\n\n\n
\n
kibana_system<\/code><\/strong> and its password from the command output above,
1LaDy3SBeDRKhvXpZG1A<\/code><\/strong>.<\/p>\n\n\n\n
\/usr\/share\/kibana\/bin\/kibana-keystore create<\/code><\/pre>\n\n\n\n
\/usr\/share\/kibana\/bin\/kibana-keystore add elasticsearch.password<\/code><\/pre>\n\n\n\n
kibana_system<\/code><\/strong> user.<\/p>\n\n\n\n
elasticsearch.username<\/code><\/strong>.<\/p>\n\n\n\n
sed -i '\/\\.username\/s\/^#\/\/' \/etc\/kibana\/kibana.yml<\/code><\/pre>\n\n\n\n
\n
xpack.encryptedSavedObjects.encryptionKey: value<\/strong><\/code>. This is required by the Fleet to save API keys and encrypt them in Kibana<\/em>.<\/li>\n<\/ul>\n\n\n\n
\/usr\/share\/kibana\/bin\/kibana-encryption-keys generate -q<\/code><\/pre>\n\n\n\n
echo \"xpack.encryptedSavedObjects.encryptionKey: f5021271183cff711f40f41686bb1a46\" >> \/etc\/kibana\/kibana.yml<\/code><\/pre>\n\n\n\n
\n
systemctl start kibana<\/code><\/pre>\n\n\n\n
\n
elastic<\/code> user<\/li>\n<\/ul>\n\n\n\n
elastic<\/code> user and its password generated above.<\/p>\n\n\n
<\/figure><\/div>\n\n\n
3. Setup Elastic Fleet<\/h4>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
\n
<\/figure>\n\n\n\n
\n
\n
wget https:\/\/artifacts.elastic.co\/downloads\/beats\/elastic-agent\/elastic-agent-7.16.0-amd64.deb\napt install .\/elastic-agent-7.16.0-amd64.deb<\/code><\/pre>\n\n\n\n
apt install elastic-agent -y<\/code><\/pre>\n\n\n\n
.\/elastic-agent install...<\/code> If you used DEB or RPM binary or installed from repos, then start fleet server using
elastic-agent enroll...<\/code> command.<\/p>\n\n\n\n
\n
\/usr\/share\/elasticsearch\/bin\/elasticsearch-certutil ca --pem --out \/etc\/elasticsearch\/elastic-stack-ca.zip --days 3650<\/code><\/pre>\n\n\n\n
\/etc\/elasticsearch\/elastic-stack-ca.zip<\/code>.<\/p>\n\n\n\n
\/etc\/elasticsearch\/<\/code><\/pre>\n\n\n\n
unzip elastic-stack-ca.zip<\/code><\/pre>\n\n\n\n
ls -1 $PWD\/ca\/*<\/code><\/pre>\n\n\n\n
\/etc\/elasticsearch\/ca\/ca.crt\n\/etc\/elasticsearch\/ca\/ca.key<\/code><\/pre>\n\n\n\n
\/usr\/share\/elasticsearch\/bin\/elasticsearch-certutil cert \\\n--name kifarunix-demo-fleet-server \\\n--ca-cert \/etc\/elasticsearch\/ca\/ca.crt \\\n--ca-key \/etc\/elasticsearch\/ca\/ca.key \\\n--dns elk.kifarunix-demo.com \\\n--ip 192.168.58.22 \\\n--days 3650 \\\n--out \/etc\/elasticsearch\/certificate-bundle.zip \\\n--pem\n<\/code><\/pre>\n\n\n\n
unzip certificate-bundle.zip<\/code><\/pre>\n\n\n\n
ls -1 $PWD\/kifarunix-demo-fleet-server\/*<\/code><\/pre>\n\n\n\n
\/etc\/elasticsearch\/kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.crt\n\/etc\/elasticsearch\/kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.key<\/code><\/pre>\n\n\n\n
\ncat >> \/etc\/elasticsearch\/elasticsearch.yml << 'EOL'\nxpack.security.http.ssl.verification_mode: certificate\nxpack.security.http.ssl.key: kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.key\nxpack.security.http.ssl.certificate: kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.crt\nxpack.security.http.ssl.certificate_authorities: ca\/ca.crt\nEOL\n<\/code><\/pre>\n\n\n\n
systemctl stop elasticsearch<\/code><\/pre>\n\n\n\n
systemctl start elasticsearch<\/code><\/pre>\n\n\n\n
cp \/etc\/elasticsearch\/ca\/ca.crt \/etc\/kibana\/<\/code><\/pre>\n\n\n\n
sed -i '\/elasticsearch.ssl.certificateAuthorities:\/s\/^#\/\/;\/elasticsearch.ssl.certificateAuthorities:\/s\/\".*.\"\/\\\/etc\\\/kibana\\\/ca.crt\/' \/etc\/kibana\/kibana.yml<\/code><\/pre>\n\n\n\n
sed -i '\/:9200\/s\/http\/https\/' \/etc\/kibana\/kibana.yml<\/code><\/pre>\n\n\n\n
systemctl stop kibana<\/code><\/pre>\n\n\n\n
systemctl start kibana<\/code><\/pre>\n\n\n\n
\n
<\/figure>\n\n\n\n
\n
AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuLTE2MzkyNTMzMjE4ODY6dVloNERYWlVUQVctbHR5VDlNSmNlQQ<\/code><\/pre>\n\n\n\n
\n
cd elastic-agent-7.16.0-linux-x86_64\/\nsudo .\/elastic-agent install -f --url=https:\/\/192.168.58.22:8220 \\\n --fleet-server-es=https:\/\/192.168.58.22:9200 \\\n --fleet-server-service-token=AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuLTE2MzkyNTY0MTk5NDg6NjJOR1hua0xRcXFpb2J0VmlHWmRGZw \\\n --fleet-server-policy=7ffb6ea0-5a5f-11ec-a136-bbf180de0c1d \\\n --certificate-authorities=\/etc\/elasticsearch\/ca\/ca.crt \\\n --fleet-server-es-ca=\/etc\/elasticsearch\/ca\/ca.crt \\\n --fleet-server-cert=\/etc\/elasticsearch\/kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.crt \\\n --fleet-server-cert-key=\/etc\/elasticsearch\/kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.key\n<\/code><\/pre>\n\n\n\n
sudo elastic-agent enroll -f --url=https:\/\/192.168.58.22:8220 \\\n --fleet-server-es=https:\/\/192.168.58.22:9200 \\\n --fleet-server-service-token=AAEAAWVsYXN0aWMvZmxlZXQtc2VydmVyL3Rva2VuLTE2MzkyNTY0MTk5NDg6NjJOR1hua0xRcXFpb2J0VmlHWmRGZw \\\n --fleet-server-policy=7ffb6ea0-5a5f-11ec-a136-bbf180de0c1d \\\n --certificate-authorities=\/etc\/elasticsearch\/ca\/ca.crt \\\n --fleet-server-es-ca=\/etc\/elasticsearch\/ca\/ca.crt \\\n --fleet-server-cert=\/etc\/elasticsearch\/kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.crt \\\n --fleet-server-cert-key=\/etc\/elasticsearch\/kifarunix-demo-fleet-server\/kifarunix-demo-fleet-server.key\n<\/code><\/pre>\n\n\n\n
2021-12-12T00:00:49.379+0300\tINFO\tcmd\/enroll_cmd.go:555\tSpawning Elastic Agent daemon as a subprocess to complete bootstrap process.\n2021-12-12T00:00:49.561+0300\tINFO\tapplication\/application.go:67\tDetecting execution mode\n2021-12-12T00:00:49.564+0300\tINFO\tapplication\/application.go:88\tAgent is in Fleet Server bootstrap mode\n2021-12-12T00:00:49.799+0300\tINFO\t[api]\tapi\/server.go:62\tStarting stats endpoint\n2021-12-12T00:00:49.799+0300\tINFO\tapplication\/fleet_server_bootstrap.go:130\tAgent is starting\n2021-12-12T00:00:49.800+0300\tINFO\t[api]\tapi\/server.go:64\tMetrics endpoint listening on: \/var\/lib\/elastic-agent\/data\/tmp\/elastic-agent.sock (configured: unix:\/\/\/var\/lib\/elastic-agent\/data\/tmp\/elastic-agent.sock)\n2021-12-12T00:00:49.800+0300\tINFO\tapplication\/fleet_server_bootstrap.go:140\tAgent is stopped\n2021-12-12T00:00:49.802+0300\tINFO\tstateresolver\/stateresolver.go:48\tNew State ID is zS4iwuzb\n2021-12-12T00:00:49.802+0300\tINFO\tstateresolver\/stateresolver.go:49\tConverging state requires execution of 1 step(s)\n2021-12-12T00:00:49.857+0300\tINFO\toperation\/operator.go:284\toperation 'operation-install' skipped for fleet-server.7.16.0\n2021-12-12T00:00:50.028+0300\tINFO\tstateresolver\/stateresolver.go:66\tUpdating internal state\n2021-12-12T00:00:50.028+0300\tINFO\tlog\/reporter.go:40\t2021-12-12T00:00:50+03:00 - message: Application: fleet-server--7.16.0[]: State changed to STARTING: Starting - type: 'STATE' - sub_type: 'STARTING'\n2021-12-12T00:00:50.384+0300\tINFO\tcmd\/enroll_cmd.go:760\tFleet Server - Starting\n2021-12-12T00:00:51.575+0300\tWARN\tstatus\/reporter.go:236\tElastic Agent status changed to: 'degraded'\n2021-12-12T00:00:51.576+0300\tINFO\tlog\/reporter.go:40\t2021-12-12T00:00:51+03:00 - message: Application: fleet-server--7.16.0[]: State changed to DEGRADED: Running on policy with Fleet Server integration: 7ffb6ea0-5a5f-11ec-a136-bbf180de0c1d; missing config fleet.agent.id (expected during bootstrap process) - type: 'STATE' - sub_type: 'RUNNING'\n2021-12-12T00:00:52.386+0300\tINFO\tcmd\/enroll_cmd.go:741\tFleet Server - Running on policy with Fleet Server integration: 7ffb6ea0-5a5f-11ec-a136-bbf180de0c1d; missing config fleet.agent.id (expected during bootstrap process)\n2021-12-12T00:00:53.096+0300\tINFO\tcmd\/enroll_cmd.go:442\tStarting enrollment to URL: https:\/\/192.168.58.22:8220\/\n2021-12-12T00:01:02.899+0300\tINFO\tcmd\/enroll_cmd.go:254\tElastic Agent has been enrolled; start Elastic Agent\nSuccessfully enrolled the Elastic Agent.\n2021-12-12T00:01:02.900+0300\tINFO\tcmd\/run.go:184\tShutting down Elastic Agent and sending last events...\n2021-12-12T00:01:02.900+0300\tINFO\toperation\/operator.go:216\twaiting for installer of pipeline 'default' to finish\n2021-12-12T00:01:02.900+0300\tINFO\tprocess\/app.go:176\tSignaling application to stop because of shutdown: fleet-server--7.16.0\n2021-12-12T00:01:04.431+0300\tINFO\tstatus\/reporter.go:236\tElastic Agent status changed to: 'online'\n2021-12-12T00:01:04.431+0300\tINFO\tlog\/reporter.go:40\t2021-12-12T00:01:04+03:00 - message: Application: fleet-server--7.16.0[]: State changed to STOPPED: Stopped - type: 'STATE' - sub_type: 'STOPPED'\n2021-12-12T00:01:04.431+0300\tINFO\tcmd\/run.go:192\tShutting down completed.\n2021-12-12T00:01:04.432+0300\tINFO\t[api]\tapi\/server.go:66\tStats endpoint (\/var\/lib\/elastic-agent\/data\/tmp\/elastic-agent.sock) finished: accept unix \/var\/lib\/elastic-agent\/data\/tmp\/elastic-agent.sock: use of closed network connection\n<\/code><\/pre>\n\n\n\n
\n
systemctl enable --now elastic-agent<\/code><\/pre>\n\n\n\n
\n
<\/figure>\n\n\n\n
\n
<\/figure>\n\n\n\n
Viewing Logs Collected by Elastic Agent on Fleet Manager<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n