From cebcdf5fca38134e56d746fd388cbf46a95e3193 Mon Sep 17 00:00:00 2001 From: Qiming Teng Date: Thu, 22 Oct 2020 11:54:28 +0800 Subject: [PATCH] Tweak coding styles for guestbook logging tutorial --- .../guestbook-logs-metrics-with-elk.md | 231 +++++++++++------- 1 file changed, 145 insertions(+), 86 deletions(-) diff --git a/content/en/docs/tutorials/stateless-application/guestbook-logs-metrics-with-elk.md b/content/en/docs/tutorials/stateless-application/guestbook-logs-metrics-with-elk.md index de67b9eab3a56..d3a38c4df5b34 100644 --- a/content/en/docs/tutorials/stateless-application/guestbook-logs-metrics-with-elk.md +++ b/content/en/docs/tutorials/stateless-application/guestbook-logs-metrics-with-elk.md @@ -19,17 +19,14 @@ This tutorial builds upon the [PHP Guestbook with Redis](/docs/tutorials/statele * Metricbeat * Packetbeat - - ## {{% heading "objectives" %}} * Start up the PHP Guestbook with Redis. * Install kube-state-metrics. -* Create a Kubernetes secret. +* Create a Kubernetes Secret. * Deploy the Beats. * View dashboards of your logs and metrics. - ## {{% heading "prerequisites" %}} @@ -40,16 +37,18 @@ Additionally you need: * A running deployment of the [PHP Guestbook with Redis](/docs/tutorials/stateless-application/guestbook) tutorial. -* A running Elasticsearch and Kibana deployment. You can use [Elasticsearch Service in Elastic Cloud](https://cloud.elastic.co), run the [download files](https://www.elastic.co/guide/en/elastic-stack-get-started/current/get-started-elastic-stack.html) on your workstation or servers, or the [Elastic Helm Charts](https://github.com/elastic/helm-charts). - - +* A running Elasticsearch and Kibana deployment. You can use [Elasticsearch Service in Elastic Cloud](https://cloud.elastic.co), + run the [downloaded files](https://www.elastic.co/guide/en/elastic-stack-get-started/current/get-started-elastic-stack.html) + on your workstation or servers, or the [Elastic Helm Charts](https://github.com/elastic/helm-charts). ## Start up the PHP Guestbook with Redis + This tutorial builds on the [PHP Guestbook with Redis](/docs/tutorials/stateless-application/guestbook) tutorial. If you have the guestbook application running, then you can monitor that. If you do not have it running then follow the instructions to deploy the guestbook and do not perform the **Cleanup** steps. Come back to this page when you have the guestbook running. ## Add a Cluster role binding + Create a [cluster level role binding](/docs/reference/access-authn-authz/rbac/#rolebinding-and-clusterrolebinding) so that you can deploy kube-state-metrics and the Beats at the cluster level (in kube-system). ```shell @@ -60,31 +59,39 @@ kubectl create clusterrolebinding cluster-admin-binding \ ## Install kube-state-metrics Kubernetes [*kube-state-metrics*](https://github.com/kubernetes/kube-state-metrics) is a simple service that listens to the Kubernetes API server and generates metrics about the state of the objects. Metricbeat reports these metrics. Add kube-state-metrics to the Kubernetes cluster that the guestbook is running in. + ```shell git clone https://github.com/kubernetes/kube-state-metrics.git kube-state-metrics kubectl apply -f kube-state-metrics/examples/standard ``` ### Check to see if kube-state-metrics is running + ```shell kubectl get pods --namespace=kube-system -l app.kubernetes.io/name=kube-state-metrics ``` + Output: -```shell + +``` NAME READY STATUS RESTARTS AGE kube-state-metrics-89d656bf8-vdthm 1/1 Running 0 21s ``` + ## Clone the Elastic examples GitHub repo + ```shell git clone https://github.com/elastic/examples.git ``` The rest of the commands will reference files in the `examples/beats-k8s-send-anywhere` directory, so change dir there: + ```shell cd examples/beats-k8s-send-anywhere ``` ## Create a Kubernetes Secret + A Kubernetes {{< glossary_tooltip text="Secret" term_id="secret" >}} is an object that contains a small amount of sensitive data such as a password, a token, or a key. Such information might otherwise be put in a Pod specification or in an image; putting it in a Secret object allows for more control over how it is used, and reduces the risk of accidental exposure. {{< note >}} @@ -95,55 +102,70 @@ There are two sets of steps here, one for *self managed* Elasticsearch and Kiban {{% tab name="Self Managed" %}} ### Self managed + Switch to the **Managed service** tab if you are connecting to Elasticsearch Service in Elastic Cloud. ### Set the credentials + There are four files to edit to create a k8s secret when you are connecting to self managed Elasticsearch and Kibana (self managed is effectively anything other than the managed Elasticsearch Service in Elastic Cloud). The files are: -1. ELASTICSEARCH_HOSTS -1. ELASTICSEARCH_PASSWORD -1. ELASTICSEARCH_USERNAME -1. KIBANA_HOST +1. `ELASTICSEARCH_HOSTS` +1. `ELASTICSEARCH_PASSWORD` +1. `ELASTICSEARCH_USERNAME` +1. `KIBANA_HOST` Set these with the information for your Elasticsearch cluster and your Kibana host. Here are some examples (also see [*this configuration*](https://stackoverflow.com/questions/59892896/how-to-connect-from-minikube-to-elasticsearch-installed-on-host-local-developme/59892897#59892897)) #### `ELASTICSEARCH_HOSTS` + 1. A nodeGroup from the Elastic Elasticsearch Helm Chart: - ```shell - ["http://elasticsearch-master.default.svc.cluster.local:9200"] - ``` + ``` + ["http://elasticsearch-master.default.svc.cluster.local:9200"] + ``` + 1. A single Elasticsearch node running on a Mac where your Beats are running in Docker for Mac: - ```shell - ["http://host.docker.internal:9200"] - ``` + ``` + ["http://host.docker.internal:9200"] + ``` + 1. Two Elasticsearch nodes running in VMs or on physical hardware: - ```shell - ["http://host1.example.com:9200", "http://host2.example.com:9200"] - ``` -Edit `ELASTICSEARCH_HOSTS` + ``` + ["http://host1.example.com:9200", "http://host2.example.com:9200"] + ``` + +Edit `ELASTICSEARCH_HOSTS`: + ```shell vi ELASTICSEARCH_HOSTS ``` #### `ELASTICSEARCH_PASSWORD` -Just the password; no whitespace, quotes, or <>: - +Just the password; no whitespace, quotes, `<` or `>`: + +``` + +``` + +Edit `ELASTICSEARCH_PASSWORD`: -Edit `ELASTICSEARCH_PASSWORD` ```shell vi ELASTICSEARCH_PASSWORD ``` #### `ELASTICSEARCH_USERNAME` -Just the username; no whitespace, quotes, or <>: - +Just the username; no whitespace, quotes, `<` or `>`: + +``` + +``` + +Edit `ELASTICSEARCH_USERNAME`: -Edit `ELASTICSEARCH_USERNAME` ```shell vi ELASTICSEARCH_USERNAME ``` @@ -152,78 +174,98 @@ vi ELASTICSEARCH_USERNAME 1. The Kibana instance from the Elastic Kibana Helm Chart. The subdomain `default` refers to the default namespace. If you have deployed the Helm Chart using a different namespace, then your subdomain will be different: - ```shell - "kibana-kibana.default.svc.cluster.local:5601" - ``` + ``` + "kibana-kibana.default.svc.cluster.local:5601" + ``` + 1. A Kibana instance running on a Mac where your Beats are running in Docker for Mac: - ```shell - "host.docker.internal:5601" - ``` + ``` + "host.docker.internal:5601" + ``` 1. Two Elasticsearch nodes running in VMs or on physical hardware: - ```shell - "host1.example.com:5601" - ``` -Edit `KIBANA_HOST` + ``` + "host1.example.com:5601" + ``` + +Edit `KIBANA_HOST`: + ```shell vi KIBANA_HOST ``` -### Create a Kubernetes secret -This command creates a secret in the Kubernetes system level namespace (kube-system) based on the files you just edited: +### Create a Kubernetes Secret + +This command creates a Secret in the Kubernetes system level namespace (`kube-system`) based on the files you just edited: - kubectl create secret generic dynamic-logging \ - --from-file=./ELASTICSEARCH_HOSTS \ - --from-file=./ELASTICSEARCH_PASSWORD \ - --from-file=./ELASTICSEARCH_USERNAME \ - --from-file=./KIBANA_HOST \ - --namespace=kube-system +```shell +kubectl create secret generic dynamic-logging \ + --from-file=./ELASTICSEARCH_HOSTS \ + --from-file=./ELASTICSEARCH_PASSWORD \ + --from-file=./ELASTICSEARCH_USERNAME \ + --from-file=./KIBANA_HOST \ + --namespace=kube-system +``` {{% /tab %}} {{% tab name="Managed service" %}} ## Managed service + This tab is for Elasticsearch Service in Elastic Cloud only, if you have already created a secret for a self managed Elasticsearch and Kibana deployment, then continue with [Deploy the Beats](#deploy-the-beats). + ### Set the credentials -There are two files to edit to create a k8s secret when you are connecting to the managed Elasticsearch Service in Elastic Cloud. The files are: -1. ELASTIC_CLOUD_AUTH -1. ELASTIC_CLOUD_ID +There are two files to edit to create a Kubernetes Secret when you are connecting to the managed Elasticsearch Service in Elastic Cloud. The files are: + +1. `ELASTIC_CLOUD_AUTH` +1. `ELASTIC_CLOUD_ID` Set these with the information provided to you from the Elasticsearch Service console when you created the deployment. Here are some examples: -#### ELASTIC_CLOUD_ID -```shell +#### `ELASTIC_CLOUD_ID` + +``` devk8s:ABC123def456ghi789jkl123mno456pqr789stu123vwx456yza789bcd012efg345hijj678klm901nop345zEwOTJjMTc5YWQ0YzQ5OThlN2U5MjAwYTg4NTIzZQ== ``` -#### ELASTIC_CLOUD_AUTH +#### `ELASTIC_CLOUD_AUTH` + Just the username, a colon (`:`), and the password, no whitespace or quotes: -```shell + +``` elastic:VFxJJf9Tjwer90wnfTghsn8w ``` ### Edit the required files: + ```shell vi ELASTIC_CLOUD_ID vi ELASTIC_CLOUD_AUTH ``` -### Create a Kubernetes secret -This command creates a secret in the Kubernetes system level namespace (kube-system) based on the files you just edited: - kubectl create secret generic dynamic-logging \ - --from-file=./ELASTIC_CLOUD_ID \ - --from-file=./ELASTIC_CLOUD_AUTH \ - --namespace=kube-system +### Create a Kubernetes Secret + +This command creates a Secret in the Kubernetes system level namespace (`kube-system`) based on the files you just edited: + +```shell +kubectl create secret generic dynamic-logging \ + --from-file=./ELASTIC_CLOUD_ID \ + --from-file=./ELASTIC_CLOUD_AUTH \ + --namespace=kube-system +``` + +{{% /tab %}} - {{% /tab %}} {{< /tabs >}} ## Deploy the Beats + Manifest files are provided for each Beat. These manifest files use the secret created earlier to configure the Beats to connect to your Elasticsearch and Kibana servers. ### About Filebeat + Filebeat will collect logs from the Kubernetes nodes and the containers running in each pod running on those nodes. Filebeat is deployed as a {{< glossary_tooltip text="DaemonSet" term_id="daemonset" >}}. Filebeat can autodiscover applications running in your Kubernetes cluster. At startup Filebeat scans existing containers and launches the proper configurations for them, then it will watch for new start/stop events. Here is the autodiscover configuration that enables Filebeat to locate and parse Redis logs from the Redis containers deployed with the guestbook application. This configuration is in the file `filebeat-kubernetes.yaml`: @@ -242,20 +284,25 @@ Here is the autodiscover configuration that enables Filebeat to locate and parse enabled: true var.hosts: ["${data.host}:${data.port}"] ``` + This configures Filebeat to apply the Filebeat module `redis` when a container is detected with a label `app` containing the string `redis`. The redis module has the ability to collect the `log` stream from the container by using the docker input type (reading the file on the Kubernetes node associated with the STDOUT stream from this Redis container). Additionally, the module has the ability to collect Redis `slowlog` entries by connecting to the proper pod host and port, which is provided in the container metadata. ### Deploy Filebeat: + ```shell kubectl create -f filebeat-kubernetes.yaml ``` #### Verify + ```shell kubectl get pods -n kube-system -l k8s-app=filebeat-dynamic ``` ### About Metricbeat + Metricbeat autodiscover is configured in the same way as Filebeat. Here is the Metricbeat autodiscover configuration for the Redis containers. This configuration is in the file `metricbeat-kubernetes.yaml`: + ```yaml - condition.equals: kubernetes.labels.tier: backend @@ -267,22 +314,27 @@ Metricbeat autodiscover is configured in the same way as Filebeat. Here is the # Redis hosts hosts: ["${data.host}:${data.port}"] ``` + This configures Metricbeat to apply the Metricbeat module `redis` when a container is detected with a label `tier` equal to the string `backend`. The `redis` module has the ability to collect the `info` and `keyspace` metrics from the container by connecting to the proper pod host and port, which is provided in the container metadata. ### Deploy Metricbeat + ```shell kubectl create -f metricbeat-kubernetes.yaml ``` + #### Verify + ```shell kubectl get pods -n kube-system -l k8s-app=metricbeat ``` ### About Packetbeat + Packetbeat configuration is different than Filebeat and Metricbeat. Rather than specify patterns to match against container labels the configuration is based on the protocols and port numbers involved. Shown below is a subset of the port numbers. {{< note >}} -If you are running a service on a non-standard port add that port number to the appropriate type in `filebeat.yaml` and delete / create the Packetbeat DaemonSet. +If you are running a service on a non-standard port add that port number to the appropriate type in `filebeat.yaml` and delete/create the Packetbeat DaemonSet. {{< /note >}} ```yaml @@ -309,11 +361,13 @@ packetbeat.flows: ``` #### Deploy Packetbeat + ```shell kubectl create -f packetbeat-kubernetes.yaml ``` #### Verify + ```shell kubectl get pods -n kube-system -l k8s-app=packetbeat-dynamic ``` @@ -328,15 +382,17 @@ Similarly, view dashboards for Apache and Redis. You will see dashboards for lo To enable Metricbeat to retrieve the Apache metrics, enable server-status by adding a ConfigMap including a mod-status configuration file and re-deploy the guestbook. +## Scale your Deployments and see new pods being monitored + +List the existing Deployments: -## Scale your deployments and see new pods being monitored -List the existing deployments: ```shell kubectl get deployments ``` The output: -```shell + +``` NAME READY UP-TO-DATE AVAILABLE AGE frontend 3/3 3 3 3h27m redis-master 1/1 1 1 3h27m @@ -344,54 +400,56 @@ redis-slave 2/2 2 2 3h27m ``` Scale the frontend down to two pods: + ```shell kubectl scale --replicas=2 deployment/frontend ``` + The output: -```shell + +``` deployment.extensions/frontend scaled ``` + Scale the frontend back up to three pods: + ```shell kubectl scale --replicas=3 deployment/frontend ``` ## View the changes in Kibana + See the screenshot, add the indicated filters and then add the columns to the view. You can see the ScalingReplicaSet entry that is marked, following from there to the top of the list of events shows the image being pulled, the volumes mounted, the pod starting, etc. ![Kibana Discover](https://raw.githubusercontent.com/elastic/examples/master/beats-k8s-send-anywhere/scaling-up.png) - - ## {{% heading "cleanup" %}} Deleting the Deployments and Services also deletes any running Pods. Use labels to delete multiple resources with one command. 1. Run the following commands to delete all Pods, Deployments, and Services. - ```shell - kubectl delete deployment -l app=redis - kubectl delete service -l app=redis - kubectl delete deployment -l app=guestbook - kubectl delete service -l app=guestbook - kubectl delete -f filebeat-kubernetes.yaml - kubectl delete -f metricbeat-kubernetes.yaml - kubectl delete -f packetbeat-kubernetes.yaml - kubectl delete secret dynamic-logging -n kube-system - ``` + ```shell + kubectl delete deployment -l app=redis + kubectl delete service -l app=redis + kubectl delete deployment -l app=guestbook + kubectl delete service -l app=guestbook + kubectl delete -f filebeat-kubernetes.yaml + kubectl delete -f metricbeat-kubernetes.yaml + kubectl delete -f packetbeat-kubernetes.yaml + kubectl delete secret dynamic-logging -n kube-system + ``` 1. Query the list of Pods to verify that no Pods are running: - ```shell - kubectl get pods - ``` - - The response should be this: - - ``` - No resources found. - ``` + ```shell + kubectl get pods + ``` + The response should be this: + ``` + No resources found. + ``` ## {{% heading "whatsnext" %}} @@ -399,3 +457,4 @@ Deleting the Deployments and Services also deletes any running Pods. Use labels * Read more about [logging architecture](/docs/concepts/cluster-administration/logging/) * Read more about [application introspection and debugging](/docs/tasks/debug-application-cluster/) * Read more about [troubleshoot applications](/docs/tasks/debug-application-cluster/resource-usage-monitoring/) +