Openshift - Run pod only for specific time period

8/5/2021

I'm new to Openshfit. We are using openshift deployments to deploy our multiple microservice (SpringBoot application). The deployment is done from docker image.

We have a situation that we need to stop one micro service alone from Midnight till morning 5 AM ( due to an external dependency ).

Could someone suggest a way to do this automatically?

I was able to run oc scale deployment/sampleservice--replicas=0 manually to make number of pods as zero and scale up to 1 manually later.

I'm not sure how to run this command on specific time automatically. The CronJob in Openshift should be able to do this. But not sure how to configure cronjob to execute an OC command.

Any guidance will be of great help

-- Hari M
kubernetes
openshift

2 Answers

8/5/2021

Using a cronjob is a good option.

First, you'll need an image that has the oc command line client available. I'm sure there's a prebuilt one out there somewhere, but since this will be running with privileges in your OpenShift cluster you want something you trust, which probably means building it yourself. I used:

FROM quay.io/centos/centos:8

RUN curl -o /tmp/openshift-client.tar.gz \
                https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest/openshift-client-linux.tar.gz; \
    tar -C /bin -xf /tmp/openshift-client.tar.gz oc kubectl; \
    rm -f /tmp/openshift-client.tar.gz

ENTRYPOINT ["/bin/oc"]

In order to handle authentication correctly, you'll need to create a ServiceAccount and then assign it appropriate privileges through a Role and a RoleBinding. I created a ServiceAccount named oc-client-sa:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: oc-client-sa
  namespace: oc-client-example

A Role named oc-client-role that grants privileges to Pod and Deployment objects:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: oc-client-role
  namespace: oc-client-example
rules:
  - verbs:
      - get
      - list
      - create
      - watch
      - patch
    apiGroups:
      - ''
    resources:
      - pods
  - verbs:
      - get
      - list
      - create
      - watch
      - patch
    apiGroups:
      - 'apps'
    resources:
      - deployments
      - deployments/scale

And a RoleBinding that connects the oc-client-sa ServiceAccount to the oc-client-role Role:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: oc-client-rolebinding
  namespace: oc-client-example
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: oc-client-role
subjects:
  - kind: ServiceAccount
    name: oc-client-sa

With all this in place, we can write a CronJob like this that will scale down a deployment at a specific time. Note that we're running the jobs using the oc-client-sa ServiceAccount we created earlier:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: scale-web-down
  namespace: oc-client-example
spec:
  schedule: "00 00 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: oc-client-sa
          restartPolicy: Never
          containers:
          - image: docker.io/larsks/openshift-client
            args:
              - scale
              - deployment/sampleservice
              - --replicas=0
            name: oc-scale-down

You would write a similar one to scale things back up at 5AM.

The oc client will automatically use the credentials provided to your pod by Kubernetes because of the serviceAccountName setting.

-- larsks
Source: StackOverflow

8/5/2021

API

You can use the OC rest api client and write the simple python code which will scale down replicas. Pack this python into a docker image and run it as a cronjob inside the OC cluster.

Simple Curl

Run a simple curl inside the cronjob to scale up & down deployment at a certain time.

Here is a simple Curl to scale the deployment: https://docs.openshift.com/container-platform/3.7/rest_api/apis-apps/v1beta1.Deployment.html#Get-apis-apps-v1beta1-namespaces-namespace-deployments-name-scale

API documentation : https://docs.openshift.com/container-platform/3.7/rest_api/apis-apps/v1beta1.Deployment.html

CLI

If you don't want to run code as docker image in cronjob of K8s, you can also run the command, in that case, use the docker image inside cronjob, and fire the command

OC-cli : https://hub.docker.com/r/widerin/openshift-cli

Dont forget authentication is required in both cases either API or running a command inside the cronjob.

-- Harsh Manvar
Source: StackOverflow