Commit 7745ed2b authored by Santhosh Kumar's avatar Santhosh Kumar

initial working code for deployment

parent 376680b5
......@@ -2,3 +2,11 @@ kubectl create secret generic regcred --from-file=.dockerconfigjson=faas/con
kubectl config view --minify --raw > kube_config
# Copy this kube_config to ~/.kube/config and use kubectl to access my kubernetes cluster.
kubectl delete service <name>
sudo kubeadm token create --print-join-command
# Run this command to join.
No preview for this file type
apiVersion: v1
kind: Service
metadata:
name: hello-python-service
spec:
selector:
app: hello-python
ports:
- protocol: "TCP"
port: 6000
targetPort: 5000
type: LoadBalancer
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: hello-python
spec:
replicas: 4
template:
metadata:
labels:
app: hello-python
spec:
containers:
- name: hello-python
image: msanth/example1:latest
imagePullPolicy: Never
ports:
- containerPort: 5000
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 requests
RUN pip install -r requirements.txt
EXPOSE 5000
# Define the command to run the function
CMD ["python", "function.py"]
CMD ["python", "/app/function.py"]
def main(request):
"""
Entry point of the function.
"""
if request is not None and 'name' in request:
name = request['name']
return f"Hello, {name}!"
else:
return "Hello, World!"
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from Python function"
if __name__ == "__main__":
app.run(host='0.0.0.0')
......@@ -45,7 +45,7 @@ def create_deployment(function_name):
"containers": [{
"name": function_name,
"image": f"{docker_username}/{function_name}:latest",
"ports": [{"containerPort": 8080}] # Assuming function listens on port 8080
"ports": [{"containerPort": 5000}] # Assuming function listens on port 8080
}]
}
}
......@@ -67,8 +67,8 @@ def create_service(function_name):
"metadata": {"name": f"{function_name}-service"},
"spec": {
"selector": {"app": function_name},
"ports": [{"protocol": "TCP", "port": 8080, "targetPort": 8080}], # Assuming function listens on port 8080
"type": "ClusterIP" # Expose service only within the cluster
"ports": [{"protocol": "TCP", "port": 8080, "targetPort": 5000}], # Assuming function listens on port 8080
"type": "LoadBalancer" # Expose service only within the cluster
}
}
......
......@@ -40,7 +40,7 @@ def deploy_function(function_name, image):
'containers': [{
'name': function_name,
'image': image,
'ports': [{'containerPort': 8080}]
'ports': [{'containerPort': 5000}]
}]
}
}
......@@ -75,8 +75,9 @@ def scale_function(function_name, replicas):
}
# Create HPA in Kubernetes
api_instance = client.CoreV1Api()
k8s_client.create_namespaced_horizontal_pod_autoscaler(namespace='default', body=hpa_spec)
#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}.")
......@@ -85,12 +86,15 @@ def handle_function(event):
Handle function events (create, update, delete).
"""
function = event['object']
function_name = function['metadata']['name']
#metadata = function['metadata']
#function_name = metadata.get('name')
function_name = function.metadata.name
function_status = event['type']
if function_status == 'ADDED':
print(f"Function {function_name} added.")
deploy_function(function_name, function['spec']['image'])
#deploy_function(function_name, function['spec']['image'])
#deploy_function(function_name, function.spec['image'])
scale_function(function_name, 10) # Initially scale to max replicas of 10
elif function_status == 'MODIFIED':
print(f"Function {function_name} modified.")
......@@ -104,13 +108,25 @@ def watch_functions():
Watch for changes to function resources.
"""
resource_version = ''
v1 = client.CoreV1Api()
api_instance = client.AppsV1Api(k8s_client)
w = watch.Watch()
my_resource = {
"apiVersion": "example.com/v1",
"kind": "deployment",
"metadata": {"name": "my-new-cron-object"},
"spec": {
"cronSpec": "* * * * */5",
"image": "my-awesome-cron-image"
}
}
while True:
stream = w.stream(v1.list_namespaced_custom_object, group='example.com', version='v1', namespace='default', plural='functions', resource_version=resource_version)
for event in stream:
resource_version = event['object']['metadata']['resourceVersion']
deployment_watch = watch.Watch().stream(api_instance.list_namespaced_deployment, namespace='default')
#stream = w.stream(client.CustomObjectsApi().list_namespaced_custom_object, group='example.com', version='v1', namespace='default', plural='functions', resource_version=resource_version)
#for event in stream:
for event in deployment_watch:
#resource_version = event['object']['metadata']['resourceVersion']
print(event)
handle_function(event)
......
This diff is collapsed.
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