image.png
    image.png
    新建pod yaml配置文件并指定挂载目录与类别,指定volume类别和volume策略

    “vim hostpath.yaml”

    apiVersion: v1
    kind: Pod
    metadata:
    name: volume-hostpath
    namespace: storage
    spec:
    containers:
    - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs-volume
    mountPath: /var/log/nginx
    - name: busybox
    image: busybox:latest
    command: [“/bin/sh”,”-c”,”tail -f /logs/access.log”]
    volumeMounts:
    - name: logs-volume
    mountPath: /logs
    volumes:
    - name: logs-volume
    hostPath:
    path: /root/mengjb
    type: DirectoryOrCreate # 目录存在就使用,不存在就先创建后使用

    image.png
    image.png
    应用并创建hostpath配置,随后检查状态,也可以在dashboard上看得到:

    “kubectl apply -f hostpath.yaml”

    “kubectl get pods -n storage -o wide”

    image.png
    image.png
    image.png
    根据hostpath的概念和原理,他们之间的共享目录应该是被存放在pod之外的node之上,所以我们先看一下这个pod被部署分配在哪个node节点之上,然后我们再去node-2上去看是否有我们刚才指定的目录是否被成功创建,很明显,已经创建成功,并产生了我们想要的日志文件