k8s目錄和文件掛載到宿主機的方式
k8s生產(chǎn)中常用的volumes掛載方式有:hostPath、pv,pvc、nfs
1.hostPath掛載
hostPath是將主機節(jié)點文件系統(tǒng)上的文件或目錄掛載到Pod 中,同時pod中的目錄或者文件也會實時存在宿主機上,如果pod刪除,hostpath中的文件不會被刪除。(生成的pod只能在同一個節(jié)點上,調(diào)度到其他節(jié)點就不會掛載)
配置文件:
[root@master1 k8s-nginx]# cat nginx-test.yaml
apiVersion: v1 kind: Service metadata: labels: app: nginx-service name: nginx-service namespace: default spec: ports: #對外暴露端口30003 - nodePort: 30003 port: 8010 protocol: TCP targetPort: 8010 selector: app: nginx-web #NodePort對外暴露端口 type: NodePort --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-web name: nginx-web namespace: default spec: replicas: 1 selector: matchLabels: app: nginx-web template: metadata: labels: app: nginx-web namespace: default spec: imagePullSecrets: - name: secret-key containers: - image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2 name: nginx env: - name: TZ value: Asia/Shanghai imagePullPolicy: Always ports: - containerPort: 8010 resources: requests: cpu: 100m memory: 512Mi limits: cpu: 1000m memory: 1Gi volumeMounts: - name: nginx-volume-dir mountPath: /var/log/nginx - name: nginx-volume-file mountPath: /var/log/nginx/access2.log volumes: - name: nginx-volume-dir hostPath: path: /root/k8s-nginx/nginx/log type: DirectoryOrCreate #如果目錄不存在就創(chuàng)建 - name: nginx-volume-file hostPath: path: /root/k8s-nginx/nginx/log/access2.log type: FileOrCreate ## 如果文件不存在則創(chuàng)建
這個是master1節(jié)點創(chuàng)建的,pod是在node1節(jié)點上運行的,所以日志是存儲在node1節(jié)點上
需要登錄到node1節(jié)點上查看掛載的情況:
在node1節(jié)點上查看是否目錄和日志文件:
2.nfs掛載
nfs掛載是hostPath掛載的升級版,優(yōu)點是在不同的node節(jié)點上的日志,文件都可以掛載到nfs的機器上,只需要配置上nfs掛載的機器ip和掛載的路徑就行。
安裝nfs,建立共享服務器(單獨服務器安裝nfs掛載,ip:10.10.10.25) [root@localhost ~]# yum -y install nfs-utils ... 創(chuàng)建存儲目錄: [root@localhost ~]# mkdir -p /data/nfs/{conf,dist,log} #可以創(chuàng)建多個存儲目錄 [root@localhost ~]# vim /etc/exports /data/nfs 10.10.10.24(rw,no_root_squash) #可以添加多個存儲目錄 #將共享目錄以讀寫權(quán)限給node1機器,因為pod是跑在node1節(jié)點上:10.10.10.24 啟動nfs應用: [root@localhost ~]# systemctl start nfs 查看進程: [root@localhost ~]# ps -ef |grep nfs root 104715 2 0 15:56 ? 00:00:00 [nfsd4_callbacks] root 104721 2 0 15:56 ? 00:00:00 [nfsd] root 104722 2 0 15:56 ? 00:00:00 [nfsd] root 104723 2 0 15:56 ? 00:00:00 [nfsd] root 104724 2 0 15:56 ? 00:00:00 [nfsd] root 104725 2 0 15:56 ? 00:00:00 [nfsd] root 104726 2 0 15:56 ? 00:00:00 [nfsd] root 104727 2 0 15:56 ? 00:00:00 [nfsd] root 104728 2 0 15:56 ? 00:00:00 [nfsd] root 104750 103971 0 15:56 pts/0 00:00:00 grep --color=auto nfs 修改/etc/exports后,使文件生效: [root@localhost ~]# exportfs -r /data/nfs 10.10.10.24 查看掛載目錄: [root@localhost nfs]# exportfs /data/nfs/conf 10.10.10.24 /data/nfs/log 10.10.10.24 /data/nfs/dist 10.10.10.24 [root@localhost nfs]# exportfs -v /data/nfs/conf 10.10.10.24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash) /data/nfs/log 10.10.10.24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash) /data/nfs/dist 10.10.10.24(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)
編寫pod的yaml文件:
vim nginx-nfs.yaml
apiVersion: v1 kind: Service metadata: labels: app: nginx-service name: nginx-service namespace: default spec: ports: #對外暴露端口30003 - nodePort: 30003 port: 8010 protocol: TCP targetPort: 8010 selector: app: nginx-web #NodePort對外暴露端口 type: NodePort --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-web name: nginx-web namespace: default spec: replicas: 1 selector: matchLabels: app: nginx-web template: metadata: labels: app: nginx-web namespace: default spec: imagePullSecrets: - name: secret-key containers: - image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2 name: nginx env: - name: TZ value: Asia/Shanghai imagePullPolicy: Always ports: - containerPort: 8010 resources: requests: cpu: 100m memory: 512Mi limits: cpu: 1000m memory: 1Gi volumeMounts: - name: nginx-volume-dir mountPath: /var/log/nginx #- name: nginx-volume-file # mountPath: /var/log/nginx/access2.log #- name: nginx-config # mountPath: /etc/nginx/conf.d volumes: - name: nginx-volume-dir nfs: server: 10.10.10.25 path: /data/nfs #- name: nginx-volume-file # server: 10.10.10.25 # path: /data/nfs #- name: nginx-config # nfs: # server: 10.10.10.25 # path: /data/nfs
驗證:到安裝nfs機器上的/data/nfs/log目錄查看是否有文件
問題:單獨nfs掛載好像只能掛載一個目錄,掛載多個目錄不生效并且導致部分文件消失?
/etc/exports 配置:
yaml配置:
volumeMounts: - name: nginx-dir mountPath: /etc/nginx/dist - name: nginx-log mountPath: /var/log/nginx - name: nginx-config mountPath: /etc/nginx/conf.d volumes: - name: nginx-dir nfs: server: 10.10.10.25 path: /data/nfs/dist - name: nginx-log nfs: server: 10.10.10.25 path: /data/nfs/log - name: nginx-config nfs: server: 10.10.10.25 path: /data/nfs/conf
待續(xù)...
3.pv、pvc掛載
pv,pvc掛載是基于nfs掛載的高級方式(如果不搭配nfs使用,側(cè)配置的pv,pvc默認是pod所在node節(jié)點上),通過PV和PVC,Kubernetes可以實現(xiàn)存儲資源的動態(tài)供給、自動擴展和縮減,以及共享和負載均衡等高級特性。PV和PVC的出現(xiàn)使得應用容器可以隨時地掛載或卸載存儲資源,而無需手動管理存儲卷的創(chuàng)建、掛載和卸載等操作。
1.創(chuàng)建pv(相當于存儲設備)
vim pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: pv labels: pv: pv-nfs spec: capacity: storage: 10Gi accessModes: - ReadWriteMany volumeMode: Filesystem persistentVolumeReclaimPolicy: Retain storageClassName: nfs nfs: server: 10.10.10.25 path: /data/nfs
kubectl apply -f pv.yaml
2.創(chuàng)建pvc(相當于調(diào)度存儲設備資源的)
vim nginx-pvc.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc spec: accessModes: - ReadWriteMany volumeMode: Filesystem resources: requests: storage: 2Gi storageClassName: nfs selector: matchLabels: pv: pv-nfs
kubectl apply -f pvc.yaml
3.創(chuàng)建pod(去請求pvc的)
vim nginx-pod.yaml
apiVersion: v1 kind: Service metadata: labels: app: nginx-service name: nginx-service namespace: default spec: ports: #對外暴露端口30003 - nodePort: 30003 port: 8010 protocol: TCP targetPort: 8010 selector: app: nginx-web #NodePort對外暴露端口 type: NodePort --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-web name: nginx-web namespace: default spec: replicas: 1 selector: matchLabels: app: nginx-web template: metadata: labels: app: nginx-web namespace: default spec: imagePullSecrets: - name: secret-key containers: - image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2 name: nginx env: - name: TZ value: Asia/Shanghai imagePullPolicy: Always ports: - containerPort: 8010 resources: requests: cpu: 100m memory: 512Mi limits: cpu: 1000m memory: 1Gi volumeMounts: - name: nginx-log mountPath: /var/log/nginx #- name: nginx-conf # mountPath: /etc/nginx/conf.d #- name: nginx-dist # mountPath: /etc/nginx/dist volumes: - name: nginx-log persistentVolumeClaim: claimName: pvc #- name: nginx-conf # persistentVolumeClaim: # claimName: pvc #- name: nginx-dist # persistentVolumeClaim: # claimName: pvc
kubectl apply -f nginx-pod.yaml
查看運行狀態(tài):
kubectl describe pod nginx-web-6665c66698-fxhzl
驗證:
登錄到nfs的服務器,進到掛載路徑下看是否有文件
總結(jié)
到此這篇關(guān)于k8s目錄和文件掛載到宿主機的文章就介紹到這了,更多相關(guān)k8s目錄文件掛載到宿主機內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你在k8s上部署HADOOP-3.2.2(HDFS)的方法
這篇文章主要介紹了k8s-部署HADOOP-3.2.2(HDFS)的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04kubernetes需要默認的serviceaccount的原因解析
這篇文章主要介紹了kubernetes為何需要默認的serviceaccount,ServiceAccount 是 Kubernetes 中的一種重要概念,它的實際使用場景包括很多,本文給大家講解的非常詳細,需要的朋友可以參考下2023-04-04ES業(yè)務數(shù)據(jù)遷移遇到的精度問題BUG
這篇文章主要為大家介紹了ES業(yè)務數(shù)據(jù)遷移遇到的BUG精度問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06