I have a program which I have set up several jobs inside it. According to the figure, these jobs are executed every day at a certain time and, for example, send an SMS to a group of numbers. When I deploy this to Kubernetes, multiple copies are created. I want to know, do all these original and replica versions do this and send SMS? If it is true that one SMS should be sent to one number, not that several SMS messages should be sent to the same number. My question is, how does Kubernetes deal with these programs and how should we deploy them correctly?
I have read various articles but I don't know which is the right way.
CodePudding user response:
You can use CronJob for that.
Since you have a single application with all cronjob code. You can modify the c# application to use an environment variable to decide the job type. This way you can create CronJob
resource for each job you want.
Notification Job
apiVersion: batch/v1
kind: CronJob
metadata:
name: notification-job
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: notification-job
image: org/my-csharp-cron-app:latest
imagePullPolicy: IfNotPresent
env:
- name: JOB_TYPE
value: "NotificationJob"
restartPolicy: OnFailure
Logger Job
apiVersion: batch/v1
kind: CronJob
metadata:
name: logger-job
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: logger-job
image: org/my-csharp-cron-app:latest
imagePullPolicy: IfNotPresent
env:
- name: JOB_TYPE
value: "LoggerJob"
restartPolicy: OnFailure
CodePudding user response:
Your application has several jobs running of which one of them is sending an SMS to a certain number. When you instruct Kubernetes to deploy multiple instances of the same application, K8s' "only" task is to ensure to keep those multiple instances alive. K8s is not aware of the business logic you deploy. In other words, it is up to you to handle sending the SMS to the specified number only once.
Some suggestions to handle this (other possibilities exist):
- Encode your application such way that it only sends the message once no matter how many instances you have running. Many different approaches exist for doing so.
- Take a look at cron deployments allowing you to spin up K8s containers for a particular, scheduled task. This most likely requires you to redesign your application.
- Do you require multiple instances running together? Since all tasks are implemented in the same application, I assume the application was not designed to ran in parallel initially. Moving in that direction, most likely requires some refactoring/redesigning. If it is not a requirement, you could limit the K8s replica set to one instance instead.