搭建共享存储(以MySQL为例)

持久卷使用(nfs存储数据)

Kubernetes 为了使应用程序及其开发人员能够正常请求存储资源,避免处理存储设施细节,引入了 PV 和 PVC。创建 PV 有两种方式:

  • 集群管理员通过手动方式静态创建应用所需要的 PV;
  • 用户手动创建 PVC 并由 Provisioner 组件动态创建对应的 PV。

搭建nfs服务器(ip:192.168.3.210)

1. 安装工具

1
yum -y install nfs-utils

2. 创建nfs目录

1
mkdir -p /nfs/data/

3. 修改全新

1
chmod -R 777 /nfs/data

4. 编辑export文件

1
nano /etc/exports

5. 写入如下内容

“*” 代表所有人都能连接,建议换成具体ip或ip段,如192.168.3.0/24

1
/nfs/data 192.168.3.0/24(rw,no_root_squash,sync)

6. 配置生效

1
exportfs -r

7. 查看生效

1
exportfs

8. 启动rpcbind、nfs服务

1
2
systemctl restart rpcbind && systemctl enable rpcbind
systemctl restart nfs && systemctl enable nfs

9. 查看 RPC 服务的注册状况

1
rpcinfo -p localhost

10. 所有node节点安装nfs客户端

1
yum -y install nfs-utils && systemctl start nfs && systemctl enable nfs

静态创建PV卷

添加pv卷对应目录,这里创建个pv卷,则添加个pv卷的目录作为挂载点。

1. 创建pv卷对应的目录

1
mkdir -p /nfs/data/mysql

2. 配置exportrs,新增如下内容

1
/nfs/data/mysql 192.168.3.0/24(rw,no_root_squash,sync)

3. 配置生效

1
exportfs -r

4. 重启rpcbind、nfs服务

1
systemctl restart rpcbind && systemctl restart nfs

创建 NFS PV与PVC

创建命名空间

1
2
3
4
5
6
7
cat >> common-tools.yaml << EOF
# 创建命名空间
apiVersion: v1
kind: Namespace
metadata:
name: common-tools
EOF
1
kubectl apply -f common-tools.yaml

创建nfs-mysql.yaml文件

注意:修改server里的地址为您的nfs服务地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
cat >> nfs-mysql.yaml << EOF
# 创建PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-mysql
namespace: common-tools
labels:
pv: nfs-mysql
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfs/data/mysql
server: 192.168.3.210

---

# 创建PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-mysql
namespace: common-tools
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
selector:
matchLabels:
pv: nfs-mysql
EOF

创建pv与pvc

1
kubectl apply -f nfs-mysql.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
配置说明:
① capacity 指定 PV 的容量为 1G。
② accessModes 指定访问模式为 ReadWriteOnce,支持的访问模式有:

ReadWriteOnce – PV 能以 read-write 模式 mount 到单个节点。
ReadOnlyMany – PV 能以 read-only 模式 mount 到多个节点。
ReadWriteMany – PV 能以 read-write 模式 mount 到多个节点。

③ persistentVolumeReclaimPolicy 指定当 PV 的回收策略为 Recycle,支持的策略有:

Retain – 需要管理员手工回收。
Recycle – 清除 PV 中的数据,效果相当于执行 rm -rf /thevolume/*。
Delete – 删除 Storage Provider 上的对应存储资源,例如 AWS EBS、GCE PD、Azure
Disk、OpenStack Cinder Volume 等。

④ storageClassName 指定 PV 的 class 为 nfs。相当于为 PV 设置了一个分类,PVC 可以指定 class 申请相应 class 的 PV。
⑤ 指定 PV 在 NFS 服务器上对应的目录。

安装MySQL

创建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
cat >> mysql.yaml << EOF
# 创建配置
kind: ConfigMap
apiVersion: v1
metadata:
name: mysql-config
namespace: common-tools
labels:
app: mysql
data:
my.cnf: |-
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
skip-character-set-client-handshake = true
max_connections=2000
secure_file_priv=/var/lib/mysql
bind-address=0.0.0.0
symbolic-links=0
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

---

# 创建服务
kind: Service
apiVersion: v1
metadata:
labels:
app: mysql
name: mysql-svc
namespace: common-tools
spec:
type: NodePort
ports:
- name: http
port: 3306
nodePort: 30306
protocol: TCP
targetPort: 3306
selector:
app: mysql

---

# 部署配置
kind: Deployment
apiVersion: apps/v1
metadata:
name: mysql
namespace: common-tools
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- args:
- --datadir
- /var/lib/mysql/datadir
env:
- name: MYSQL_ROOT_PASSWORD
value: root
- name: MYSQL_USER
value: user
- name: MYSQL_PASSWORD
value: user
image: mysql:5.7
name: mysql-container
ports:
- containerPort: 3306
name: dbapi
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-storage
- name: config
mountPath: /etc/mysql/conf.d/my.cnf
subPath: my.cnf
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: nfs-mysql
- name: config
configMap:
name: mysql-config
- name: localtime
hostPath:
type: File
path: /etc/localtime
EOF

执行mysql.yaml

1
kubectl apply -f mysql.yaml