k8s目錄和文件掛載到宿主機(jī)的方式
k8s生產(chǎn)中常用的volumes掛載方式有:hostPath、pv,pvc、nfs
1.hostPath掛載
hostPath是將主機(jī)節(jié)點(diǎn)文件系統(tǒng)上的文件或目錄掛載到Pod 中,同時(shí)pod中的目錄或者文件也會(huì)實(shí)時(shí)存在宿主機(jī)上,如果pod刪除,hostpath中的文件不會(huì)被刪除。(生成的pod只能在同一個(gè)節(jié)點(diǎn)上,調(diào)度到其他節(jié)點(diǎn)就不會(huì)掛載)
配置文件:
[root@master1 k8s-nginx]# cat nginx-test.yaml
apiVersion: v1 kind: Service metadata: labels: app: nginx-service name: nginx-service namespace: default spec: ports: #對(duì)外暴露端口30003 - nodePort: 30003 port: 8010 protocol: TCP targetPort: 8010 selector: app: nginx-web #NodePort對(duì)外暴露端口 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)建
這個(gè)是master1節(jié)點(diǎn)創(chuàng)建的,pod是在node1節(jié)點(diǎn)上運(yùn)行的,所以日志是存儲(chǔ)在node1節(jié)點(diǎn)上
需要登錄到node1節(jié)點(diǎn)上查看掛載的情況:
在node1節(jié)點(diǎn)上查看是否目錄和日志文件:
2.nfs掛載
nfs掛載是hostPath掛載的升級(jí)版,優(yōu)點(diǎn)是在不同的node節(jié)點(diǎn)上的日志,文件都可以掛載到nfs的機(jī)器上,只需要配置上nfs掛載的機(jī)器ip和掛載的路徑就行。
安裝nfs,建立共享服務(wù)器(單獨(dú)服務(wù)器安裝nfs掛載,ip:10.10.10.25) [root@localhost ~]# yum -y install nfs-utils ... 創(chuàng)建存儲(chǔ)目錄: [root@localhost ~]# mkdir -p /data/nfs/{conf,dist,log} #可以創(chuàng)建多個(gè)存儲(chǔ)目錄 [root@localhost ~]# vim /etc/exports /data/nfs 10.10.10.24(rw,no_root_squash) #可以添加多個(gè)存儲(chǔ)目錄 #將共享目錄以讀寫權(quán)限給node1機(jī)器,因?yàn)閜od是跑在node1節(jié)點(diǎn)上:10.10.10.24 啟動(dòng)nfs應(yīng)用: [root@localhost ~]# systemctl start nfs 查看進(jìn)程: [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: #對(duì)外暴露端口30003 - nodePort: 30003 port: 8010 protocol: TCP targetPort: 8010 selector: app: nginx-web #NodePort對(duì)外暴露端口 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
驗(yàn)證:到安裝nfs機(jī)器上的/data/nfs/log目錄查看是否有文件
問(wèn)題:單獨(dú)nfs掛載好像只能掛載一個(gè)目錄,掛載多個(gè)目錄不生效并且導(dǎo)致部分文件消失?
/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掛載的高級(jí)方式(如果不搭配nfs使用,側(cè)配置的pv,pvc默認(rèn)是pod所在node節(jié)點(diǎn)上),通過(guò)PV和PVC,Kubernetes可以實(shí)現(xiàn)存儲(chǔ)資源的動(dòng)態(tài)供給、自動(dòng)擴(kuò)展和縮減,以及共享和負(fù)載均衡等高級(jí)特性。PV和PVC的出現(xiàn)使得應(yīng)用容器可以隨時(shí)地掛載或卸載存儲(chǔ)資源,而無(wú)需手動(dòng)管理存儲(chǔ)卷的創(chuàng)建、掛載和卸載等操作。
1.創(chuàng)建pv(相當(dāng)于存儲(chǔ)設(shè)備)
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(相當(dāng)于調(diào)度存儲(chǔ)設(shè)備資源的)
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(去請(qǐng)求pvc的)
vim nginx-pod.yaml
apiVersion: v1 kind: Service metadata: labels: app: nginx-service name: nginx-service namespace: default spec: ports: #對(duì)外暴露端口30003 - nodePort: 30003 port: 8010 protocol: TCP targetPort: 8010 selector: app: nginx-web #NodePort對(duì)外暴露端口 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
查看運(yùn)行狀態(tài):
kubectl describe pod nginx-web-6665c66698-fxhzl
驗(yàn)證:
登錄到nfs的服務(wù)器,進(jìn)到掛載路徑下看是否有文件
總結(jié)
到此這篇關(guān)于k8s目錄和文件掛載到宿主機(jī)的文章就介紹到這了,更多相關(guān)k8s目錄文件掛載到宿主機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
超詳細(xì)的Kubernetes?(k8s)常用命令整理
這篇文章主要介紹了Kubernetes?(k8s)常用命令整理的相關(guān)資料,講解了Kubernetes集群管理、節(jié)點(diǎn)資源查看、Pod管理、部署管理、命名空間管理、服務(wù)負(fù)載均衡、調(diào)試排錯(cuò)以及備份恢復(fù)等操作的命令,需要的朋友可以參考下2025-03-03教你在k8s上部署HADOOP-3.2.2(HDFS)的方法
這篇文章主要介紹了k8s-部署HADOOP-3.2.2(HDFS)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04kubernetes需要默認(rèn)的serviceaccount的原因解析
這篇文章主要介紹了kubernetes為何需要默認(rèn)的serviceaccount,ServiceAccount 是 Kubernetes 中的一種重要概念,它的實(shí)際使用場(chǎng)景包括很多,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-04-04ES業(yè)務(wù)數(shù)據(jù)遷移遇到的精度問(wèn)題BUG
這篇文章主要為大家介紹了ES業(yè)務(wù)數(shù)據(jù)遷移遇到的BUG精度問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06使用sealos快速搭建K8s集群環(huán)境的過(guò)程
這篇文章主要介紹了使用sealos快速搭建K8s集群環(huán)境,主要包括sealos安裝方法,虛擬機(jī)設(shè)置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09