Skip to content

Commit 8a892d3

Browse files
committed
Add kubernetes CURL cron job example
1 parent e85f2cd commit 8a892d3

5 files changed

+119
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### System
2+
.DS_Store
3+
4+
### IDE
5+
.idea/*
6+
!.idea/runConfigurations/
7+
*.iml

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# kubernetes-curl-job
2+
Kubernetes Job to execute a curl request against a protected API and post execution status to a Slack channel
3+
4+
## Cron In Your Cluster
5+
Technologies change, infrastructure and codebases change, but cron has been basically the same since its initial release from almost 50 years ago in in 1975! Modern cron is mostly done with Vixie cron, first created in 1987.
6+
7+
For most teams, cron jobs are useful for periodic and recurring tasks, like batch emails or running backups. They also support more fine-tuned scheduling to run tasks at a specific time, such as every Sunday night to trigger a script to email people about their upcoming week.
8+
9+
For those that have migrated to Kubernetes, a new iteration of cron is now available. With this option, we can manage cron tasks within our existing running clusters with infrastructure-as-code on our side. Plus, we can utilize containers to have clean installs and stable program environments embedded in immutable Docker images.
10+
11+
>Cron + K8s + Immutable Images = Happy Cron Jobs
12+
13+
## How Kunernetes Cron is this better than regular cron?
14+
Suppose you don’t want to run this once a month anymore, but rather every minute. All you need to change is the schedule property to */1 * * * * to make the job run every minute directly from your kubernetes dashboard.
15+
16+
## How to deploy this cron job
17+
* First take a look in the job definition file *kubernetes-curl-job.yaml* and provide your own configurations or adapt the script according to your own needs.
18+
* Deploy a config-map with the content of the script
19+
```
20+
kubectl create configmap kubernetes-curl-job-script --from-file=kubernetes-curl-job.sh
21+
```
22+
* Deploy the cron-job
23+
```
24+
kubectl apply -f kubernetes-curl-job.yaml
25+
```
26+
27+
> You can also change the execution frequency by changing the cron expression
28+

kubernetes-curl-job-deploy.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#! /bin/sh
2+
3+
# Create config for kubernetes-curl-job.sh script
4+
kubectl create configmap kubernetes-curl-job-script --from-file=kubernetes-curl-job.sh
5+
6+
# Apply Job
7+
kubectl apply -f kubernetes-curl-job.yaml

kubernetes-curl-job.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
notifySuccess() {
4+
curl -X POST -H 'Content-type: application/json'\
5+
--data "{\"text": "$SUCCESS_MESSAGE" :"\"rocket:\"}" \
6+
$WEBHOOK
7+
}
8+
9+
notifyFail() {
10+
curl -X POST -H 'Content-type: application/json'\
11+
--data "{\"text": "$FAIL_MESSAGE": "\"rotating_light:\"}" \
12+
$WEBHOOK
13+
}
14+
15+
# Authenticate with your Auth API using OAuth2 Client credentials flow and get an ACCESS TOKEN
16+
accessToken=$(curl -H "Authorization: Basic $BASIC_CLIENT_CREDENTIALS" \
17+
-d grant_type=client_credentials \
18+
--silent $OAUTH_TOKEN_ENDPOINT | jq -r .access_token)
19+
20+
# Execute API request using previously requested Access Token
21+
status_code=$(curl -H "Authorization: Bearer ${accessToken}" \
22+
-X POST --write-out %{http_code} --silent --output /dev/null \
23+
$JOB_ENDPOINT)
24+
25+
if [[ "$status_code" -ne 202 ]] ; then
26+
echo $FAIL_MESSAGE
27+
notifyFail
28+
else
29+
echo $SUCCESS_MESSAG
30+
notifySuccess
31+
fi

kubernetes-curl-job.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apiVersion: batch/v1beta1
2+
kind: CronJob
3+
metadata:
4+
name: my-kubernetes-job
5+
labels:
6+
app: cellarcollective
7+
type: cron
8+
spec:
9+
schedule: "* 1 * * *" # Run every hour
10+
jobTemplate:
11+
metadata:
12+
labels:
13+
app: my-kubernetes-job
14+
type: cron
15+
spec:
16+
template:
17+
spec:
18+
containers:
19+
- name: my-kubernetes-job
20+
image: manusant/curl-jq
21+
imagePullPolicy: IfNotPresent
22+
env:
23+
- name: WEBHOOK
24+
value: <your-slack-web-hook>
25+
- name: OAUTH_TOKEN_ENDPOINT
26+
value: <yout-oauth-token-endpoint> # https://api.example.com/uaa/oauth/token
27+
- name: BASIC_CLIENT_CREDENTIALS
28+
value: <{base64}clientId:clientSecret}> # c2NoXXX1bGVyLXNlcnZpY2U6MTIzUVdFYXNk
29+
- name: JOB_ENDPOINT
30+
value: <http-endpointç-to-call>
31+
- name: CHANNEL
32+
value: "#<your-slack-channel>"
33+
- name: SUCCESS_MESSAGE
34+
value: "Your success message"
35+
- name: FAIL_MESSAGE
36+
value: "Your error message"
37+
command: ["/scripts/kubernetes-curl-job.sh"]
38+
volumeMounts:
39+
- name: my-kubernetes-job-volume
40+
mountPath: /scripts
41+
volumes:
42+
- name: my-kubernetes-job-volume
43+
configMap:
44+
name: kubernetes-curl-job-script
45+
defaultMode: 0744
46+
restartPolicy: Never

0 commit comments

Comments
 (0)