在 kubernetes 中挂载 hostPath 的问题

Issues mounting hostPath in kubernetes

好的,我正在尝试在 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 从未启动,我在仪表板中收到以下错误:

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

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

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

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

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

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

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