Running a bash script using a Kubenetes Service

6/2/2020

I am not sure how dumb or un-reasonable this question is, but we are trying to see if we can do this in any way.

I have a .bash file. And I want to run this when I invoke a url.

Let's take the url is https://domainname.com/jobapi When I invoke this on the browser, this should invoke a .bash script on the container.

Is this really possible? If it is possible, want to know if I need to add this script as a deployment or a job?

-- SkyArc
bash
kubernetes
kubernetes-service

3 Answers

6/2/2020

The first step, before looking at Kubernetes, is to configure a web server to run your script. This could be a generic web server like nginx or Apache, and you could add your script as a CGI script. There's plenty of tutorials out there that explain how to write CGI scripts.

Depending on the requirements of your application, a simple HTTP hook server might be a better match. Have a look at, for example, https://github.com/adnanh/webhook.

Either way, try this out with just Docker first, before trying to create a pod and potentially a service and an ingress in Kubernetes.

In a second step, to be able to access your service (the server invoking your script), you need to create a pod, probably through a deployment, and potentially a service and an ingress for it.

Kubernetes jobs are for running a script (or other program) once. They're most useful to automate maintaince tasks for your application.

-- stblassitude
Source: StackOverflow

6/2/2020

What I would try to do is to run the shell script from a php file. Otherwise you are going to need some sort of driver to trigger the script.

So you would have the script as a regular executable, and upon the request php will execute it via shell.

Actually you can make it like an API; domain.com/job1 could execute job1, domain.com/jobn could execute jobn and so on.

Now, the way I'm describing would work only as a Deployment, as you want the server to be always up and ready to get requests.

-- suren
Source: StackOverflow

7/29/2020
  • Create a ingress service (NodePort if external facing) which will call a service
  • Call a service which maps the labels defined on the pod(runs script).This pod can be from a deployment or a simple pod
  • Make this service expose a pod/deployment
  • deployment can trigger the pods with shell script or a pod can trigger a shell script as well.

Ingress service:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: "domainame.com"
     http:
      paths:
      - path: /jobapi
        pathType: Prefix
        backend:
          serviceName: my-service
          servicePort: 8080

my-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
     nodePort: 30007
     port: 8080
     targetPort: 8080

run your bash script, this can be done by defining a deployment or a pod Pod:

k run MyApp --image=nginx --labels=app=MyApp --port=8080 -- /bin/sh -c echo 'Im up'

or

Deployment.yaml

controllers/nginx-deployment.yaml 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: MyAppdep
spec:
  replicas: 2
  selector:
    matchLabels:
      app: MyApp
  template:
    metadata:
      labels:
        app: MyApp
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        commands: ["/bin/sh","-c","echo 'test'"]
        ports:
        - containerPort: 8080
-- DBSand
Source: StackOverflow