0

Below is my statefulset.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: dev
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 2
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:
      - name: init-mysql
        image: mysql:5.7
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Generate mysql server-id from pod ordinal index.
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # Add an offset to avoid reserved server-id=0 value.
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # Copy appropriate conf.d files from config-map to emptyDir.
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/master.cnf /mnt/conf.d/
          else
            cp /mnt/config-map/slave.cnf /mnt/conf.d/
          fi
        volumeMounts:
        - name: conf
          mountPath: /mnt/conf.d
        - name: config-map
          mountPath: /mnt/config-map
      - name: clone-mysql
        image: gcr.io/google-samples/xtrabackup:1.0
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Skip the clone if data already exists.
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # Skip the clone on leader (ordinal index 0).
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          # Clone data from previous peer.
          ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
          # Prepare the backup.
          xtrabackup --prepare --target-dir=/var/lib/mysql
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "1"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 500m
            memory: 1Gi
        livenessProbe:
          exec:
            command: ["mysqladmin", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            # Check we can execute queries over TCP (skip-networking is off).
            command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      - name: xtrabackup
        image: gcr.io/google-samples/xtrabackup:1.0
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql

          # Determine binlog position of cloned data, if any.
          if [[ -f xtrabackup_slave_info ]]; then
            # XtraBackup already generated a partial "CHANGE MASTER TO" query
            # because we're cloning from an existing follower.
            mv xtrabackup_slave_info change_master_to.sql.in
            # Ignore xtrabackup_binlog_info in this case (it's useless).
            rm -f xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # We're cloning directly from leader. Parse binlog position.
            [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
            rm xtrabackup_binlog_info
            echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
                  MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
          fi

          # Check if we need to complete a clone by starting replication.
          if [[ -f change_master_to.sql.in ]]; then
            echo "Waiting for mysqld to be ready (accepting connections)"
            until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done

            echo "Initializing replication from clone position"
            # In case of container restart, attempt this at-most-once.
            mv change_master_to.sql.in change_master_to.sql.orig
            mysql -h 127.0.0.1 <<EOF
          $(<change_master_to.sql.orig),
            MASTER_HOST='mysql-0.mysql',
            MASTER_USER='root',
            MASTER_PASSWORD='',
            MASTER_CONNECT_RETRY=10;
          START SLAVE;
          EOF
          fi

          # Start a server to send backups when requested by peers.
          exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
            "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
      volumes:
      - name: conf
        emptyDir: {}
      - name: config-map
        configMap:
          name: mysql-config

  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "mysql-gp2-sc"
      resources:
        requests:
          storage: 10Gi

After apply, I'm getting stuck with this,

    NAME      READY   STATUS     RESTARTS   AGE
mysql-0   3/3     Running    0          8m13s
mysql-1   0/3     Init:1/3   0          7m46s

Cannot see any logs as it is initilization stage only!.

Kubectl describe pod

   Events:
  Type    Reason                  Age    From                     Message
  ----    ------                  ----   ----                     -------
  Normal  Scheduled               4m3s   default-scheduler        Successfully assigned dev/mysql-1 to ip-10-8-0-50.ap-south-1.compute.internal
  Normal  SuccessfulAttachVolume  4m1s   attachdetach-controller  AttachVolume.Attach succeeded for volume "pvc-07f32fcd-5382-4cfe-a73d-19eea7404c13"
  Normal  Pulled                  3m53s  kubelet                  Container image "mysql:5.7" already present on machine
  Normal  Created                 3m53s  kubelet                  Created container init-mysql
  Normal  Started                 3m53s  kubelet                  Started container init-mysql
  Normal  Pulled                  3m52s  kubelet                  Container image "gcr.io/google-samples/xtrabackup:1.0" already present on machine
  Normal  Created                 3m52s  kubelet                  Created container clone-mysql
  Normal  Started                 3m52s  kubelet                  Started container clone-mysql

Kubectl get pvc

NAME           STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    data-mysql-0   Bound    pvc-6eada99d-fb72-4419-9181-adfeabe960a1   10Gi       RWO            mysql-gp2-sc   9m55s
    data-mysql-1   Bound    pvc-07f32fcd-5382-4cfe-a73d-19eea7404c13   10Gi       RWO            mysql-gp2-sc   9m28s

Hell all, Am stuck with this for 2 days any help or debugging is highly appreciate. Thanks in advance.

suren
  • 7,817
  • 1
  • 30
  • 51
Jithin Kumar S
  • 701
  • 2
  • 9
  • 20
  • the answer is in the logs of the `initContainers`. – suren Mar 14 '21 at 13:58
  • am not finding any errror in the that, + [[ -d /var/lib/mysql/mysql ]] ++ hostname + [[ mysql-1 =~ -([0-9]+)$ ]] + ordinal=1 + [[ 1 -eq 0 ]] + xbstream -x -C /var/lib/mysql + ncat --recv-only mysql-0.mysql 3307 – Jithin Kumar S Mar 14 '21 at 14:16
  • you dont get anything with `kubectl logs mysql-1 -c init-mysql -n dev`? – suren Mar 14 '21 at 14:23
  • + [[ -d /var/lib/mysql/mysql ]] ++ hostname + [[ mysql-1 =~ -([0-9]+)$ ]] + ordinal=1 + [[ 1 -eq 0 ]] + ncat --recv-only mysql-0.mysql 3307 + xbstream -x -C /var/lib/mysql – Jithin Kumar S Mar 14 '21 at 15:05
  • the above comment is the output of the init process running, but it is expected to completed and terminate the init but seems in running state, not sure y – Jithin Kumar S Mar 14 '21 at 15:05
  • there are some weird things. that command is in the second init container, but from your debug it seems to be stuck on the first one. then you have 3 containers in you get pods, but your yaml shows two. – suren Mar 14 '21 at 15:32
  • yes, istio is the side car which is default. that is why it is 3 and yml showing 2 – Jithin Kumar S Mar 14 '21 at 16:23
  • any one any idea why this init command got stuck "ncat --recv-only mysql-0.mysql 3307" – Jithin Kumar S Mar 14 '21 at 18:50
  • hi team any idea on this ? – Jithin Kumar S Mar 15 '21 at 08:46

1 Answers1

0

plz remove istio !! istioctl manifest generate --set profile=demo

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – mufazmi Jun 16 '22 at 00:53