【问题标题】:Issues mounting hostPath in kubernetes在 Kubernetes 中挂载 hostPath 的问题
【发布时间】:2017-07-29 02:43:34
【问题描述】:

好的,我正在尝试在 kubernetes 的 minikube 中设置 Jenkins 容器的本地版本,并且我使用 this guide 作为模型。

我遇到的问题是 hostPath 似乎没有正确安装。

当我尝试在不使用“persistentVolumeClaim”的情况下挂载主机路径时 我的代码如下所示:

deployment_jenkins.yaml

apiVersion: extensions/v1beta1

kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: master
    spec:
      containers:
      - name: master
        image: jenkins:2.7.2
        ports:
        - containerPort: 8080
        - containerPort: 50000
        readinessProbe:
          httpGet:
            path: /login
            port: 8080
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 2
          failureThreshold: 5
        env:
        - name: JENKINS_OPTS
          valueFrom:
            secretKeyRef:
              name: jenkins
              key: options
        volumeMounts:
        - mountPath: /var/jenkins_home
          name: jenkins-home
        resources:
          limits:
            cpu: 500m
            memory: 1500Mi
          requests:
            cpu: 500m
            memory: 1500Mi
      volumes:
      - name: jenkins-home
      hostPath:
        path: /data

当我运行 kubectl apply -f deployment_jenkins.yaml 时出现此错误:

my-mac kubernetes $ kubectl apply -f deployment_jenkins.yaml
error: error validating "deployment_jenkins.yaml": error validating data: 
found invalid field hostPath for v1.PodSpec; if you choose to ignore these 
errors, turn validation off with --validate=false

然后当我尝试使用声明时,我得到了这个:

persistent_volume_claim_jenkins.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: jenkins-home-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: jenkins-home

persistent_volume_jenkins.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: jenkins-home
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /data

deployment_jenkins.yaml

apiVersion: extensions/v1beta1

kind: Deployment
metadata:
  name: jenkins
  namespace: jenkins
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: master
    spec:
      containers:
      - name: master
        image: jenkins:2.7.2
        ports:
        - containerPort: 8080
        - containerPort: 50000
        readinessProbe:
          httpGet:
            path: /login
            port: 8080
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 2
          failureThreshold: 5
        env:
        - name: JENKINS_OPTS
          valueFrom:
            secretKeyRef:
              name: jenkins
              key: options
        volumeMounts:
        - mountPath: /var/jenkins_home
          name: jenkins-home
        resources:
          limits:
            cpu: 500m
            memory: 1500Mi
      requests:
        cpu: 500m
        memory: 1500Mi
      volumes:
      - name: jenkins-home
        persistentVolumeClaim:
          claimName: jenkins-home-claim

然后我运行命令:

my-mac kubernetes $ kubectl apply -f .
deployment "jenkins" configured
persistentvolumeclaim "jenkins-home-claim" configured
persistentvolume "jenkins-home" configured

最终,我的 pod 从未启动,我在仪表板中收到以下错误:

我非常困惑,如果能帮助我弄清楚我做错了什么以及如何让它发挥作用,我将不胜感激。

【问题讨论】:

    标签: kubernetes minikube


    【解决方案1】:

    您的某些 yaml 缩进错误,导致不同的(损坏的)解释:

    volumes: - name: jenkins-home hostPath: path: /data

    这是一个具有两个字段的对象,volumeshostPath。相反,您想要的是:

    volumes: - name: jenkins-home hostPath: path: /data

    另外,我建议您始终在 yaml 中引用(非块)字符串以避免意外:

    volumes: - name: 'jenkins-home' hostPath: path: '/data'

    【讨论】:

    • 成功,我现在有点远了 :) 现在我收到这个错误:无法写入 /var/jenkins_home/copy_reference_file.log。错误的卷权限?触摸:无法触摸'/var/jenkins_home/copy_reference_file.log':权限被拒绝,你知道如何通过它吗?
    • 当然。运行您的容器,并有权写入您为其提供的文件系统。也许使用 init 容器以合适的方式设置文件系统。这取决于 Jenkins 容器的工作方式。
    • 谢谢,我将您的答案标记为正确答案。我从来没有完全做到这一点,因为使用注册表来提取我的自定义 Docker 映像变得太痛苦了(谷歌云容器注册表身份验证权限文档不太工作),所以我跳过了这部分并推送我的新变化出来了。但是双引号完全解决了我的第一个错误。谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 2017-07-24
    • 2018-12-02
    • 2021-02-16
    • 2020-02-25
    • 1970-01-01
    • 2021-03-19
    • 2018-11-21
    相关资源
    最近更新 更多