AWS EKS: schedule a deployment to scale down and up replicas at specific time using CronJob

164 views Asked by At

I am managing a Kubernetes Cluster running on AWS EKS. To minimize my monthly usage cost I would like to implement a CronJob that scales my deployment replicas to zero during weekends and from 8 pm to 8 am during working days.
The pod is running on a Fargate Node.

This is the nginx.cronjob.yaml file:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: scaledown-cron-job
  namespace: test
spec:
  schedule: "0 20 * * 1-5"
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: scale-job-service-account
          containers:
            - name: scaledown-cron-job
              image: bitnami/kubectl:1.28.3
              command: [ "/bin/sh", "-c"]
              args:
                - kubectl scale deployment nginx-deployment --replicas=0
          restartPolicy: Never
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: scaleup-cron-job
  namespace: test
spec:
  schedule: "0 8 * * 1-5"
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: scale-job-service-account
          containers:
            - name: scaleup-cron-job
              image: bitnami/kubectl:1.28.3
              command: [ "/bin/sh", "-c" ]
              args:
                - kubectl scale deployment nginx-deployment --replicas=1
          restartPolicy: Never
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: scale-job-service-account
  namespace: test
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: scale-job-role
  namespace: test
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "watch", "list", "patch"]
  - apiGroups: ["apps"]
    resources: ["deployments/scale"]
    verbs: ["patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: scale-job-role-binding
  namespace: test
subjects:
  - kind: ServiceAccount
    name: scale-job-service-account
    namespace: test
roleRef:
  kind: Role
  name: scale-job-role
  apiGroup: rbac.authorization.k8s.io

This is the nginx.deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: test
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

When testing, the Job created by the CronJob tries to initialize a Pod but this stays on "Pending" because it has no Fargate node to run on.

0

There are 0 answers