Docker 容器在 kubernetes 内工作/不工作

标签 docker kubernetes kubernetes-pod

我在这里有点困惑。它确实像普通的 docker 容器一样工作,但是当它进入 pod 时它不会。所以这就是我的做法。

Dockerfile in my local to create the image and publish to docker registry


FROM alpine:3.7
COPY . /var/www/html
CMD tail -f /dev/null

现在,如果我只是拉出图像(在删除本地之后)并作为容器运行。它可以工作,我可以在/var/www/html 中看到我的文件。

现在我想在我的 kubernetes 集群中使用它。

Def : Minikube --vm-driver=none



我在 minikube 中运行 kube,但没有驱动程序选项。所以对于单节点集群。

编辑

如果我从部署文件中删除卷安装和声明,我可以在/var/www/html 中看到我的数据。

Deployment file


apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    io.kompose.service: app
  name: app
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        io.kompose.service: app
    spec:
      securityContext:
        runAsUser: 1000
        runAsGroup: 1000
      containers:
      - image: kingshukdeb/mycode
        name: pd-mycode
        resources: {}
        volumeMounts:
        - mountPath: /var/www/html
          name: claim-app-storage
      restartPolicy: Always
      volumes:
      - name: claim-app-storage
        persistentVolumeClaim:
          claimName: claim-app-nginx
status: {}

PVC file


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: app-nginx1
  name: claim-app-nginx
spec:
  storageClassName: testmanual
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}

PV file


apiVersion: v1
kind: PersistentVolume
metadata:
  name: app-nginx1
  labels:
    type: local
spec:
  storageClassName: testmanual
  capacity:
    storage: 100Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/volumes/app"

现在,当我运行这些文件时,它会创建 pod、pv、pvc 和 pvc 绑定(bind)到 pv。但如果我进入我的容器,我看不到我的文件。 hostpath/数据/卷/应用程序 .任何想法将不胜感激。

最佳答案

当 PVC 绑定(bind)到 pod 时,卷将安装在 pod/deployment yaml 文件中描述的位置。在您的情况下:mountPath: /var/www/html .这就是无法访问“烘焙到”容器镜像中的文件的原因(简单解释为什么 here )

您可以通过运行 kubectl exec YOUR_POD -i -t -- /bin/sh 对容器执行 exec 来确认这一点。 ,并运行 mount | grep "/var/www/html" .

解决方案

您可以通过多种方式解决此问题。最好的做法是将静态数据分开(即在 PV 中),并使容器镜像尽可能小且快。

如果您将要在 PV 中挂载的文件传输到主机路径 /data/volumes/app它们将可以在您的 pod 中访问,然后您可以创建新图像而省略 COPY手术。这样,即使 pod 崩溃,您的应用程序对文件所做的更改也将被保存。

如果 PV 将被多个 pod 认领,您需要更改 accessModes如所述here :

The access modes are:

  • ReadWriteOnce – the volume can be mounted as read-write by a single node
  • ReadOnlyMany – the volume can be mounted read-only by many nodes
  • ReadWriteMany – the volume can be mounted as read-write by many nodes


Kubernetes 文档中 Volumes 的深入解释:https://kubernetes.io/docs/concepts/storage/persistent-volumes/

关于Docker 容器在 kubernetes 内工作/不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56682297/

相关文章:

docker - 将Docker日志输出输出到Mac和Linux上的文件中

spring-boot - Kubernetes网络,如何将变量传递到容器

Azure Kubernetes - 副本与 HPA?

kubernetes - 我们可以在 Kubernetes 的一个 Pod 中拥有相同类型的多个容器吗?

kubernetes - 如何通过API获取kubernetes资源信息(总体CPU和内存使用情况)

ubuntu - Docker :/var/run/docker. sock:没有这样的文件或目录

docker - 版本 `GLIBC_2.29' 未找到

azure - 使用私有(private)注册表中的镜像进行 k8s 部署

kubernetes - Configmap - 现有文件被删除

kubernetes - 找不到新 pods 的原因