Kubernetes MySQL5.7 启动报错解决

2024-07-30 456 0

MySQL 57 StatefulSet

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: java-project
spec:
  ports:
  - port: 3306
    name: mysql
  clusterIP: None
  selector:
    app: mysql57

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: java-project
spec:
  selector:
    matchLabels:
      app: mysql57
  serviceName: "mysql"
  template:
    metadata:
      labels:
        app: mysql57
    spec:
      nodeName: k8s-node02
      containers:
      - name: mysql
        image: mysql:5.7.31
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "mypass"
        - name: MYSQL_DATABASE
          value: test
        ports:
        - containerPort: 3306
        volumeMounts:
        - mountPath: "/var/lib/mysql"
          name: mysql-data
  volumeClaimTemplates:
  - metadata:
      name: mysql-data
    spec:
      accessModes: ["ReadWriteMany"]
      storageClassName: "nfs-client"
      resources:
        requests:
          storage: 5Gi
[root@harbor yaml]# kubectl get pod -n java-project
NAME                            READY   STATUS    RESTARTS   AGE
mysql-0                         1/1     Running   0          48s

[root@harbor yaml]# kubectl logs mysql-0 -n java-project
2024-07-30 05:21:20+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.31-1debian10 started.
2024-07-30 05:21:28+00:00 [ERROR] [Entrypoint]: mysqld failed while attempting to check config
    command was: mysqld --verbose --help

[root@harbor yaml]# kubectl describe pod  mysql-0 -n java-project
Name:             mysql-0
Namespace:        java-project
Priority:         0
Service Account:  default
Node:             k8s-node01/192.168.77.94
Start Time:       Tue, 30 Jul 2024 01:20:34 -0400
Labels:           app=mysql57
                  controller-revision-hash=mysql-6d955944dd
                  statefulset.kubernetes.io/pod-name=mysql-0
Annotations:      cni.projectcalico.org/containerID: ed23108d013995c24317604a1fafefe74a755d62a028356a9a61ecbee62a7843
                  cni.projectcalico.org/podIP: 10.243.85.204/32
                  cni.projectcalico.org/podIPs: 10.243.85.204/32
Status:           Running
IP:               10.243.85.204
IPs:
  IP:           10.243.85.204
Controlled By:  StatefulSet/mysql
Containers:
  mysql:
    Container ID:   containerd://7f1799e2d8be599703b8243211770dcbe74d0ea53cb583d28d9343ade2772fc7
    Image:          mysql:5.7.31
    Image ID:       docker.io/library/mysql@sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b
    Port:           3306/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 30 Jul 2024 01:22:30 -0400
    Last State:     Terminated
      Reason:       OOMKilled
      Exit Code:    1
      Started:      Tue, 30 Jul 2024 01:21:57 -0400
      Finished:     Tue, 30 Jul 2024 01:22:00 -0400
    Ready:          True
    Restart Count:  3
    Environment:
      MYSQL_ROOT_PASSWORD:  mypass
      MYSQL_DATABASE:       test
    Mounts:
      /var/lib/mysql from mysql-data (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-v6ltd (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  mysql-data:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mysql-data-mysql-0
    ReadOnly:   false
  kube-api-access-v6ltd:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  2m5s               default-scheduler  Successfully assigned java-project/mysql-0 to k8s-node01
  Normal   Pulling    2m1s               kubelet            Pulling image "mysql:5.7.31"
  Normal   Pulled     80s                kubelet            Successfully pulled image "mysql:5.7.31" in 40.722219334s (40.722235034s including waiting)
  Warning  BackOff    24s (x3 over 56s)  kubelet            Back-off restarting failed container mysql in pod mysql-0_java-project(e8dbc0ab-4418-4e49-a1fe-041fc82c2369)
  Normal   Pulled     10s (x3 over 67s)  kubelet            Container image "mysql:5.7.31" already present on machine
  Normal   Created    9s (x4 over 79s)   kubelet            Created container mysql
  Normal   Started    9s (x4 over 79s)   kubelet            Started container mysql

内存不足导致OOM

Last State:     Terminated
Reason:       OOMKilled
[root@k8s-master01 ~]# kubectl top node
NAME           CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%   
k8s-master01   417m         20%    2625Mi          74%       
k8s-master02   655m         32%    2206Mi          62%       
k8s-master03   300m         15%    2934Mi          83%       
k8s-node01     392m         19%    2230Mi          63%       
k8s-node02     130m         6%     1516Mi          43% 

查看k8s-node01日志

[root@k8s-node01 ~]# dmesg
...
[12040047.844537] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/kubepods.slice/kubepods-besteffort.slice/kubepods-besteffort-pode8dbc0ab_4418_4e49_a1fe_041fc82c2369.slice/cri-containerd-08a6b07b695cf08c1a1223beeac66f24686fda4e7ffb0bd9bebf6c3277b3b074.scope,task=mysqld,pid=909867,uid=0
[12040047.844555] Out of memory: Killed process 909867 (mysqld) total-vm:16813772kB, anon-rss:2534396kB, file-rss:4kB, shmem-rss:0kB, UID:0 pgtables:5068kB oom_score_adj:1000

原因:看着是内存占用不够,实际是Rocky Kubernetes MySQL57 不兼容,
open_files_limit初始化太大,导致内存全部用光

修正后的

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: java-project
spec:
  ports:
  - port: 3306
    name: mysql
  clusterIP: None
  selector:
    app: mysql57

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: java-project
spec:
  selector:
    matchLabels:
      app: mysql57
  serviceName: "mysql"
  template:
    metadata:
      labels:
        app: mysql57
    spec:
      nodeName: k8s-node02
      containers:
      - name: mysql
        image: mysql:5.7.31
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "mypass"
        - name: MYSQL_DATABASE
          value: test
        resources:
          limits:
            cpu: 500m
            memory: 500Mi
          requests:
            cpu: 500m
            memory: 300Mi
        ports:
        - containerPort: 3306
        command: ["sh","-c","ulimit -n 1048576 && exec /entrypoint.sh mysqld"]
        volumeMounts:
        - mountPath: "/var/lib/mysql"
          name: mysql-data
  volumeClaimTemplates:
  - metadata:
      name: mysql-data
    spec:
      accessModes: ["ReadWriteMany"]
      storageClassName: "nfs-client"
      resources:
        requests:
          storage: 5Gi

https://github.com/kubernetes/minikube/issues/5751
https://github.com/bottlerocket-os/bottlerocket/issues/1136
https://blog.csdn.net/qq_26363645/article/details/133638251

相关文章

KubeSphere DevOps 流水线JAVA项目配置
虚拟机热添加内存 Kubernetes未生效
Containerd镜像加速及私有仓库配置(用户密码和忽略HTTPS)
在Kubernetes集群部署kubesphere
使用KubeKey快速部署Kubernetes集群1.28.8
Rancher 快速创建RKE K8S集群

发布评论