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

查看:108
本文介绍了在本地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是否也可能?
    • What is the meaning of a service's externalIP? Why do I need an externalIP when I do not directly access a service from external?
    • What is the meaning of the service port 31137?
      • The kubernetes docs describe a method to [publish a service in minikube via NodePort][4]. Is this also possible with kubernetes bundled on docker?

      推荐答案

      有几种解决方案可以在kubernetes中公开服务: http://alesnosek .com/blog/2017/02/14/access-kubernetes-pods-from-outside-of-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

      hostNetwork: true
      

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

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

      2. hostPort

      hostPort: 8086
      

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

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

      3. NodePort

      通过定义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. LoadBalancer

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

      EDIT @MathObsessed posted the solution in his anwer.

      5.入口

      a.安装入口控制器

      a. Install Ingress Controller

      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/

      Now I can access my apache deployed with kubernetes by calling http://localhost/

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

      Remarks for using 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天全站免登陆