说明: 很多时候我不想重新构建镜像,并且想要将完成shell脚本而不仅仅是简单的命令作为k8s容器实例command参数输入并且执行。可以借鉴comfigmap的写法使用管道符来输入一个完整的的文件内容。在k8s容器环境,更加适合使用定时任务定时执行一段完整的shell脚本
1、普通实例的写法
apiVersion: v1
kind: Pod
metadata:
labels:
run: my-pod
name: my-pod
namespace: default
spec:
containers:
- args:
- /bin/sh
- -c
- |-
#!/bin/sh
cat /etc/hosts
echo "这是一个测试实例"
echo $(date)
tail -f /etc/hosts
image: busybox:latest
imagePullPolicy: Always
name: my-pod
2、定时任务写法
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-container
image: busybox:latest
command:
- /bin/sh
- -c
- |
#!/bin/sh
cat /etc/hosts
echo "这是一个测试实例"
echo $(date)
#tail -f /etc/hosts
restartPolicy: OnFailure