In the following exam, you’re gonna implement a logical autoscaler software that pulls scaling configurations and scales relevant K8S deployments replicas.
The autoscaler can be written in any language (preferred Python)
The autoscaler will pull the scaling configurations with REST API:
url: http://wscdevopsexam.eastus.azurecontainer.io
path: /autoscale
method: GET
The scaling configuration is a list of deployments and the target replicas for each deployment.
Based on the configurations, the autoscaler will scale the deployment’s replicas
All the deployments are in “wsc” namespace
Bonus: Create a Dockerfile to pack your software
Bonus: Create a README.md file
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To implement the autoscaler software, we will use Python and the Kubernetes Python client library.
First, we will create a function to retrieve the scaling configurations from the REST API. This function will make a GET request to the provided URL and path, and parse the response as a JSON object.
Next, we will create a function to scale the deployment replicas based on the retrieved configurations. This function will use the Kubernetes Python client to get the deployment objects and update their replicas to the specified target replicas in the scaling configurations.
We will then create a main function that will run the above functions and periodically retrieve and apply the scaling configurations.
To pack our software in a Docker image, we will create a Dockerfile that installs the necessary dependencies and copies our code into the image.
In the README.md file, we will include instructions for running the autoscaler software, including how to build and run the Docker image.
from kubernetes import client
def scale_deployments(configurations):
for deployment, target_replicas in configurations.items():
# Get deployment object
v1 = client.AppsV1Api()
deployment_obj = v1.read_namespaced_deployment(name=deployment, namespace="wsc")
# Update deployment replicas
deployment_obj.spec.replicas = target_replicas
v1.patch_namespaced_deployment(name=deployment, namespace="wsc", body=deployment_obj)
Main function
code
import time
def main():
while True:
# Get scaling configurations
configurations = get_scaling_configurations()
# Scale deployments
scale_deployments(configurations)
# Sleep for 60 seconds before checking for new configurations
time.sleep(60)
Dockerfile
code
FROM python:3.8
# Install Kubernetes Python client
RUN pip install kubernetes
# Copy autoscaler code
COPY autoscaler.py /app/autoscaler.py
# Run main function
CMD ["python", "/app/autoscaler.py"]
README.md
To build and run the autoscaler software in a Docker image, follow these steps:
Build the Docker image:
code
docker build -t autoscaler .
Run the Docker image:
code
docker run -it autoscaler
The autoscaler will run in the background, periodically checking for new scaling configurations and scaling the relevant deployments accordingly.