k8s部署单机mysql

部署 mysql 的 yaml 文件如下图所示

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: default
data:
  custom.cnf: |
    [mysqld]
    lower_case_table_names=1    

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-init-sql
  namespace: default
data:
  init1.sql: |
    CREATE DATABASE IF NOT EXISTS database1;    
  init2.sql: |
    CREATE DATABASE IF NOT EXISTS database2;    

---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: mysql
  namespace: default
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: mysql-config
          configMap:
            name: mysql-config
        - name: mysql-init-sql
          configMap:
            name: mysql-init-sql

      containers:
        - name: mysql
          # arm64 设置为mysql:oracle, amd64设置为mysql:latest
          image: registry.cn-hangzhou.aliyuncs.com/iuxt/mysql:8.0.38
          ports:
            - name: tcp-3306
              containerPort: 3306
              protocol: TCP
            - name: tcp-33060
              containerPort: 33060
              protocol: TCP
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: com.012

          volumeMounts:
            - name: mysql-config
              mountPath: /etc/mysql/conf.d
            - name: mysql-init-sql
              mountPath: /docker-entrypoint-initdb.d
            - name: host-time
              mountPath: /etc/localtime
            - name: mysql-data
              mountPath: /var/lib/mysql
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      dnsPolicy: ClusterFirst
  volumeClaimTemplates:
    - kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: mysql-data
        namespace: default
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        storageClassName: managed-nfs-storage
        volumeMode: Filesystem
  serviceName: mysql

---
kind: Service
apiVersion: v1
metadata:
  name: mysql
  namespace: default
  labels:
    app: mysql
spec:
  ports:
    - name: http-3306
      protocol: TCP
      port: 3306
      targetPort: 3306
  selector:
    app: mysql
  type: LoadBalancer

kubectl get svc 可以通过 iptable 转发到 kind 之外

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
iptables -F
iptables -t nat -F
iptables -X

dev_gw="172.18.0.5"

fedora_dev="172.18.255.202"
fedora_dev_ssh="10001"

iptables -t nat -A PREROUTING -p tcp --dport ${fedora_dev_ssh} -j DNAT --to-destination ${fedora_dev}:3306
iptables -t nat -A POSTROUTING -p tcp -d ${fedora_dev} --dport 3306 -j SNAT --to-source ${dev_gw}

这样就可以通过 127.0.0.1 10001 来访问 mysql image.png ![[image-20240819172338841.png]]

Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 06, 2025 05:52 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up