Kubernetes 之 Nginx 动静态 PV 持久存储 下篇

接上篇 Kubernetes 中部署 NFS Provisioner 为 NFS 提供动态分配卷(上篇)请添加链接描述

一、nginx 使用 nfs 静态 PV

1、静态 nfs-static-nginx-rc.yaml

##清理资源
kubectl delete -f nfs-static-nginx-rc.yaml -n test

cat >nfs-static-nginx-rc.yaml<<\EOF
##创建namespace
---
apiVersion: v1
kind: Namespace
metadata:
   name: test
   labels:
     name: test
##创建nfs-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
  labels:
    pv: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 注意这里使用nfs的storageClassName,如果没改k8s的默认storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创建nfs-pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-pvc
  namespace: test
  labels:
    pvc: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nfs-pv
##部署应用nginx
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-test
  namespace: test
  labels:
    name: nginx-test
spec:
  replicas: 2
  selector:
    name: nginx-test
  template:
    metadata:
      labels:
       name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: docker.io/nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-pvc
##创建service
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30080
  selector:
    name: nginx-test
EOF

##创建资源
kubectl apply -f nfs-static-nginx-rc.yaml -n test

##查看pv资源
kubectl get pv -n test --show-labels

##查看pvc资源
kubectl get pvc -n test --show-labels

##查看pod
$ kubectl get pods -n test
NAME               READY   STATUS    RESTARTS   AGE
nginx-test-r4n2j   1/1     Running   0          54s
nginx-test-zstf5   1/1     Running   0          54s

#可以看到,nginx应用已经部署成功。
#nginx应用的数据目录是使用的nfs共享存储,我们在nfs共享的目录里加入index.html文件,然后再访问nginx-service暴露的端口
#切换到到nfs-server服务器上

echo "Test NFS Share discovery with nfs-static-nginx-rc" > /data/nfs/nginx/index.html

#在浏览器上访问kubernetes主节点的 http://master:30080,就能访问到这个页面内容了

2、静态 nfs-static-nginx-deployment.yaml

##清理资源
kubectl delete -f nfs-static-nginx-deployment.yaml -n test

cat >nfs-static-nginx-deployment.yaml<<\EOF
##创建namespace
---
apiVersion: v1
kind: Namespace
metadata:
   name: test
   labels:
     name: test
##创建nfs-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
  labels:
    pv: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 注意这里使用nfs的storageClassName,如果没改k8s的默认storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创建nfs-pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-pvc
  namespace: test
  labels:
    pvc: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nfs-pv
##部署应用nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: test
  labels:
    name: nginx-test
spec:
  replicas: 2
  selector:
    matchLabels:
      name: nginx-test
  template:
    metadata:
      labels:
       name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: docker.io/nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-pvc
##创建service
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30080
  selector:
    name: nginx-test
EOF

##创建资源
kubectl apply -f nfs-static-nginx-deployment.yaml -n test

##查看pv资源
kubectl get pv -n test --show-labels

##查看pvc资源
kubectl get pvc -n test --show-labels

##查看pod
$ kubectl get pods -n test
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-64d6f78cdf-8bw8t   1/1     Running   0          55s
nginx-deployment-64d6f78cdf-n5n4q   1/1     Running   0          55s

#可以看到,nginx应用已经部署成功。
#nginx应用的数据目录是使用的nfs共享存储,我们在nfs共享的目录里加入index.html文件,然后再访问nginx-service暴露的端口
#切换到到nfs-server服务器上

echo "Test NFS Share discovery with nfs-static-nginx-deployment" > /data/nfs/nginx/index.html

#在浏览器上访问kubernetes主节点的 http://master:30080,就能访问到这个页面内容了

3、nginx?多目录挂载

  1. PV 和 PVC 是一一对应关系,当有 PV 被某个 PVC ?所占用时,会显示 banding,其它 PVC 不能再使用绑定过的 PV。
  2. PVC 一旦绑定 PV,就相当于是一个存储卷,此时 PVC 可以被多个? Pod 所使用。(PVC支不支持被多个 Pod 访问,取决于访问模型? ?accessMode 的定义)。
  3. PVC 若没有找到合适的 PV 时,则会处于 pending 状态。
  4. PV 是属于集群级别的,不能定义在名称空间中。
  5. PVC 时属于名称空间级别的。
##清理资源
kubectl delete -f nfs-static-nginx-dp-many.yaml -n test

cat >nfs-static-nginx-dp-many.yaml<<\EOF
##创建namespace
---
apiVersion: v1
kind: Namespace
metadata:
   name: test
   labels:
     name: test
##创建nginx-data-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-data-pv
  labels:
    pv: nginx-data-pv
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 注意这里使用nfs的storageClassName,如果没改k8s的默认storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创建nginx-etc-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-etc-pv
  labels:
    pv: nginx-etc-pv
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 注意这里使用nfs的storageClassName,如果没改k8s的默认storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创建pvc名字为nfs-nginx-data,存放数据
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-nginx-data
  namespace: test
  labels:
    pvc: nfs-nginx-data
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nginx-data-pv
##创建pvc名字为nfs-nginx-etc,存放配置文件
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-nginx-etc
  namespace: test
  labels:
    pvc: nfs-nginx-etc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nginx-etc-pv
##部署应用nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: test
  labels:
    name: nginx-test
spec:
  replicas: 2
  selector:
    matchLabels:
      name: nginx-test
  template:
    metadata:
      labels:
       name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: docker.io/nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        # - mountPath: /etc/nginx   #--这里需要注意,如果是这么挂载,那么需要事先现在/data/nfs/nginx/目录下把nginx的完整配置提前拷贝好
        #   name: nginx-etc
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-nginx-data
      # - name: nginx-etc
      #   persistentVolumeClaim:
      #     claimName: nfs-nginx-etc
##创建service
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30080
  selector:
    name: nginx-test
EOF

##创建资源
kubectl apply -f nfs-static-nginx-dp-many.yaml -n test

##查看pv资源
kubectl get pv -n test --show-labels

##查看pvc资源
kubectl get pvc -n test --show-labels

##查看pod
$ kubectl get pods -n test
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-64d6f78cdf-8bw8t   1/1     Running   0          55s
nginx-deployment-64d6f78cdf-n5n4q   1/1     Running   0          55s

##进入容器
kubectl exec -it nginx-deployment-f687cdf47-xncj8 -n test /bin/bash

#可以看到,nginx应用已经部署成功。
#nginx应用的数据目录是使用的nfs共享存储,我们在nfs共享的目录里加入index.html文件,然后再访问nginx-service暴露的端口
#切换到到nfs-server服务器上

echo "Test NFS Share discovery with nfs-static-nginx-dp-many" > /data/nfs/nginx/index.html

#在浏览器上访问kubernetes主节点的 http://master:30080,就能访问到这个页面内容了

4、参数 namespace

##清理资源
export NAMESPACE="mos-namespace"

kubectl delete -f nfs-static-nginx-dp-many.yaml -n ${NAMESPACE}

cat >nfs-static-nginx-dp-many.yaml<<-EOF
##创建namespace
---
apiVersion: v1
kind: Namespace
metadata:
   name: ${NAMESPACE}
   labels:
     name: ${NAMESPACE}
##创建nginx-data-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-data-pv
  labels:
    pv: nginx-data-pv
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 注意这里使用nfs的storageClassName,如果没改k8s的默认storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创建nginx-log-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-log-pv
  labels:
    pv: nginx-log-pv
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 注意这里使用nfs的storageClassName,如果没改k8s的默认storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创建pvc名字为nfs-nginx-data,存放数据
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-nginx-data
  labels:
    pvc: nfs-nginx-data
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nginx-data-pv
##创建pvc名字为nfs-nginx-log,存放日志文件
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-nginx-log
  labels:
    pvc: nfs-nginx-log
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nginx-log-pv
##部署应用nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    name: nginx-test
spec:
  replicas: 2
  selector:
    matchLabels:
      name: nginx-test
  template:
    metadata:
      labels:
       name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: docker.io/nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        - mountPath: /var/log/nginx
          name: nginx-log
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-nginx-data
      - name: nginx-log
        persistentVolumeClaim:
          claimName: nfs-nginx-log
##创建service
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30180
  selector:
    name: nginx-test
EOF

##创建资源
kubectl apply -f nfs-static-nginx-dp-many.yaml -n ${NAMESPACE}

二、nginx 使用 nfs 动态 PV

https://github.com/Lancger/opsfull/blob/master/components/external-storage/3%E3%80%81%E5%8A%A8%E6%80%81%E7%94%B3%E8%AF%B7PV%E5%8D%B7.md

1、动态 nfs-dynamic-nginx.yaml

通过参数控制在哪个命名空间创建

##清理命名空间
kubectl delete ns k8s-public

##创建命名空间
kubectl create ns k8s-public

##清理资源
kubectl delete -f nfs-dynamic-nginx-deployment.yaml -n k8s-public

cat >nfs-dynamic-nginx-deployment.yaml<<\EOF
##动态申请nfs-dynamic-pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-dynamic-claim
spec:
  storageClassName: nfs-storage #--需要与上面创建的storageclass的名称一致
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 90Gi
##部署应用nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    name: nginx-test
spec:
  replicas: 3
  selector:
    matchLabels:
      name: nginx-test
  template:
    metadata:
      labels:
       name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: docker.io/nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-dynamic-claim
##创建service
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30090
  selector:
    name: nginx-test
EOF

##创建资源
kubectl apply -f nfs-dynamic-nginx-deployment.yaml -n k8s-public

##查看pv资源
kubectl get pv -n k8s-public --show-labels

##查看pvc资源
kubectl get pvc -n k8s-public --show-labels

##查看pod
$ kubectl get pods -n k8s-public
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-544f569478-5t8wm   1/1     Running   0          40s
nginx-deployment-544f569478-8gks5   1/1     Running   0          40s
nginx-deployment-544f569478-pw96x   1/1     Running   0          40s

#可以看到,nginx应用已经部署成功。
#nginx应用的数据目录是使用的nfs共享存储,我们在nfs共享的目录里加入index.html文件,然后再访问nginx-service暴露的端口
#切换到到nfs-server服务器上

#注意动态的在这个目录,创建的目录命名方式为 “namespace名称-pvc名称-pv名称”
/data/nfs/kube-public-test-claim-pvc-ad304939-e75d-414f-81b5-7586ef17db6c

echo "Test NFS Share discovery with nfs-dynamic-nginx-deployment" > /data/nfs/kube-public-test-claim-pvc-ad304939-e75d-414f-81b5-7586ef17db6c/index.html

#在浏览器上访问kubernetes主节点的 http://master:30090,就能访问到这个页面内容了

参考文档:

作者:Lancger
github 地址:https://url.cn/5osLAuY
转载请获得作者授权
Golang 课程火热招生资料找WeChat:17812796384

原文地址:https://blog.51cto.com/51reboot/2456442

时间: 2024-10-11 06:00:08

Kubernetes 之 Nginx 动静态 PV 持久存储 下篇的相关文章

Nginx 动静态访问分离

1.静态 1)图片   #过虑图片     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {          root D:/www; #静态文件存放路径     } 2)js.css #过虑js和css,静态页面     location ~ .*\.(js|css|html|htm)?$  {          root D:/www; #静态文件存放路径          } 2.动态 1).jsp和.do #过虑动态页面.jsp和.do     

Kubernetes进阶之PersistentVolume 静态供给实现NFS网络存储

Kubernetes进阶之PersistentVolume 静态供给实现NFS网络存储网络存储 NFS是一种很早的技术,单机的存储在服务器方面还是非常主流的,但nfs唯一的就是缺点比较大就是没有集群版,做集群化还是比较费劲的,文件系统做不了,这是一个很大的弊端,大规模的还是需要选择一些分布式的存储,nfs就是一个网络文件存储服务器,装完nfs之后,共享一个目录,其他的服务器就可以通过这个目录挂载到本地了,在本地写到这个目录的文件,就会同步到远程服务器上,实现一个共享存储的功能,一般都是做数据的共

kubernetes 的pv/pvc存储

kubernetes 的pv/pvc存储 标签(空格分隔): kubernetes系列 一: kubernetes的PV/PVC存储 一: kubernetes的PV/PVC存储 1.1 pv PersistentVolume (PV) 是由管理员设置的存储,它是群集的一部分.就像节点是集群中的资源一样,PV 也是集群中的资源. PV 是 Volume 之类的卷插件,但具有独立于使用 PV 的 Pod 的生命周期.此 API 对象包含存储实现的细节,即 NFS. iSCSI 或特定于云供应商的存

nginx反向代理,动静态请求分离,以及nginx缓存应用,以及使用ngx_cache_purge清除指定URL

一,nginx反向代理配置 #tomcat Java代码   upstream tomcat_server{ server 127.0.0.1:8080; } erver{ listen 80; server_name www.wolfdream.com; location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X

Nginx 与Tomcat 实现动静态分离、负载均衡

一.Nginx简介: Nginx一个高性能的HTTP和反向代理服务器, 具有很高的稳定性和支持热部署.模块扩展也很容易.当遇到访问的峰值,或者有人恶意发起慢速连接时,也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器,Nginx采取了分阶段资源分配技术,处理静态文件和无缓存的反向代理加速,实现了负载均衡和容错,在这样高并发的访问情况下,能经受起高并发的处理. 二.Nginx安装与配置   第一步:下载Nginx 安装包  http://nginx.org/en/download.

Head First Python学习笔记3——持久存储

经过上几章的学习,完成如下任务:读取一个文本文件里的内容,将每一行的内容按“:”分割成两部分,根据分割出第一项判断并分别放入两个列表里,去除首尾空白,在屏幕上打印. # 两个列表用于存储数据man=[] other=[] try: data=open("demo.txt", encoding="utf-8") #文件里的内容类似“man:hello.\n woman:hello.\n man:how are you. \n woman:yes.”如果有中文别忘了编码

nginx 提供静态内容

This section describes how to serve static content, how to use different ways of setting up the paths to look for files, and how to set up index files. 这一章讨如何提供静态文件,如何使用不同方式设置查找文件的路径,如果设置索引文件. Root Directory and Index Files根目录和索引文件 The rootdirective

数据结构之第二章线性表之静态链式存储

1--特点:用一维数组来描述线性表,用游标代替指针指示节点在数组中的相对位置.不设“指针”类型的高级语言中适用链表结构. 2--线性表的静态链式存储结构 ////  静态单链表.h//  单链表的静态存储//// 6 //  Copyright (c) 2014年 dashuai. All rights reserved.// #ifndef SLIST_H#define SLIST_H#include <stdio.h>#include <stdlib.h> #define MA

静态串的存储结构以及增删改查

/** *数据结构串的操作,有增删改查 *串的初始化以及串的赋值等操作必须注意,在新学的时候必须注意值得传递,指针的指向以及初始化的结果 */ //操作效果图: 代码: //串的静态存储结构/**静态数组存储结构typedef struct{ char str[MaxSize]; int length;}String;*/ /**动态的数组存储结构typedef struct{ char *str; //表示动态数组的名 int MaxLength;//表示动态数组元素的最大个数int leng