欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

10.k8s持久化存储

程序员文章站 2024-01-15 20:00:40
...

6:持久化存储

6.1 emptyDir:

1.前期准备工作:
登录我们的web网站,创建若干条数据;然后进到容器的数据库中查询
[root@k8s-master tomcat_demo]# kubectl exec -n tomcat -it mysql-kjxqp /bin/bash

[root@k8s-master tomcat_demo]# cat mysql-rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: tomcat
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      volumes:
      - name: mysql
        emptyDir: {}
      containers:
        - name: mysql
          image: 10.0.0.11:5000/mysql:5.7
          volumeMounts:
          - name: mysql
            mountPath: /var/lib/mysql
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: '123456'

数据的存放位置
[root@k8s-node-1 ~]# cd /var/lib/kubelet/pods/a5301810-1c0b-11ea-8a15-000c29d8ae3b/volumes/kubernetes.io~empty-dir/mysql

然后登录网站进行测试---》可以看出我们的数据是保留的,但是又一个弊端;当我们的Pod发生故障时,数据也会跟着丢失

6.2 HostPath:

[root@k8s-master tomcat_demo]# cat mysql-rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: tomcat
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      nodeName: 10.0.0.13
      volumes:
      - name: mysql
        hostPath:
          path: /data/mysql
      containers:
        - name: mysql
          image: 10.0.0.11:5000/mysql:5.7
          volumeMounts:
          - name: mysql
            mountPath: /var/lib/mysql
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: '123456'

6.3 nfs:

	 volumes:
      - name: mysql
        nfs:
          path: /data/wp_mysql
          server: 10.0.0.11	
 
1.所有节点安装nfs-utils
yum install nfs-utils -y
mkdir /data
[root@k8s-master ~]# cat /etc/exports
/data 10.0.0.0/24(rw,async,no_root_squash,no_all_squash)
systemctl start rpcbind nfs-server

1.wordpress项目数据库持久化:
1.配置mysql-rc
[root@k8s-master wordpress]# cat mysql-rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: wordpress
  name: mysql-wp
spec:
  replicas: 1
  selector:
    app: mysql-wp
  template:
    metadata:
      labels:
        app: mysql-wp
    spec:
      volumes:
      - name: mysql-wp
        nfs:
          path: /data/blogsql-db
          server: 10.0.0.11
      containers:
        - name: mysql
          image: 10.0.0.11:5000/mysql:5.7
          volumeMounts:
          - name: mysql-wp
            mountPath: /var/lib/mysql
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: 'somewordpress'
          - name: MYSQL_DATABASE
            value: 'wordpress'
          - name: MYSQL_USER
            value: 'wordpress'
          - name: MYSQL_PASSWORD
            value: 'wordpress'

2.配置wordpress-rc
[root@k8s-master wordpress]# cat wordpress-rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: wordpress
  name: wordpress
spec:
  replicas: 2
  selector:
    app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      volumes:
      - name: wordpress
        nfs:
          path: /data/wordpress-db
          server: 10.0.0.11		#apiserver的ip地址
      containers:
        - name: wordpress
          image: 10.0.0.11:5000/wordpress:latest
          volumeMounts:
          - name: wordpress
            mountPath: /var/www/html
          ports:
          - containerPort: 80
          env:
          - name: WORDPRESS_DB_HOST
            value: 'mysql-wp'
          - name: WORDPRESS_DB_USER
            value: 'wordpress'
          - name: WORDPRESS_DB_PASSWORD
            value: 'wordpress'
3.创建对应的目录
mkdir -p /data/blogsql-db
mkdir -p /data/wordpress-db

4.测试数据是否持久化
kubectl delete pod --all -n wordpress
登录web界面测试---》测试数据是否存在
[root@k8s-node-1 ~]# df -Th |grep -i nfs
10.0.0.11:/data/blogsql-db   nfs4       48G  3.9G   45G   8% /var/lib/kubelet/pods/a9768011-1c80-11ea-a2dc-000c29d8ae3b/volumes/kubernetes.io~nfs/mysql-wp
10.0.0.11:/data/wordpress-db nfs4       48G  3.9G   45G   8% /var/lib/kubelet/pods/a98fa0ab-1c80-11ea-a2dc-000c29d8ae3b/volumes/kubernetes.io~nfs/wordpress

6.4 pvc:

pv: persistent volume 全局资源,k8s集群
pvc: persistent volume claim, 局部资源属于某一个namespace

6.4.1:安装nfs服务端(10.0.0.11)

yum install nfs-utils.x86_64 -y
mkdir /data
vim /etc/exports
/data  10.0.0.0/24(rw,async,no_root_squash,no_all_squash)
systemctl start rpcbind
systemctl start nfs

6.4.2:在node节点安装nfs客户端

yum install nfs-utils.x86_64 -y
showmount -e 10.0.0.11

6.4.3:创建pv和pvc

上传yaml配置文件,创建pv和pvc

6.4.4:创建mysql-rc,pod模板里使用volume

      volumes:
      - name: mysql
        persistentVolumeClaim:
          claimName: tomcat-mysql

6.4.5: 验证持久化

验证方法1:删除mysql的pod,数据库不丢

kubectl delete pod mysql-gt054

验证方法2:查看nfs服务端,是否有mysql的数据文件

6.5: 分布式存储glusterfs

a: 什么是glusterfs
Glusterfs是一个开源分布式文件系统,具有强大的横向扩展能力,可支持数PB存储容量和数千客户端,通过网络互联成一个并行的网络文件系统。具有可扩展性、高性能、高可用性等特点。

b: 安装glusterfs

所有节点安装glusterfs:
yum install  centos-release-gluster -y
yum install  install glusterfs-server -y
systemctl start glusterd.service
systemctl enable glusterd.service

#为gluster集群增加存储单元brick
mkdir -p /gfs/test1
mkdir -p /gfs/test2
mkdir -p /gfs/test3

各个节点增加3块为10G硬盘【3个节点都要操作】
echo '- - -' >/sys/class/scsi_host/host0/scan 
echo '- - -' >/sys/class/scsi_host/host1/scan 
echo '- - -' >/sys/class/scsi_host/host2/scan 
mkfs.xfs /dev/sdb 
mkfs.xfs /dev/sdc 
mkfs.xfs /dev/sdd
mount /dev/sdb /gfs/test1
mount /dev/sdc /gfs/test2
mount /dev/sdd /gfs/test3

c: 添加存储资源池

master节点:
gluster pool list
gluster peer probe k8s-node1
gluster peer probe k8s-node2
gluster pool list

d: glusterfs卷管理

1.任何节点都可以创建
gluster volume create cheng k8s-master:/gfs/test1 k8s-node-1:/gfs/test1 k8s-node-2:/gfs/test1 force

2.启动卷并查看卷信息
[root@k8s-master ~]# gluster volume info cheng 
Volume Name: cheng
Type: Distribute	分布式卷
。。。。。
gluster volume start cheng
gluster volume info cheng

3.挂载并测试
mount -t glusterfs 127.0.0.1:/cheng /mnt/
df -h	查看挂载信息
cp /data/wordpress-db/*.php /mnt/
每个节点分布了一些数据;痛点是如果又一个盘故障了,会导致数据丢失

4.增加存储单元【分布式复制卷】
gluster volume add-brick cheng replica 2 k8s-master:/gfs/test2 k8s-node-1:/gfs/test2 k8s-node-2:/
gfs/test2 force
- replica 副本数
===============================================================
#创建分布式复制卷
gluster volume create qiangge replica 2 k8s-master:/gfs/test1 k8s-node-1:/gfs/test1 k8s-master:/gfs/test2  k8s-node-1:/gfs/test2 force
#启动卷
gluster volume start qiangge
#查看卷
gluster volume info qiangge 
#挂载卷
mount -t glusterfs 10.0.0.11:/qiangge /mnt

f: 分布式复制卷扩容

扩容前查看容量:
df   -h

扩容命令:
gluster volume add-brick cheng k8s-master:/gfs/test3 k8s-node-1:/gfs/test3 force

扩容后查看容量:
df   -h

让数据均衡分布【建议在晚上做】
gluster volume rebalance cheng start
这时候分布式卷的内容一致,同时也不需要再次挂载

6.6 k8s 对接glusterfs存储

a:创建endpoint

vi  glusterfs-ep.yaml
iapiVersion: v1
kind: Endpoints
metadata:
  name: glusterfs
  namespace: tomcat
subsets:
- addresses:
  - ip: 10.0.0.11
  - ip: 10.0.0.12
  - ip: 10.0.0.13
  ports:
  - port: 49152
    protocol: TCP

b: 创建service

vi  glusterfs-svc.yaml
iapiVersion: v1
kind: Service
metadata:
  name: glusterfs
  namespace: tomcat
spec:
  ports:
  - port: 49152
    protocol: TCP
    targetPort: 49152
  sessionAffinity: None
  type: ClusterIP

===================================================================================
tomcat对接glusterfs 
===================================================================================
[root@k8s-master tomcat_demo]# cat mysql-rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: tomcat
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      nodeName: 10.0.0.13
      volumes:
      - name: mysql
        glusterfs:
          path: cheng
          endpoints: glusterfs
      containers:
        - name: mysql
          image: 10.0.0.11:5000/mysql:5.7
          volumeMounts:
          - name: mysql
            mountPath: /var/lib/mysql
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: '123456'
  

wordpress项目实战;独立数据库服务至宿主机–》k8s 映射

把外部的服务,通过创建service和endpoint,把它映射到k8s内部来使用

提前在宿主机安装mysql服务
[root@k8s-node-2 ~]# yum install mariadb-server mariadb -y
[root@k8s-node-2 ~]# systemctl start mariadb
[root@k8s-node-2 ~]# mysql_secure_installation
0.创建wordpress库,并授权
MariaDB [(none)]> grant all on wordpress.* to aaa@qq.com'%' identified by 'wordpress';
MariaDB [(none)]> create database wordpress;

1.编写mysql-ep配置文件
[aaa@qq.com wordpress]# cat mysql-ep.yaml 
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql-wp
  namespace: wordpress
subsets:
- addresses:
  - ip: 10.0.0.13
  ports:
  - port: 3306
    protocol: TCP

2.配置mysql-svc文件
[aaa@qq.com wordpress]# cat mysql-svc.yml 
apiVersion: v1
kind: Service
metadata:
  namespace: wordpress
  name: mysql-wp
spec:
  ports:
    - port: 3306
      targetPort: 3306

3.编写wordpress-rc配置文件
[aaa@qq.com wordpress]# cat wordpress-rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: wordpress
  name: wordpress
spec:
  replicas: 2
  selector:
    app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      volumes:
      - name: wordpress
        nfs:
          path: /data/wordpress-db
          server: 10.0.0.11
      containers:
        - name: wordpress
          image: 10.0.0.11:5000/wordpress:latest
          volumeMounts:
          - name: wordpress
            mountPath: /var/www/html
          ports:
          - containerPort: 80
          env:
          - name: WORDPRESS_DB_HOST
            value: 'mysql-wp'
          - name: WORDPRESS_DB_USER
            value: 'wordpress'
          - name: WORDPRESS_DB_PASSWORD
            value: 'wordpress'

4.编写wordpress-svc配置文件
[root@k8s-master wordpress]# cat wordpress-svc.yml 
apiVersion: v1
kind: Service
metadata:
  namespace: wordpress
  name: wordpress
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30009
  selector:
    app: wordpress

c: 创建gluster类型pv(全称“持久化卷”)

全局资源:所有namespace都能看见的资源 node pv namespace
局部资源:只属于某一个具体namespace下的资源, pod rc deploy rs svc daemonset

1.编写mysql_pv配置文件
[root@k8s-master pv_pvc]# cat mysql_pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1
  labels:
    type: nfs   #pv的标签
spec:
  capacity:     #提供的存储能力10G,作为筛选pv的标签
    storage: 10Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle        #pv的回收策略
  nfs:  #后端存储
    path: "/data/pv1"
    server: 10.0.0.11
    readOnly: false
[root@k8s-master pv_pvc]# kubectl create -f mysql_pv.yaml 
persistentvolume "pv1" created
[root@k8s-master pv_pvc]# kubectl get pv
==============================================================
2.编写pv2配置文件
[root@k8s-master pv_pvc]# cat mysql_pv2.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv2
  labels:
    type: nfs   #pv的标签
spec:
  capacity:     #提供的存储能力10G,作为筛选pv的标签
    storage: 30Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle        #pv的回收策略
  nfs:  #后端存储
    path: "/data/pv2"
    server: 10.0.0.11
    readOnly: false

[root@k8s-master pv_pvc]# kubectl create -f mysql_pv2.yaml 
persistentvolume "pv2" created
[root@k8s-master pv_pvc]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS      CLAIM     REASON    AGE
pv1       10Gi       RWX           Recycle         Available                       2m
pv2       30Gi       RWX           Recycle         Available                       4s

d: 创建pvc

1.创建mysql_pvc
[root@k8s-master pv_pvc]# cat mysql_pvc.yaml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc1
  namespace: tomcat
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 30Gi
      
[root@k8s-master pv_pvc]# kubectl create -f mysql_pvc.yaml 
persistentvolumeclaim "pvc1" created
[root@k8s-master pv_pvc]# kubectl get pvc -n tomcat 
NAME      STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
pvc1      Bound     pv2       30Gi       RWX           19s

2:在pod中使用pvc
[root@k8s-master tomcat_demo]# cat mysql-rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: tomcat
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      nodeName: 10.0.0.13
      volumes:
      - name: mysql
        persistentVolumeClaim:  #pvc
          claimName: pvc1   #pvc的名字
      containers:
        - name: mysql
          image: 10.0.0.11:5000/mysql:5.7
          volumeMounts:
          - name: mysql
            mountPath: /var/lib/mysql
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: '123456'

kubectl create -f .

3.测试
[root@k8s-master tomcat_demo]# ll /data/pv2/
total 237668
-rw-r----- 1 polkitd input       56 Dec 12 19:26 auto.cnf
drwxr-x--- 2 polkitd input       58 Dec 12 19:27 HPE_APP
-rw-r----- 1 polkitd input     1329 Dec 12 19:26 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 Dec 12 19:27 ibdata1
-rw-r----- 1 polkitd input 50331648 Dec 12 19:27 ib_logfile0
-rw-r----- 1 polkitd input 50331648 Dec 12 19:26 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Dec 12 19:26 ibtmp1
drwxr-x--- 2 polkitd input     4096 Dec 12 19:26 mysql
drwxr-x--- 2 polkitd input     8192 Dec 12 19:26 performance_schema
drwxr-x--- 2 polkitd input     8192 Dec 12 19:26 sys

10.k8s持久化存储

相关标签: K8S