我已经配置了一个Kubernetes集群来创建带有三个Pod的StatefulSet。每个Pod都有自己的PersistentVolumeClaim。运行此配置可以正常工作,并启动Pod。现在,我想用Gradle的配置文件填充每个PersistentVolume。这就是我的Kubernetes配置的样子(无关的部分被省略/混淆):
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
selector:
matchLabels:
app: my-statefulset
serviceName: "my-statefulset"
replicas: 3
template:
metadata:
labels:
app: my-statefulset
spec:
containers:
- name: my-statefulset
image: path.to.some.artifactory.com/some-image:1.2.3
volumeMounts:
- name: my-volume
mountPath: /home/foo/.gradle
subPath: gradle
initContainers:
- name: my-statefulset-init
image: path.to.some.artifactory.com/some-image:1.2.3
volumeMounts:
- mountPath: /home/foo/.gradle
name: my-volume
subPath: gradle
command:
- sh
- '-c'
- 'cp'
- "./gradle.properties"
- "/home/jenkins/.gradle/gradle.properties"
volumeClaimTemplates:
- metadata:
name: my-volume
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 150Gi
YAML与配置中引用的./gradle.properties
一起位于目录中。从该文件夹中依次使用kubectl appy -f .
和kubectl get pods -o wide
运行配置时,我得到以下输出:
my-statefulset-0 0/1 Init:CrashLoopBackOff 7 12m 11.32.13.2 aee05ea1-809c-4b35-8533-131e818eb1cc <none>
调用kubectl describe pod my-statefulset-0
会产生:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 27s default-scheduler Successfully assigned somuser/my-statefulset-0 to 8599877f-090d-4e6e-b556-d60f5c3d4334
Normal SuccessfulAttachVolume 25s attachdetach-controller AttachVolume.Attach succeeded for volume "pvc-b2354b68-aa0d-11e9-90f5-005056863826"
Normal Pulled 8s (x2 over 10s) kubelet, 8599877f-090d-4e6e-b556-d60f5c3d4334 Container image "path.to.some.artifactory.com/some-image:1.2.3" already present on machine
Normal Created 8s (x2 over 9s) kubelet, 8599877f-090d-4e6e-b556-d60f5c3d4334 Created container
Normal Started 8s (x2 over 9s) kubelet, 8599877f-090d-4e6e-b556-d60f5c3d4334 Started container
Warning BackOff 6s (x2 over 7s) kubelet, 8599877f-090d-4e6e-b556-d60f5c3d4334 Back-off restarting failed container
我知道该命令显然是从容器/容器内部运行的,无法访问本地计算机上的./gradle.properties
文件。但是,如何在特定位置轻松地用配置文件填充PersistentVolume? Kubernetes Documentation about InitCountainers明确提到配置文件是一个用例,但没有给出我想要实现的示例。
请注意,由于公司的限制,我不能为InitContainer
使用busybox。但是,在Pod中运行的映像包含完整的Linux发行版。这就是为什么我也将它用于InitContainer
。