# singlenode-mysql

* 数据存储采用 hostpath 方式
* Pod 多节点反亲和性调度，单节点失效
* Node 调度， 只运行在指定的节点上



```yaml
[root@master test]# cat singlenode-mysql-statefulset.yaml 
apiVersion: v1
kind: Service
metadata:
  name: singlenode-mysql
spec:
  type: NodePort
  #clusterIP: None
  ports:
    - name: singlenode-mysql
      port: 3306
      targetPort: 3306
      nodePort: 31306
  selector:
    app: singlenode-mysql

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: singlenode-mysql
spec:
  serviceName: singlenode-mysql
  replicas: 1
  selector:
    matchLabels:
      app: singlenode-mysql
  template:
    metadata:
      labels:
        app: singlenode-mysql
    spec:
      # 节点打上中间件标签并污染：kubectl taint nodes <node-name> middleware=true:NoSchedule
      #tolerations:
      #  - key: "middleware"
      #    operator: "Equal"
      #    value: "true"
      #    effect: "NoSchedule"
      
      # 当多节点生效：不允许同一个pod调度到同一个节点
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - middleware
              topologyKey: "kubernetes.io/hostname"
      initContainers:
        - name: init-singlenode-mysql
          image: busybox
          command: ['sh', '-c', 'mkdir -p /data/k8s/mysql']
          volumeMounts:
            - name: mysql-data
              mountPath: /data/k8s/mysql/
      containers:
        - name: singlenodemysql
          image: mysql:8.0
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: "123456"
            - name: MYSQL_ROOT_HOST
              value: "%"
            - name: MYSQL_DEFAULT_AUTHENTICATION_PLUGIN
              value: mysql_native_password
            - name: TZ
              value: America/Sao_Paulo # 设置时区为巴西时间
              #value: Asia/Shanghai # 设置时区为上海
          ports:
            - containerPort: 3306
              name: mysql
              protocol: TCP
          volumeMounts:
            - name: mysql-data
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-data
          hostPath:
            path: /data/k8s/mysql/
```
