Search code examples
network-programmingkubernetesiptables

Cannot access kubernetes service via outside network


I setup kubernetes environment with kubernetes 1.3.0, and running master and node on same host, I run a tomcat web application with one RC, one Service with docker, all seems running fine, I can access the service via internal network with curl command, but when I try to access the Service from Internet with public IP, it is failure.

The RC configure is:

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
    replicas: 2
    selector:
      app: myweb
    template:
      metadata:
        labels:
          app: myweb
      spec:
        containers:
        - name: myweb
          image: kubeguide/tomcat-app:v1
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: "mysql"
          - name: MYSQL_SERVICE_PORT
            value: '3306'

The Service configure is:

apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb

As you see, the Service listen the 30001 Port, when the port listen by Kubernetes Service, it cannot be accessed via Internet, but when I use nc -l 30001 command on same host, it can be accessed via Internet, so that means the networking configure is fine on system layer.

For the iptables setting of host, I accept all connections, but the issue is still appeared.

then why can I access it with kubernetes service? is there any configure I miss?


Solution

  • To expose the kubernetes service using the host network you can use Ingress rules.

    please refer: https://kubernetes.io/docs/concepts/services-networking/ingress/

    In your case the ingress rule will be as follows. apiVersion: extensions/v1beta1 kind: Ingress metadata: name: nginx-test spec: rules: - host: test.example.com http: paths: - backend: serviceName: myweb servicePort: 30001 path: /