1.k8s数据存储的分类:
1.基础存储的分类:
分类 | 说明 |
---|---|
EmptyDir | EmptyDir是在Pod被分配到Node时创建的,无须手动指定,当Pod销毁时, EmptyDir中的数据也会被永久删除 |
HostPath | HostPath就是将Node主机中一个实际目录挂载到Pod中,保证Pod销毁了,但是数据依旧可以存在于Node主机上 |
NFS | NFS是一个网络文件存储系统,解决了节点故障数据异常的问题 |
2.高级存储:
分类 | 说明 |
---|---|
PV(Persistent Volume) | 是持久化卷的意思,是对底层的共享存储的一种抽象,PV相似于卷组(存储池) |
PVC(Persistent Volume Claim) | 是持久卷声明的意思,用户对于存储需求的一种声明,PVC如同在卷组中划分出的逻辑卷 |
3.高级存储的分类:
分类 | 说明 |
---|---|
ConfigMap | ConfigMap是一种比较特殊的存储卷,它的主要作用是用来存储配置信息的,明文保存 |
Secret | 它主要用于存储敏感信息,例如密码、秘钥、证书等等,可加密保存 |
2.基础存储-hostpath
1.创建mysql的yaml文件
cat << eof > mysql.yaml
apiVersion: apps/v1 # 通过kubectl explain deployment命令查看版本
kind: Deployment # 资源类型
metadata:
name: mysql-deployment # 资源名称
namespace: mysql # 指定命名空间
spec:
replicas: 1 # 控制器下的pod数
selector:
matchLabels:
app: mysql # 这是控制器用来控制pod的标签
template:
metadata:
labels:
app: mysql # pod的标签
spec:
containers:
- name: mysql # 容器名
image: mysql:8.0.20 # 容器镜像
env: # mysql环境参数
- name: MYSQL_ROOT_PASSWORD # 指定ROOT用户密码
value: "你的密码"
ports: # 容器暴露的端口
- name: mysql
containerPort: 3306
volumeMounts: # 容器内的被挂载目录
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
volumes: # 声明volume(宿主机目录), name为logs-volume
- name: mysql-data
hostPath:
path: /root/mysql/data
type: DirectoryOrCreate # 目录存在就使用,不存在就先创建后使用
- name: mysql-config
hostPath:
path: /root/mysql/conf
type: DirectoryOrCreate # 目录存在就使用,不存在就先创建后使用
---
apiVersion: v1
kind: Service
metadata:
name: mysql-nodeport
namespace: mysql
spec:
selector:
app: mysql # 服务通过此标签连接deployment资源来暴露端口
type: NodePort # 服务类型:NodePort
ports: # 不指定NodePort端口就会自动分配,(默认的取值范围是:30000-32767)
- protocol: TCP
port: 3306
targetPort: 3306
eof
2.创建mysq应用:
kubectl apply -f mysql.yaml
3.查看pod:
[root@master mysql]# kubectl get deployment,pod,svc -n mysql -o wide
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql-deployment 1/1 1 1 7m57s
deployment.apps/nginx 1/1 1 1 15d
NAME READY STATUS RESTARTS AGE
pod/mysql-deployment-b68b5b8bc-jps5c 1/1 Running 0 7m57s
pod/nginx-6867cdf567-phn5l 1/1 Running 1 148m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
service/mysql-nodeport NodePort 10.98.118.107 <none> 3306:30160/TCP 7m57s
service/nginx NodePort 10.103.194.224 <none> 80:30105/TCP 15d
4.远程连接:
[root@localhost ~]# mysql -h192.168.174.30 -P30160 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
3.基础存储-nfs服务器
1.安装nfs服务器:
- master节点上:
-
使配置生效
exportfs -r
-
将共享目录以读写权限暴露给192.168.174.0/24网段中的所有主机
cat << eof >> /etc/exports /root/nfs/mysql 192.168.174.0/24(rw,no_root_squash) eof
-
启动nfs服务
systemctl enable nfs-server;systemctl restart nfs-server
-
准备一个共享目录
mkdir -p /root/nfs/mysql -pv;chmod o=rwx /root/nfs/mysql
-
在master上安装nfs服务:
yum install nfs-utils -y
-
-
node节点:文章来源:https://www.toymoban.com/news/detail-830030.html
-
接下来,要在的每个node节点上都安装下nfs,这样的目的是为了node节点可以驱动nfs设备文章来源地址https://www.toymoban.com/news/detail-830030.html
yum install nfs-utils -y
systemctl enable nfs-server;systemctl restart nfs-server
-
2.创建mysql的yaml文件:
cat << eof > mysql.yaml
apiVersion: apps/v1 # 通过kubectl explain deployment命令查看版本
kind: Deployment # 资源类型
metadata:
name: mysql-deployment # 资源名称
namespace: mysql # 指定命名空间
spec:
replicas: 1 # 控制器下的pod数
selector:
matchLabels:
app: mysql # 这是控制器用来控制pod的标签
template:
metadata:
labels:
app: mysql # pod的标签
spec:
containers:
- name: mysql # 容器名
image: mysql:8.0.20 # 容器镜像
env: # mysql环境参数
- name: MYSQL_ROOT_PASSWORD # 指定ROOT用户密码
value: "你的密码"
ports: # 容器暴露的端口
- name: mysql
containerPort: 3306
volumeMounts: # 容器内的被挂载目录
- name: mysql-data
mountPath: /var/lib/mysql
volumes: # 声明volume(宿主机目录), name为logs-volume
- name: mysql-data
nfs:
server: 192.168.174.30 # nfs服务器地址
# /root/nfs/mysql是共享目录,data是创建的一个目录用来存储mysql数据
path: /root/nfs/mysql/data # 共享文件路径(服务器地址)
---
apiVersion: v1
kind: Service
metadata:
name: mysql-nodeport
namespace: mysql
spec:
selector:
app: mysql # 服务通过此标签连接deployment资源来暴露端口
type: NodePort # 服务类型:NodePort
ports: # 不指定NodePort端口就会自动分配,(默认的取值范围是:30000-32767)
- protocol: TCP
port: 3306
targetPort: 3306
eof
3.创建mysq应用:
kubectl apply -f mysql.yaml
4.查看pod:
[root@master ~]# kubectl get deployment,pod,svc -n mysql -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/mysql-deployment 1/1 1 1 3m17s mysql mysql:8.0.20 app=mysql
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/mysql-deployment-5fb47b585b-k7bdm 1/1 Running 0 3m17s 10.244.2.9 node2 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/mysql-nodeport NodePort 10.99.39.122 <none> 3306:31819/TCP 3m17s app=mysql
5.查看master节点上的目录:
pod所在node节点是没有目录的,直接使用nfs服务器的目录
[root@master ~]# ll nfs/mysql/data/
total 181696
-rw-r----- 1 polkitd input 56 Nov 29 05:20 auto.cnf
-rw-r----- 1 polkitd input 912 Nov 29 05:20 binlog.000001
-rw-r----- 1 polkitd input 16 Nov 29 05:20 binlog.index
-rw------- 1 polkitd input 1680 Nov 29 05:20 ca-key.pem
-rw-r--r-- 1 polkitd input 1112 Nov 29 05:20 ca.pem
-rw-r--r-- 1 polkitd input 1112 Nov 29 05:20 client-cert.pem
-rw------- 1 polkitd input 1680 Nov 29 05:20 client-key.pem
-rw-r----- 1 polkitd input 196608 Nov 29 05:20 #ib_16384_0.dblwr
-rw-r----- 1 polkitd input 8585216 Nov 29 05:20 #ib_16384_1.dblwr
-rw-r----- 1 polkitd input 5498 Nov 29 05:20 ib_buffer_pool
-rw-r----- 1 polkitd input 12582912 Nov 29 05:20 ibdata1
-rw-r----- 1 polkitd input 50331648 Nov 29 05:20 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Nov 29 05:20 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Nov 29 05:20 ibtmp1
drwxr-x--- 2 polkitd input 187 Nov 29 05:20 #innodb_temp
drwxr-x--- 2 polkitd input 143 Nov 29 05:20 mysql
-rw-r----- 1 polkitd input 29360128 Nov 29 05:20 mysql.ibd
drwxr-x--- 2 polkitd input 8192 Nov 29 05:20 performance_schema
-rw------- 1 polkitd input 1680 Nov 29 05:20 private_key.pem
-rw-r--r-- 1 polkitd input 452 Nov 29 05:20 public_key.pem
-rw-r--r-- 1 polkitd input 1112 Nov 29 05:20 server-cert.pem
-rw------- 1 polkitd input 1680 Nov 29 05:20 server-key.pem
drwxr-x--- 2 polkitd input 28 Nov 29 05:20 sys
-rw-r----- 1 polkitd input 10485760 Nov 29 05:20 undo_001
-rw-r----- 1 polkitd input 11534336 Nov 29 05:20 undo_002
6.远程连接:
[root@localhost ~]# mysql -h192.168.174.30 -P30160 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
4.高级存储-PV和PVC:
1.安装nfs服务器:
- master节点上:
-
使配置生效
exportfs -r
-
将共享目录以读写权限暴露给192.168.174.0/24网段中的所有主机
cat << eof >> /etc/exports /root/nfs/mysql 192.168.174.0/24(rw,no_root_squash) eof
-
启动nfs服务
systemctl enable nfs-server;systemctl restart nfs-server
-
准备一个共享目录
mkdir -p /root/nfs/mysql -pv;chmod o=rwx /root/nfs/mysql
-
在master上安装nfs服务:
yum install nfs-utils -y
-
-
node节点:
-
接下来,要在的每个node节点上都安装下nfs,这样的目的是为了node节点可以驱动nfs设备
yum install nfs-utils -y
systemctl enable nfs-server;systemctl restart nfs-server
-
2.创建pv:
cat << eof > pv1.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity: # 存储能力,目前只支持存储空间的设置
storage: 3Gi
accessModes: # 访问模式:读写权限,可以被多个节点挂载
- ReadWriteMany
persistentVolumeReclaimPolicy: Retain # 回收策略
nfs:
path: /root/nfs/mysql/data # 这里的路径必须和nfs服务器配置文件中的某一个一致
server: 192.168.174.30 # nfs服务器地址
eof
kubectl apply -f pv1.yaml
3.创建mysql的yaml文件:
cat << eof > mysql.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
namespace: mysql
spec:
accessModes: # 访问模式:读写权限,可以被多个节点挂载
- ReadWriteMany
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1 # 通过kubectl explain deployment命令查看版本
kind: Deployment # 资源类型
metadata:
name: mysql-deployment # 资源名称
namespace: mysql # 指定命名空间
spec:
replicas: 3 # 控制器下的pod数
selector:
matchLabels:
app: mysql # 这是控制器用来控制pod的标签
template:
metadata:
labels:
app: mysql # pod的标签
spec:
containers:
- name: mysql # 容器名
image: mysql:8.0.20 # 容器镜像
env: # mysql环境参数
- name: MYSQL_ROOT_PASSWORD # 指定ROOT用户密码
value: "你的密码"
ports: # 容器暴露的端口
- name: mysql
containerPort: 3306
volumeMounts: # 容器内的被挂载目录
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc
readOnly: false
---
apiVersion: v1
kind: Service
metadata:
name: mysql-nodeport
namespace: mysql
spec:
selector:
app: mysql # 服务通过此标签连接deployment资源来暴露端口
type: NodePort # 服务类型:NodePort
ports: # 不指定NodePort端口就会自动分配,(默认的取值范围是:30000-32767)
- protocol: TCP
port: 3306
targetPort: 3306
eof
4.创建mysq应用:
kubectl apply -f mysql.yaml
5.查看pod:
[root@master ~]# kubectl get deployment,pod,svc,pvc -n mysql -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/mysql-deployment 3/3 3 3 3m4s mysql mysql:8.0.20 app=mysql
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/mysql-deployment-574486b789-l9f4h 1/1 Running 0 3m4s 10.244.2.14 node2 <none> <none>
pod/mysql-deployment-574486b789-ng4x8 1/1 Running 0 3m4s 10.244.1.6 node1 <none> <none>
pod/mysql-deployment-574486b789-sqsll 1/1 Running 2 3m4s 10.244.2.13 node2 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/mysql-nodeport NodePort 10.105.41.7 <none> 3306:30165/TCP 3m4s app=mysql
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE VOLUMEMODE
persistentvolumeclaim/mysql-pvc Bound pv1 3Gi RWX 3m4s Filesystem
[root@master ~]# kubectl get pv -o wide
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
pv1 3Gi RWX Retain Bound mysql/mysql-pvc 4m Filesystem
6.远程连接:
[root@localhost ~]# mysql -h192.168.174.30 -P30165 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
到了这里,关于使用k8s部署Mysql实例~hostpath,nfs服务器,PV和PVC持久化的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!