Kubernetes中服务之间的连接问题

时间:2019-03-26 12:02:26

标签: docker kubernetes google-kubernetes-engine

我有三个与我的应用程序相关的图像,它们在docker-compose中可以正常工作,并且在GCP的kubernetes集群上运行时遇到问题。

下面是部署文件。

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql-database
spec:
  type: NodePort
  ports:
    - port: 3306
      targetPort: 3306      
  selector:
    app: mysql-database
    tier: database
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql-database
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql-database
        tier: database
    spec:
      hostname: mysql
      containers:
        - image: mysql/mysql-server:5.7
          name: mysql
          env:
            - name: "MYSQL_USER"
              value: "root"
            - name: "MYSQL_HOST"
              value: "mysql"
            - name: "MYSQL_DATABASE"
              value: "xxxx"
            - name: "MYSQL_PORT"
              value: "3306"
            - name: "MYSQL_PASSWORD"
              value: "password"
            - name: "MYSQL_ROOT_PASSWORD"
              value: "password"
            - name: "RAILS_ENV"
              value: "production"
          ports:
            - containerPort: 5432
              name: db
---
apiVersion: v1
kind: Service
metadata:
  name: dgservice
  labels:
    app: dgservice
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
  selector:
    name: dgservice
    tier: dgservice
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: dgservice
  labels:
    app: dgservice
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: dgservice
        tier: dgservice
    spec:
      hostname: dgservice
      containers:
        - image: gcr.io/sample/sample-image:check_1
          name: dgservice
          ports:
            - containerPort: 8080
              name: dgservice
---
apiVersion: v1
kind: Service
metadata:
  name: dg-ui
  labels:
    name: dg-ui
spec:
  type: NodePort
  ports:
    - nodePort: 30156 
      port: 8000
      protocol: TCP
      targetPort: 8000
  selector:
    app: dg-ui
    tier: dg
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: dg-ui
  labels:
    app: dg-ui
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: dg-ui
        tier: dg
    spec:
      hostname: dg-ui
      containers:
        - image: gcr.io/sample/sample:latest
          name: dg-ui
          env:
            - name: "MYSQL_USER"
              value: "root"
            - name: "MYSQL_HOST"
              value: "mysql"
            - name: "MYSQL_DATABASE"
              value: "xxxx"
            - name: "MYSQL_PORT"
              value: "3306"
            - name: "MYSQL_PASSWORD"
              value: "password"
            - name: "MYSQL_ROOT_PASSWORD"
              value: "password"
            - name: "RAILS_ENV"
              value: "production"
            - name: "DG_SERVICE_HOST"
              value: "dgservice"
          ports:
            - containerPort: 8000
              name: dg-ui

该图像也已成功从GCR提取。

mysql和ui服务之间的连接也可以正常工作,并且我的数据正在迁移而没有任何问题。但是,服务与ui之间未建立连接。

为什么ui无法访问我的应用程序中的服务?

2 个答案:

答案 0 :(得分:2)

由于您的部署具有以下标签,因此服务需要具有相同的标签才能创建端点对象

端点是服务背后的API对象。与服务的ClusterIP建立连接时,端点将是服务将连接路由到的端点

以下是部署的标签

      labels:
        app: dgservice
        tier: dgservice

带有正确标签

的新服务定义
apiVersion: v1
kind: Service
metadata:
  name: dgservice
  labels:
    app: dgservice
spec:
  type: NodePort
  ports:
    - port: 8080
      targetPort: 8080
  selector:
    app: dgservice
    tier: dgservice

答案 1 :(得分:0)

我假设“服务”是指您的“ dgservice”。借助上面介绍的yaml,我相信您还需要指定DG_SERVICE_PORT(端口8080)才能正确访问“ dgservice”。

如Suresh在评论中所述,您应该使用 ClusterIP 类型公开内部服务。 NodePort是ClusterIP的超集,它将在service-name:port内部向群集公开服务,而在node-ip:nodeport外部向群集公开服务,目标是targetport上的部署/吊舱。