Building APIs and backend services that run PowerShell - Roberth Strand - PSConfEU 2023
HTML-код
- Опубликовано: 18 дек 2024
- PowerShell offers a range of capabilities beyond running scripts. It can be integrated into pipelines, triggered by events, and used in an API or back-end service context. There are various software solutions available for running PowerShell scripts, such as ScriptRunner. PowerShell can also be utilized in cloud-native environments like Azure Functions, AWS Lambdas, or running within containers in services like Azure Container Service or Kubernetes.
To showcase the possibilities of PowerShell in an API context, I, Robert Strand, have chosen to focus on a Kubernetes demo. We are developing a deployment platform that automates Terraform and containerized scripts, allowing users to easily expose their services through APIs. I apologize for any delays in the presentation slides, but let's get started.
In the demo, I explain the need for a container to run stuff in Kubernetes and mention using a container based on the official Microsoft container image for PowerShell, specifically using Alpine Linux. I also mention using Chain Guard images for secure containers. However, due to limitations in running demos, I opt for a slightly less secure container.
I explain that I am using a PowerShell image as a base and copying over scripts I have created. I also install the PODE module for creating web servers with PowerShell. I highlight the drawbacks of relying on a framework created by one person and express my wish for a more robust project.
I then run the server manually on my computer and demonstrate how to add endpoints and routes. I discuss the different ways this framework can be run, including as an Azure Function or in a Docker container.
Next, I discuss deployment in Kubernetes and explain the concept of pods and deployments. I provide an example deployment file with replicas and a container configuration including resource limits and port settings.
I mention the importance of troubleshooting and express my intention to share the solution and code on social media and in conference repositories.
Moving on to a practical example, I explain that I have created a script to fetch weather information from OpenWeatherMap for Prague. I use an environment variable for my API key, stored as a secret. I use the invokeRest method to perform a GET request with a specific URL, and the weather information is stored in a file with a unique name based on the time of day.
To automate this process, I have set up a cron job that runs the script every minute. The job creates a pod with a persistent volume claim to store the data. I have defined environment variables using a secret in Kubernetes.
I mention that the job has run successfully and show logs indicating the temperature in Kelvin. I also mention another job that will continue to run every minute to fetch and store the weather data. I have also created a job that converts the JSON files into a more presentable format and mention the rise in pressure.
This process demonstrates how to create a script, containerize it, run it, and store the results. The data can be stored in different locations, such as a SQL database. I then mention my attempt to connect to the Kubernetes API but encountered some issues.
Lastly, I discuss the process of running a get on the API server URL and explain how to connect directly to the Kubernetes API from within the pod. I introduce the concept of a Kubernetes operator and explain its role in monitoring resources. I also mention using health checks, logging services, and monitoring tools for Kubernetes.