【问题标题】:How to connect my application to kubernetes mysql statefulset如何将我的应用程序连接到 kubernetes mysql statefulset
【发布时间】:2020-08-25 22:58:06
【问题描述】:

我已经按照链接 https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/ 部署了 mysql statefulset,并且所有 3 个 mysql pod 都运行良好。 我在 Golang 中编写了一个应用程序,它在连接到本地机器上的 MySQL 服务器时从 config.toml 文件中读取 MySQL 环境变量。 config.toml 文件包含这些变量。当我的应用程序在我的本地机器上运行时使用这些。

MySQLServer = "127.0.0.1"
Port = "3306"
MySQLDatabase = "hss_lte_db"
User = "hss"
Password = "hss" 

现在我想在我的 Kubernetes 集群中部署我的应用程序,以便它连接到 MySQL Statefulset 服务。我创建了如下所示的部署,但 pod 显示 Error 和 CrashLoopBackOff。需要有关如何将我的应用程序连接到 MySQL Statefulset 服务的帮助。 也不确定 MySQLServer 连接字符串在 configMap 中是否正确。

apiVersion: v1 
data:
  config.toml: |
   MySQLServer = "mysql-0.mysql,mysql-1.mysql,mysql-2.mysql"
   Port = "3306"
   MySQLDatabase = "hss_lte_db"
   User = "root"
   Password = ""

   GMLCAddressPort = ":8000"
   NRFIPAddr = "192.168.31.115"
   NRFPort = "30005"

kind: ConfigMap
metadata:
  name: vol-config-gmlcapi
  namespace: default


---
apiVersion: apps/v1 
kind: Deployment
metadata:
  name: gmlc-instance
  namespace: default
spec:
  selector:
    matchLabels:
      app: gmlc-instance
  replicas: 1 
  template:
    metadata:
      labels:
        app: gmlc-instance
        version: "1.0"
    spec:
      nodeName: k8s-worker-node2
      containers:
      - name: gmlc-instance
        image: abrahaa1/gmlcapi:1.0.0
        imagePullPolicy: "Always"
        ports:
        - containerPort: 8000
        volumeMounts:
        - name: configs
          mountPath: /gmlcapp/config.toml
          subPath: config.toml
        volumeMounts:
        - name: gmlc-configs
          mountPath: /gmlcapp/profile.json
          subPath: profile.json
      volumes:
      - name: configs 
        configMap:
          name: vol-config-gmlcapi
      - name: gmlc-configs
        configMap:
          name: vol-config-profile


我对deployment做了一些变量名的修改,所以更新的deployment同上但是还是没有连接。 pod的描述为

ubuntu@k8s-master:~/gmlc$ kubectl describe pod gmlc-instance-5898989874-s5s5j -n default
Name:         gmlc-instance-5898989874-s5s5j
Namespace:    default
Priority:     0
Node:         k8s-worker-node2/192.168.31.151
Start Time:   Sun, 10 May 2020 19:50:09 +0300
Labels:       app=gmlc-instance
              pod-template-hash=5898989874
              version=1.0
Annotations:  <none>
Status:       Running
IP:           10.244.1.120
IPs:
  IP:           10.244.1.120
Controlled By:  ReplicaSet/gmlc-instance-5898989874
Containers:
  gmlc-instance:
    Container ID:   docker://b756e67a39b7397e24fe394a8b17bc6de14893329903d3eace4ffde86c335213
    Image:          abrahaa1/gmlcapi:1.0.0
    Image ID:       docker-pullable://abrahaa1/gmlcapi@sha256:e0c8ac2a3db3cde5015ea4030c2099126b79bb2472a9ade42576f7ed1975b73c
    Port:           8000/TCP
    Host Port:      0/TCP
    State:          Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Sun, 10 May 2020 19:50:33 +0300
      Finished:     Sun, 10 May 2020 19:50:33 +0300
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Sun, 10 May 2020 19:50:17 +0300
      Finished:     Sun, 10 May 2020 19:50:17 +0300
    Ready:          False
    Restart Count:  2
    Environment:    <none>
    Mounts:
      /gmlcapp/profile.json from gmlc-configs (rw,path="profile.json")
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-prqdp (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  configs:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      vol-config-gmlcapi
    Optional:  false
  gmlc-configs:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      vol-config-profile
    Optional:  false
  default-token-prqdp:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-prqdp
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason   Age               From                       Message
  ----     ------   ----              ----                       -------
  Normal   Pulling  9s (x3 over 28s)  kubelet, k8s-worker-node2  Pulling image "abrahaa1/gmlcapi:1.0.0"
  Normal   Pulled   7s (x3 over 27s)  kubelet, k8s-worker-node2  Successfully pulled image "abrahaa1/gmlcapi:1.0.0"
  Normal   Created  7s (x3 over 26s)  kubelet, k8s-worker-node2  Created container gmlc-instance
  Normal   Started  6s (x3 over 26s)  kubelet, k8s-worker-node2  Started container gmlc-instance
  Warning  BackOff  6s (x3 over 21s)  kubelet, k8s-worker-node2  Back-off restarting failed container

仍然无法连接。

日志输出: ubuntu@k8s-master:~/gmlc$ kubectl 日志 gmlc-instance-5898989874-s5s5j -n 默认 2020/05/10 18:13:21 打开 config.toml: 没有这样的文件或目录

看起来 config.toml 文件是问题所在,我的应用程序需要该文件才能运行。

我有 2 个文件(config.toml 和 profile.json)必须位于 /gmlcapp/ 目录中才能运行应用程序。因为 profile.json 像上面那样添加到部署中是巨大的,所以我单独创建了它的 configmap。这是它的 configmap 输出

ubuntu@k8s-master:~/gmlc$ kubectl get configmaps
NAME                 DATA   AGE
mysql                2      4d3h
vol-config-gmlcapi   1      97m
vol-config-profile   1      7h56m

这也是我在部署中评论 vol-config-profile 时的日志。

ubuntu@k8s-master:~/gmlc$ kubectl logs gmlc-instance-b4ddd459f-fd8nr -n default
root:@tcp(mysql-0.mysql,mysql-1.mysql,mysql-2.mysql:3306)/hss_lte_db
2020/05/10 18:39:43 GMLC cannot ping MySQL sever
2020/05/10 18:39:43 Cannot read json file
panic: Cannot read json file

goroutine 1 [running]:
log.Panic(0xc00003dda8, 0x1, 0x1)
    /usr/local/go/src/log/log.go:351 +0xac
gmlc-kube/handler.init.0()
    /app/handler/init.go:43 +0x5e9


【问题讨论】:

  • kubectl describe pod POD_NAME -n NAMESPACE的完整输出添加到您的问题中
  • 同时添加 pod 日志:kubectl logs POD_NAME
  • 我已经添加了 pod 的日志。
  • 问题在于volumeMount。我已经用volumeMount在编辑过的帖子中改变了它,一切都运行良好。谢谢!!
  • 好消息。如果他们帮助您解决了问题,请考虑接受我的 cmets

标签: mysql kubernetes


【解决方案1】:

我已通过更改部署中的 volumeMount 使其运行。

解决方法如下:

apiVersion: apps/v1 
kind: Deployment
metadata:
  name: gmlc-instance
  namespace: default
spec:
  selector:
    matchLabels:
      app: gmlc-instance
  replicas: 1 
  template:
    metadata:
      labels:
        app: gmlc-instance
        version: "1.0"
    spec:
      nodeName: k8s-worker-node2
      containers:
      - name: gmlc-instance
        image: abrahaa1/gmlcapi:1.0.0
        imagePullPolicy: "Always"
        ports:
        - containerPort: 8000
        volumeMounts:
        - name: configs
          mountPath: /gmlcapp/config.toml
          subPath: config.toml
          readOnly: true
        - name: gmlc-configs
          mountPath: /gmlcapp/profile.json
          subPath: profile.json
      volumes:
      - name: configs 
        configMap:
          name: vol-config-gmlcapi
      - name: gmlc-configs
        configMap:
          name: vol-config-profile

【讨论】:

    猜你喜欢
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2015-12-01
    • 2018-04-18
    • 2014-11-13
    • 1970-01-01
    相关资源
    最近更新 更多