Service monitor issue in Prometheus operator - prometheus-operator

I have service and servicemonitor defined as below,
kind: Service
apiVersion: v1
metadata:
name: example-application
labels:
app: example-application
teamname: neon
spec:
selector:
app: example-application
ports:
- name: backend
port: 8080
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: example-application
namespace: monitoring
spec:
selector:
matchLabels:
app: example-application
endpoints:
- port: backend
path: /prometheus
namespaceSelector:
matchNames:
- testns
targetLabels:
- teamname
Pods are all available.I have tested the service.
But the Targets are showing as DOWN in Prometheus.Please let me know what I am missing.

I found the missing piece..I had to expose metrics in the application on that path..
So I followed this article https://medium.com/kubernetes-tutorials/simple-management-of-prometheus-monitoring-pipeline-with-the-prometheus-operator-b445da0e0d1a
For an example purpose, I used the same image given in this article as side car container for metrics in my deployment object as shown below,
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: example-application
spec:
replicas: 4
template:
metadata:
labels:
app: example-application
spec:
containers:
- name: example-application
image: nginx
ports:
- name: backend
containerPort: 80
- name: rpc-app-cont
image: supergiantkir/prometheus-test-app
ports:
- name: web
containerPort: 8081
Then added this port "web" to service monitor.
Now its working fine..

Related

MetalLB Cannot access service from outside of k8s cluster

I'm getting problem accessing k8s service from outside of cluster
K8s version: v1.25
MetalLB version: 0.13 (Flannel v0.20)
IP pool
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: first-pool
namespace: metallb-system
spec:
addresses:
- 151.62.196.222-151.62.196.222
Advertisement
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: adv
namespace: metallb-system
spec:
ipAddressPools:
- first-pool
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Service
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: LoadBalancer
When I check services, I saw the nginx-service has been assigned IP (EXTERNAL-IP: 151.62.196.222)
But when I try to access, it returns me error
curl: (7) Failed to connect to 151.62.196.222 port 80 after 15 ms: Connection refused
Anyone experienced the same problem? Thanks!
I tried to access it from the cluster, and it works, returned me the nginx welcome page

Unable to reach service with ingress-nginx

I am really new to Kubernetes/minikube... I have to make service and hit "/first" and use ingress-nginx without ingress addon in minikube. Even though I have added to my YAML file ingressClassName: nginx spec field. I am unable to reach my service and I am getting 404 Not Found. With ingress addon it works perfectly.Any idea what am I doing wrong?
Thank you for your time!
My Yaml file with some changes:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /first
pathType: Prefix
backend:
service:
name: first
port:
number: 8080
---
apiVersion: v1
kind: Service
metadata:
name: first
spec:
type: ClusterIP
ports:
- port: 8080
selector:
app: first
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: first
spec:
replicas: 1
selector:
matchLabels:
app: first
template:
metadata:
labels:
app: first
spec:
containers:
- name: first
image: myimage:latest
env:
- name: MESSAGE
value: "This is the first service"
resources:
limits:
cpu: "200m"
memory: "256Mi"
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: first-flask
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: first
minReplicas: 1
maxReplicas: 8
targetCPUUtilizationPercentage: 80

Script to produces Kubernetes manifests to deploy bare nginx container

I would like to have a script that produces Kubernetes manifests to deploy a bare nginx container with service port 80 and ingress for host as example.nginx.com. I will deploy it into EKS cluster. Can someone give me clue?
Recently I started configuring NLB with Nginx controller on EKS. So documenting the complete flow with the script you needed.
I tried other approached like cloud provider based Nginx deployment but it didn't work as expected ( instead of ELB it was creating Classic LB).
Ref- https://github.com/kubernetes/ingress-nginx/issues/6292
In short below approach is the best so far.
Install Nginx controller- This will create a deployment and a NodePort service for HTTP say port- 31848, HTTPS- 30099
#kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.1/deploy/static/provider/baremetal/deploy.yaml
Create Production deployment, service and ingress resource.
apiVersion: apps/v1
kind: Deployment
metadata:
name: production
labels:
app: production
namespace: app
spec:
replicas: 1
selector:
matchLabels:
app: production
template:
metadata:
labels:
app: production
spec:
containers:
- name: production
image: mirrorgooglecontainers/echoserver:1.10
ports:
- containerPort: 8080
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
---
apiVersion: v1
kind: Service
metadata:
name: production
labels:
app: production
namespace: app
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
app: production
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: production
annotations:
kubernetes.io/ingress.class: nginx
namespace: app
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
serviceName: production
servicePort: 80
Create Canary Deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
name: canary
labels:
app: canary
namespace: app
spec:
replicas: 1
selector:
matchLabels:
app: canary
template:
metadata:
labels:
app: canary
spec:
containers:
- name: canary
image: mirrorgooglecontainers/echoserver:1.10
ports:
- containerPort: 8080
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
---
apiVersion: v1
kind: Service
metadata:
name: canary
labels:
app: canary
namespace: app
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
app: canary
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: canary
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-weight: "30"
namespace: app
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
serviceName: canary
servicePort: 80
Create a NLB type Load Balancer on EKS. If you are choosing "internet-facing".
Create a Target Group with "Target Type" as instance and Port/Health Check Port- 31848 (HTTP).
Attach Target Group to Autoscaling group.
Create a listener on NLB (TLS- Secure TCP) and forward it to the Target Group.
Although we would be launching worker nodes on Private subnets but we need to open port "31848" for all the IP. This is how EC2 would be able to communicate with NLB.
Hope I am able to provide you clear idea on this. Please do let me know in case you face any issue.
You must have deployed nginx-ingress to your cluster. Then run the following script at your command prompt to deploy a bare nginx container with service port 80 and ingress for host as example.nginx.com:
cat << EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
selector:
app: nginx
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: example.nginx.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80
EOF

Need help on configuring Nginx ingress controller for hcloud-cloud-controller-manager

I'm trying to follow this suggestion to use Hetzner load balancer as a Nginx ingress controller.
helm install ingress-nginx with these configurations:
controller:
config:
use-proxy-protocol: "true"
replicaCount: 3
service:
type: LoadBalancer
annotations:
load-balancer.hetzner.cloud/name: nginx-controller-new
load-balancer.hetzner.cloud/location: hel1
load-balancer.hetzner.cloud/use-private-ip: true
load-balancer.hetzner.cloud/algorithm-type: least_connections
load-balancer.hetzner.cloud/uses-proxyprotocol: true
load-balancer.hetzner.cloud/hostname: somehost.com
Deployments:
apiVersion: apps/v1
kind: Deployment
metadata:
name: echo1
spec:
selector:
matchLabels:
app: echo1
replicas: 3
template:
metadata:
labels:
app: echo1
spec:
containers:
- name: echo1
image: hashicorp/http-echo
args:
- "-text=echo1"
ports:
- containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
name: echo-service
spec:
selector:
app: echo1
ports:
- name: http
protocol: TCP
port: 80
targetPort: 5678
ipFamilyPolicy: PreferDualStack
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
spec:
ingressClassName: nginx
rules:
- http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: echo-service
port:
number: 80
host: somehost.com
After installation, a Hetzner load balancer is successfully provisioned, however, it isn't able to detect the services:
I'm at a loss here. How can I connect the echo1 app to the ingress-nginx-controller service? I check out all of the available helm values but I cannot find something like service.selector to target echo1's service and make it publicly available. Can someone help me? Are there any alternatives?
I am not the Kubernetes master (more a noob) but I got it working with a L4-loadbalancer.
An annotation has to be set to your Ingress:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
And to use it without nginx-ingress, that worked for me (not tested with your labels)
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "nginx-hello-world"
labels:
app: "hello-world"
spec:
selector:
matchLabels:
app: "hello-world"
strategy:
type: "Recreate"
template:
metadata:
labels:
app: "hello-world"
spec:
containers:
- image: "rancher/hello-world"
name: "nginx-hello-world"
imagePullPolicy: "Always"
ports:
- containerPort: 80
name: "http"
Service
for HTTP (to test your deployment)
---
apiVersion: "v1"
kind: Service
metadata:
name: "nginx-hello-world"
labels:
app: "hello-world"
annotations:
load-balancer.hetzner.cloud/name: lb-development
load-balancer.hetzner.cloud/hostname: somehost.com
load-balancer.hetzner.cloud/protocol: http
load-balancer.hetzner.cloud/health-check-port: 10254
spec:
type: LoadBalancer
selector:
app: "hello-world"
ports:
- name: "http"
port: 80
targetPort: 80
for SSL
apiVersion: v1
kind: Service
metadata:
name: nginx-hello-world
labels:
app: hello-world
annotations:
load-balancer.hetzner.cloud/hostname: somehost.com
load-balancer.hetzner.cloud/http-certificates: managed-certificate-1-wildcard-somehost.com
load-balancer.hetzner.cloud/name: lb-development
load-balancer.hetzner.cloud/protocol: https
spec:
ports:
- name: https
nodePort: 32725
port: 443
protocol: TCP
targetPort: 80
selector:
app: hello-world
type: LoadBalancer

Cronjob for service

I need your help in understanding how to create a Kubernetes cronjob that will use curl using service nginx-server-service to get /customdir/index.html is there any way to do that?
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-server
labels:
tier: application
spec:
replicas: 1
selector:
matchLabels:
app: nginx-server
template:
metadata:
labels:
app: nginx-server
spec:
volumes:
- name: index-html
emptyDir: {}
initContainers:
- name: setup
image: alpine:3.8
command:
- /bin/sh
- -c
- echo kubernetes works > /task-dir/index.html
volumeMounts:
- name: index-html
mountPath: "/task-dir"
containers:
- name: nginx
image: nginx:mainline
ports:
- containerPort: 80
volumeMounts:
- name: index-html
mountPath: /usr/share/nginx/html/customdir
---
# next, a service is required to handle traffic to the pods created by the deployment
apiVersion: v1
kind: Service
metadata:
name: nginx-server-service
labels:
tier: networking
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30001
selector:
app: nginx-server
type: ClusterIP
I will very much appreciate any help in digging into taht
I'm working on solution so may i ask you if it will work :
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: curljob
spec:
jobTemplate:
metadata:
name: curljob
spec:
template:
metadata:
spec:
containers:
- command:
- curl
- http://myapp-service:30001/customdir
image: curlimages/curl
imagePullPolicy: Always
name: curljobt
restartPolicy: OnFailure
schedule: '*/1 * * * *'
i edited a bit but not sure why its not working :( begging for help
first of all you you are using a ClusterIP service type as a service. so you can't provide NodePort here.
then when you are trying to access a pod with service you need to call with a dns name that maintain a template.
The template is like my_Service_Name.my_Namespace.svc.cluster-domain.example , but you can skip the cluster-domain.example part. Only Service_Name.Namespace.svc will work fine.
so here you need to curl http://nginx-server-service.default.svc/customdir/index.html this address.
here i have given the whole yaml that worked for me;
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-server
labels:
tier: application
spec:
replicas: 1
selector:
matchLabels:
app: nginx-server
template:
metadata:
labels:
app: nginx-server
spec:
volumes:
- name: index-html
emptyDir: {}
initContainers:
- name: setup
image: alpine:3.8
command:
- /bin/sh
- -c
- echo kubernetes works > /task-dir/index.html
volumeMounts:
- name: index-html
mountPath: "/task-dir"
containers:
- name: nginx
image: nginx:mainline
ports:
- containerPort: 80
volumeMounts:
- name: index-html
mountPath: /usr/share/nginx/html/customdir
---
# next, a service is required to handle traffic to the pods created by the deployment
apiVersion: v1
kind: Service
metadata:
name: nginx-server-service
labels:
tier: networking
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx-server
type: ClusterIP
---
here is the updated cronjob
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: curljob
spec:
jobTemplate:
metadata:
name: curljob
spec:
template:
metadata:
spec:
containers:
- command:
- curl
- http://nginx-server-service.default.svc/customdir/index.html
image: curlimages/curl
imagePullPolicy: Always
name: curljobt
restartPolicy: OnFailure
schedule: '*/1 * * * *'

Resources