gpt4 book ai didi

mysql - 如何将我的应用程序连接到kubernetes mysql statefulset

转载 作者:行者123 更新时间:2023-12-02 12:12:01 30 4
gpt4 key购买 nike

我已经在链接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



我对部署进行了一些变量名更改,因此更新的部署如上所述,但仍未连接。 pods 的说明如下
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文件是问题,我的应用程序使该文件运行。

我必须在/ gmlcapp /目录中有2个文件(config.toml和profile.json)才能运行该应用程序。因为如上所述,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


最佳答案

我通过更改部署中的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

关于mysql - 如何将我的应用程序连接到kubernetes mysql statefulset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61712133/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com