转自:www.baeldung.com/linux/kuber…
方法一
$ cat /home/muasif80/k8s/test-job3.yml
apiVersion: v1
kind: Pod
metadata:
name: baeldung-test-pod
spec:
containers:
- name: baeldung-test-container
image: ubuntu:latest
command: ["/bin/bash", "-c"]
args: ["/bin/baeldung-test-script.sh"]
volumeMounts:
- name: baeldung-script-volume
mountPath: /bin/
readOnly: true
subPath: baeldung-test-script.sh
volumes:
- name: baeldung-script-volume
configMap:
defaultMode: 0700
name: baeldung-script-configmap
---
apiVersion: v1
kind: ConfigMap
metadata:
name: baeldung-script-configmap
data:
baeldung-test-script.sh: |
#!/bin/bash
echo "Command 1"
echo "Command 2"
echo "Command 3"
方法二
cat /home/muasif80/k8s/test-job2.yml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: ubuntu:latest
command: ["sh", "-c"]
args:
- |
echo "Command 1"
echo "Command 2"
echo "Command 3"
方法三:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: busybox
command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]
或者:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: busybox
command: ["/bin/sh", "-c"]
args:
- echo "This is the first command";
sleep 1;
echo "This is the second command";
方法四
apiVersion: v1
kind: Pod
metadata:
labels:
run: busybox
name: busybox
spec:
containers:
- command:
- /bin/sh
- -c
- |
echo "running below scripts"
i=0;
while true;
do
echo "$i: $(date)";
i=$((i+1));
sleep 1;
done
name: busybox
image: busybox
或者
apiVersion: v1
kind: Pod
metadata:
name: hello-world
spec: # specification of the pod’s contents
restartPolicy: Never
containers:
- name: hello
image: "ubuntu:14.04"
command: ["/bin/sh"]
args:
- -c
- >-
command1 arg1 arg2 &&
command2 arg3 &&
command3 arg4