k8s生產(chǎn)中常用的volumes掛載方式有:hostPath、pv,pvc、nfs
1.hostPath掛載
? ? ?hostPath是將主機(jī)節(jié)點(diǎn)文件系統(tǒng)上的文件或目錄掛載到Pod 中,同時pod中的目錄或者文件也會實(shí)時存在宿主機(jī)上,如果pod刪除,hostpath中的文件不會被刪除。(生成的pod只能在同一個節(jié)點(diǎn)上,調(diào)度到其他節(jié)點(diǎn)就不會掛載)
配置文件:
[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é)點(diǎn)創(chuàng)建的,pod是在node1節(jié)點(diǎn)上運(yùn)行的,所以日志是存儲在node1節(jié)點(diǎn)上
需要登錄到node1節(jié)點(diǎn)上查看掛載的情況:
?在node1節(jié)點(diǎn)上查看是否目錄和日志文件:
2.nfs掛載
? ? nfs掛載是hostPath掛載的升級版,優(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)建存儲目錄:
[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機(jī)器,因?yàn)閜od是跑在node1節(jié)點(diǎn)上:10.10.10.24
啟動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:
#對外暴露端口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
驗(yàn)證:到安裝nfs機(jī)器上的/data/nfs/log目錄查看是否有文件
問題:單獨(dú)nfs掛載好像只能掛載一個目錄,掛載多個目錄不生效并且導(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掛載的高級方式(如果不搭配nfs使用,側(cè)配置的pv,pvc默認(rèn)是pod所在node節(jié)點(diǎn)上),通過PV和PVC,Kubernetes可以實(shí)現(xiàn)存儲資源的動態(tài)供給、自動擴(kuò)展和縮減,以及共享和負(fù)載均衡等高級特性。PV和PVC的出現(xiàn)使得應(yīng)用容器可以隨時地掛載或卸載存儲資源,而無需手動管理存儲卷的創(chuàng)建、掛載和卸載等操作。
1.創(chuàng)建pv(相當(dāng)于存儲設(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)度存儲設(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(去請求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
查看運(yùn)行狀態(tài):
kubectl describe pod nginx-web-6665c66698-fxhzl
驗(yàn)證:
登錄到nfs的服務(wù)器,進(jìn)到掛載路徑下看是否有文件文章來源:http://www.zghlxwxcb.cn/news/detail-805813.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-805813.html
到了這里,關(guān)于k8s 目錄和文件掛載到宿主機(jī)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!