安装

  1. #所有机器安装
  2. yum install -y nfs-utils

ren

  1. #nfs主节点
  2. echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports
  3. mkdir -p /nfs/data
  4. systemctl enable rpcbind --now
  5. systemctl enable nfs-server --now
  6. #配置生效
  7. exportfs -r
  8. #查询
  9. exportfs

yan/bai

检查挂载点

  1. showmount -e 1.15.230.38

创建对应目录

  1. mkdir -p /nfs/data

挂载

  1. mount -t nfs 1.15.230.38:/nfs/data /nfs/data

验证

  1. echo "hello nfs server" > /nfs/data/test.txt

nginx挂载

ren

创建对应目录(否则会报错)

  1. mkdir -p /nfs/data/nginx-pv
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: nginx-pv-demo
  6. name: nginx-pv-demo
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: nginx-pv-demo
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx-pv-demo
  16. spec:
  17. containers:
  18. - image: nginx
  19. name: nginx
  20. volumeMounts:
  21. - name: html
  22. mountPath: /usr/share/nginx/html
  23. volumes:
  24. - name: html
  25. nfs:
  26. server: 1.15.230.38
  27. path: /nfs/data/nginx-pv

pv

创建pv

ren

创建相应目录

  1. #nfs主节点
  2. mkdir -p /nfs/data/01
  3. mkdir -p /nfs/data/02
  4. mkdir -p /nfs/data/03
  1. apiVersion: v1
  2. kind: PersistentVolume
  3. metadata:
  4. name: pv01-10m
  5. spec:
  6. capacity:
  7. storage: 10M
  8. accessModes:
  9. - ReadWriteMany
  10. storageClassName: nfs
  11. nfs:
  12. path: /nfs/data/01
  13. server: 1.15.230.38
  14. ---
  15. apiVersion: v1
  16. kind: PersistentVolume
  17. metadata:
  18. name: pv02-1gi
  19. spec:
  20. capacity:
  21. storage: 1Gi
  22. accessModes:
  23. - ReadWriteMany
  24. storageClassName: nfs
  25. nfs:
  26. path: /nfs/data/02
  27. server: 1.15.230.38
  28. ---
  29. apiVersion: v1
  30. kind: PersistentVolume
  31. metadata:
  32. name: pv03-3gi
  33. spec:
  34. capacity:
  35. storage: 3Gi
  36. accessModes:
  37. - ReadWriteMany
  38. storageClassName: nfs
  39. nfs:
  40. path: /nfs/data/03
  41. server: 1.15.230.38

查询pv

  1. kubectl get PersistentVolume
  2. #或
  3. kubectl get pv

image.png

pvc

创建pvc

ren

  1. kind: PersistentVolumeClaim
  2. apiVersion: v1
  3. metadata:
  4. name: nginx-pvc
  5. spec:
  6. accessModes:
  7. - ReadWriteMany
  8. resources:
  9. requests:
  10. storage: 200Mi
  11. storageClassName: nfs

image.png

查询pvnc

  1. kubectl get pvc

image.png

pod绑定pvc

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: nginx-deploy-pvc
  6. name: nginx-deploy-pvc
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: nginx-deploy-pvc
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx-deploy-pvc
  16. spec:
  17. containers:
  18. - image: nginx
  19. name: nginx
  20. volumeMounts:
  21. - name: html
  22. mountPath: /usr/share/nginx/html
  23. volumes:
  24. - name: html
  25. persistentVolumeClaim:
  26. claimName: nginx-pvc

image.png

ConfigMap

redis

  1. echo 'appendonly yes
  2. requirepass 123456789
  3. ' >> /root/redis.conf

创建配置集(configmap,简称cm)

  1. # 创建配置,redis保存到k8s的etcd;
  2. kubectl create cm redis-conf --from-file=/root/redis.conf
  1. apiVersion: v1
  2. data: #data是所有真正的数据,key:默认是文件名 value:配置文件的内容
  3. redis.conf: |+
  4. appendonly yes
  5. requirepass 123456789
  6. kind: ConfigMap
  7. metadata:
  8. name: redis-conf
  9. namespace: default

查询配置集

  1. #查询全部
  2. kubectl get cm
  3. #查询指定
  4. kubectl get cm redis-conf -oyaml

image.png

创建之后就可以删除本地文件了,因为已经保存到etcd rm -rf /root/redis.conf

修改配置集

  1. kubectl edit cm redis-conf

创建pod,引用配置集

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: redis
  5. spec:
  6. containers:
  7. - name: redis
  8. image: redis
  9. command:
  10. - redis-server
  11. - "/redis-master/redis.conf" #指的是redis容器内部的位置
  12. ports:
  13. - containerPort: 6379
  14. volumeMounts:
  15. - mountPath: /data
  16. name: data
  17. - mountPath: /redis-master
  18. name: config
  19. volumes:
  20. - name: data
  21. emptyDir: {}
  22. - name: config
  23. configMap:
  24. name: redis-conf
  25. items:
  26. - key: redis.conf
  27. path: redis.conf