
How to monitor Persistent Volume disk usage in EKS using CloudWatch
Scenario: How to monitor Persistent Volume disk usage in EKS using CloudWatch Observability Add-on? https://repost.aws/questions/QUkku6VTB5TFORR4ezGhaV5A/how-to-monitor-persistent-volume-disk-usage-in-eks-using-cloudwatch-observability-add-on
Yes, I’ve had the same question while working on the EKS project. Suppose we have only Container Insights for EKS Prod monitoring, and we need to monitor the PVC usage attached to the deployment and stateful sets.
So I tried different methods, and at last tried the method with the sidecar container.
- Update the Nodes, role group with the CloudWatch PutMetricData and EC2 Describe policy.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "cloudwatch:PutMetricData" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "ec2:DescribeVolumes", "ec2:DescribeTags", "ec2:DescribeInstances" ], "Resource": "*" } ]}In this example
- Deploys an NGINX app on EKS using a Persistent Volume (PV) and Persistent Volume Claim (PVC) to serve a
index.htmlfile.
- Mounts the same PV in a CloudWatch Agent sidecar container to collect disk usage metrics.
- Create PersistentVolumeClaim (PVC)
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: nginx-pvcspec: accessModes: - ReadWriteOnce storageClassName: standard-retain-policy resources: requests: storage: 1Gi2. Create the ConfigMap for CloudWatch Agent
apiVersion: v1kind: ConfigMapmetadata: name: cwagent-configdata: config.json: | { "metrics": { "metrics_collected": { "disk": { "measurement": [ "used_percent" ], "metrics_collection_interval": 60, "resources": [ "/mnt/pvc" ] } } } }3. Create the NGINX Deployment with CloudWatch Agent sidecar
apiVersion: apps/v1kind: Deploymentmetadata: name: nginx-with-sidecarspec: replicas: 1 selector: matchLabels: app: nginx-sidecar template: metadata: labels: app: nginx-sidecar spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html - name: cloudwatch-agent image: amazon/cloudwatch-agent:latest volumeMounts: - name: html mountPath: /mnt/pvc - name: cwagentconfig mountPath: /etc/cwagentconfig command: ["/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent"] args: - "-config" - "/etc/cwagentconfig/config.json" volumes: - name: html persistentVolumeClaim: claimName: nginx-pvc - name: cwagentconfig configMap: name: cwagent-config- Apply all the above manifests using
kubectl apply -f.
- Ensure the IAM role attached to the node has the necessary permissions to push metrics to CloudWatch.
- In the CloudWatch Console → Metrics → CWAgent namespace
Press enter or click to view image in full size
How It Pushes Metrics to CloudWatch
- CloudWatch Agent collects metrics:
- The sidecar runs
amazon-cloudwatch-agent, configured via aconfig.jsonfile (we passed via aConfigMap). - It collects metrics like diskused_percentfrom the mounted volume (/mnt/pv).
- The agent uses EC2 instance metadata or IAM role: - The CloudWatch Agent uses the IAM role assigned to the EKS node (EC2 instance) to authenticate with AWS. - It fetches temporary credentials using the instance profile.
- Metrics are sent using the CloudWatch API:
- The agent periodically pushes data (based on
metrics_collection_interval) to CloudWatch usingPutMetricDataAPI. - Metrics go under the namespaceCWAgentby default, unless customized.
- You can view these metrics:
- In the CloudWatch Console → Metrics →
CWAgentnamespace
Another example with the StatefulSet
apiVersion: v1kind: ConfigMapmetadata: name: cwagent-config-ssdata: config.json: | { "metrics": { "metrics_collected": { "disk": { "measurement": [ "used_percent" ], "metrics_collection_interval": 60, "resources": [ "/mnt/pv-nginx" ] } } } }apiVersion: apps/v1kind: StatefulSetmetadata: name: nginx-statefulspec: replicas: 3 selector: matchLabels: app: nginx-stateful serviceName: "nginx-headless" template: metadata: labels: app: nginx-stateful spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html - name: cloudwatch-agent image: public.ecr.aws/cloudwatch-agent/cloudwatch-agent:latest command: ["/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent"] args: ["-config", "/etc/cwagentconfig/config.json"] volumeMounts: - name: html mountPath: /mnt/pv - name: cwagentconfig mountPath: /etc/cwagentconfig volumes: - name: cwagentconfig configMap: name: cwagent-config-ss volumeClaimTemplates: - metadata: name: html spec: accessModes: [ "ReadWriteOnce" ] storageClassName: standard-retain-policy resources: requests: storage: 1GiIf your StatefulSet is running in a different namespace (e.g.
monitoring , dev , etc.), ensure that you apply the ConfigMap in the same namespace.If you have more than 3 subnets, sometimes you will face the issue with disk mount,as below. On two nodes /dev/nvme1n1(Block Device) is used to mount the PVC
/dev/nvme1n1 974M 201M 758M 21% /usr/share/nginx/html
/dev/nvme2n1 974M 101M 858M 11% /usr/share/nginx/html
/dev/nvme1n1 974M 301M 658M 32% /usr/share/nginx/htmlTo overcome the situation, please use the below YAML file
apiVersion: v1kind: ConfigMapmetadata: name: cwagent-config-ssdata: config.json: | { "agent": { "omit_hostname": false, "region": "us-east-1" }, "metrics": { "namespace": "PVCDiskMetrics", "metrics_collected": { "disk": { "measurement": [ "used_percent" ], "resources": [ "/mnt/pvaws" ], "drop_device": true, "metrics_collection_interval": 60 } } } }
apiVersion: apps/v1kind: StatefulSetmetadata: name: nginx-statefulspec: replicas: 3 selector: matchLabels: app: nginx-stateful serviceName: "nginx-headless" template: metadata: labels: app: nginx-stateful spec:#serviceAccountName: cwagent-sa containers: - name: nginx image: nginx:latest ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html - name: cloudwatch-agent image: public.ecr.aws/cloudwatch-agent/cloudwatch-agent:latest command: ["/opt/aws/amazon-cloudwatch-agent/bin/start-amazon-cloudwatch-agent"] args: ["-config", "/etc/cwagentconfig/config.json"] volumeMounts: - name: html mountPath: /mnt/pvaws - name: cwagentconfig mountPath: /etc/cwagentconfig resources: limits: memory: "200Mi" cpu: "200m" requests: memory: "100Mi" cpu: "100m" volumes: - name: cwagentconfig configMap: name: cwagent-config-ss volumeClaimTemplates: - metadata: name: html spec: accessModes: [ "ReadWriteOnce" ] storageClassName: standard-retain-policy resources: requests: storage: 1GiPress enter or click to view image in full size
Please let me know if you encounter any issues with this method. Happy to help you.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article