Kubernetes:位于不同容器中的容器之间的通信

时间:2019-03-21 16:05:58

标签: kubernetes containers microservices

我有两个通过http协议进行通信的微服务。

  • AC6K:从Atlas Copco 6000设备获取数据的C#微服务。

  • LocalWriter:从AC6K获取数据并将信息存储在数据库中的python应用程序

我已经在Windows和Linux环境中对其进行了测试,并且工作正常。当我集中化每个微服务并进行部署时,没有通信。请随函附上用于容器化和部署应用程序的相应docker&yaml文件

ac6k docker文件:

FROM microsoft/aspnetcore-build

EXPOSE 5010

WORKDIR /app

COPY . .

RUN dotnet restore

ENTRYPOINT ["dotnet", "ac6kcore.dll"]




-  ac6kUp.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: ac6kcore
      labels:
        run: ac6kcore  
    spec:
      type: NodePort
      ports: 
      - port: 5010
        name: ac6kcore
        targetPort: 5010
        nodePort: 32766
        protocol: TCP
      selector:
        run: ac6kcore
    ---
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: ac6kcore
    spec:
      selector:
        matchLabels:
          run: ac6kcore
      replicas: 1
      template:
        metadata:
          labels:
            run: ac6kcore
        spec:
          hostNetwork: true
          containers:
          - image: afierro/ac6kcore:lw
            name: ac6kcore
            ports:
            - containerPort: 5010
          restartPolicy: Always

本地作家docker文件:

FROM python:3.6

RUN mkdir -p /mongodbapp

WORKDIR /mongodbapp

COPY requirements.txt /mongodbapp

RUN pip install --no-cache-dir -r requirements.txt

ADD . /mongodbapp

EXPOSE 9090

CMD ["python", "runapp.py"]

- LocalWriter.yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
  name: localwriter
  labels:
    app: localwriter
spec:
  type: NodePort
  ports:
  - port: 9090
    name: localwriter
    targetPort: 9090
    nodePort: 32756
    protocol: TCP
  selector:
    app: localwriter
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: localwriter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: localwriter
  template:
    metadata:
      labels:
        app: localwriter
    spec:
      containers:
      - name: flasknode
        image: afierro/localwriter:v1
        imagePullPolicy: Always
        ports:
        - containerPort: 9090

预先感谢

2 个答案:

答案 0 :(得分:1)

您使用服务将一个部署中的Pod与另一个部署中的Pod通信。

在此处检查服务:

https://kubernetes.io/docs/concepts/services-networking/service/

确保服务中有正确的选择器。

您可以使用pod内的env var调用其他服务。 Kubernetes使用服务ip和port作为env vars启动Pod,要进行检查,您可以在Pod中ssh并使用命令printenv。

答案 1 :(得分:1)

根据Leandro Donizetti Soares的建议,我输入了以下命令来查看环境变量,结果对我来说似乎是多余的。

kubectl get pods

NAME                           READY   STATUS    RESTARTS   AGE
ac6kcore-77bc4c4987-dxn29      1/1     Running   0          14m
localwriter-55467c5495-px8m4   1/1     Running   0          14m
mongo                          1/1     Running   0          14m

kubectl exec ac6kcore-77bc4c4987-dxn29 -- printenv | grep SERVICE   

AC6KCORE_SERVICE_PORT_AC6KCORE=5010
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT_HTTPS=443
AC6KCORE_SERVICE_PORT=5010
AC6KCORE_SERVICE_HOST=10.107.208.212
LOCALWRITER_SERVICE_HOST=10.100.103.114
LOCALWRITER_SERVICE_PORT=9090
LOCALWRITER_SERVICE_PORT_LOCALWRITER=9090
KUBERNETES_SERVICE_PORT=443

kubectl exec localwriter-55467c5495-px8m4 -- printenv | grep SERVICE    

KUBERNETES_SERVICE_HOST=10.96.0.1
LOCALWRITER_SERVICE_PORT_LOCALWRITER=9090
AC6KCORE_SERVICE_PORT_AC6KCORE=5010
KUBERNETES_SERVICE_PORT=443
AC6KCORE_SERVICE_HOST=10.107.208.212
LOCALWRITER_SERVICE_PORT=9090
KUBERNETES_SERVICE_PORT_HTTPS=443
AC6KCORE_SERVICE_PORT=5010
LOCALWRITER_SERVICE_HOST=10.100.103.114