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-servie
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
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 #如果目录不存在就创建
- name: nginx-volume-file
hostPath:
path: /root/k8s-nginx/nginx/log/access2.log
type: FileOrCreate ## 如果文件不存在则创建
这个是master1节点创建的,pod是在node1节点上运行的,所以日志是存储在node1节点上
需要登录到node1节点上查看挂载的情况:
在node1节点上查看是否目录和日志文件:
2.pv、pvc挂载
3.nfs挂载
待续 ...