Kubernetes(Minikube):客户端和服务器之间的链接

时间:2018-06-05 09:56:32

标签: kubernetes microservices minikube

我正在使用Minikube运行一个简单的弹簧微服务项目。我有两个项目:lucky-word-client(在端口8080上)和lucky-word-server(在端口8888上)。但我无法与服务器通信客户端。事实上,如果幸运字客户端与幸运字服务器进行通信,则结果是单词" Evviva",否则单词是"默认"。当我在终端上运行时:minikube service lucky-client输出为Default,而不是Evviva。

这是lucky-word-server的文件Dockerfile:

FROM frolvlad/alpine-oraclejdk8 

ADD build/libs/common-config-server-0.0.1-SNAPSHOT.jar common-config-server.jar

EXPOSE 8888

ENTRYPOINT ["/usr/bin/java", "-Xmx128m", "-Xms128m"]

CMD ["-jar", "common-config-server.jar"]

这是lucky-word-client的文件Dockerfile:

FROM frolvlad/alpine-oraclejdk8 

ADD build/libs/lucky-word-client-0.0.1-SNAPSHOT.jar lucky-word-client.jar

EXPOSE 8080

ENTRYPOINT ["/usr/bin/java", "-Xmx128m", "-Xms128m"]

CMD ["-jar", "-Dspring.profiles.active=italian", "lucky-word-client.jar"]

这是lucky-word-server的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-server
spec:
  selector:
      matchLabels:
        app: lucky-server
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-server
    spec:
      containers:
        - name: lucky-server
          image: lucky-server-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8888

这是lucky-word-server的服务:

kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    port: 8888
  type: NodePort

这是lucky-word-client的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lucky-client
spec:
  selector:
      matchLabels:
        app: lucky-client
  replicas: 1
  template:
    metadata:
      labels:
        app: lucky-client
    spec:
      containers:
        - name: lucky-client
          image: lucky-client-img
          imagePullPolicy: Never
          ports:
            - containerPort: 8080

这是lucky-word-client的服务:

kind: Service
apiVersion: v1
metadata:
  name: lucky-client
spec:
  selector:
    app: lucky-client
  ports:
  - protocol: TCP
    port: 8080
  type: NodePort

2 个答案:

答案 0 :(得分:1)

正如@suren所说,你应该在服务定义中指定目标端口。

您应该更改客户端调用的服务器的端点URL以反映minikube_host_ip。有几种方法可以实现这一目标。天真的方法如下。

将服务器的Kubernetes服务更改为具有静态Nodeport,如下所示:

kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    port: 8080
    nodePort: 32002
  type: NodePort

在您的客户端代码中,只需更改服务器的端点,如下所示: http:// {minikube_host_ip}:32002将{minikube_host_ip}替换为minikube主机的ip地址。

但是,如果您不想对minikube ip进行硬编码,则可以将其作为环境变量注入Kuberenetes部署脚本中。并且应该在docker文件中捕获该环境变量。

答案 1 :(得分:0)

您的服务现在正在将请求发送到端口80。您需要指定参数targetPort。应该是这样的:

kind: Service
apiVersion: v1
metadata:
  name: lucky-server
spec:
  selector:
    app: lucky-server
  ports:
  - protocol: TCP
    targetPort: 8888  #this is your container port. where to send the requests
    port: 8888   #this is the service port. it is running on svc-ip:8888
  type: NodePort

您应该对其他服务执行相同操作。另请检查服务端口。现在是8080和8888.你可能会在80号端口击中它们。

可能会有更多问题,但就目前而言,这些确实会导致问题。