k8s生产中常用的volumes挂载方式有:hostPath、pv,pvc、nfs
1.hostPath挂载
hostPath是将主机节点文件系统上的文件或目录挂载到Pod 中,同时pod中的目录或者文件也会实时存在宿主机上,如果pod删除,hostpath中的文件不会被删除。(生成的pod只能在同一个节点上,调度到其他节点就不会挂载)
配置文件:
[root@master1 k8s-nginx]# cat nginx-test.yaml
apiVersion: v1
kind: Service
metadata:labels:app: nginx-serviename: nginx-servicenamespace: default
spec:ports:#对外暴露端口30003- nodePort: 30003port: 8010protocol: TCPtargetPort: 8010selector:app: nginx-web#NodePort对外暴露端口type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: nginx-webname: nginx-webnamespace: default
spec:replicas: 1selector:matchLabels:app: nginx-webtemplate:metadata:labels:app: nginx-webnamespace: defaultspec:imagePullSecrets:- name: secret-keycontainers:- image: registry.cn-zhangjiakou.aliyuncs.com/ymku/nginx:v2name: nginximagePullPolicy: Alwaysports:- containerPort: 8010resources:requests:cpu: 100mmemory: 512Milimits:cpu: 1000mmemory: 1GivolumeMounts:- name: nginx-volume-dirmountPath: /var/log/nginx- name: nginx-volume-filemountPath: /var/log/nginx/access2.logvolumes:- name: nginx-volume-dirhostPath:path: /root/k8s-nginx/nginx/logtype: DirectoryOrCreate #如果目录不存在就创建- name: nginx-volume-filehostPath:path: /root/k8s-nginx/nginx/log/access2.logtype: FileOrCreate ## 如果文件不存在则创建
这个是master1节点创建的,pod是在node1节点上运行的,所以日志是存储在node1节点上
需要登录到node1节点上查看挂载的情况:
在node1节点上查看是否目录和日志文件:
2.pv、pvc挂载
3.nfs挂载
待续 ...