Commit 3e66101f authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

Add Service monitor

parent 4224f4b3
...@@ -11,9 +11,15 @@ Deploy Prometheus for monitoring in the cluster ...@@ -11,9 +11,15 @@ Deploy Prometheus for monitoring in the cluster
```sh ```sh
kubectl apply -f Prometheus/* kubectl apply -f Prometheus/*
``` ```
###### note
when we have a monitoring namespace for kubernetes we only need to apply service monitor for our service e.g:
```sh
kubectl apply -f service-monitor.yaml
```
##### Step 3 ##### Step 3
Deploy the service to cluster with horizontal pod autoscaling Deploy the service to cluster with horizontal pod autoscaling
```sh ```sh
kubectl apply -f app-deployment.yaml kubectl apply -f app-deployment.yaml
kubectl apply -f hpa.yaml kubectl apply -f hpa.yaml
``` ```
\ No newline at end of file
...@@ -2,6 +2,11 @@ apiVersion: v1 # Kubernetes API version ...@@ -2,6 +2,11 @@ apiVersion: v1 # Kubernetes API version
kind: Service # Kubernetes resource kind we are creating kind: Service # Kubernetes resource kind we are creating
metadata: # Metadata of the resource kind we are creating metadata: # Metadata of the resource kind we are creating
name: ms-demo-service name: ms-demo-service
annotations:
prometheus.io/port: "ms-metrics"
prometheus.io/scrape: "true"
labels:
app.kubernetes.io/part-of: ms-demo-svc
spec: spec:
selector: selector:
app: ms-demo app: ms-demo
...@@ -10,6 +15,7 @@ spec: ...@@ -10,6 +15,7 @@ spec:
port: 8080 # The port that the service is running on in the cluster port: 8080 # The port that the service is running on in the cluster
targetPort: 8080 # The port exposed by the service targetPort: 8080 # The port exposed by the service
nodePort: 30999 nodePort: 30999
name: "ms-metrics"
type: LoadBalancer # type of the service. type: LoadBalancer # type of the service.
--- ---
apiVersion: apps/v1 apiVersion: apps/v1
...@@ -34,9 +40,9 @@ spec: ...@@ -34,9 +40,9 @@ spec:
- containerPort: 8080 # The port that the container is running on in the cluster - containerPort: 8080 # The port that the container is running on in the cluster
resources: # important for hpa resources: # important for hpa
limits: limits:
cpu: "2" cpu: "1"
memory: 2Gi memory: 1Gi
requests: requests:
cpu: 500m cpu: 500m
memory: 1Gi memory: 500Mi
...@@ -24,6 +24,12 @@ spec: ...@@ -24,6 +24,12 @@ spec:
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
name: ms-demo name: ms-demo
# Doc
# kubectl autoscale deployment ms-demo-hpa --cpu-percent=50 --min=1 --max=10 // another way to create hpa # kubectl autoscale deployment ms-demo-hpa --cpu-percent=50 --min=1 --max=10 // another way to create hpa
# kubectl get hpa ms-demo-hpa --watch // monitor hpa # kubectl get hpa ms-demo-hpa --watch // monitor hpa
# kubectl scale --replicas=2 deployment.apps/ms-demo //scale manually # kubectl scale --replicas=2 deployment.apps/ms-demo //scale manually
\ No newline at end of file ## To monitor utilization:
# kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler='ms-demo-hpa'}/
# kube_horizontalpodautoscaler_spec_max_replicas{horizontalpodautoscaler='ms-demo-hpa'}*100
## To Monitor current replica
# kube_horizontalpodautoscaler_status_current_replicas{horizontalpodautoscaler='ms-demo-hpa'}
\ No newline at end of file
# A ServiceMonitor is a Kubernetes object that defines how Prometheus should monitor a specific service running in a Kubernetes cluster.
#ServiceMonitor allows Prometheus to automatically discover and scrape metrics from services that are running in the same Kubernetes cluster.
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: ms-demo-service-monitor
namespace: monitoring
spec:
endpoints: #This field specifies the endpoints that Prometheus should scrape for metrics. You can specify the port, path, and other information for each endpoint.
- interval: 15s
port: ms-metrics
scrapeTimeout: 14s
path: /actuator/prometheus
namespaceSelector:
matchNames:
- default
selector: #This field specifies how Prometheus should discover the target service to monitor. You can use labels, namespaces, and other selectors to choose the target service.
matchLabels:
app.kubernetes.io/part-of: ms-demo-svc
#ref: https://dev.to/apgapg/creating-a-service-monitor-in-k8s-nl8
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment