k8s 使用nfs创建pv和pvc,将pvc挂到nginx应用中的完整配置文件
时间: 2024-02-05 08:11:27 浏览: 155
以下是使用NFS创建PV和PVC,并将PVC挂到Nginx应用中的完整配置文件:
1. 创建NFS服务器和共享目录
在NFS服务器上创建一个共享目录,并将其导出为NFS共享。
2. 创建PV和PVC
创建PV和PVC的yaml文件,示例:
```yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
server: nfs-server-ip
path: /path/to/nfs/share
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
```
3. 创建Nginx Deployment和Service
创建Nginx Deployment和Service的yaml文件,示例:
```yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: nfs-volume
mountPath: "/usr/share/nginx/html"
volumes:
- name: nfs-volume
persistentVolumeClaim:
claimName: nfs-pvc
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
type: LoadBalancer
```
4. 应用配置文件
应用配置文件的yaml文件,示例:
```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
worker_processes 1;
events { worker_connections 1024; }
http {
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: nfs-volume
mountPath: "/usr/share/nginx/html"
- name: nginx-config
mountPath: "/etc/nginx/nginx.conf"
subPath: nginx.conf
volumes:
- name: nfs-volume
persistentVolumeClaim:
claimName: nfs-pvc
- name: nginx-config
configMap:
name: nginx-config
```
其中,应用配置文件中的ConfigMap可自定义Nginx配置。
5. 应用配置文件
将以上配置文件应用到Kubernetes集群中:
```
kubectl apply -f pv-pvc.yaml
kubectl apply -f nginx.yaml
kubectl apply -f nginx-config.yaml
```
这样,就完成了使用NFS创建PV和PVC,并将PVC挂到Nginx应用中的完整配置。
阅读全文