k8s 从一项服务与另一项服务进行通信

tmp*_*dev 0 kubernetes minikube

I have two services deployed on the same k8s (minikube cluster). What is the url/approach I should use for one service to communicate with another service. I tried searching a bit on the web but most of them are communicating with an external db which is not what I'm after. This is what my deployments look like. I am looking for the goclient to be able to communicate with goserver. I know I need to go through the service but not sure what the url should look like. And is this dynamically discoverable? In addition to this if I expose goserver though ingress will this change ?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: goserver
  namespace: golang-ns
  labels:
    app: goserver
spec:
  replicas: 1
  selector:
    matchLabels:
      app: goserver
  template:
    metadata:
      labels:
        app: goserver
    spec:
      containers:
      - name: goserver
        image: goserver:1.0.0
        imagePullPolicy: Never
        ports:
        - containerPort: 8080

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: goclient
  namespace: golang-ns
  labels:
    app: goclient
spec:
  replicas: 1
  selector:
    matchLabels:
      app: goclient
  template:
    metadata:
      labels:
        app: goclient
    spec:
      containers:
      - name: goclient
        image: goclient:1.0.0
        imagePullPolicy: Never
        ports:
        - containerPort: 8081

---

apiVersion: v1
kind: Service
metadata:
  name: goserver-service
  namespace: golang-ns
spec:
  selector:
    app: goserver  
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  type: LoadBalancer

---

apiVersion: v1
kind: Service
metadata:
  name: goclient-service
  namespace: golang-ns
spec:
  selector:
    app: goclient
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
  type: LoadBalancer
Run Code Online (Sandbox Code Playgroud)

mar*_*rio 5

请注意,在kubernetes上下文中使用术语“服务”可能会非常含糊。

\n

您问题中的服务用于表示您的微服务之一,部署为容器化应用程序,在 中运行Pods,由 2 个单独的Deployments.

\n

ServiceDavid Maze 的评论中提到的 ,指的是一种特定的资源类型,用于在 kubernetes 集群内部和外部公开您的应用程序/微服务。这种资源类型称为Service. Services但我假设您知道您的示例中也添加了此类内容。

\n

这就是为什么如果我真的想调用部署在kubernetes 集群上的应用程序(客户端、服务器等)之一的“服务”,我更喜欢使用术语“微服务。是的,这是非常重要的区别,因为谈论从一个到另一个(kubernetes 资源类型)的通信根本没有任何意义。您可以通过公开这一秒的a 与不同的人进行通信,但根本不互相通信。我希望这一点很清楚。ServiceServicePodPodServicePodServices

\n

因此,为了公开集群中的一个微服务 并使其可供在同一集群上运行的其他微服务轻松访问,请使用. 但在您的情况下,您真正​​需要的是最简单的形式。\xe2\x9d\x97这里不需要使用type。在您的情况下,您希望公开您的named,以便从第二个named访问它,而不是通过外部客户端从公共互联网发送请求。ServiceLoadBalancerDeploymentgoserverPodsDeploymentgoclient

\n

请注意,LoadBalancer您在 yaml 清单中使用的类型Service具有完全不同的用途 - 它用于向从kubernetes 集群外部访问它的客户端公开您的应用程序,并且主要适用于云环境。

\n

同样,您的情况需要的是最简单的Service(通常称为ClusterIP默认类型),它集群内Service公开 a 。\xe2\x9a\xa0\xef\xb8\x8f 请记住,还具有负载平衡功能。Deployment ClusterIP Service

\n

好的,解释得够多了,让我们进入实际部分。正如我所说,这非常简单,只需一个命令即可完成:

\n
kubectl expose deployment goserver --port 8080 --namespace golang-ns\n
Run Code Online (Sandbox Code Playgroud)\n

是的!就这样!它将创建一个Service命名的goserver(没有理由将其命名为与它Deployment公开的不同的名称),它将公开Pods属于goserver Deployment您的kubernetes 集群,使其可以通过其DNS名称轻松访问(和发现)。

\n

如果您更喜欢声明性Service定义,也可以在这里:

\n
apiVersion: v1\nkind: Service\nmetadata:\n  name: goserver\n  namespace: golang-ns\nspec:\n  selector:\n    app: goserver  \n  ports:\n    - port: 8080\n
Run Code Online (Sandbox Code Playgroud)\n

golang-client Pods只需要Service名称即可goserver访问goserver Pods,因为它们部署在同一命名空间 ( golang-ns) 中。如果您需要从Pod部署到不同的命名空间来访问它们,则需要使用<servicename>.<namespace>ie goserver.golang-ns。您还可以使用完全限定域名 (FQDN) (请参阅此处的官方文档):

\n
my-svc.my-namespace.svc.cluster-domain.example\n
Run Code Online (Sandbox Code Playgroud)\n

在您的情况下可能如下所示:

\n
goserver.golang-ns.svc.cluster.local\n
Run Code Online (Sandbox Code Playgroud)\n

至于:

\n
\n

除此之外,如果我通过入口公开 goserver,这会改变吗?

\n
\n

\xe2\x9d\x97除非你想将你的内容暴露goserver给外部世界,否则不要使用Ingress,你不需要它。

\n