Kubernetes cron 作业在循环中调用 curl
Kubernetes cron job call curl in loop
我正在使用 kubernetes 1.10 集群,我想安排 cron 作业,它将使用 bash 脚本永远循环并每两秒向 http 端点发送一次获取请求。
这是我的工作 yaml:
apiVersion: batch/v1
kind: Job
metadata:
name: notifsender-sms-cron
namespace: staging
spec:
template:
spec:
containers:
- name: notifsender-sms-cron
image: alpine:latest
command: ["/bin/sh"]
args:
- -c
- >
apk update && apk add --no-cache curl bash && bash -c
" echo \"Running cron loop\";
while true;
do
exit_status=$(curl -v -o /dev/null -w '%{http_code}' http://bbc.com);
if [ $exit_status -ne 200 ]; then
exit 1;
fi
sleep 2;
done
"
restartPolicy: OnFailure
backoffLimit: 4
问题是输出是意外的,因为 curl 请求只发出一次,甚至在循环之前,比没有发出任何 curl 请求的程序循环要好:
...
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: Varnish
< Retry-After: 0
< Content-Length: 0
< Accept-Ranges: bytes
< Date: Thu, 05 Jul 2018 17:53:32 GMT
< Via: 1.1 varnish
< Connection: close
< X-Served-By: cache-fra19122-FRA
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1530813212.281242,VS0,VE0
< Location: http://www.bbc.com/
< cache-control: public, max-age=3600
<
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
* Closing connection 0
Running cron loop
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
我认为我的脚本有误,但以我目前对 bash 脚本的了解我无法解决这个问题。谁能告诉我哪里出了问题?
谢谢
小修改:
美元符号 $ 应该被转义 -> \$
"\$exit_status" 代替 $exit_status
bash -c " echo \"Running cron loop\";
while true;
do
exit_status=$(curl -v -o /dev/null -w '%{http_code}' http://bbc.com);
if [ $exit_status -ne 200 ]; then
exit 1;
fi
sleep 2;
done
"
我正在使用 kubernetes 1.10 集群,我想安排 cron 作业,它将使用 bash 脚本永远循环并每两秒向 http 端点发送一次获取请求。
这是我的工作 yaml:
apiVersion: batch/v1
kind: Job
metadata:
name: notifsender-sms-cron
namespace: staging
spec:
template:
spec:
containers:
- name: notifsender-sms-cron
image: alpine:latest
command: ["/bin/sh"]
args:
- -c
- >
apk update && apk add --no-cache curl bash && bash -c
" echo \"Running cron loop\";
while true;
do
exit_status=$(curl -v -o /dev/null -w '%{http_code}' http://bbc.com);
if [ $exit_status -ne 200 ]; then
exit 1;
fi
sleep 2;
done
"
restartPolicy: OnFailure
backoffLimit: 4
问题是输出是意外的,因为 curl 请求只发出一次,甚至在循环之前,比没有发出任何 curl 请求的程序循环要好:
...
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: Varnish
< Retry-After: 0
< Content-Length: 0
< Accept-Ranges: bytes
< Date: Thu, 05 Jul 2018 17:53:32 GMT
< Via: 1.1 varnish
< Connection: close
< X-Served-By: cache-fra19122-FRA
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1530813212.281242,VS0,VE0
< Location: http://www.bbc.com/
< cache-control: public, max-age=3600
<
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
* Closing connection 0
Running cron loop
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
bash: line 4: [: -ne: unary operator expected
我认为我的脚本有误,但以我目前对 bash 脚本的了解我无法解决这个问题。谁能告诉我哪里出了问题?
谢谢
小修改:
美元符号 $ 应该被转义 -> \$
"\$exit_status" 代替 $exit_status
bash -c " echo \"Running cron loop\";
while true;
do
exit_status=$(curl -v -o /dev/null -w '%{http_code}' http://bbc.com);
if [ $exit_status -ne 200 ]; then
exit 1;
fi
sleep 2;
done
"