安装
#所有机器安装
yum install -y nfs-utils
ren
#nfs主节点
echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports
mkdir -p /nfs/data
systemctl enable rpcbind --now
systemctl enable nfs-server --now
#配置生效
exportfs -r
#查询
exportfs
yan/bai
检查挂载点
showmount -e 1.15.230.38
创建对应目录
mkdir -p /nfs/data
挂载
mount -t nfs 1.15.230.38:/nfs/data /nfs/data
验证
echo "hello nfs server" > /nfs/data/test.txt
nginx挂载
ren
创建对应目录(否则会报错)
mkdir -p /nfs/data/nginx-pv
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-pv-demo
name: nginx-pv-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx-pv-demo
template:
metadata:
labels:
app: nginx-pv-demo
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
nfs:
server: 1.15.230.38
path: /nfs/data/nginx-pv
pv
创建pv
ren
创建相应目录
#nfs主节点
mkdir -p /nfs/data/01
mkdir -p /nfs/data/02
mkdir -p /nfs/data/03
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv01-10m
spec:
capacity:
storage: 10M
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/01
server: 1.15.230.38
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv02-1gi
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/02
server: 1.15.230.38
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv03-3gi
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/03
server: 1.15.230.38
查询pv
kubectl get PersistentVolume
#或
kubectl get pv
pvc
创建pvc
ren
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs
查询pvnc
kubectl get pvc
pod绑定pvc
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-deploy-pvc
name: nginx-deploy-pvc
spec:
replicas: 2
selector:
matchLabels:
app: nginx-deploy-pvc
template:
metadata:
labels:
app: nginx-deploy-pvc
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: nginx-pvc
ConfigMap
redis
echo 'appendonly yes
requirepass 123456789
' >> /root/redis.conf
创建配置集(configmap,简称cm)
# 创建配置,redis保存到k8s的etcd;
kubectl create cm redis-conf --from-file=/root/redis.conf
apiVersion: v1
data: #data是所有真正的数据,key:默认是文件名 value:配置文件的内容
redis.conf: |+
appendonly yes
requirepass 123456789
kind: ConfigMap
metadata:
name: redis-conf
namespace: default
查询配置集
#查询全部
kubectl get cm
#查询指定
kubectl get cm redis-conf -oyaml
创建之后就可以删除本地文件了,因为已经保存到etcd rm -rf /root/redis.conf
修改配置集
kubectl edit cm redis-conf
创建pod,引用配置集
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
command:
- redis-server
- "/redis-master/redis.conf" #指的是redis容器内部的位置
ports:
- containerPort: 6379
volumeMounts:
- mountPath: /data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: redis-conf
items:
- key: redis.conf
path: redis.conf