环境
CentOS 7.6
三台主机做 GlusterFS 存储
K8s集群环境
准备环境
# 关闭selinux
setenforce 0
sed -i 's#^SELINUX=.*#SELINUX=disabled#' /etc/selinux/config
# 配置 hosts
cat >> /etc/hosts <<-EOF
192.168.0.71 node1.local
192.168.0.72 node2.local
192.168.0.73 node3.local
EOF
# 分别设置主机名
hostnamectl set-hostname node1.local
hostnamectl set-hostname node2.local
hostnamectl set-hostname node3.local
安装 GlusterFS
# 先安装 gluster 源
yum install centos-release-gluster -y
# 安装 glusterfs 组件
yum install -y glusterfs glusterfs-server glusterfs-fuse glusterfs-rdma glusterfs-geo-replication glusterfs-devel
## 创建 glusterfs 目录
mkdir /opt/glusterd
## 修改 glusterd 目录
sed -i 's#/var/lib/glusterd#/opt/glusterd#' /etc/glusterfs/glusterd.vol
# 启动 glusterfs
systemctl start glusterd.service
# 设置开机启动
systemctl enable glusterd.service
#查看状态
systemctl status glusterd.service
配置 GlusterFS
# 开放端口
iptables -I INPUT -p tcp --dport 24007 -j ACCEPT
# 创建存储目录
mkdir /opt/gfs_data
# 添加节点到 集群
# 执行操作的本机不需要 probe 本机
gluster peer probe node2.local
gluster peer probe node3.local
# 查看集群状态
gluster peer status
配置 volume
GlusterFS 中的 volume 的模式有很多种,包括以下几种:
- 分布卷(默认模式):即 DHT, 也叫 分布卷: 将文件以 hash 算法随机分布到 一台服务器节点中存储。
- 复制模式:即 AFR, 创建 volume 时带 replica x 数量: 将文件复制到 replica x 个节点中。
- 条带模式:即 Striped, 创建 volume 时带 stripe x 数量: 将文件切割成数据块,分别存储到 stripe x 个节点中 (类似 raid 0)。
- 分布式条带模式:最少需要 4 台服务器才能创建。 创建 volume 时 stripe 2 server = 4 个节点: 是 DHT 与 Striped 的组合型。
- 分布式复制模式:最少需要 4 台服务器才能创建。 创建 volume 时 replica 2 server = 4 个节点:是 DHT 与 AFR 的组合型。
- 条带复制卷模式:最少需要 4 台服务器才能创建。 创建 volume 时 stripe 2 replica 2 server = 4 个节点: 是 Striped 与 AFR 的组合型。
- 三种模式混合: 至少需要 8 台 服务器才能创建。 stripe 2 replica 2 , 每 4 个节点 组成一个 组。
这几种模式的示例图参考 GlusterFS Documentation。
因为我们只有三台主机,在此我们使用默认的分布卷模式。请勿在生产环境上使用该模式,容易导致数据丢失。
# 创建分布卷
gluster volume create k8s-volume transport tcp node1.local:/opt/gfs_data node2.local:/opt/gfs_data node3.local:/opt/gfs_data force
# 查看 volume 状态
gluster volume info
# 启动 分布卷
gluster volume start k8s-volume
Glusterfs 调优
# 开启 指定 volume 的配额
gluster volume quota k8s-volume enable
# 限制 指定 volume 的配额
gluster volume quota k8s-volume limit-usage / 1TB
# 设置 cache 大小, 默认 32MB(此处设置过大会导致挂载失败)
gluster volume set k8s-volume performance.cache-size 1GB
# 设置 io 线程, 太大会导致进程崩溃
gluster volume set k8s-volume performance.io-thread-count 16
# 设置 网络检测时间, 默认 42s
gluster volume set k8s-volume network.ping-timeout 10
# 设置 写缓冲区的大小, 默认 1M
gluster volume set k8s-volume performance.write-behind-window-size 1024MB
Kubernetes 中使用 GlusterFS
官方的文档见https://github.com/kubernetes/examples/tree/master/staging/volumes/glusterfs. 以下用到的所有 yaml 和 json 配置文件可以在 glusterfs 中找到。注意替换其中私有镜像地址为你自己的镜像地址。
kubernetes 安装客户端
# 先安装 gluster 源
yum install centos-release-gluster -y
# 在所有 k8s node 中安装 glusterfs 客户端
yum install -y glusterfs glusterfs-fuse
# 配置 hosts
cat >> /etc/hosts <<-EOF
192.168.0.71 node1.local
192.168.0.72 node2.local
192.168.0.73 node3.local
EOF
因为我们 glusterfs 跟 kubernetes 集群复用主机,因为此这一步可以省去。
配置 endpoints
curl -OL https://raw.githubusercontent.com/kubernetes/examples/master/staging/volumes/glusterfs/glusterfs-endpoints.json
cat > glusterfs-endpoints.json <<-EOF
{
"kind": "Endpoints",
"apiVersion": "v1",
"metadata": {
"name": "glusterfs-cluster"
},
"subsets": [
{
"addresses": [{ "ip": "192.168.0.71" }],
"ports": [{ "port": 1990 }]
},
{
"addresses": [{ "ip": "192.168.0.72" }],
"ports": [{ "port": 1990 }]
},
{
"addresses": [{ "ip": "192.168.0.73" }],
"ports": [{ "port": 1990 }]
}
]
}
EOF
# 导入 glusterfs-endpoints.json
kubectl apply -f glusterfs-endpoints.json
# 查看 endpoints 信息
kubectl get endpoints
配置 service
curl -OL https://raw.githubusercontent.com/kubernetes/examples/master/staging/volumes/glusterfs/glusterfs-service.json
# service.json 里面查找的是 enpointes 的名称与端口,端口默认配置为 1,我改成了 1990
cat > glusterfs-service.json <<-EOF
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "glusterfs-cluster"
},
"spec": {
"ports": [
{"port": 1990}
]
}
}
EOF
# 导入 glusterfs-service.json
kubectl apply -f glusterfs-service.json
# 查看 service 信息
kubectl get svc
创建测试 pod
curl -OL https://github.com/kubernetes/examples/raw/master/staging/volumes/glusterfs/glusterfs-pod.json
# 编辑 glusterfs-pod.json 修改 volumes 下的 path 为上面创建的 volume 名称 ("path": "k8s-volume")
cat > glusterfs-pod.json <<-EOF
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "glusterfs"
},
"spec": {
"containers": [
{
"name": "glusterfs",
"image": "nginx",
"volumeMounts": [
{
"mountPath": "/mnt/glusterfs",
"name": "glusterfsvol"
}
]
}
],
"volumes": [
{
"name": "glusterfsvol",
"glusterfs": {
"endpoints": "glusterfs-cluster",
"path": "k8s-volume",
"readOnly": true
}
}
]
}
}
EOF
# 导入 glusterfs-pod.json
kubectl apply -f glusterfs-pod.json
# 查看 pods 状态
kubectl get pods
# 查看 pods 所在 node
kubectl describe pods/glusterfs
# 查看容器内挂载
kubectl exec glusterfs -- mount | grep gluster
# 登陆 node 物理机,使用 df 可查看挂载目录
df -h
配置 PersistentVolume
PersistentVolume(PV)和 PersistentVolumeClaim(PVC)是 kubernetes 提供的两种 API 资源,用于抽象存储细节。管理员关注于如何通过 pv 提供存储功能而无需关注用户如何使用,同样的用户只需要挂载 PVC 到容器中而不需要关注存储卷采用何种技术实现。PVC 和 PV 的关系跟 pod 和 node 关系类似,前者消耗后者的资源。PVC 可以向 PV 申请指定大小的存储资源并设置访问模式。
PV 属性
- storage 容量
- 读写属性:分别为
- ReadWriteOnce:单个节点读写;
- ReadOnlyMany:多节点只读 ;
- ReadWriteMany:多节点读写
cat > glusterfs-pv.yaml <<-EOF
apiVersion: v1
kind: PersistentVolume
metadata:
name: gluster-dev-volume
spec:
capacity:
storage: 8Gi
accessModes:
- ReadWriteMany
glusterfs:
endpoints: "glusterfs-cluster"
path: "k8s-volume"
readOnly: false
EOF
# 导入 PV
kubectl apply -f glusterfs-pv.yaml
# 查看 pv
kubectl get pv
PVC 属性
- 访问属性与 PV 相同
- 容量:向 PV 申请的容量 <= PV 总容量
配置 PVC
cat > glusterfs-pvc.yaml <<-EOF
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: glusterfs-nginx
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 8Gi
EOF
# 导入 pvc
kubectl apply -f glusterfs-pvc.yaml
# 查看 pvc
kubectl get pv
创建 nginx deployment 挂载 volume
cat > nginx-deployment.yaml <<-EOF
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-dm
spec:
replicas: 2
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: gluster-dev-volume
mountPath: "/usr/share/nginx/html"
volumes:
- name: gluster-dev-volume
persistentVolumeClaim:
claimName: glusterfs-nginx
EOF
# 导入 deployment
kubectl apply -f nginx-deployment.yaml
# 查看 deployment
kubectl get pods |grep nginx-dm
# 查看 挂载
kubectl exec nginx-dm-8df56c754-xwlpz -- mount | grep gluster
kubectl exec nginx-dm-8df56c754-ww6xs -- mount | grep gluster
# 创建文件 测试
kubectl exec -it nginx-dm-8df56c754-xwlpz -- touch /usr/share/nginx/html/index.html
kubectl exec -it nginx-dm-8df56c754-xwlpz -- ls -lt /usr/share/nginx/html/
kubectl exec -it nginx-dm-8df56c754-ww6xs -- ls -lt /usr/share/nginx/html/
# 验证 glusterfs
# 因为我们使用分布卷,所以可以看到某个节点中有文件
ls /opt/gfs_data/
所有评论(0)