Commit 08d8655c authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

ADD Ingress Controller (Nginx) + Expose metrics for Prometheus

parent 689c1543
## Deploy Nginx as Ingress Controller
![img.png](img.png)
Read installation guide
* https://timtech4u.medium.com/understanding-kubernetes-ingress-resources-and-controllers-6caeb417acad
* https://devopscube.com/install-configure-helm-kubernetes/
##### Deploy Ingress Resource
```sh
kubectl apply -f ingress-resource.yaml
```
#### issue for metrics
* https://github.com/kubernetes/ingress-nginx/issues/8782
please adjust the args parameters in the deployment
args:
- /nginx-ingress-controller
- '--publish-service=$(POD_NAMESPACE)/ingress-nginx-controller'
- '--metrics-per-host=false' <<--------
\ No newline at end of file
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ms-demo-ingress
annotations:
# nginx.ingress.kubernetes.io/rewrite-target: /$1
kubernetes.io/ingress.class: "nginx"
spec:
defaultBackend:
service:
name: ms-demo-service
port:
number: 8080
rules:
#- host: my-app.com
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: hello-world-two
port:
number: 8080
- path: /fib
pathType: ImplementationSpecific
backend:
service:
name: ms-demo-service
port:
number: 8080
# Alternative Way to install Prometheus using Helm
* https://github.com/prometheus-community/helm-charts/tree/main/charts/prometheus
```sh
kubectl create namespace prometheus
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts --namespace prometheus
helm repo update
helm upgrade --install prometheus prometheus-community/prometheus -n prometheus
```
###### Note
you should prepare nfs server between your nodes then create the pv (persistent volume) as a shared folder to Promdata
* https://rudimartinsen.com/2022/01/05/nginx-nfs-kubernetes/
\ No newline at end of file
......@@ -23,3 +23,13 @@ data:
static_configs:
# Use the service name (see docker-compose-desktop.yml) instead of localhost
- targets: ['ms-demo-service.default.svc:8080']
- job_name: 'ms-demo-ingress'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
honor_timestamps: true
metrics_path: /metrics
scheme: http
static_configs:
# Use the service name (see docker-compose-desktop.yml) instead of localhost
- targets: ['ingress-controller-ingress-nginx-controller-metrics.ingress-nginx.svc:10254']
......@@ -20,6 +20,6 @@ kubectl apply -f service-monitor.yaml
##### Step 3
Deploy the service to cluster with horizontal pod autoscaling
```sh
kubectl apply -f app-deployment.yaml
kubectl apply -f app-service-deployment.yaml
kubectl apply -f hpa.yaml
```
apiVersion: v1
kind: Service
metadata:
name: hello-world-two
annotations:
prometheus.io/port: "hello2-metrics"
prometheus.io/scrape: "true"
labels:
app.kubernetes.io/part-of: hello-world-two-svc
spec:
ports:
- port: 8080
protocol: TCP
targetPort: 8080
name: "hello2-metrics"
selector:
app: hello-world-two
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: hello-world-two
name: hello-world-two
spec:
replicas: 2
selector:
matchLabels:
app: hello-world-two
template:
metadata:
labels:
app: hello-world-two
spec:
containers:
- image: mhdbashar/ms-demo
name: ms-demo
ports:
- containerPort: 8080
resources:
limits:
cpu: "1"
memory: 1Gi
requests:
cpu: 500m
memory: 500Mi
......@@ -19,3 +19,21 @@ spec:
app.kubernetes.io/part-of: ms-demo-svc
#ref: https://dev.to/apgapg/creating-a-service-monitor-in-k8s-nl8
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: hello2-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: hello2-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: hello-world-two-svc
......@@ -17,6 +17,6 @@ docker build -t ms-demo .
Deploy Services to Docker Desktop Kubernetes
```sh
kubectl apply -f monitoring-deployment.yaml
kubectl apply -f app-deployment.yaml
kubectl apply -f app-service-deployment.yaml
```
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