Tag Archives: prometheus

Chapter 15: Monitoring and Logging in Kubernetes

Introduction to Monitoring and Logging

Effective monitoring and logging are critical for managing Kubernetes clusters. These systems help identify performance issues, debug errors, and ensure smooth operations. Kubernetes provides tools and integrations for collecting, analyzing, and visualizing cluster and application data.

Why Monitoring and Logging?

  1. Visibility: Gain insights into cluster performance and resource utilization.
  2. Troubleshooting: Debug issues and resolve them faster.
  3. Optimization: Identify and reduce inefficiencies in the cluster.
  4. Compliance: Meet audit and regulatory requirements with robust logging.

Monitoring in Kubernetes

Monitoring involves collecting metrics from the Kubernetes control plane, nodes, and applications.

Popular Monitoring Tools

  1. Prometheus: Metrics collection and storage.
  2. Grafana: Visualization and alerting dashboards.
  3. Kube-State-Metrics: Provides Kubernetes resource metrics.
  4. cAdvisor: Container resource usage monitoring.

Step-by-Step Implementation of Monitoring

Step 1: Deploy Prometheus

1. Install Prometheus using Helm:

    helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
    helm repo update
    helm install prometheus prometheus-community/prometheus

    2. Verify Prometheus Installation:

    kubectl get pods -l app=prometheus

    3. Access Prometheus Dashboard: Forward the port to localhost:

    kubectl port-forward svc/prometheus-server 9090:80

    Access Prometheus at http://localhost:9090.

    Step 2: Deploy Grafana

    1. Install Grafana using Helm:

      helm repo add grafana https://grafana.github.io/helm-charts
      helm repo update
      helm install grafana grafana/grafana

      2. Retrieve Admin Password:

      kubectl get secret --namespace default grafana -o jsonpath="{.data.admin-password}" | base64 --decode ; echo

      3. Access Grafana Dashboard: Forward the port to localhost:

      kubectl port-forward svc/grafana 3000:80

      Access Grafana at http://localhost:3000.

      4. Add Prometheus as a Data Source:

      Select Prometheus and set the URL to http://<prometheus-service-ip>:9090.

      Navigate to Configuration > Data Sources.

      Step 3: Visualize Kubernetes Metrics

      1. Import pre-built Kubernetes dashboards from Grafana’s dashboard repository:
        • Dashboard ID for Kubernetes cluster metrics: 6417.
        • Dashboard ID for Node Exporter metrics: 1860.
      2. Use the dashboards to monitor cluster health, resource usage, and workload performance.

      Logging in Kubernetes

      Logging captures detailed application and cluster events, essential for debugging and analysis.

      Popular Logging Tools

      1. Fluentd: Aggregates and ships logs.
      2. Elasticsearch: Stores logs for querying and analysis.
      3. Kibana: Visualizes logs.
      4. Loki: Lightweight log aggregation solution.

      Step-by-Step Implementation of Logging

      Step 1: Deploy EFK (Elasticsearch, Fluentd, Kibana) Stack

      1. Deploy Elasticsearch:
      kubectl apply -f https://raw.githubusercontent.com/elastic/helm-charts/main/elasticsearch/examples/elasticsearch.yaml

      2. Deploy Fluentd:

      kubectl apply -f https://raw.githubusercontent.com/fluent/fluentd-kubernetes-daemonset/master/fluentd-daemonset-elasticsearch-rbac.yaml

      3. Deploy Kibana:

      kubectl apply -f https://raw.githubusercontent.com/elastic/helm-charts/main/kibana/examples/kibana.yaml

      4. Access Kibana Dashboard: Forward the port to localhost:

      kubectl port-forward svc/kibana 5601:5601

      Access Kibana at http://localhost:5601.

      Step 2: Collect Application Logs

      1. Annotate your Pods to send logs to Fluentd:

        metadata:
          annotations:
            fluentd.io/tag: "my-application"

        2. View logs in Kibana by filtering with your application tag.

        Step 3: Debugging with Logs

        1. View Logs for a Pod:

          kubectl logs <pod-name>

          2. Stream Logs:

          kubectl logs -f <pod-name>

          3. Filter Logs by Container:

          kubectl logs <pod-name> -c <container-name>

          4. View Previous Logs:

          kubectl logs <pod-name> --previous

          Monitoring and Logging Best Practices

          1. Set Alerts: Use Prometheus and Grafana to trigger alerts for critical issues.
          2. Retain Logs: Define a retention policy for storing logs in Elasticsearch or Loki.
          3. Secure Logs: Use role-based access control (RBAC) for sensitive log data.
          4. Optimize Performance: Scale monitoring and logging stacks as the cluster grows.
          5. Automate Dashboards: Use scripts or CI/CD pipelines to deploy and configure dashboards.

          Production Example: Comprehensive Monitoring and Logging

          Scenario

          You need to monitor a production-grade web application running in Kubernetes.

          1. Deploy the Web Application:
          kubectl run web-app --image=nginx --replicas=3
          1. Monitor Resource Usage:
            • Use Grafana dashboards to check CPU, memory, and storage usage.
          2. Set Alerts:
            • Configure Prometheus to alert if CPU usage exceeds 80%.
          3. Collect Logs:
            • Use Fluentd to ship logs to Elasticsearch.
            • Visualize logs in Kibana to debug errors.
          4. Troubleshoot Issues:
            • Use kubectl logs and Fluentd logs to investigate and resolve errors.

          Conclusion

          In this chapter, you learned:

          1. How to set up monitoring with Prometheus and Grafana.
          2. How to implement logging using the EFK stack.
          3. Best practices for managing monitoring and logging in production.