将 Persistent Volume Claim 添加到容器中的现有文件
Adding Persistent Volume Claim to the existing file in Container
在我的 docker 图像中,我有一个目录 /opt/myapp/etc
,其中包含一些文件和目录。我想为我的应用程序创建状态集。在该状态集中,我正在创建持久卷声明并附加到 /opt/myapp/etc
。 Statefulset yaml
附在下面。谁能告诉我在这种情况下如何将卷附加到容器?
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: statefulset
labels:
app: myapp
spec:
serviceName: myapp
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: 10.1.23.5:5000/redis
name: redis
ports:
- containerPort: 6379
name: redis-port
- image: 10.1.23.5:5000/myapp:18.1
name: myapp
ports:
- containerPort: 8181
name: port
volumeMounts:
- name: data
mountPath: /opt/myapp/etc
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: standard
resources:
requests:
storage: 5Gi
这是 describe pod 的输出
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 3m (x4 over 3m) default-scheduler pod has unbound PersistentVolumeClaims
Normal Scheduled 3m default-scheduler Successfully assigned controller-statefulset-0 to dev-k8s-2
Normal SuccessfulMountVolume 3m kubelet, dev-k8s-2 MountVolume.SetUp succeeded for volume "default-token-xpskd"
Normal SuccessfulAttachVolume 3m attachdetach-controller AttachVolume.Attach succeeded for volume "pvc-77d2cef8-a674-11e8-9358-fa163e3294c1"
Normal SuccessfulMountVolume 3m kubelet, dev-k8s-2 MountVolume.SetUp succeeded for volume "pvc-77d2cef8-a674-11e8-9358-fa163e3294c1"
Normal Pulling 2m kubelet, dev-k8s-2 pulling image "10.1.23.5:5000/redis"
Normal Pulled 2m kubelet, dev-k8s-2 Successfully pulled image "10.1.23.5:5000/redis"
Normal Created 2m kubelet, dev-k8s-2 Created container
Normal Started 2m kubelet, dev-k8s-2 Started container
Normal Pulled 1m (x4 over 2m) kubelet, dev-k8s-2 Container image "10.1.23.5:5000/myapp:18.1" already present on machine
Normal Created 1m (x4 over 2m) kubelet, dev-k8s-2 Created container
Normal Started 1m (x4 over 2m) kubelet, dev-k8s-2 Started container
Warning BackOff 1m (x7 over 2m) kubelet, dev-k8s-2 Back-off restarting failed container
存储类定义
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: standard
namespace: controller
provisioner: kubernetes.io/cinder
reclaimPolicy: Retain
parameters:
availability: nova
检查您的集群中是否定义了存储 class。
kubectl get storageclass
如果您使用默认存储 class 作为主机路径(在 minikube 的情况下),那么您不需要将存储 class 包含到您的模板中。
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 5Gi
通过指定无存储 class,k8s 将继续并使用默认存储 class 调度持久卷,如果是 minikube,这将是主机路径还确保 /opt/myapp/etc
存在于节点上pod 将被安排。
Kubernetes 不允许将 2 个卷安装到同一目录。第二次安装将覆盖第一次创建的文件。
在我的例子中 docker 图像在 etc 目录中有一些文件,这些文件在安装卷后被删除。使用子路径解决了问题。
在我的 docker 图像中,我有一个目录 /opt/myapp/etc
,其中包含一些文件和目录。我想为我的应用程序创建状态集。在该状态集中,我正在创建持久卷声明并附加到 /opt/myapp/etc
。 Statefulset yaml
附在下面。谁能告诉我在这种情况下如何将卷附加到容器?
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: statefulset
labels:
app: myapp
spec:
serviceName: myapp
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: 10.1.23.5:5000/redis
name: redis
ports:
- containerPort: 6379
name: redis-port
- image: 10.1.23.5:5000/myapp:18.1
name: myapp
ports:
- containerPort: 8181
name: port
volumeMounts:
- name: data
mountPath: /opt/myapp/etc
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: standard
resources:
requests:
storage: 5Gi
这是 describe pod 的输出
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 3m (x4 over 3m) default-scheduler pod has unbound PersistentVolumeClaims
Normal Scheduled 3m default-scheduler Successfully assigned controller-statefulset-0 to dev-k8s-2
Normal SuccessfulMountVolume 3m kubelet, dev-k8s-2 MountVolume.SetUp succeeded for volume "default-token-xpskd"
Normal SuccessfulAttachVolume 3m attachdetach-controller AttachVolume.Attach succeeded for volume "pvc-77d2cef8-a674-11e8-9358-fa163e3294c1"
Normal SuccessfulMountVolume 3m kubelet, dev-k8s-2 MountVolume.SetUp succeeded for volume "pvc-77d2cef8-a674-11e8-9358-fa163e3294c1"
Normal Pulling 2m kubelet, dev-k8s-2 pulling image "10.1.23.5:5000/redis"
Normal Pulled 2m kubelet, dev-k8s-2 Successfully pulled image "10.1.23.5:5000/redis"
Normal Created 2m kubelet, dev-k8s-2 Created container
Normal Started 2m kubelet, dev-k8s-2 Started container
Normal Pulled 1m (x4 over 2m) kubelet, dev-k8s-2 Container image "10.1.23.5:5000/myapp:18.1" already present on machine
Normal Created 1m (x4 over 2m) kubelet, dev-k8s-2 Created container
Normal Started 1m (x4 over 2m) kubelet, dev-k8s-2 Started container
Warning BackOff 1m (x7 over 2m) kubelet, dev-k8s-2 Back-off restarting failed container
存储类定义
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: standard
namespace: controller
provisioner: kubernetes.io/cinder
reclaimPolicy: Retain
parameters:
availability: nova
检查您的集群中是否定义了存储 class。
kubectl get storageclass
如果您使用默认存储 class 作为主机路径(在 minikube 的情况下),那么您不需要将存储 class 包含到您的模板中。
volumeClaimTemplates:
- metadata:
name: data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 5Gi
通过指定无存储 class,k8s 将继续并使用默认存储 class 调度持久卷,如果是 minikube,这将是主机路径还确保 /opt/myapp/etc
存在于节点上pod 将被安排。
Kubernetes 不允许将 2 个卷安装到同一目录。第二次安装将覆盖第一次创建的文件。 在我的例子中 docker 图像在 etc 目录中有一些文件,这些文件在安装卷后被删除。使用子路径解决了问题。