[原创]学习Kubernetes笔记——部署数据库站点(MySql)
2022-12-31 14:56:54 Author: bbs.pediy.com(查看原文) 阅读量:16 收藏

[原创]学习Kubernetes笔记——部署数据库站点(MySql)

3小时前 155

[原创]学习Kubernetes笔记——部署数据库站点(MySql)

进入k8s集群主节点:10.10.30.201

实际生产环境中,为了稳定和高可用,一般不会把数据库部署在 Kubernetes 集群中,一般是用云厂商的数据库或者自己在高性能机器上搭建。

但是在这次实验环境中,将数据库部署到K8S集群中,可节约资源,也可做测试数据库,且有助于运维效率。

本次数据库搭建为MySQL数据库。

拉取1个镜像:mysql:8.0.30

准备YMAL文件

自定义MySQL配置文件,通过创建 configmap 并挂载到容器中

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

apiVersion: v1

kind: ConfigMap

metadata:

  name: mysql-config

data:

  mysql.cnf: |

    [mysqld]

    default_storage_engine=innodb

    skip_external_locking

    lower_case_table_names=1

    skip_host_cache

    skip_name_resolve

    max_connections=2000

    innodb_buffer_pool_size=8589934592

    init_connect='SET collation_connection = utf8_unicode_ci'

    init_connect='SET NAMES utf8'

    character-set-server=utf8

    collation-server=utf8_unicode_ci

    skip-character-set-client-handshake

    query_cache_type=0

    innodb_flush_log_at_trx_commit = 0

    sync_binlog = 0

    query_cache_size = 104857600

    slow_query_log =1

    slow_query_log_file=/var/lib/mysql/slow-query.log

    log-error=/var/lib/mysql/mysql.err

    long_query_time = 0.02

    table_open_cache_instances=16

    table_open_cache = 6000

    skip-grant-tables

    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION   

构建svc,以便集群内外均可访问数据库,其中集群外需通过 nodePort 设置的 30336 端口访问。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

apiVersion: v1

kind: Service

metadata:

  labels:

    app: mysql-svc

    release: mysql-svc

  name: mysql-svc

  namespace: default

spec:

  ports:

  - name: mysql

    port: 3306

    protocol: TCP

    nodePort: 30336

    targetPort: mysql

  selector:

    app: mysql

  sessionAffinity: None

  type: NodePort

构建部署文件,

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

apiVersion: apps/v1

kind: StatefulSet

metadata:

  generation: 1

  labels:

    app: mysql

    release: mysql

  name: mysql

  namespace: default

spec:

  replicas: 1

  selector:

    matchLabels:

      app: mysql 

  template:

    metadata:

      labels:

        app: mysql

    spec:

      containers:

        - name: mysql

          env:

            - name: MYSQL_ROOT_PASSWORD

              value: root

            - name: MYSQL_USER

              value: test

            - name: MYSQL_PASSWORD

              value: test

          image: 'mysql:8.0.30'

          imagePullPolicy: 

          livenessProbe:

            exec:

              command:

                - /bin/sh

                - "-c"

                - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"

                - mysql -h 127.0.0.1 -u root -e "SELECT 1"

            failureThreshold: 3

            initialDelaySeconds: 30

            periodSeconds: 10

            successThreshold: 1

            timeoutSeconds: 5

          readinessProbe:

            exec:

              command:

                - /bin/sh

                - "-c"

                - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"

                - mysql -h 127.0.0.1 -u root -e "SELECT 1"

            failureThreshold: 3

            initialDelaySeconds: 5

            periodSeconds: 10

            successThreshold: 1

            timeoutSeconds: 1

          ports:

            - containerPort: 3306

              name: mysql

              protocol: TCP

          resources:

            requests:

              cpu: 100m

              memory: 256Mi         

          volumeMounts:

            - mountPath: /var/lib/mysql

              name: data

            - name: mysql-config

              mountPath: /etc/mysql/mysql.conf.d

            - name: localtime

              readOnly: true

              mountPath: /etc/localtime     

      volumes:

        - name: data

          nfs:

            path: /k8s/sql/data

            server: nfs

        - name: mysql-config

          configMap:

            name: mysql-config

        - name: localtime

          hostPath:

            type: File

            path: /etc/localtime

执行

创建mysql.yaml将上面三个YAML文件结合在一起

1

kubectl apply -f mysql.yaml

输出

1

2

3

4

configmap/mysql-config create

service/mysql-svc create

statefulset.apps/mysql create

验证环境

1

2

3

4

mysql-0                                   1/1     Running   0             91m

mysql-svc       NodePort    10.103.149.178   <none>        3306:30336/TCP   125m

考虑到数据安全性,需要定期备份数据库,在K8S集群中,可配置CronJob实现自动备份作业。备份挂载到NFS服务器/k8s/sql/backup路径下,且以日期命名。每1小时的第1分钟备份一次,一天内的备份文件会重复覆盖。

准备YAML文件

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

apiVersion: batch/v1

kind: CronJob

metadata:

  name: mysql-backup

spec:

  schedule: "1 * * * *"

  jobTemplate:

    spec:

      template:

        spec:

          containers:

            - name: mysql-backup

              imagePullPolicy: IfNotPresent

              image: 'mysql:8.0.30'

              env:

                - name: MYSQL_ROOT_PASSWORD

                  value: root

                - name: MYSQL_USER

                  value: test

                - name: MYSQL_PASSWORD

                  value: test

              command:

                - /bin/sh

                - -c

                - |

                  set -ex

                  mysqldump --host=$MYSQL_HOST --user=$MYSQL_ROOT_PASSWORD \

                            --password=$mysql-root-password \

                            --routines --databases mall --single-transaction \

                            > /mysql-backup/mysql-`date +"%Y%m%d"`.sql

              volumeMounts:

                - name: mysql-backup

                  mountPath: /mysql-backup

          restartPolicy: OnFailure

          volumes:

            - name: mysql-backup

              nfs:

                path: /k8s/sql/backup

                server: nfs

执行

1

kubectl apply -f mysql.yaml

验证

进入NFS服务器10.10.30.211

查看挂载文件夹

[2022冬季班]《安卓高级研修班(网课)》月薪两万班招生中~


文章来源: https://bbs.pediy.com/thread-275688.htm
如有侵权请联系:admin#unsafe.sh