PersistentVolume与PersistentVolumeClaim的使用
制作nginx执行
下载镜像
swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/rockylinux:9
编写Dockerfile
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/rockylinux:9
LABEL maintainer="huang.jinjin"
RUN yum install wget -y
RUN yum install nginx -y
COPY test.html /usr/share/nginx/html/
EXPOSE 80
ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"]
test.html
文件是随便编写的html文件
构建镜像
docker build -t nginx:v1.0 .
运行镜像
docker run --name mynginx -p 80:80 nginx
访问地址
http://127.0.0.1/test.html
使用nfs-server
创建namespace
nfs-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: nfs-ns
创建PersistentVolume
nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
namespace: nfs-ns
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
nfs:
server: 10.0.2.11
path: "/root/share"
创建PersistentVolumeClaim
nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
namespace: nfs-ns
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
创建nginx deployment
nfs-nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nfs-ns
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:v1.0
ports:
- containerPort: 80
volumeMounts:
- name: nginx-storage
mountPath: /usr/share/nginx/html
volumes:
- name: nginx-storage
persistentVolumeClaim:
claimName: nfs-pvc
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: nfs-ns
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30018
type: NodePort
这样/usr/share/nginx/html
目录就挂载到了宿主机的/root/share
目录。在/root/share
创建 test.html 文件,并写入内容。
启动
kubectl apply -f .
输出
namespace/nfs-ns created
deployment.apps/nginx-deployment created
service/nginx-service created
persistentvolume/nfs-pv created
persistentvolumeclaim/nfs-pvc created
查看
kubectl get namespace
kubectl get pv -n nfs-ns
kubectl get pvc -n nfs-ns
kubectl get pvc -n nfs-ns
kubectl get svc -n nfs-ns
访问
http://10.0.2.12:30018/test.html
10.0.2.12
为部署的pod的宿主机ip;30018
为service映射的port