0
0
GCPcloud~5 mins

GKE monitoring and logging in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run applications on Google Kubernetes Engine (GKE), you need to see how they perform and if any errors happen. GKE monitoring and logging help you watch your apps and find problems quickly by collecting data and showing it in easy dashboards.
When you want to check if your app on GKE is running smoothly without crashes.
When you need to find out why your app is slow or not responding.
When you want to keep track of resource use like CPU and memory for your containers.
When you want to collect logs from all your app parts in one place for easy searching.
When you want to get alerts if something goes wrong with your GKE cluster or apps.
Config File - gke-monitoring.yaml
gke-monitoring.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: fluentd-config
  namespace: kube-system
data:
  fluent.conf: |
    <source>
      @type tail
      path /var/log/containers/*.log
      pos_file /var/log/fluentd-containers.log.pos
      tag kubernetes.*
      format json
    </source>
    <match kubernetes.**>
      @type stackdriver
      resource_type k8s_container
    </match>

This ConfigMap sets up Fluentd to collect logs from all containers in the GKE cluster. It tells Fluentd where to find the logs and how to send them to Google Cloud's logging service called Stackdriver (now Cloud Logging). This helps centralize logs for easy viewing and searching.

Commands
This command applies the logging configuration to your GKE cluster, setting up Fluentd to collect and send logs to Cloud Logging.
Terminal
kubectl apply -f gke-monitoring.yaml
Expected OutputExpected
configmap/fluentd-config created
This command checks if the Fluentd pods responsible for sending logs are running properly in the kube-system namespace.
Terminal
kubectl get pods -n kube-system -l k8s-app=fluentd-cloud-logging
Expected OutputExpected
NAME READY STATUS RESTARTS AGE fluentd-cloud-logging-abc123 1/1 Running 0 2m
-n kube-system - Specifies the namespace where Fluentd runs
-l k8s-app=fluentd-cloud-logging - Filters pods by label to find Fluentd pods
This command shows which logging and monitoring services are enabled on your GKE cluster to confirm Cloud Logging and Cloud Monitoring are active.
Terminal
gcloud container clusters describe example-cluster --zone us-central1-a --format='value(loggingService,monitoringService)'
Expected OutputExpected
logging.googleapis.com/kubernetes monitoring.googleapis.com/kubernetes
--zone us-central1-a - Specifies the cluster zone
--format='value(loggingService,monitoringService)' - Formats output to show only logging and monitoring services
This command shows the current CPU and memory usage of pods in the default namespace, helping you monitor resource use.
Terminal
kubectl top pods --namespace default
Expected OutputExpected
NAME CPU(cores) MEMORY(bytes) my-app-1234567890-abcde 50m 100Mi
--namespace default - Shows resource usage for pods in the default namespace
Key Concept

If you remember nothing else, remember: setting up Fluentd in GKE sends your app logs to Cloud Logging so you can easily watch and troubleshoot your apps.

Common Mistakes
Not applying the logging ConfigMap to the correct namespace.
Fluentd runs in the kube-system namespace, so applying config elsewhere means logs won't be collected.
Always apply logging configurations in the kube-system namespace where Fluentd runs.
Forgetting to check if Cloud Logging and Monitoring are enabled on the cluster.
Without these services enabled, logs and metrics won't be collected or visible in Google Cloud Console.
Use gcloud commands to verify loggingService and monitoringService are set to Kubernetes services.
Running kubectl top without metrics-server installed.
kubectl top needs metrics-server to provide resource usage data; without it, the command fails.
Ensure metrics-server is installed and running in your cluster before using kubectl top.
Summary
Apply a Fluentd ConfigMap to collect container logs and send them to Cloud Logging.
Check Fluentd pods are running in the kube-system namespace to confirm logging is active.
Verify Cloud Logging and Monitoring services are enabled on your GKE cluster.
Use kubectl top to monitor pod resource usage in real time.