为什么在kubernetes集群中不能使用$(服务名).$(namespace).svc.cluster.local来ping statefulset?

问题描述 投票:0回答:1

我在kubernetes v1.16.0集群中使用舵机安装MySQL HA。

./helm install report incubator/mysqlha -n middleware

现在,我ping的状态集服务,但给我的提示。

/ # ping report-mysqlha.middleware.svc.cluster.local
PING report-mysqlha.middleware.svc.cluster.local (10.244.2.57): 56 data bytes
64 bytes from 10.244.2.57: seq=0 ttl=62 time=0.223 ms
64 bytes from 10.244.2.57: seq=1 ttl=62 time=0.173 ms
64 bytes from 10.244.2.57: seq=2 ttl=62 time=0.190 ms
^C
--- report-mysqlha.middleware.svc.cluster.local ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.173/0.195/0.223 ms
/ # ping report-mysqlha-0.middleware.svc.cluster.local
ping: bad address 'report-mysqlha-0.middleware.svc.cluster.local'

这是我的状态集定义。

kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: report-mysqlha
  namespace: middleware
  selfLink: /apis/apps/v1/namespaces/middleware/statefulsets/report-mysqlha
  uid: d0a67983-3198-48e9-bad8-b06f3312945b
  resourceVersion: '311068'
  generation: 1
  creationTimestamp: '2020-06-07T08:27:37Z'
  labels:
    app: report-mysqlha
    app.kubernetes.io/managed-by: Helm
    chart: mysqlha-2.0.0
    heritage: Helm
    release: report
  annotations:
    meta.helm.sh/release-name: report
    meta.helm.sh/release-namespace: middleware
spec:
  replicas: 3
  selector:
    matchLabels:
      app: report-mysqlha
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: report-mysqlha
        release: report
    spec:
      volumes:
        - name: conf
          emptyDir: {}
        - name: config-map
          configMap:
            name: report-mysqlha
            defaultMode: 420
        - name: scripts
          emptyDir: {}
      initContainers:
        - name: clone-mysql
          image: 'gcr.io/google-samples/xtrabackup:1.0'
          command:
            - bash
            - '-c'
            - >
              set -ex

              # Skip the clone on master (ordinal index 0).

              [[ `hostname` =~ -([0-9]+)$ ]] || exit 1

              ordinal=${BASH_REMATCH[1]}

              [[ $ordinal -eq 0 ]] && exit 0


              # If data already exists, delete and proceed to clone.

              [[ -d /var/lib/mysql/mysql ]] && rm -fr /var/lib/mysql/*


              # Clone data from previous peer.

              ncat --recv-only report-mysqlha-$(($ordinal-1)).report-mysqlha
              3307 | xbstream -x -C /var/lib/mysql

              # Prepare the backup.

              xtrabackup --prepare --user=${MYSQL_REPLICATION_USER}
              --password=${MYSQL_REPLICATION_PASSWORD}
              --target-dir=/var/lib/mysql
          env:
            - name: MYSQL_REPLICATION_USER
              value: repl
            - name: MYSQL_REPLICATION_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: report-mysqlha
                  key: mysql-replication-password
          resources: {}
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
        - name: init-mysql
          image: 'mysql:5.7.29'
          command:
            - /bin/bash
          args:
            - '-c'
            - >
              set -ex

              # Generate mysql server-id from pod ordinal index.

              [[ `hostname` =~ -([0-9]+)$ ]] || exit 1

              ordinal=${BASH_REMATCH[1]}

              # Copy server-id.conf adding offset to avoid reserved server-id=0
              value.

              cat /mnt/config-map/server-id.cnf | sed s/@@SERVER_ID@@/$((100 +
              $ordinal))/g > /mnt/conf.d/server-id.cnf

              # Copy appropriate conf.d files from config-map to config mount.

              if [[ $ordinal -eq 0 ]]; then
                cp -f /mnt/config-map/master.cnf /mnt/conf.d/
              else
                cp -f /mnt/config-map/slave.cnf /mnt/conf.d/
              fi

              # Copy replication user script

              if [[ $ordinal -eq 0 ]]; then
                cp -f /mnt/config-map/create-replication-user.sh /mnt/scripts/create-replication-user.sh
                chmod 700 /mnt/scripts/create-replication-user.sh
              fi
          resources: {}
          volumeMounts:
            - name: conf
              mountPath: /mnt/conf.d
            - name: config-map
              mountPath: /mnt/config-map
            - name: scripts
              mountPath: /mnt/scripts
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      containers:
        - name: mysql
          image: 'mysql:5.7.29'
          ports:
            - name: mysql
              containerPort: 3306
              protocol: TCP
          env:
            - name: MYSQL_DATABASE
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: report-mysqlha
                  key: mysql-root-password
            - name: MYSQL_REPLICATION_USER
              value: repl
            - name: MYSQL_REPLICATION_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: report-mysqlha
                  key: mysql-replication-password
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - '-c'
                - 'mysqladmin ping -h 127.0.0.1 -u root -p${MYSQL_ROOT_PASSWORD}'
            initialDelaySeconds: 30
            timeoutSeconds: 5
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - '-c'
                - 'MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"'
                - mysql -h 127.0.0.1 -u root -e "SELECT 1"
            initialDelaySeconds: 10
            timeoutSeconds: 1
            periodSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
        - name: xtrabackup
          image: 'gcr.io/google-samples/xtrabackup:1.0'
          command:
            - /bin/bash
          args:
            - '-c'
            - |
              set -ex

              echo "Waiting for mysqld to be ready (accepting connections)"
              until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 5; done

              # Create replication user
              cd /mnt/scripts
              # file exists and is not empty with -s
              if [[ -s create-replication-user.sh  ]]; then
                ls -la
                ./create-replication-user.sh
              fi

              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 slave.
                cp xtrabackup_slave_info change_master_to.sql.in
              elif [[ -f xtrabackup_binlog_info ]]; then
                # We're cloning directly from master. Parse binlog position.
                [[ $(cat xtrabackup_binlog_info) =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
                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

                # In case of container restart, attempt this at-most-once.
                cp change_master_to.sql.in change_master_to.sql.orig
                mysql -h 127.0.0.1 --verbose<<EOF
                STOP SLAVE IO_THREAD;
                $(<change_master_to.sql.orig),
                MASTER_HOST='report-mysqlha-0.report-mysqlha',
                MASTER_USER='${MYSQL_REPLICATION_USER}',
                MASTER_PASSWORD='${MYSQL_REPLICATION_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=${MYSQL_REPLICATION_USER} --password=${MYSQL_REPLICATION_PASSWORD}"
          ports:
            - name: xtrabackup
              containerPort: 3307
              protocol: TCP
          env:
            - name: MYSQL_PWD
              valueFrom:
                secretKeyRef:
                  name: report-mysqlha
                  key: mysql-root-password
            - name: MYSQL_REPLICATION_USER
              value: repl
            - name: MYSQL_REPLICATION_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: report-mysqlha
                  key: mysql-replication-password
          resources:
            requests:
              cpu: 100m
              memory: 100Mi
          volumeMounts:
            - name: data
              mountPath: /var/lib/mysql
              subPath: mysql
            - name: conf
              mountPath: /etc/mysql/conf.d
            - name: scripts
              mountPath: /mnt/scripts
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      securityContext: {}
      schedulerName: default-scheduler
  volumeClaimTemplates:
    - metadata:
        name: data
        creationTimestamp: null
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        volumeMode: Filesystem
      status:
        phase: Pending
  serviceName: report-mysqlha
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  revisionHistoryLimit: 10
status:
  observedGeneration: 1
  replicas: 3
  readyReplicas: 3
  currentReplicas: 3
  updatedReplicas: 3
  currentRevision: report-mysqlha-f7648c684
  updateRevision: report-mysqlha-f7648c684
  collisionCount: 0

我是否遗漏了什么?

kubernetes
1个回答
3
投票

随着Statefulset的每个Pod被创建,它得到一个匹配的DNS子域,采取的形式。$(podname).$(governing service domain).

对于你的情况。

  • podname = report-mysqlha-0
  • 治理服务领域= report-mysqlha.middleware.svc.cluster.local

花苞的子域将是。report-mysqlha-0.report-mysqlha.middleware.svc.cluster.local


1
投票

report-mysqlha-0 是 pod 的名称,而不是服务的名称。因此,你不能通过 report-mysqlha-0.middleware.svc.cluster.local

© www.soinside.com 2019 - 2024. All rights reserved.