How to execute docker images inside a Kubernetes cluster when receive a http request

140 views Asked by At

I have some docker images and I want to run them in a Kubernetes cluster when I receive a http request.

How I could do that? I would need to create a flask app for example that would be listening for requests and then when a request is received execute a bash command to deploy and run these images from a registry?

Or there is another easy way to achieve this?

1

There are 1 answers

0
Howard_Roark On

If you have a flask app, you can use the Kubernetes python api to create Kubernetes pods or jobs. They have an example of creating a deployment here based on a yaml file that exists, but you also could define the yaml within your code or use their api spec. Their Deployment example is below, but again you would probably want to use Pods or Jobs.

from os import path
import yaml
from kubernetes import client, config

def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    config.load_kube_config()

    with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
        dep = yaml.safe_load(f)
        k8s_apps_v1 = client.AppsV1Api()
        resp = k8s_apps_v1.create_namespaced_deployment(
            body=dep, namespace="default")
        print("Deployment created. status='%s'" % resp.metadata.name)


if __name__ == '__main__':
    main()