k8s 中 nfs作為存儲的三種方式


1、安裝nfs服務。直接給命令

yum install nfs-utils

   
vim /etc/exports
/data/k8s/ 172.16.1.0/24(sync,rw,no_root_squash)
   
systemctl start nfs;  systemctl start rpcbind 
systemctl enable nfs
 
測試:
yum install nfs-utils
showmount -e 172.16.1.131


 

2、nfs 可以直接作為存儲卷使用,下面是一個生產環境部署的YAML配置文件。在此示例中,redis在容器中的持久化數據保存在/data目錄下;存儲卷使用nfs,nfs的服務地址為:192.168.8.150,存儲路徑為:/k8s-nfs/redis/data。容器通過volumeMounts.name的值確定所使用的存儲卷。生產中一般掛載日志以及一些永久存儲的文件使用
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      app: redis
  revisionHistoryLimit: 2
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      
# 應用的鏡像
      - image: redis
        name: redis
        imagePullPolicy: IfNotPresent
        
# 應用的內部端口
        ports:
        - containerPort: 6379
          name: redis6379
        env:
        - name: ALLOW_EMPTY_PASSWORD
          value: "yes"
        - name: REDIS_PASSWORD
          value: "redis"          
# 持久化掛接位置,在docker中 
        volumeMounts:
        - name: redis-persistent-storage
          mountPath: /data
      volumes:      
# 宿主機上的目錄
      - name: redis-persistent-storage
        nfs:
          path: /data/k8s
          server: 172.16.1.131

 

3、nfs作為在Kubernetes當前版本的中,可以創建類型為nfs的持久化存儲卷,用於為PersistentVolumClaim提供存儲卷。在下面的PersistenVolume YAML配置文件中,定義了一個名為nfs-pv的持久化存儲卷,此存儲卷提供了5G的存儲空間,只能由一個PersistentVolumClaim進行可讀可寫操作。此持久化存儲卷使用的nfs服務器地址為192.168.5.150,存儲的路徑為/tmp。

這里簡單提供下使用方法。具體可以查看我的博客中動態pv和靜態PV的區別和使用

 
          
[root@VM_0_48_centos prometheus]# cat mypv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv001
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/k8s
    server: 172.19.0.14

[root@VM_0_48_centos prometheus]# cat mypvc.yaml   ###會根據大小和類型自動匹配到上面的PV
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  namespace: kube-system
  name: prometheus-claim
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

[root@VM_0_48_centos prometheus]# kubectl  get pv,pvc -n kube-system
NAME                     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                          STORAGECLASS   REASON   AGE
persistentvolume/pv001   10Gi       RWX            Retain           Bound    kube-system/prometheus-claim                           17m

NAME                                     STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/prometheus-claim   Bound    pv001    10Gi       RWX  


apiVersion: apps/v1
kind: StatefulSet
metadata:
   name: prometheus
   namespace : kube-system
   labels:
     k8s-app: prometheus
     kubernetes.io/cluster-service:  "true"
     addonmanager.kubernetes.io/mode: Reconcile
     version: v2.2.1
spec:
   serviceName:  "prometheus"
   replicas: 1
   podManagementPolicy:  "Parallel"
   updateStrategy:
    type:  "RollingUpdate"
   selector:
     matchLabels:
       k8s-app: prometheus
   template:
     metadata:
       labels:
         k8s-app: prometheus
       annotations:
         scheduler.alpha.kubernetes.io/critical-pod:  ''
     spec:
       priorityClassName: system-cluster-critical
       serviceAccountName: prometheus
       initContainers:
       - name:  "init-chown-data"
         image:  "busybox:latest"
         imagePullPolicy:  "IfNotPresent"
         command: [ "chown" "-R" "65534:65534" "/data" ]
         volumeMounts:
         - name: prometheus-data
           mountPath: /data
           subPath:  ""
       containers:
         - name: prometheus-server-configmap-reload
           image:  "jimmidyson/configmap-reload:v0.1"
           imagePullPolicy:  "IfNotPresent"
           args:
             - --volume-dir=/etc/config
             - --webhook-url=http: //localhost:9090/-/reload
           volumeMounts:
             - name: config-volume
               mountPath: /etc/config
               readOnly:  true
           resources:
             limits:
               cpu: 10m
               memory: 10Mi
             requests:
               cpu: 10m
               memory: 10Mi
 
         - name: prometheus-server
           image:  "prom/prometheus:v2.2.1"
           imagePullPolicy:  "IfNotPresent"
           args:
             - --config.file=/etc/config/prometheus.yml
             - --storage.tsdb.path=/data
             - --web.console.libraries=/etc/prometheus/console_libraries
             - --web.console.templates=/etc/prometheus/consoles
             - --web.enable-lifecycle
           ports:
             - containerPort: 9090
           readinessProbe:
             httpGet:
               path: /-/ready
               port: 9090
             initialDelaySeconds: 30
             timeoutSeconds: 30
           livenessProbe:
             httpGet:
               path: /-/healthy
               port: 9090
             initialDelaySeconds: 30
             timeoutSeconds: 30
           # based on 10 running nodes with 30 pods each
           resources:
             limits:
               cpu: 200m
               memory: 1000Mi
             requests:
               cpu: 200m
               memory: 1000Mi
 
           volumeMounts:
             - name: config-volume
               mountPath: /etc/config
             - name: prometheus-data
               mountPath: /data
               subPath:  ""
       terminationGracePeriodSeconds: 300
       volumes:
         - name: config-volume
           configMap:
             name: prometheus-config
         - name: prometheus-data
           persistentVolumeClaim:   #申明使用靜態PVC永久化存儲
             claimName: prometheus-claim 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM