K8s pod monitor keeps track of the following two metrics
- the total number of pods that have been created, not necessarily scheduled, from the moment the k8s-pod-monitor has been deployed and until the moment of request
- the number of pods that are running at the moment of request
- Log in to kubernetes cluster
- Clone K8s-pod-monitor repository
git clone git@github.com:jayapriya90/k8s-pod-monitor.git
- cd into the repository
cd k8s-pod-monitor
- Apply pod monitor deployment
kubectl apply -f pod-monitor-deployment.yaml
- List pods from all the namespaces
kubectl get pods --all-namespaces
- Get
pod-monitor
custom resource.podRunningCount
in the pod-monitor status reflects the number of running pods in the cluster at the moment of request
kubectl get pm pod-monitor -o yaml
- cd into test_k8s_manifests repository
cd test_k8s_manifests
- Apply nginx deployment (replicas = 2. this'll create 2 nginx pods)
kubectl apply -f nginx-deployment.yaml
- Get
pod-monitor
custom resource.PodCreatedCount
in the pod-monitor status reflects the number of pods created from the moment the k8s-pod-monitor has been deployed and until the moment of request.podCreatedCount
is2
as we created 2 pods via the previous nginx deployment. The pods are currently in running state and hence thepodRunningCount
is updated from 9 to 11
kubectl get pm pod-monitor -o yaml
- Delete nginx test deployment
kubectl delete deployment nginx-deployment-test
- Get
pod-monitor
custom resource.podRunningCount
in the pod-monitor status is updated from 11 to 9 as the 2 nginx test pods are not running anymore (since deletion in the previous step)
- Go 1.10+
- Docker
- Kubernetes cluster
- Kubectl cli client
- dep
- gox (https://github.com/mitchellh/gox)
- cd into the repository
cd k8s-pod-monitor
- Build and Run. This will fetch all the dependencies, build the binary and start the controller
make run
- Run test. Open a new terminal and cd into
k8s-pod-monitor
directory.
make test
- Observe the controller log after running
make test
. The test auto-deploys test nginx deployment and checks if thepodRunningCount
is updated accordingly in thepod-monitor
CRD. At the end of the test,kubectl delete -f nginx-deployment.yaml
is automatically run to clean up the resources created for test
- Dockerize the solution and push to public docker hub so that this image can be referenced in the pod-monitor deployment yaml (
pod-monitor-deployment.yaml
). This is optional if there are no new enhancements/changes as we already have the image in docker hub.
make push-docker
- To make pod-monitor controller (responsible for metrics acquisition and storage into etcd) highly available.
leader-election
package in theclient-go
can be used to implement HA controllers (https://github.com/kubernetes/client-go/tree/066127c6df69d5945429ced9754841765b5a0310/tools/leaderelection)
- https://github.com/kubernetes/client-go
- https://github.com/kubernetes/sample-controller
- https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/
- https://medium.com/velotio-perspectives/extending-kubernetes-apis-with-custom-resource-definitions-crds-139c99ed3477
- https://medium.com/@trstringer/create-kubernetes-controllers-for-core-and-custom-resources-62fc35ad64a3
- https://itnext.io/how-to-create-a-kubernetes-custom-controller-using-client-go-f36a7a7536cc
- https://engineering.bitnami.com/articles/kubewatch-an-example-of-kubernetes-custom-controller.html
- https://github.com/kubernetes-sigs/kubebuilder