How to expose kubernetes service to public without hardcoding to minion IP?

前端 未结 5 892
傲寒
傲寒 2020-12-24 13:12

I have a kubernetes cluster running with 2 minions. Currently I make my service accessible in 2 steps:

  1. Start replication controller & pod
  2. Get mini
5条回答
  •  余生分开走
    2020-12-24 13:52

    You can use Ingress resource to allow external connections from outside of a Kubernetes cluster to reach the cluster services.

    Assuming that you already have a Pod deployed, you now need a Service resource, e.g.:

    apiVersion: v1 kind: Service metadata: name: frontend-service labels: tier: frontend spec: type: ClusterIP selector: name: frontend-pod ports: - name: http protocol: TCP # the port that will be exposed by this service port: 8000 # port in a docker container; defaults to what "port" has set targetPort: 8000

    And you need an Ingress resource: apiVersion: extensions/v1beta1 kind: Ingress metadata: name: frontend-ingress spec: rules: - host: foo.bar.com http: paths: - path: / backend: serviceName: frontend-service # the targetPort from service (the port inside a container) servicePort: 8000 In order to be able to use Ingress resources, you need some ingress controller deployed.

    Now, providing that you know your Kubernetes master IP, you can access your application from outside of a Kubernetes cluster with: curl http://:80/ -H 'Host: foo.bar.com'


    If you use some DNS server, you can add this record: foo.bar.com IN A or add this line to your /etc/hosts file: foo.bar.com and now you can just run: curl foo.bar.com


    Notice, that this way you will always access foo.bar.com using port 80. If you want to use some other port, I recommend using a Service of type NodePort, only for that one not-80 port. It will make that port resolvable, no matter which Kubernetes VM IP you use (any master or any minion IP is fine). Example of such a Service: apiVersion: v1 kind: Service metadata: name: frontend-service-ssh labels: tier: frontend spec: type: NodePort selector: name: frontend-pod ports: - name: ssh targetPort: 22 port: 22 nodePort: 2222 protocol: TCP And if you have foo.bar.com in your /etc/hosts file, then you can access: foo.bar.com:2222

提交回复
热议问题