在本地 kubernetes 上公开服务 [英] Expose service on local kubernetes

查看:34
本文介绍了在本地 kubernetes 上公开服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Mac OS 上运行与 docker 捆绑在一起的本地 kubernetes.

I'm running a local kubernetes bundled with docker on Mac OS.

如何公开服务,以便我可以通过 Mac 上的浏览​​器访问该服务?

How can I expose a service, so that I can access the service via a browser on my Mac?

我已经创建:

a) 部署包括 apache httpd.

a) deployment including apache httpd.

b) 通过 yaml 提供服务:

b) service via yaml:

apiVersion: v1
kind: Service
metadata:
  name: apaches
spec:
  selector:
    app: web
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
  externalIPs:
  - 192.168.1.10 # Network IP of my Mac

我的服务看起来像:

$ kubectl get service apaches
NAME      TYPE       CLUSTER-IP       EXTERNAL-IP    PORT(S)        AGE
apaches   NodePort   10.102.106.158   192.168.1.10   80:31137/TCP   14m

我可以通过wget $CLUSTER-IP

我尝试在我的 Mac 上调用 http://192.168.1.10/,但它不起作用.

I tried to call http://192.168.1.10/ on my Mac, but it doesn't work.

这个问题处理类似的问题.但该解决方案没有帮助,因为我不知道我可以使用哪个 IP.

This question deals to a similar issue. But the solution does not help, because I do not know which IP I can use.

更新

感谢 Michael Hausenblas,我使用 Ingress 制定了一个解决方案.尽管如此,仍有一些悬而未决的问题:

Thanks to Michael Hausenblas I worked out a solution using Ingress. Nevertheless there are still some open questions:

  • 服务的 externalIP 是什么意思?当我不直接从外部访问服务时,为什么需要外部 IP?
  • 服务端口 31137 是什么意思?
    • kubernetes 文档描述了一种[通过 NodePort 在 minikube 中发布服务][4] 的方法.docker 上捆绑的 kubernetes 也可以这样做吗?

    推荐答案

    在 kubernetes 中暴露服务有几种解决方案:http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/

    There are several solutions to expose services in kubernetes: http://alesnosek.com/blog/2017/02/14/accessing-kubernetes-pods-from-outside-of-the-cluster/

    这是我根据 alesnosek 针对与 docker 捆绑的本地 kubernetes 的解决方案:

    Here are my solutions according to alesnosek for a local kubernetes bundled with docker:

    1.主机网络

    hostNetwork: true
    

    脏(出于安全原因不应共享主机网络)=> 我没有检查此解决方案.

    Dirty (the host network should not be shared for security reasons) => I did not check this solution.

    2.主机端口

    hostPort: 8086
    

    不适用于服务 => 我没有检查这个解决方案.

    Does not apply to services => I did not check this solution.

    3.节点端口

    通过定义一个 nodePort 来公开服务:

    Expose the service by defining a nodePort:

    apiVersion: v1
    kind: Service
    metadata:
      name: apaches
    spec:
      type: NodePort
      ports:
        - port: 80
          nodePort: 30000
      selector:
        app: apache
    

    4.负载均衡器

    编辑@MathObsessed 在他的回答中发布了解决方案.

    EDIT @MathObsessed posted the solution in his anwer.

    5.入口

    一个.安装 入口控制器

    git clone https://github.com/jnewland/local-dev-with-docker-for-mac-kubernetes.git
    
    kubectl apply -f nginx-ingress/namespaces/nginx-ingress.yaml -Rf nginx-ingress
    

    b.配置入口

    kubectl apply -f apache-ing.yaml

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: apache-ingress
    spec:
      rules:
      - host: localhost
        http:
          paths:
          - path: /
            backend:
              serviceName: apaches
              servicePort: 80
    

    现在我可以通过调用 http://localhost/

    使用备注local-dev-with-docker-for-mac-kubernetes

    • The repo simplifies the deployment of the offical ingress-nginx controller
    • For production use I would follow the official guide.
    • The repos ships with a tiny full featured ingress example. Very useful for getting quickly a working example application.

    更多文档

    这篇关于在本地 kubernetes 上公开服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆