github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/internal/testutil/testdata/dataset3/mysql/mysql-statefulset.resource.yaml (about)

     1  # Copyright 2019 The kpt Authors
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  apiVersion: apps/v1
    16  kind: StatefulSet
    17  metadata:
    18    name: mysql
    19  spec:
    20    replicas: 3 # {"$ref": "#/definitions/io.k8s.cli.setters.replicas"}
    21    selector:
    22      matchLabels:
    23        app: mysql
    24    template:
    25      metadata:
    26        labels:
    27          app: mysql
    28      spec:
    29        initContainers:
    30          - name: init-mysql
    31            image: mysql:5.7
    32            command:
    33              - bash
    34              - -c
    35              - |
    36                set -ex
    37                # Generate mysql server-id from pod ordinal index.
    38                [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
    39                ordinal=${BASH_REMATCH[1]}
    40                echo [mysqld] > /mnt/conf.d/server-id.cnf
    41                # Add an offset to avoid reserved server-id=0 value.
    42                echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
    43                # Copy appropriate conf.d files from config-map to emptyDir.
    44                if [[ $ordinal -eq 0 ]]; then
    45                  cp /mnt/config-map/master.cnf /mnt/conf.d/
    46                else
    47                  cp /mnt/config-map/slave.cnf /mnt/conf.d/
    48                fi
    49            volumeMounts:
    50              - name: conf
    51                mountPath: /mnt/conf.d
    52              - name: config-map
    53                mountPath: /mnt/config-map
    54          - name: clone-mysql
    55            image: gcr.io/google-samples/xtrabackup:1.0
    56            command:
    57              - bash
    58              - -c
    59              - |
    60                set -ex
    61                # Skip the clone if data already exists.
    62                [[ -d /var/lib/mysql/mysql ]] && exit 0
    63                # Skip the clone on master (ordinal index 0).
    64                [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
    65                ordinal=${BASH_REMATCH[1]}
    66                [[ $ordinal -eq 0 ]] && exit 0
    67                # Clone data from previous peer.
    68                ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
    69                # Prepare the backup.
    70                xtrabackup --prepare --target-dir=/var/lib/mysql
    71            volumeMounts:
    72              - name: data
    73                mountPath: /var/lib/mysql
    74                subPath: mysql
    75              - name: conf
    76                mountPath: /etc/mysql/conf.d
    77        containers:
    78          - name: mysql
    79            image: mysql:8.0
    80            ports:
    81              - name: mysql
    82                containerPort: 3306
    83            env:
    84              - name: MYSQL_ALLOW_EMPTY_PASSWORD
    85                value: "1"
    86            resources:
    87              requests:
    88                cpu: 500m
    89                memory: 1Gi
    90            volumeMounts:
    91              - name: data
    92                mountPath: /var/lib/mysql
    93                subPath: mysql
    94              - name: conf
    95                mountPath: /etc/mysql/conf.d
    96            livenessProbe:
    97              exec:
    98                command:
    99                  - mysqladmin
   100                  - ping
   101              initialDelaySeconds: 45
   102              periodSeconds: 15
   103              timeoutSeconds: 5
   104            readinessProbe:
   105              exec:
   106                command:
   107                  - mysql
   108                  - -h
   109                  - 127.0.0.1
   110                  - -e
   111                  - SELECT 1
   112              initialDelaySeconds: 5
   113              periodSeconds: 2
   114              timeoutSeconds: 1
   115          - name: xtrabackup
   116            image: gcr.io/google-samples/xtrabackup:1.0
   117            command:
   118              - bash
   119              - -c
   120              - |
   121                set -ex
   122                cd /var/lib/mysql
   123                # Determine binlog position of cloned data, if any.
   124                if [[ -f xtrabackup_slave_info ]]; then
   125                  # XtraBackup already generated a partial "CHANGE MASTER TO" query
   126                  # because we're cloning from an existing slave.
   127                  mv xtrabackup_slave_info change_master_to.sql.in
   128                  # Ignore xtrabackup_binlog_info in this case (it's useless).
   129                  rm -f xtrabackup_binlog_info
   130                elif [[ -f xtrabackup_binlog_info ]]; then
   131                  # We're cloning directly from master. Parse binlog position.
   132                  [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
   133                  rm xtrabackup_binlog_info
   134                  echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
   135                        MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
   136                fi
   137                # Check if we need to complete a clone by starting replication.
   138                if [[ -f change_master_to.sql.in ]]; then
   139                  echo "Waiting for mysqld to be ready (accepting connections)"
   140                  until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
   141                  echo "Initializing replication from clone position"
   142                  # In case of container restart, attempt this at-most-once.
   143                  mv change_master_to.sql.in change_master_to.sql.orig
   144                  mysql -h 127.0.0.1 <<EOF
   145                $(<change_master_to.sql.orig),
   146                  MASTER_HOST='mysql-0.mysql',
   147                  MASTER_USER='root',
   148                  MASTER_PASSWORD='',
   149                  MASTER_CONNECT_RETRY=10;
   150                START SLAVE;
   151                EOF
   152                fi
   153                # Start a server to send backups when requested by peers.
   154                exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
   155                  "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
   156            ports:
   157              - name: xtrabackup
   158                containerPort: 3307
   159            resources:
   160              requests:
   161                cpu: 100m
   162                memory: 100Mi
   163            volumeMounts:
   164              - name: data
   165                mountPath: /var/lib/mysql
   166                subPath: mysql
   167              - name: conf
   168                mountPath: /etc/mysql/conf.d
   169        volumes:
   170          - name: conf
   171            emptyDir: {}
   172          - name: config-map
   173            configMap:
   174              name: mysql
   175    volumeClaimTemplates:
   176      - metadata:
   177          name: data
   178        spec:
   179          resources:
   180            requests:
   181              storage: 10Gi
   182          accessModes:
   183            - ReadWriteOnce
   184    serviceName: mysql