服务中的Pod to Pod通信

问题描述 投票:0回答:2

是否可以将http Rest请求发送到属于Kubernetes中相同服务的另一个K8 Pod?

E. G.服务名称= UserService,2 Pods(replica = 2)

Pod 1 --> Pod 2 //using pod ip not load balanced hostname 
Pod 2 --> Pod 1

连接在休息GET 1.2.3.4:7079/user/1

host + port的值取自kubectl get ep

两个pod IP都在pod之外成功工作但是当我在pod中执行kubectl exec -it并通过CURL发出请求时,它会返回找不到端点的404。

问:如果可以向同一服务中的另一个K8 Pod发出请求,我想知道什么?

问:为什么我能够获得成功的ping 1.2.3.4,但没有点击Rest API?

下面是我的配置文件

 #values.yml
replicaCount: 1

 image:
  repository: "docker.hosted/app"
  tag: "0.1.0"
  pullPolicy: Always
  pullSecret: "a_secret"

service:
 name: http
 type: NodePort
 externalPort: 7079
 internalPort: 7079

ingress:
 enabled: false

deployment.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "app.fullname" . }}
  labels:
    app: {{ template "app.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  template:
    metadata:
      labels:
        app: {{ template "app.name" . }}
        release: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:

            - name: MY_POD_IP
              valueFrom:
               fieldRef:
                fieldPath: status.podIP
            - name: MY_POD_PORT
              value: "{{ .Values.service.internalPort }}"
          ports:
            - containerPort: {{ .Values.service.internalPort }}
          livenessProbe:
            httpGet:
              path: /actuator/alive
              port: {{ .Values.service.internalPort }}
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 1
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /actuator/ready
              port: {{ .Values.service.internalPort }}
          initialDelaySeconds: 60
          periodSeconds: 10
          timeoutSeconds: 1
          successThreshold: 1
          failureThreshold: 3
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
      imagePullSecrets:
        - name: {{ .Values.image.pullSecret }

service.yml

kind: Service
metadata:
  name: {{ template "app.fullname" . }}
  labels:
    app: {{ template "app.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.externalPort }}
      targetPort: {{ .Values.service.internalPort }}
      protocol: TCP
      name: {{ .Values.service.name }}
  selector:
    app: {{ template "app.name" . }}
    release: {{ .Release.Name }}

executed from master

executed from k8 master

executed from inside a pod of the same MicroService

executed from inside a pod of the same MicroService

spring-boot kubernetes
2个回答
1
投票

是否可以将http Rest请求发送到属于Kubernetes中相同服务的另一个K8 Pod?

当然,是的,这实际上就是为什么集群中的每个Pod都有一个集群范围的可路由地址。您可以通过请求与Endpoint命名相同的Service对象,以编程方式向kubernetes询问Pod的“对等”列表,然后减去您自己的Pod的IP地址。看起来你从kubectl get ep那里知道了,但后来你问了这个问题,所以我想我会明确表示你的经历不是偶然的。

问:为什么我能够成功ping 1.2.3.4,但没有点击Rest API?

我们无法帮助您在没有应用程序日志的情况下对您的应用程序进行故障排除,但事实上您有404并且没有“连接被拒绝”或504或类似意味着您的连接工作正常,这只是应用程序被破坏。


0
投票

是的,Mathew回答说你可以确实在服务中的pod与Kubernetes之间进行通信,我遇到的问题是Istio阻止了对彼此的请求。

解决方案:禁用Istio注入为我解决了这个问题,然后我继续在病房和负载平衡之后启用它,希望它可以在将来帮助某个人。

在这里看到答案:Envoy Pod to Pod communication within a Service in K8

© www.soinside.com 2019 - 2024. All rights reserved.