Operator for CronJob resources
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.6 KiB

1 year ago
1 year ago
1 year ago
5 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
5 months ago
5 months ago
5 months ago
1 year ago
1 year ago
5 months ago
1 year ago
1 year ago
5 months ago
  1. # Cron operator for Kubernetes
  2. Manages multiple CronJobs with same image, but different commands and workdirs. Supports mounting of persistent volume to save results somewhere, ENV variables configmaps, concurrency and restart policy.
  3. ## Build sources
  4. - You definitly need a Golang installed ([easy install for Ubuntu](http://deb.blindage.org/readme.txt))
  5. - Clone repository https://git.blindage.org/21h/cron-operator.git to your computer
  6. - Open `Makefile` and change images registry to yours, then run `make && make push`
  7. Or run `docker build -t cron-operator .` to make image and push to your repo later
  8. - Upload image to your registry
  9. ## Prebuilt images
  10. You can find images here https://hub.docker.com/r/iam21h/cron-operator. Pin version number in `operator.yaml` if you do not want automaticaly get new features and bugs.
  11. ## Deploy operator
  12. Edit files and run:
  13. ```
  14. kubectl -f deploy/crds/blindage_v1alpha1_cronop_crd.yaml
  15. kubectl -f deploy/clusterrole.yaml
  16. kubectl -f deploy/clusterrole_binding.yaml # edit service account namespace here
  17. kubectl -n operators -f deploy/service_account.yaml
  18. kubectl -n operators -f deploy/operator.yaml
  19. ```
  20. or use Helm:
  21. ```
  22. helm repo add 21h https://charts.blindage.org
  23. helm repo update
  24. helm install cron-operator 21h/cron-operator -n operators
  25. ```
  26. Change `operator.yaml` if you want operator watch only specified namespace. Check `deploy` directory, may be you want to use Role instead of ClusterRole.
  27. ## Settings
  28. In your `CronOp` resource you can set image, parameters and tasks. CronJobs is a result of reconciling tasks section, some parameters can be global and overrided in each task. Remember, CronJob, Pod and Job is a different things.
  29. Look into `pkg/apis/blindage/v1alpha1/cronop_types.go` for your CustomResource syntax and comments.
  30. This parameters can be set in global section or each task:
  31. - Set Configmap with ENV variables
  32. `envConfigmap`: _default empty_ | string
  33. - Mount configmap as files
  34. `mountConfigmap`: _default empty_ | string
  35. `mountConfigmapPath`: by default mounts to /configmap-local | string
  36. - Mount secret as files
  37. `mountSecret`: _default empty_ | string
  38. `mountSecretPath`: by default mounts to /secret-local | string
  39. `mountSecretPermissions`: default permissions, use decimal notation, default 256 | integer
  40. - How to restart Pod
  41. `restartPolicy`: **OnFailure** | *Never* | *Always*
  42. - Limit Job restarts
  43. `backoffLimit`: **0** | integer
  44. - Allow Cronjob to run concurrent Jobs
  45. `concurrencyPolicy`: **Allow** | *Forbid* | *Replace*
  46. - Set standard kubernetes Pod security context
  47. `podSecurityContext`: _default empty_
  48. Global history limits:
  49. - successfulJobsHistoryLimit: **3** | integer
  50. - failedJobsHistoryLimit: **3** | integer
  51. Also you can save intermediate results in mounted persistent volume. You can set PVC name with `PersistentVolumeName`, mount point with `PersistentVolumePath` and `PersistentVolumeSubPath`. It's easy.
  52. Global affinity, node selection and tolerations, have standard kubernetes specs:
  53. - nodeSelector: _default empty_
  54. - affinity: _default empty_
  55. - tolerations: _default empty_
  56. Global resource limiting also kubernetes standard, but separated:
  57. - podRequests: _default empty_
  58. - podLimits: _default empty_
  59. ## Labels and annotations:
  60. Operator automatically copies labels from CR _meta_ to Cronjob, Job and Pod.
  61. Also you cat set `annotations` in CR _spec_ to set it for Pod and merge it with individual task annotations:
  62. ```
  63. ---
  64. apiVersion: blindage.org/v1alpha1
  65. kind: CronOp
  66. metadata:
  67. name: mycron
  68. labels:
  69. my-application: python-app
  70. spec:
  71. image: "python:3"
  72. annotations:
  73. co.elastic.logs.backup/multiline.match: after
  74. co.elastic.logs.backup/multiline.negate: "true"
  75. co.elastic.logs.backup/multiline.pattern: ^\[
  76. tasks:
  77. - name: print-test
  78. annotations:
  79. logger: do-not-log
  80. commands: ['python','-c', 'print("testtesttest")']
  81. schedule: "* * * * *"
  82. ```
  83. ## Your first CronOp deployment
  84. Good sample you can find in `deploy/test-cron.yaml`, see it in case of questions.
  85. This resource can help you run your first tasks:
  86. ```
  87. ---
  88. apiVersion: blindage.org/v1alpha1
  89. kind: CronOp
  90. metadata:
  91. name: mycron
  92. labels:
  93. app: python
  94. service: testing-cron
  95. spec:
  96. image: "python:3"
  97. tasks:
  98. - name: print-test
  99. commands: ['python','-c', 'print("doing nothing")']
  100. schedule: "* * * * *"
  101. - name: hello
  102. commands: ['python','-c', 'print("hello world hourly")']
  103. schedule: "0 * * * *"
  104. ```
  105. If you delete this resource operator will delete CronJobs and Jobs.
  106. ---
  107. Copyright by Vladimir Smagin (21h) 2019
  108. http://blindage.org email: 21h@blindage.org
  109. Project page: https://git.blindage.org/21h/cron-operator