Sub topic 13
Logs and Metrics
You can't fix what you can't see. This chapter covers the three places an EKS cluster produces diagnostic data and how to collect each one: control plane logs, container logs, and metrics. It assumes you can run kubectl and the AWS CLI, know what a DaemonSet is, understand IAM roles for pods (IRSA or EKS Pod Identity from earlier chapters), and have a running cluster such as the demo cluster created earlier.
Where the Data Comes From
EKS splits observability along the same line as responsibility. AWS runs the control plane, so you can't log in to it. The only way to see what it did is to ask EKS to send its logs to CloudWatch. You run the data plane, so you must install collectors there.
| Source | Where it runs | How you get it | Destination |
|---|---|---|---|
| Control plane logs | AWS-managed | Turn on per log type | CloudWatch Logs group `/aws/eks/<cluster>/cluster` |
| Container logs | Your nodes | Log agent (Fluent Bit) | CloudWatch Logs groups under `/aws/containerinsights/<cluster>/` |
| Metrics | Your nodes | CloudWatch agent, metrics-server | CloudWatch namespace `ContainerInsights`, or the Kubernetes Metrics API |
Control Plane Logs
EKS can emit five log types. All are off by default.
| Type | What it records | Typical use |
|---|---|---|
| `api` | kube-apiserver process logs | API server errors and throttling |
| `audit` | Every request to the API server, with user and verb | Who deleted that Deployment? |
| `authenticator` | IAM-to-Kubernetes authentication decisions | Debugging "Unauthorized" errors |
| `controllerManager` | Built-in controller activity | Stuck rollouts, node lifecycle |
| `scheduler` | Scheduling decisions | Pods stuck in `Pending` |
You can declare the setting in the same eksctl config you used to create the cluster:
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: demo
region: us-east-1
cloudWatch:
clusterLogging:
enableTypes: ["api", "audit", "authenticator", "controllerManager", "scheduler"]
logRetentionInDays: 30For a cluster that already exists, apply only the logging section:
eksctl utils update-cluster-logging --config-file cluster.yaml --approveThe AWS CLI does the same without eksctl:
aws eks update-cluster-config \
--name demo \
--region us-east-1 \
--logging '{"clusterLogging":[{"types":["api","audit","authenticator","controllerManager","scheduler"],"enabled":true}]}'Either command starts a cluster update. Wait for it to finish before you change the logging setting again.
Querying with Logs Insights
Each log type writes to its own log streams inside one log group, with names such as kube-apiserver-audit-... and authenticator-.... Use CloudWatch Logs Insights on /aws/eks/demo/cluster to find, for example, recent deletions:
fields @timestamp, user.username, verb, objectRef.resource, objectRef.name
| filter @logStream like /kube-apiserver-audit/
| filter verb = "delete"
| sort @timestamp desc
| limit 50The user.username field shows the IAM identity that EKS mapped to Kubernetes. That makes the audit log the first place to look when you want to connect an action to a person or role. For access problems, query the authenticator stream instead. It shows which IAM principal was presented and whether a mapping was found.
Container Logs and Metrics with Container Insights
Kubernetes keeps container output (stdout and stderr) on the node, and the file disappears when the pod is removed. To keep logs, you need an agent on every node that ships them elsewhere. On EKS, the standard AWS choice is CloudWatch Container Insights, installed as the amazon-cloudwatch-observability add-on. It deploys two things:
- The CloudWatch agent as a DaemonSet. It collects node, pod and container metrics and sends them to CloudWatch.
- Fluent Bit as a DaemonSet. It tails container log files and sends them to CloudWatch Logs.
The agents need permission to write to CloudWatch. The simplest route is to attach the AWS-managed CloudWatchAgentServerPolicy to the node role. Pod-level permissions are tighter. Create the add-on with a Pod Identity association for the agent's service account instead, using the approach from the "Giving Pods AWS Permissions" chapter. The add-on creates a cloudwatch-agent service account in the amazon-cloudwatch namespace.
aws eks create-addon \
--cluster-name demo \
--region us-east-1 \
--addon-name amazon-cloudwatch-observability
aws eks describe-addon \
--cluster-name demo \
--region us-east-1 \
--addon-name amazon-cloudwatch-observability \
--query 'addon.status'
kubectl get pods -n amazon-cloudwatchWait until the status is ACTIVE. You should see one cloudwatch-agent pod and one fluent-bit pod for each node.
Container Insights creates these log groups:
| Log group | Contents |
|---|---|
| `/aws/containerinsights/demo/application` | Your containers' stdout and stderr |
| `/aws/containerinsights/demo/dataplane` | kubelet, kube-proxy and container runtime logs |
| `/aws/containerinsights/demo/host` | Node operating system logs |
| `/aws/containerinsights/demo/performance` | Performance events behind the metrics |
Metrics appear in the ContainerInsights CloudWatch namespace. Examples are pod_cpu_utilization, pod_memory_utilization, node_cpu_utilization and pod_number_of_container_restarts. They are tagged by dimensions such as ClusterName, Namespace and PodName. The CloudWatch console also gives you prebuilt Container Insights dashboards that group these by cluster, node, namespace and pod.
Fargate is different
Fargate pods can't run DaemonSets, so the add-on's agents don't cover them. For logs, Fargate has a built-in Fluent Bit router that you configure with a ConfigMap in a namespace named aws-observability. Fargate pods also need an execution role that allows writing to CloudWatch Logs. If your cluster mixes node groups and Fargate profiles, check each pod type separately.
The Metrics API: metrics-server
CloudWatch metrics are for dashboards and alarms outside the cluster. Inside the cluster, kubectl top and the Horizontal Pod Autoscaler read the Kubernetes Metrics API, which is served by metrics-server. EKS doesn't install it for you. Add it from the add-on catalog or with the project's manifest, then check it:
kubectl top nodes
kubectl top pods -A --sort-by=memorymetrics-server keeps only recent values and no history. Container Insights keeps history but is not what the HPA reads by default. You usually want both. The first gives the autoscaler live numbers, and the second lets you ask what happened last Tuesday.
Common Problems
- The log group exists but is empty. Check that the log type is actually enabled with
aws eks describe-cluster --name demo --query cluster.logging. Then check that the cluster update completed. - No application logs in CloudWatch. Run
kubectl logs -n amazon-cloudwatch -l k8s-app=fluent-bit. AnAccessDeniedmessage means the agent's role lacks CloudWatch permissions, so recheck the node role or Pod Identity association. - Metrics missing for some pods. Check whether those pods run on Fargate or on nodes that the DaemonSets cannot be scheduled on because of taints.
kubectl topreturns "Metrics API not available". metrics-server is not installed or its pod is unhealthy.- The bill grows faster than expected. Check which log group has the highest
IncomingBytes. Chatty applications and the audit log are the usual causes. Lower the log level, set retention, or filter noisy namespaces in the Fluent Bit configuration.
Exercise
A teammate says "someone deleted the web Deployment at around 14:00, but kubectl logs shows nothing useful and the pods are gone." Which log do you query, in which place, and what do you need to have enabled beforehand?
Show answer
Query the control plane audit log in the CloudWatch log group /aws/eks/<cluster>/cluster, using Logs Insights on the kube-apiserver-audit log streams. Filter on verb = "delete" and objectRef.name = "web" around that time. The user.username field shows who made the request. The audit log type must have been enabled before the deletion, because EKS doesn't record past events retroactively.
Exercise
Container Insights is installed, but the application log group has no entries and the fluent-bit pod logs show AccessDenied. What is wrong, and which two ways can you fix it?
Show answer
The Fluent Bit pods don't have permission to write to CloudWatch Logs. Either attach the CloudWatchAgentServerPolicy managed policy to the node IAM role, or give the agent's service account (cloudwatch-agent in amazon-cloudwatch) its own role through a Pod Identity association or IRSA. The second option limits the permission to those pods.