Kubernetes服务连接被拒绝

时间:2019-01-06 15:55:08

标签: spring-boot kubernetes docker-image kubernetes-pod kubernetes-service

我想用Kubernetes创建一个示例应用程序,但是如果我尝试连接到Kubernetes中的响应服务,我会被拒绝连接。

例如,如果我从另一个Pod连接到http://random-generator-svc:5050/,则会收到一条错误消息,提示连接被拒绝。

这是为随机生成器创建服务和部署的yaml文件:

apiVersion: v1
  kind: Service
  metadata:
    name: random-generator-svc
    labels:
      app: rand-gen
 spec:
   selector:
     app: rand-gen
     type: NodePort
     ports:
     - protocol: "TCP"
       port: 5050
       targetPort: 5050
       name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: random-generator-deployment
  labels:
    app: rand-gen
spec:
  replicas: 2
  selector:
    matchLabels:
      app: rand-gen
  template:
    metadata:
    labels:
      app: rand-gen
  spec:
    containers:
    - name: random-generator-container
      image: toky03/random-generator-image:1.2
      ports:
      - containerPort: 5050

这是yaml文件,用于指定“呼叫者”应用程序的服务和部署:

apiVersion: v1
kind: Service
metadata:
  name: middle-tier-svc
  labels:
    app: rand-gen
spec:
  selector:
    app: rand-gen
  type: NodePort
  ports:
  - protocol: "TCP"
    port: 7070
    targetPort: 7070
    name: http

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: middle-tier-controller
  labels:
    app: rand-gen
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rand-gen
  template:
    metadata:
      labels:
        app: rand-gen
    spec:
      containers:
      - name: random-controller-container
        image: toky03/random-controller-image:1.2
        ports:
        - containerPort: 7070

我将类型更改为NodePort,以尝试是否还存在该错误,但是我可以从集群外部访问该服务。我的Kubernetes DNS解析器可能有问题吗?

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

默认情况下,kubernetes创建一个虚拟代理。端口转发后即可访问服务。

kubectl port-forward svc random-generator-svc 5050:5050

答案 1 :(得分:0)

那是因为您的yaml缩进略有错别字。试试这个

apiVersion: v1
kind: Service
metadata:
  name: random-generator-svc
  labels:
    app: rand-gen
spec:
  selector:
    app: rand-gen
  type: NodePort
  ports:
  - protocol: "TCP"
    port: 5050
    targetPort: 5050
    name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: random-generator-deployment
  labels:
    app: rand-gen
spec:
  replicas: 2
  selector:
    matchLabels:
      app: rand-gen
  template:
    metadata:
      labels:
        app: rand-gen
    spec:
      containers:
      - name: random-generator-container
        image: toky03/random-generator-image:1.2
        ports:
        - containerPort: 5050

如果只希望将应用程序公开到群集中,则只需删除type: NodePort,例如:

apiVersion: v1
kind: Service
metadata:
  name: random-generator-svc
  labels:
    app: rand-gen
spec:
  selector:
    app: rand-gen
  ports:
  - protocol: "TCP"
    port: 5050
    targetPort: 5050
    name: http

答案 2 :(得分:0)

尝试“http://random-generator-svc.default.svc.cluster.local:5050”。 如果您使用任何命名空间,您可以将“默认”替换为命名空间。