如何在K8S上将我的Web服务与mysql连接起来?

时间:2020-11-07 22:19:01

标签: mysql docker kubernetes minikube

我正在尝试部署必须与Mysql连接的Web服务。我在docker-compose上运行它们,但在 Kubernetes(Minikube)上,一切正常,我收到以下错误消息:拨号tcp:在10.96.0.10:53上查找mysql:没有这样的主机。知道我可能会缺少什么吗?这是我的清单文件:

该Web服务必须可以通过Internet访问,并且正在侦听8080端口。服务:

apiVersion: v1
kind: Service
metadata:
  name: webservice-svc
spec:
  selector:
    app: blur
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  type: NodePort

部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: blur-service
spec:
  selector:
    matchLabels:
      app: blur
  template:
    metadata:
      labels:
        app: blur
    spec:
      containers:
      - name: blur-service
        image: marjugoncalves/blur-service
        ports:
        - containerPort: 8080

mysql的清单文件:

服务:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: blur
  ports: 
    - protocol: TCP
      port: 3306
      targetPort: 3306

部署:

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: mysql
spec:
  selector:
    matchLabels:
      app: blur
  template:
    metadata:
      labels:
        app: blur
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        ports:
        - containerPort: 3306

1 个答案:

答案 0 :(得分:1)

我发现了我的错误:在我的应用程序中,在用于连接数据库的字符串中,我使用“ mysql”作为主机名,因此还必须这样调用服务名 。我的服务应如下所示:

apiVersion: v1
kind: Service
metadata:
  name: mysql //here was the mistake
spec:
  selector:
    app: blur
  ports: 
    - protocol: TCP
      port: 3306
      targetPort: 3306