Commit 8cac1560 authored by SIVAPRASAD S's avatar SIVAPRASAD S

added metrics

parent 7745ed2b
ssh cs695@http://10.129.131.202
passwd:1234
sudo vi /etc/nginx/conf.d/loadbalancer.conf
systemctl restart nginx
FROM python:3.9-slim
RUN mkdir /app
# Set the working directory in the container
WORKDIR /app
ADD . /app/
# Copy the function code into the container
COPY function.py .
# Install any dependencies
RUN pip install -r requirements.txt
RUN pip install prometheus_client
RUN pip install psutil
EXPOSE 5000
# Define the command to run the function
CMD ["python", "/app/function.py"]
from flask import Flask
from prometheus_client import Counter, Gauge, start_http_server
from prometheus_client import generate_latest, CONTENT_TYPE_LATEST, CollectorRegistry
import psutil
import time
app = Flask(__name__)
# Define Prometheus metrics
function_requests_total = Counter('function_requests_total', 'Total number of requests to the function')
cpu_utilization_gauge = Gauge('cpu_utilization_percentage', 'CPU utilization percentage')
request_rate_gauge = Gauge('function_request_rate', 'Function request rate (requests per second)')
service_time_gauge = Gauge('function_service_time_seconds', 'Service time of the function in seconds')
network_bytes_sent = Counter('network_bytes_sent_total', 'Total number of bytes sent over the network')
network_bytes_recv = Counter('network_bytes_received_total', 'Total number of bytes received over the network')
start_time=time.time()
@app.route("/clear_metrics")
def clear_metrics():
function_requests_total._value.set(0)
cpu_utilization_gauge.set(0)
service_time_gauge.set(0)
network_bytes_sent._value.set(0)
network_bytes_recv._value.set(0)
start_time=time.time()
registry = CollectorRegistry()
return generate_latest(registry), 200, {"Content-Type": CONTENT_TYPE_LATEST}
@app.route("/")
def hello():
time_invoked=time.time()
# Increment request counter
function_requests_total.inc()
time.sleep(0.1)
service_time = time.time() - time_invoked
service_time_gauge.set(service_time)
# Collect CPU utilization
cpu_utilization = psutil.cpu_percent()
cpu_utilization_gauge.set(cpu_utilization)
return "Hello from Python function"
@app.route("/metrics")
def metrics():
registry = CollectorRegistry()
# Register your Prometheus metrics collectors here
# For example:
# registry.register(...)
elapsed_time = time.time() - start_time
registry.register(function_requests_total)
registry.register(cpu_utilization_gauge)
registry.register(service_time_gauge)
registry.register(network_bytes_sent)
registry.register(network_bytes_recv)
metric=generate_latest(registry)
for line in metric.decode().split("\n"):
if "function_requests_total" in line:
try:
value = float(line.split()[1])
request_rate = value/elapsed_time
except:
pass
if "function_requests_total" in line:
try:
no_of_requests = float(line.split()[1])
except:
pass
if "cpu_utilization_percentage" in line:
try:
cpu_utilization = float(line.split()[1])
except:
pass
if "function_service_time_seconds" in line:
try:
service_time= float(line.split()[1])
except:
pass
if "network_bytes_sent_total" in line:
try:
bytes_sent= float(line.split()[1])
except:
pass
if "network_bytes_received_total" in line:
try:
bytes_received= float(line.split()[1])
except:
pass
request_rate_gauge.set(request_rate)
registry.register(request_rate_gauge)
network_stats = psutil.net_io_counters()
network_bytes_sent.inc(network_stats.bytes_sent)
network_bytes_recv.inc(network_stats.bytes_recv)
print("Number of Requests: ",no_of_requests)
print("Request_rate: ",request_rate)
print("Service Time: ",service_time)
print("CPU utilization: ",cpu_utilization)
print("Bytes send: ",bytes_sent)
print("Bytes received: ",bytes_received)
return generate_latest(registry), 200, {"Content-Type": CONTENT_TYPE_LATEST}
if __name__ == "__main__":
# Start Prometheus HTTP server to expose metrics
start_http_server(8000)
# Run Flask app
app.run(host='0.0.0.0')
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
spec:
rules:
- host: exmample1-service.com
http:
paths:
- path: /example1
pathType: Prefix
backend:
service:
name: example1-service
port:
number: 8080
-----BEGIN PGP PUBLIC KEY BLOCK-----
mQGNBGYuJHcBDAC3RyCWZT3ZeFpMIfpP3OTy1T99bgCG7ZTZ2IyYncQ1RWDTVY2B
uyB7y55wonqqemehs7mh721iwrX2oOgvxkV/6VV2ibQvMq9Qy4GR66vSbeLdumpN
Hux7sQV6RCahtYauNvfOxexuRZHEidNZXJXNWebI+u/SUSctH7yggpW67xKx3g92
XwEixd8MnIUjAyi96CiAjU20fnMuBNH7MMY+pGR9sO9sHnsvrsiu7KVdRmRNeenF
579Uh3Dko9qyhVQn8ROJ1/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config()
k8s_client = client.ApiClient()
# Docker Hub username (replace with your Docker Hub username)
docker_username = "msanth"
docker_username = "gsiva333"
def build_and_push_image(function_name):
"""
Build the Docker image and push it to Docker Hub.
"""
# Change directory to function directory
os.chdir(function_name)
try:
# Change directory to function directory
os.chdir(function_name)
# Build Docker image
subprocess.run(["docker", "build", "-t", f"{docker_username}/{function_name}:latest", "."], check=True)
# Build Docker image
subprocess.run(["docker", "build", "-t", f"{docker_username}/{function_name}:latest", "."], check=True)
# Push Docker image to Docker Hub
subprocess.run(["docker", "push", f"{docker_username}/{function_name}:latest"], check=True)
# Push Docker image to Docker Hub
subprocess.run(["docker", "push", f"{docker_username}/{function_name}:latest"], check=True)
# Change back to original directory
os.chdir("..")
except subprocess.CalledProcessError as e:
print(f"Error occurred while building/pushing Docker image: {e}")
finally:
# Change back to original directory
os.chdir("..")
def create_deployment(function_name):
"""
Create a Kubernetes Deployment resource for the function.
"""
deployment_manifest = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": function_name},
"spec": {
"replicas": 1,
"selector": {"matchLabels": {"app": function_name}},
"template": {
"metadata": {"labels": {"app": function_name}},
"spec": {
"containers": [{
"name": function_name,
"image": f"{docker_username}/{function_name}:latest",
"ports": [{"containerPort": 5000}] # Assuming function listens on port 8080
}]
try:
deployment_manifest = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": function_name},
"spec": {
"replicas": 1,
"selector": {"matchLabels": {"app": function_name}},
"template": {
"metadata": {"labels": {"app": function_name}},
"spec": {
"containers": [{
"name": function_name,
"image": f"{docker_username}/{function_name}:latest",
"ports": [{"containerPort": 5000}] # Assuming function listens on port 8080
}]
}
}
}
}
}
#yourusername Create Deployment in Kubernetes
api_instance = client.AppsV1Api(k8s_client)
api_instance.create_namespaced_deployment(namespace="default", body=deployment_manifest)
print(f"Deployment created for {function_name}.")
#yourusername Create Deployment in Kubernetes
api_instance = client.AppsV1Api(k8s_client)
api_instance.create_namespaced_deployment(namespace="default", body=deployment_manifest)
print(f"Deployment created for {function_name}.")
except client.rest.ApiException as e:
print(f"Error occurred while creating Deployment: {e}")
def create_service(function_name):
"""
Create a Kubernetes Service resource for the function.
"""
service_manifest = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"name": f"{function_name}-service"},
"spec": {
"selector": {"app": function_name},
"ports": [{"protocol": "TCP", "port": 8080, "targetPort": 5000}], # Assuming function listens on port 8080
"type": "LoadBalancer" # Expose service only within the cluster
try:
service_manifest = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"name": f"{function_name}-service"},
"spec": {
"selector": {"app": function_name},
"ports": [{"protocol": "TCP", "port": 8080, "targetPort": 5000}], # Assuming function listens on port 8080
"type": "LoadBalancer" # Expose service only within the cluster
}
}
}
# Create Service in Kubernetes
#k8s_client.create_namespaced_service(namespace="default", body=service_manifest)
api_instance = client.CoreV1Api()
api_instance.create_namespaced_service(namespace="default", body=service_manifest)
print(f"Service created for {function_name}.")
# Create Service in Kubernetes
#k8s_client.create_namespaced_service(namespace="default", body=service_manifest)
api_instance = client.CoreV1Api()
api_instance.create_namespaced_service(namespace="default", body=service_manifest)
print(f"Service created for {function_name}.")
except client.rest.ApiException as e:
print(f"Error occurred while creating Service: {e}")
"""
def get_external_ip(service_name):
try:
# Wait for the external IP to be assigned
while True:
service = api_instance.read_namespaced_service(name=service_name, namespace="default")
if service.status.load_balancer.ingress:
external_ip = service.status.load_balancer.ingress[0].ip
if external_ip:
return external_ip
time.sleep(5) # Wait for 5 seconds before checking again
except client.rest.ApiException as e:
print(f"Error occurred while getting external IP: {e}")
"""
def deploy_function(function_name):
"""
Deploy the function to Kubernetes.
"""
build_and_push_image(function_name)
create_deployment(function_name)
create_service(function_name)
try:
build_and_push_image(function_name)
create_deployment(function_name)
create_service(function_name)
# Wait for the external IP to be assigned
#external_ip = get_external_ip(f"{function_name}-service")
#print(f"External IP for {function_name}-service: {external_ip}")
except Exception as e:
print(f"Error occurred during deployment of {function_name}: {e}")
if __name__ == "__main__":
function_name = input("Enter the name of the function directory: ")
......
import os
import subprocess
import yaml
import time
from kubernetes import client, config
# Load Kubernetes configuration from default location
config.load_kube_config()
# Kubernetes API client
k8s_client = client.ApiClient()
# Docker Hub username (replace with your Docker Hub username)
docker_username = "gsiva333"
def build_and_push_image(function_name):
"""
Build the Docker image and push it to Docker Hub.
"""
try:
# Change directory to function directory
os.chdir(function_name)
# Build Docker image
subprocess.run(["docker", "build", "-t", f"{docker_username}/{function_name}:latest", "."], check=True)
# Push Docker image to Docker Hub
subprocess.run(["docker", "push", f"{docker_username}/{function_name}:latest"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while building/pushing Docker image: {e}")
finally:
# Change back to original directory
os.chdir("..")
def create_deployment(function_name):
"""
Create a Kubernetes Deployment resource for the function.
"""
try:
deployment_manifest = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": function_name},
"spec": {
"replicas": 1,
"selector": {"matchLabels": {"app": function_name}},
"template": {
"metadata": {"labels": {"app": function_name}},
"spec": {
"containers": [{
"name": function_name,
"image": f"{docker_username}/{function_name}:latest",
"ports": [{"containerPort": 5000}] # Assuming function listens on port 8080
}]
}
}
}
}
#yourusername Create Deployment in Kubernetes
api_instance = client.AppsV1Api(k8s_client)
api_instance.create_namespaced_deployment(namespace="default", body=deployment_manifest)
print(f"Deployment created for {function_name}.")
except client.rest.ApiException as e:
print(f"Error occurred while creating Deployment: {e}")
def create_service(function_name):
"""
Create a Kubernetes Service resource for the function.
"""
try:
service_manifest = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"name": f"{function_name}-service"},
"spec": {
"selector": {"app": function_name},
"ports": [{"protocol": "TCP", "port": 8080, "targetPort": 5000}], # Assuming function listens on port 8080
"type": "LoadBalancer" # Expose service only within the cluster
}
}
# Create Service in Kubernetes
#k8s_client.create_namespaced_service(namespace="default", body=service_manifest)
api_instance = client.CoreV1Api()
api_instance.create_namespaced_service(namespace="default", body=service_manifest)
print(f"Service created for {function_name}.")
except client.rest.ApiException as e:
print(f"Error occurred while creating Service: {e}")
def get_external_ip(service_name):
"""
Get the external IP of the service.
"""
try:
# Wait for the external IP to be assigned
api_instance = client.CoreV1Api()
while True:
service = api_instance.read_namespaced_service(name=service_name, namespace="default")
if service.status.load_balancer.ingress:
external_ip = service.status.load_balancer.ingress[0].ip
if external_ip:
return external_ip
time.sleep(5) # Wait for 5 seconds before checking again
except client.rest.ApiException as e:
print(f"Error occurred while getting external IP: {e}")
def deploy_function(function_name):
"""
Deploy the function to Kubernetes.
"""
try:
build_and_push_image(function_name)
create_deployment(function_name)
create_service(function_name)
# Wait for the external IP to be assigned
#external_ip = get_external_ip(f"{function_name}-service")
#print(f"External IP for {function_name}-service: {external_ip}")
except Exception as e:
print(f"Error occurred during deployment of {function_name}: {e}")
if __name__ == "__main__":
function_name = input("Enter the name of the function directory: ")
deploy_function(function_name)
from flask import Flask, request, jsonify
from kubernetes import client, config, watch
from prometheus_client import Counter, Gauge, generate_latest, CONTENT_TYPE_LATEST
from prometheus_client import Counter, Gauge, generate_latest, CONTENT_TYPE_LATEST, Histogram
from prometheus_client.exposition import make_wsgi_app
from werkzeug.middleware.dispatcher import DispatcherMiddleware
import threading
......@@ -20,37 +20,43 @@ app = Flask(__name__)
# Prometheus metrics
function_invocation_counter = Counter('function_invocation_count', 'Number of function invocations', ['function_name'])
function_request_rate_gauge = Gauge('function_request_rate', 'Request rate for functions', ['function_name'])
cpu_utilization_gauge= Gauge('cpu_utilization','CPU utilization for functions',['function_name'])
storage_utilization_gauge = Gauge('storage_utilization', 'Storage utilization for functions', ['function_name'])
service_time_histogram = Histogram('service_time_seconds', 'Service time for functions', ['function_name'])
def deploy_function(function_name, image):
"""
"""
Deploy a function to Kubernetes as a Deployment.
"""
# Define deployment spec
deployment_spec = {
'apiVersion': 'apps/v1',
'kind': 'Deployment',
'metadata': {'name': function_name},
'spec': {
'replicas': 1,
'selector': {'matchLabels': {'app': function_name}},
'template': {
'metadata': {'labels': {'app': function_name}},
'spec': {
'containers': [{
'name': function_name,
'image': image,
'ports': [{'containerPort': 5000}]
}]
try:
deployment_spec = {
'apiVersion': 'apps/v1',
'kind': 'Deployment',
'metadata': {'name': function_name},
'spec': {
'replicas': 1,
'selector': {'matchLabels': {'app': function_name}},
'template': {
'metadata': {'labels': {'app': function_name}},
'spec': {
'containers': [{
'name': function_name,
'image': image,
'ports': [{'containerPort': 5000}]
}]
}
}
}
}
}
# Create deployment in Kubernetes
api_instance = client.AppsV1Api(k8s_client)
api_instance.create_namespaced_deployment(namespace='default', body=deployment_spec)
print(f"Function {function_name} deployed with image {image}.")
# Create deployment in Kubernetes
api_instance = client.AppsV1Api(k8s_client)
api_instance.create_namespaced_deployment(namespace='default', body=deployment_spec)
print(f"Function {function_name} deployed with image {image}.")
except Exception as e:
print(f"Error deploying {function_name}: {e}")
def scale_function(function_name, replicas):
......@@ -58,27 +64,30 @@ def scale_function(function_name, replicas):
Scale the number of replicas for a function in Kubernetes.
"""
# Define HPA spec
hpa_spec = {
'apiVersion': 'autoscaling/v1',
'kind': 'HorizontalPodAutoscaler',
'metadata': {'name': f'{function_name}-hpa'},
'spec': {
'scaleTargetRef': {
'apiVersion': 'apps/v1',
'kind': 'Deployment',
'name': function_name
},
'minReplicas': 1,
'maxReplicas': replicas,
'targetCPUUtilizationPercentage': 50 # Adjust as needed
try:
hpa_spec = {
'apiVersion': 'autoscaling/v1',
'kind': 'HorizontalPodAutoscaler',
'metadata': {'name': f'{function_name}-hpa'},
'spec': {
'scaleTargetRef': {
'apiVersion': 'apps/v1',
'kind': 'Deployment',
'name': function_name
},
'minReplicas': 1,
'maxReplicas': replicas,
'targetCPUUtilizationPercentage': 50 # Adjust as needed
}
}
}
# Create HPA in Kubernetes
#api_instance = client.CoreV1Api()
api_instance = client.AutoscalingV1Api(k8s_client)
api_instance.create_namespaced_horizontal_pod_autoscaler(namespace='default', body=hpa_spec)
print(f"Function {function_name} auto-scaler created with max replicas {replicas}.")
# Create HPA in Kubernetes
#api_instance = client.CoreV1Api()
api_instance = client.AutoscalingV1Api(k8s_client)
api_instance.create_namespaced_horizontal_pod_autoscaler(namespace='default', body=hpa_spec)
print(f"Function {function_name} auto-scaler created with max replicas {replicas}.")
except Exception as e:
print(f"Error scaling {function_name}: {e}")
def handle_function(event):
......@@ -141,8 +150,12 @@ def invoke_function():
function_request_rate_gauge.labels(function_name=function_name).inc()
# Increment invocation count for function
function_invocation_counter.labels(function_name=function_name).inc()
start_time=time.time()
# Invoke function
result = invoke_kubernetes_function(function_name)
end_time=time.time()
service_time=end_time-start_time
service_time_histogram.labels(function_name=function_name).observe(service_time)
return jsonify({'message': f"Function {function_name} invoked. Result: {result}"})
......@@ -168,6 +181,7 @@ def invoke_kubernetes_function(function_name):
except requests.exceptions.RequestException as e:
return f"Error: {e}"
@app.route('/metrics')
def metrics_app(environ, start_response):
"""
Serve Prometheus metrics.
......
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