day23 : TIDB on K8S (下)

尝试使用了TIDB後,接着就开始让他跟k8s做结合吧,我会介绍一次完整的TIoperator,然後配置一份TIDB deployment去使用昨天的PD进行服务串接。

首先透过helm的方式配置TIoperator


kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/manifests/crd.yaml
helm repo add pingcap https://charts.pingcap.org/

mkdir -p ${HOME}/tidb-operator && \
helm inspect values pingcap/tidb-operator --version=${chart_version} > ${HOME}/tidb-operator/values-tidb-operator.yaml

kubectl create ns tidb-admin

helm install tidb-operator pingcap/tidb-operator --namespace=tidb-admin --version=${chart_version} -f ${HOME}/tidb-operator/values-tidb-operator.yaml

https://ithelp.ithome.com.tw/upload/images/20210923/20139661IRS8bRZM2V.png
https://ithelp.ithome.com.tw/upload/images/20210923/20139661SyylxT2mGs.png

这样就可以开始透过operator布署TIDB的元件罗,布建tidb cluster的yaml官方有提供参数参考的yaml。
https://github.com/pingcap/tidb-operator/blob/master/examples/advanced/tidb-cluster.yaml

接着来玩看看为了配合vm配置的tidb deployment吧。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/component: tidb
    app.kubernetes.io/instance: demo
  name: demo-tidb
  namespace: tidb
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/component: tidb
      app.kubernetes.io/instance: demo
  template:
    metadata:
      annotations:
        prometheus.io/path: /metrics
        prometheus.io/port: "10080"
        prometheus.io/scrape: "true"
      creationTimestamp: null
      labels:
        app.kubernetes.io/component: tidb
        app.kubernetes.io/instance: demo
    spec:
      affinity: {}
      containers:
      - command:
        - sh
        - -c
        - touch /var/log/tidb/slowlog; tail -n0 -F /var/log/tidb/slowlog;
        image: busybox:1.26.2
        imagePullPolicy: IfNotPresent
        name: slowlog
        resources:
          limits:
            cpu: 100m
            memory: 10Mi
          requests:
            cpu: 20m
            memory: 5Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /var/log/tidb
          name: slowlog
      - command:
        - /bin/sh
        - /usr/local/bin/tidb_start_script.sh
        env:
        - name: CLUSTER_NAME
          value: demo
        - name: BINLOG_ENABLED
          value: "false"
        - name: SLOW_LOG_FILE
          value: /var/log/tidb/slowlog
        - name: POD_NAME
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.name
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        - name: POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        image: pingcap/tidb:v5.0.1
        imagePullPolicy: IfNotPresent
        name: tidb
        ports:
        - containerPort: 4000
          name: server
          protocol: TCP
        - containerPort: 10080
          name: status
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: 4000
          timeoutSeconds: 1
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /etc/podinfo
          name: annotations
          readOnly: true
        - mountPath: /etc/tidb
          name: config
          readOnly: true
        - mountPath: /usr/local/bin
          name: startup-script
          readOnly: true
        - mountPath: /var/log/tidb
          name: slowlog
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - downwardAPI:
          defaultMode: 420
          items:
          - fieldRef:
              apiVersion: v1
              fieldPath: metadata.annotations
            path: annotations
        name: annotations
      - configMap:
          defaultMode: 420
          items:
          - key: config-file
            path: tidb.toml
          name: demo-tidb-0
        name: config
      - configMap:
          defaultMode: 420
          items:
          - key: startup-script
            path: tidb_start_script.sh
          name: demo-tidb-0
        name: startup-script
      - emptyDir: {}
        name: slowlog
---
apiVersion: v1
data:
  config-file: |
    [log]
      [log.file]
        max-backups = 3
  startup-script: |
    #!/bin/sh

    # This script is used to start tidb containers in kubernetes cluster

    # Use DownwardAPIVolumeFiles to store informations of the cluster:
    # https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#the-downward-api
    #
    #   runmode="normal/debug"
    #
    set -uo pipefail

    ANNOTATIONS="/etc/podinfo/annotations"

    if [[ ! -f "${ANNOTATIONS}" ]]
    then
        echo "${ANNOTATIONS} does't exist, exiting."
        exit 1
    fi
    source ${ANNOTATIONS} 2>/dev/null
    runmode=${runmode:-normal}
    if [[ X${runmode} == Xdebug ]]
    then
        echo "entering debug mode."
        tail -f /dev/null
    fi

    # Use HOSTNAME if POD_NAME is unset for backward compatibility.
    POD_NAME=${POD_NAME:-$HOSTNAME}
    ARGS="--store=tikv \
    --advertise-address=${POD_IP} \
    --host=0.0.0.0 \
    --path=PD_IP:2379 \
    --config=/etc/tidb/tidb.toml
    "

    if [[ X${BINLOG_ENABLED:-} == Xtrue ]]
    then
        ARGS="${ARGS} --enable-binlog=true"
    fi

    SLOW_LOG_FILE=${SLOW_LOG_FILE:-""}
    if [[ ! -z "${SLOW_LOG_FILE}" ]]
    then
        ARGS="${ARGS} --log-slow-query=${SLOW_LOG_FILE:-}"
    fi

    echo "start tidb-server ..."
    echo "/tidb-server ${ARGS}"
    exec /tidb-server ${ARGS}
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/component: tidb
    app.kubernetes.io/instance: demo
  name: demo-tidb-0
  namespace: tidb
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: tidb
    app.kubernetes.io/instance: demo
  name: demo-tidb
  namespace: tidb
spec:
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - name: mysql-client
    port: 4000
    protocol: TCP
    targetPort: 4000
  - name: status
    port: 10080
    protocol: TCP
    targetPort: 10080
  selector:
    app.kubernetes.io/component: tidb
    app.kubernetes.io/instance: demo
  sessionAffinity: None
  type: ClusterIP

将这个这个yaml中cm的PD_IP修改成实体PD的IP就会跟既有的PD去串接罗,在这边提点关於PD的概念,PD虽然是分散式的管理服务,但是实际上接收请求和回应请求的其实都是leader,所以建议上PD要在可以回应所有TIDB的环境下运行。


<<:  IT铁人DAY 8-Singleton 单例模式

>>:  Day 08 : Longest Mountain in Array

[DAY 14]cog架构用法(1/2)

目前discord bot已经有初步的功能了 之後写的功能一定会越来越多 决定使用cog的架构来写 ...

Day 29 - [Android APP] 07-MVVM中的RecyclerView与Adapter

铁人赛快到尾声了,今天想介绍在控制 RecyclerView 中回馈按钮时,遇到的状况。想想,这篇好...

一键更新HTTPS凭证 - Automation Accounts

说明 在前篇介绍建立可提供 Let’s Encrypt 申请凭证的 Application Gate...

Day 23 dio函数库

昨天提到Flutter最常用的网路函数库有HttpClient和http函数库,但其实还有一种叫做d...

#26 No-code 之旅 — 实作 Dark Mode 和加入 Google Fonts ft. Chakra UI

连假结束了Q 今天来讲怎麽实作 dark mode 还有怎麽使用 Google Fonts 让网站看...