美文网首页
回调钩子 && 数据持久化

回调钩子 && 数据持久化

作者: 慕知 | 来源:发表于2021-04-08 22:10 被阅读0次

一,回调钩子



1、PostStart : 启动回调钩子,是在容器启动之后立即执行


2、PreStop : 结束回调钩子,是在容器结束之前立即执行



1,配置清单

[root@\ k8s-m-01~]# vim lifecycle.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: lifecycle
spec:
  selector:
    matchLabels:
      app: cycle
  template:
    metadata:
      labels:
        app: cycle
    spec:
      nodeName: k8s-n-01
      containers:
        - name: nginx
          image: nginx
          volumeMounts:
            - mountPath: /usr/share/nginx/html/
              name: lifecycle-data
          lifecycle:
            postStart:
              exec:
                command:
                  - "/bin/bash"
                  - "-c"
                  - "echo 'This is Lifecycle' > /usr/share/nginx/html/index.html"
            preStop:
              exec:
                command:
                  - "/bin/bash"
                  - "-c"
                  - "echo 'This is Lifecycle preStop' >> /usr/share/nginx/html/index.html"
      volumes:
        - name: lifecycle-data
          hostPath:
            path: /opt/discuz/data


2,启动回调钩子

[root@\ k8s-m-01~]# kubectl apply -f lifecycle.yaml 
deployment.apps/lifecycle created

#检测方式一:
[root@\ k8s-m-01~]# kubectl get pods
NAME                         READY   STATUS             RESTARTS   AGE
lifecycle-679bdc58c5-6mlgl   1/1     Running            0          7s
probe-exec                   0/1     CrashLoopBackOff   617        31h


[root@\ k8s-m-01~]# kubectl exec -it lifecycle-679bdc58c5-6mlgl -- bash
root@lifecycle-679bdc58c5-6mlgl:/usr/share/nginx/html# ls 
index.html
root@lifecycle-679bdc58c5-6mlgl:/usr/share/nginx/html# cat index.html 
This is Lifecycle



#检测方式二:
[root@\ k8s-m-01~]# curl 10.244.1.59
This is Lifecycle



#检测方式三:
[root@\ k8s-n-01~]# cd /opt/discuz/data/
[root@\ k8s-n-01/opt/discuz/data]# ll
total 4
-rw-r--r--. 1 root root 18 Apr  8 18:25 index.html
[root@\ k8s-n-01/opt/discuz/data]# cat index.html 
This is Lifecycle


3,结束回调钩子

[root@\ k8s-m-01~]# kubectl delete deployments.apps lifecycle 
deployment.apps "lifecycle" deleted


[root@\ k8s-n-01/opt/discuz/data]# cat index.html 
This is Lifecycle
This is Lifecycle preStop

二,数据持久化

类型

1、emptyDir 
    是pod调度到节点上时创建的一个空目录,当pod被删除时,emptyDir中的数据也随即被删除,emptyDir长用于容器间分享文件,或者用于创建临时目录。


2、hostPath
    hostPath类似于docker -v参数,将宿主主机中的文件挂载pod中,但是hostPath比docker -v参数更强大,(Pod调度到哪个节点,则直接挂载到当前节点上)




3、pv/PVC



1,emptyDir

- - -(不能够用来做数据持久化)

1) 配置文件

[root@\ k8s-m-01~]# vim empytdir.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  name: emptydir
spec:
  selector:
    matchLabels:
      app: emptydir
  template:
    metadata:
      labels:
        app: emptydir
    spec:
      containers:
        - name: nginx
          image: nginx
          volumeMounts:
            - mountPath: /usr/share/nginx/nginx
              name: test-emptydir
        - name: mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
          volumeMounts:
            - mountPath: /usr/share/nginx
              name: test-emptydir

      volumes:
        - name: test-emptydir
          emptyDir: {}


2) 测试

测试一:
[root@\ k8s-m-01~]# kubectl get pod -o wide
NAME                        READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
emptydir-5dc7dcd9fd-kgn9b   2/2     Running   0          14m   10.244.1.62   k8s-n-01   <none>           <none>



#进入容器,创建几个文件
[root@\ k8s-m-01~]# kubectl exec -it emptydir-5dc7dcd9fd-kgn9b -- bash
Defaulting container name to nginx.
Use 'kubectl describe pod/emptydir-5dc7dcd9fd-kgn9b -n default' to see all of the containers in this pod.
root@emptydir-5dc7dcd9fd-kgn9b:/# cd /usr/share/nginx/nginx/
root@emptydir-5dc7dcd9fd-kgn9b:/usr/share/nginx/nginx# ls
root@emptydir-5dc7dcd9fd-kgn9b:/usr/share/nginx/nginx# touch {1..5}.txt
root@emptydir-5dc7dcd9fd-kgn9b:/usr/share/nginx/nginx# ls
1.txt  2.txt  3.txt  4.txt  5.txt
root@emptydir-5dc7dcd9fd-kgn9b:/usr/share/nginx/nginx# exit
exit



# 在node01节点上查看挂载信息
[root@\ k8s-n-01~]# find /var/lib/kubelet/ -name "1.txt"
/var/lib/kubelet/pods/44080d58-4f86-4097-9c5f-26b496a01207/volumes/kubernetes.io~empty-dir/test-emptydir/1.txt



[root@\ k8s-n-01~]# cd /var/lib/kubelet/pods/44080d58-4f86-4097-9c5f-26b496a01207/volumes/kubernetes.io~empty-dir/test-emptydir
[root@\ k8s-n-01/var/lib/kubelet/pods/44080d58-4f86-4097-9c5f-26b496a01207/volumes/kubernetes.io~empty-dir/test-emptydir]# ls
1.txt  2.txt  3.txt  4.txt  5.txt






# 删除pod测试
[root@\ k8s-m-01~]# kubectl delete pod emptydir-5dc7dcd9fd-kgn9b 
pod "emptydir-5dc7dcd9fd-kgn9b" deleted



[root@\ k8s-n-01~]# cd -
-bash: cd: /var/lib/kubelet/pods/44080d58-4f86-4097-9c5f-26b496a01207/volumes/kubernetes.io~empty-dir/test-emptydir: No such file or directory

已经没有该文件










=====================================



测试二:
[root@\ k8s-m-01~]# kubectl get pods -o wide
NAME                        READY   STATUS    RESTARTS   AGE    IP            NODE       NOMINATED NODE   READINESS GATES
emptydir-5dc7dcd9fd-sm2gt   2/2     Running   0          5m8s   10.244.2.30   k8s-n-02   <none>           <none>



# 先进入mysql容器,创建文件(mysql容器本身是没有/usr/share/nginx/目录)

[root@\ k8s-m-01~]# kubectl exec -it emptydir-5dc7dcd9fd-sm2gt  -c mysql -- bash
root@emptydir-5dc7dcd9fd-sm2gt:/# cd /usr/share/nginx/
root@emptydir-5dc7dcd9fd-sm2gt:/usr/share/nginx# ls

root@emptydir-5dc7dcd9fd-sm2gt:/usr/share/nginx# touch {a..e}
root@emptydir-5dc7dcd9fd-sm2gt:/usr/share/nginx# ls
a  b  c  d  e
root@emptydir-5dc7dcd9fd-sm2gt:/usr/share/nginx# exit
exit





# 再进入nginx容器查看
[root@\ k8s-m-01~]# kubectl exec -it emptydir-5dc7dcd9fd-sm2gt  -c nginx -- bash
root@emptydir-5dc7dcd9fd-sm2gt:/# cd /usr/share/nginx/nginx/
root@emptydir-5dc7dcd9fd-sm2gt:/usr/share/nginx/nginx# ls
a  b  c  d  e


# 容器之间互通,文件共享

2, hostPath

image.png
hostPath类似于docker -v参数,将宿主主机中的文件挂载pod中,
hostPath比docker -v参数更强大,(Pod调度到哪个节点,则直接挂载到当前节点上)

1) 配置清单



# 查看详情
[root@\ k8s-m-01~]# kubectl explain deployment.spec.template.spec.volumes


[root@\ k8s-m-01~]# vim hostpath.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: hostpath
spec:
  selector:
    matchLabels:
      app: hostpath
  template:
    metadata:
      labels:
        app: hostpath
    spec:
      containers:
        - name: nginx
          image: nginx
          volumeMounts:
            - mountPath: /usr/share/nginx/nginx
              name: hostpath
        - name: mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
          volumeMounts:
            - mountPath: /usr/share/nginx
              name: hostpath

      volumes:
        - name: hostpath
          hostPath:
            path: /opt/abc
            type: DirectoryOrCreate

2) 测试

[root@\ k8s-m-01~]# kubectl get pod -o wide
NAME                       READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
hostpath-98fd86bb4-4bk98   2/2     Running   0          10s   10.244.1.66   k8s-n-01   <none>           <none>

[root@\ k8s-m-01~]# kubectl exec -it hostpath-98fd86bb4-4bk98  -- bash
root@hostpath-98fd86bb4-4bk98:/usr/share/nginx/html# cd /usr/share/nginx/nginx/
root@hostpath-98fd86bb4-4bk98:/usr/share/nginx/nginx# ls
root@hostpath-98fd86bb4-4bk98:/usr/share/nginx/nginx# touch {2..6}.txt




#到node01 节点查看
[root@\ k8s-n-01~]# cd /opt/abc/
[root@\ k8s-n-01/opt/abc]# ll
total 0
-rw-r--r--. 1 root root 0 Apr  8 19:38 2.txt
-rw-r--r--. 1 root root 0 Apr  8 19:38 3.txt
-rw-r--r--. 1 root root 0 Apr  8 19:38 4.txt
-rw-r--r--. 1 root root 0 Apr  8 19:38 5.txt
-rw-r--r--. 1 root root 0 Apr  8 19:38 6.txt



注;
abc文件夹是本身不存在的

3, PV,PVC

1) nfs准备

[root@\ k8s-m-01~]# mkdir -p /nfs/v{1..10}
[root@\ k8s-m-01~]# cat > /etc/exports <<EOF
/nfs/v1  172.16.0.0/16(rw,no_root_squash)
/nfs/v2  172.16.0.0/16(rw,no_root_squash)
/nfs/v3  172.16.0.0/16(rw,no_root_squash)
/nfs/v4  172.16.0.0/16(rw,no_root_squash)
/nfs/v5  172.16.0.0/16(rw,no_root_squash)
/nfs/v6  172.16.0.0/16(rw,no_root_squash)
/nfs/v7  172.16.0.0/16(rw,no_root_squash)
/nfs/v8  172.16.0.0/16(rw,no_root_squash)
/nfs/v9  172.16.0.0/16(rw,no_root_squash)
/nfs/v10 172.16.0.0/16(rw,no_root_squash)


[root@\ k8s-m-01/nfs]# systemctl enable nfs --now


[root@\ k8s-m-01~]# exportfs -arv
exporting 172.16.0.0/16:/nfs/v10
exporting 172.16.0.0/16:/nfs/v9
exporting 172.16.0.0/16:/nfs/v8
exporting 172.16.0.0/16:/nfs/v7
exporting 172.16.0.0/16:/nfs/v6
exporting 172.16.0.0/16:/nfs/v5
exporting 172.16.0.0/16:/nfs/v4
exporting 172.16.0.0/16:/nfs/v3
exporting 172.16.0.0/16:/nfs/v2
exporting 172.16.0.0/16:/nfs/v1



[root@\ k8s-m-01/nfs]# showmount -e
Export list for k8s-m-01:
/nfs/v10 172.16.0.0/16
/nfs/v9  172.16.0.0/16
/nfs/v8  172.16.0.0/16
/nfs/v7  172.16.0.0/16
/nfs/v6  172.16.0.0/16
/nfs/v5  172.16.0.0/16
/nfs/v4  172.16.0.0/16
/nfs/v3  172.16.0.0/16
/nfs/v2  172.16.0.0/16


2) 配置清单

[root@\ k8s-m-01~]# vim nfs.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs
spec:
  selector:
    matchLabels:
      app: nfs

  template:
    metadata:
      labels:
        app: nfs
    spec:
      nodeName: k8s-n-02
      containers:
        - name: mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: nfs
      volumes:
        - name: nfs
          nfs:
            path: /nfs/v1
            server: 172.16.1.31

3) 测试

[root@\ k8s-m-01~]# kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
nfs-6f9f56fb7d-w5r6x   1/1     Running   0          40m

# 进入容器,创建数据库
[root@\ k8s-m-01~]# kubectl exec -it nfs-6f9f56fb7d-w5r6x  -- bash
root@nfs-6f9f56fb7d-w5r6x:/# mysql -u root -p123456
mysql> create database aaa;
Query OK, 1 row affected (0.00 sec)

mysql> exit
Bye





# 到挂载点查看
[root@\ k8s-m-01/nfs/v1]# ll
total 188484
drwxr-x---. 2 polkitd input       20 Apr  8 20:51 aaa
-rw-r-----. 1 polkitd input       56 Apr  8 20:39 auto.cnf
-rw-------. 1 polkitd input     1676 Apr  8 20:39 ca-key.pem
-rw-r--r--. 1 polkitd input     1112 Apr  8 20:39 ca.pem
-rw-r--r--. 1 polkitd input     1112 Apr  8 20:39 client-cert.pem
-rw-------. 1 polkitd input     1680 Apr  8 20:39 client-key.pem
... ...





# 这时删除pod(系统会再次生成一个新pod);会发现数据库依然存在

[root@\ k8s-m-01~]# kubectl delete pod nfs-6f9f56fb7d-w5r6x 
pod "nfs-6f9f56fb7d-w5r6x" deleted
[root@\ k8s-m-01~]# kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
nfs-6f9f56fb7d-849d6   1/1     Running   0          16s

[root@\ k8s-m-01~]# kubectl exec -it nfs-6f9f56fb7d-849d6 -- bash
root@nfs-6f9f56fb7d-849d6:/# mysql -u root -p123456
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aaa                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.02 sec)


4) 使用pv,pvc管理nfs

# 创建pv (pv是集群级资源)

[root@\ k8s-m-01~]# vim pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
 name: pv1
spec:
 nfs:
   path: /nfs/v2
   server: 172.16.1.31
 capacity:
     storage: 20Gi
 persistentVolumeReclaimPolicy: Retain
 accessModes:
   - "ReadWriteMany"
   - "ReadWriteOnce"



# 查看pv信息
[root@\ k8s-m-01~]# kubectl apply -f pv.yaml 
persistentvolume/pv1 created
[root@\ k8s-m-01~]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv1    20Gi       RWO,RWX        Retain           Available                                   9s



# 使用pv 挂载到存储卷(即pvc,指定使用的pv)  (pvc是名称空间级资源)
[root@\ k8s-m-01~]# vim pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: pvc1
spec:
 accessModes:
   - "ReadWriteMany"
 resources:
   requests:
     storage: "6Gi"


[root@\ k8s-m-01~]# kubectl apply -f pvc.yaml 
persistentvolumeclaim/pvc1 created
[root@\ k8s-m-01~]# kubectl get pvc
NAME   STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc1   Bound    pv1      20Gi       RWO,RWX                       8s


# pv的状态变成bound状态
[root@\ k8s-m-01~]# kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM          STORAGECLASS   REASON   AGE
pv1    20Gi       RWO,RWX        Retain           Bound    default/pvc1                           12m






--------------------------          --------------------------          --------------------------
[root@\ k8s-m-01~]# vim pv-pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: pv-pvc
spec:
 selector:
   matchLabels:
     app: pv-pvc
 template:
   metadata:
     labels:
       app: pv-pvc
   spec:
     containers:
       - name: nginx
         image: nginx
         volumeMounts:
           - mountPath: /usr/share/nginx/html
             name: pv-pvc
     volumes:
       - name: pv-pvc
         persistentVolumeClaim:
           claimName: pvc1












# 查看已经挂载到nfs/v2里

[root@\ k8s-m-01~]# cd /nfs/v2/
[root@\ k8s-m-01/nfs/v2]# ll
total 267332
-rw-r-----. 1 polkitd input       56 Apr  9 16:16 auto.cnf
-rw-------. 1 polkitd input     1680 Apr  9 16:16 ca-key.pem
-rw-r--r--. 1 polkitd input     1112 Apr  9 16:16 ca.pem
-rw-r--r--. 1 polkitd input     1112 Apr  9 16:16 client-cert.pem
-rw-------. 1 polkitd input     1676 Apr  9 16:16 client-key.pem
-rw-r-----. 1 polkitd input     1359 Apr  9 16:16 ib_buffer_pool
-rw-r-----. 1 polkitd input 79691776 Apr  9 16:16 ibdata1
-rw-r-----. 1 polkitd input 50331648 Apr  9 16:16 ib_logfile0
-rw-r-----. 1 polkitd input 50331648 Apr  9 16:16 ib_logfile1
-rw-r-----. 1 polkitd input 12582912 Apr  9 16:16 ibtmp1
drwxr-x---. 2 polkitd input     4096 Apr  9 16:16 mysql
drwxr-x---. 2 polkitd input     8192 Apr  9 16:16 performance_schema
-rw-------. 1 polkitd input     1676 Apr  9 16:16 private_key.pem
-rw-r--r--. 1 polkitd input      452 Apr  9 16:16 public_key.pem
-rw-r--r--. 1 polkitd input     1112 Apr  9 16:16 server-cert.pem
-rw-------. 1 polkitd input     1680 Apr  9 16:16 server-key.pem
drwxr-x---. 2 polkitd input     8192 Apr  9 16:16 sys


[root@\ k8s-m-01~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
nfs-6f9f56fb7d-849d6     1/1     Running   0          19h   10.244.2.32   k8s-n-02   <none>           <none>
pv-pvc-b847d65d7-b7lp4   1/1     Running   0          12s   10.244.1.73   k8s-n-01   <none>           <none>

# 访问,没有数据
[root@\ k8s-m-01~]# curl 10.244.1.73
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.19.9</center>
</body>
</html>

# 到挂载目录下,创建index.html
[root@\ k8s-m-01/nfs/v2]# echo "hello,nginx" > index.html
[root@\ k8s-m-01/nfs/v2]# 


# 再次访问
[root@\ k8s-m-01~]# curl 10.244.1.73
hello,nginx




# 弹性扩容
[root@\ k8s-m-01~]# kubectl edit deployments.apps pv-pvc 
replicas:3



# 都可以访问
[root@\ k8s-m-01~]# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE   READINESS GATES
pv-pvc-b847d65d7-b7lp4   1/1     Running   0          4m36s   10.244.1.73   k8s-n-01   <none>           <none>
pv-pvc-b847d65d7-hxl8b   1/1     Running   0          19s     10.244.1.75   k8s-n-01   <none>           <none>
pv-pvc-b847d65d7-qzskv   1/1     Running   0          19s     10.244.1.74   k8s-n-01   <none>           <none>

[root@\ k8s-m-01~]# curl 10.244.1.75
hello,nginx
[root@\ k8s-m-01~]# curl 10.244.1.74
hello,nginx

image.png image.png image.png

相关文章

  • 回调钩子 && 数据持久化

    一,回调钩子 1,配置清单 2,启动回调钩子 3,结束回调钩子 二,数据持久化 类型 1,emptyDir - -...

  • Docker学习(13) 卷与持久化数据

    Docker学习(13) 卷与持久化数据 卷与持久化数据——简介 数据主要分为两种:持久化和非持久化。 持久化:就...

  • iOS本地数据持久化

    iOS本地数据持久化 iOS本地数据持久化

  • Java回调

    回调callback 回调又称钩子函数 (hook), 模板方法, 利用对象的多态特性, 先定义函数结构, 再利用...

  • Redis-2 数据持久化及持久化配置

    一、数据持久化 开启持久化功能后,重启redis,数据会自动通过持久化文件恢复!! 1、redis持久化 – 两种...

  • 面试相关

    数据持久化 什么是持久化狭义的理解: “持久化”仅仅指把域对象永久保存到数据库中;广义的理解,“持久化”包括和数据...

  • GeekBand iOS开发高级进阶学习笔记(第四周)

    简易数据存储 数据持久化分为本体持久化和云端持久化本体持久化可以存在本地文件或数据库。云端可以存在iCloud,存...

  • iOS数据持久化

    Title: iOS数据持久化 ##数据持久化概念 数据持久化就是将内存中的数据模型转换为存储模型,以及将存储模型...

  • Spring之jdbc Template实现CRUD操作

    Spring为各种持久化技术都提供了简单操作的模板回调。比如jdbc、hibernate、Mybatis以及JPA...

  • 【redis】redis 主从哨兵架构重启

    一、背景 master、slave数据复制同步,如果master持久化数据小于slave,slave回滚到跟mas...

网友评论

      本文标题:回调钩子 && 数据持久化

      本文链接:https://www.haomeiwen.com/subject/wqmvkltx.html