Sunday, July 28, 2024

Kubernetes , Prometheus and Grafana

Kubernetes with Prometheus and Grafane

  • Helm Chart : Package installer for kubernetes

  • Prometheus: Open-source monitoring and alerting toolkit ( A Prometheus deployment needs dedicated storage space to store scraping data)

  • Grafana : visualization, and observability in Prometheus

1. Pre-Req

minikube start 		# minikube start --memory='12g' --cpus='4' -n 3
kubectl config view | grep namespace #get current namespace
kubectl cluster-info
minikube addons list

# Extra 
		minikube addons enable csi-hostpath-driver	 
		# Enable dedicated storage space to store scraping data  
		kubectl patch storageclass csi-hostpath-sc -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
		kubectl patch storageclass standard -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"false"}}}'

2. helm:

	Install helm - https://helm.sh/docs/intro/install/
	kubectl get ns
	kubectl create namespace monitoring
	kubectl get deployments --namespace=monitoring
	kubectl get pods --namespace=monitoring
	kubectl get configmap -n monitoring
	helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
	helm repo list
	helm repo update

3. prometheus:

	helm install prometheus prometheus-community/prometheus --namespace monitoring
	kubectl get pods -n monitoring
	kubectl expose service prometheus-server --namespace monitoring --type=NodePort --target-port=9090 --name=prometheus-server-ext
	minikube ip
	kubectl get svc -n monitoring
	#Prometheus Server UI = minikubeIP + 30333 [prometheus-server-ext port] eg : 192.168.49.2:30333

4. grafana

	helm repo add grafana https://grafana.github.io/helm-charts
	helm install grafana grafana/grafana --namespace monitoring
	#run the command in the display to get password -SCaxM0KE4GRzxxxx
	
	kubectl expose service grafana --namespace monitoring --type=NodePort --target-port=3000 --name=grafana-ext
	kubectl get svc -n monitoring
	#Grafana  UI = minikubeIP + 32084 [grafana-ext ] 

5. Integrate Promethus to Grafana

	kubectl get svc -n monitoring #copy PORT
	Open URL Grafana = minikubeIP + 32084 [grafana-ext port]
	usn = admin , password from output command during installation	
	Home > Add first Datasource > prometheus >Add URL =  http://prometheusURL >save and test
	Home > Dashboard > New > Import > Enter 3662 >Load

Sunday, July 21, 2024

Multi thread in Python

'''
threading
1. No return
2. Can run seperatr function
concurrent.futures
1. Return
2. Only run 1 Function in 1 map
'''
from concurrent.futures import *
import threading
import time
l=[]
def fn(i):
print(f"{threading.get_native_id()}")
time.sleep(i)
l.append(i)
return i
t1=threading.Thread(target=fn,args=(1,))
t1.start()
t1.join()
with ThreadPoolExecutor(2) as e:
res=e.map(fn,[2,3])
for i in res:print(i)
print(l)
view raw multiThread.py hosted with ❤ by GitHub

Async in Python

'''
Calling Normal Function using Async Functions
1. to_thread
2. gather+to_thread
Calling ASync Function using Async Functions
1. No need of to_thread
2. only gather
'''
import asyncio
import threading
import time
#Normal Function
def normal(a):
print("Running normal")
time.sleep(a)
print(f"{threading.get_native_id()}")
print(f"done wait {a}")
#Async Function
async def asyncFn(i):
print("Running asyncFn")
await asyncio.sleep(i)
print(i)
return i
async def main():
t1=asyncio.create_task(asyncFn(3))
result1=await t1
await asyncio.gather(asyncFn(3),asyncFn(1))
await asyncio.to_thread(normal,1)
await asyncio.gather(asyncio.to_thread(normal,3),asyncio.to_thread(normal,1))
asyncio.run(main())
view raw async.py hosted with ❤ by GitHub