AWS Builder Center
How to monitor Persistent Volume disk usage in EKS using CloudWatch

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.html  file.
  • Mounts the same PV in a CloudWatch Agent sidecar container to collect disk usage metrics.
  1. Create PersistentVolumeClaim (PVC)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: standard-retain-policy
resources:
requests:
storage: 1Gi
2. Create the ConfigMap for CloudWatch Agent
apiVersion: v1
kind: ConfigMap
metadata:
name: cwagent-config
data:
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/v1
kind: Deployment
metadata:
name: nginx-with-sidecar
spec:
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

  1. CloudWatch Agent collects metrics: - The sidecar runs  amazon-cloudwatch-agent , configured via a  config.json  file (we passed via a  ConfigMap ). - It collects metrics like disk  used_percent  from the mounted volume ( /mnt/pv ).
  1. 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.
  1. Metrics are sent using the CloudWatch API: - The agent periodically pushes data (based on  metrics_collection_interval ) to CloudWatch using  PutMetricData  API. - Metrics go under the namespace  CWAgent  by default, unless customized.
  1. You can view these metrics: - In the CloudWatch Console → Metrics →  CWAgent  namespace

Another example with the StatefulSet

apiVersion: v1
kind: ConfigMap
metadata:
name: cwagent-config-ss
data:
config.json: |
{
"metrics": {
"metrics_collected": {
"disk": {
"measurement": [
"used_percent"
],
"metrics_collection_interval": 60,
"resources": [
"/mnt/pv-nginx"
]
}
}
}
}
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx-stateful
spec:
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: 1Gi
If 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/html
To overcome the situation, please use the below YAML file
apiVersion: v1
kind: ConfigMap
metadata:
name: cwagent-config-ss
data:
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/v1
kind: StatefulSet
metadata:
name: nginx-stateful
spec:
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: 1Gi
Press 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.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article