一、NFS部署
# apt-get install nfs-kernel-server nfs-common
root@k8s-master:~/mysql# cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
# to NFS clients. See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)
#
/home/pvdata/mysql_s *(rw,sync,insecure,no_subtree_check,no_root_squash)
/data/nfs/mysql *(rw,sync,insecure,no_subtree_check,no_root_squash)
# systemctl restart nfs-server
二、k8s配置
创建命名空间
# kubectl create ns database
1、Mysql PV/PVC 创建
Persistent Volume 和 Persistent Volume Claim,主要是管理服务持久化的存储资源 Volume。这里我们先创建 PV,再创建 PVC,将两者绑定。
首先是 mysql-pv.yaml
root@k8s-master:~/mysql# cat mysql-pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: mysql-pv
namespace: database
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /data/nfs/mysql
server: 10.10.100.31
2、接下来是 mysql-pvc.yaml
这里其他配置与 PV 保持一致,同一命名空间 database 下,同一个 storageClassName
执行
root@k8s-master:~/mysql# cat mysql-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
namespace: database
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs
查看PV状态
kubectl get pv -n database
查看PVC状态
kubectl get pvc -n database
3、Mysql Deploy 创建
这里是我们服务的本体,关于 Deploy 就不多说了
这里我们创建 mysql-deploy.yaml
root@k8s-master:~/mysql# cat mysql-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: database
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
这里我们配置的是 mysql 服务相关信息,包括镜像的拉取,内部存储的映射。没有什么大问题等待服务部署成功就好了。
4、Mysql Service 创建
Deploy 部署我们服务的本体,接下来创建访问服务的通道。
创建 mysql-service.yaml
root@k8s-master:~/mysql# cat mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: database
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
nodePort: 31111
selector:
app: mysql
selector:
app: mysql
这里我选择 NodePort 类型,是因为用 31111 端口将服务暴露出来,不过用于我们同一个命名空间下的服务,可以直接通过服务名字进行访问。
root@k8s-master:~/mysql# mysql -h10.10.100.31 -uroot -ppassword -P31111
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
码云地址
https://gitee.com/ethnicity_admin/k8s-deepwise.git