How to access a server on kubernetes from your local machine?

4/3/2020

My company has a kubernetes platform on which I have a pod ingress running on a namespace my_namespace:

apiVersion: v1
kind: Pod
metadata:
 name: ingress
 labels:
  app: ingress
spec:
 containers:
  - name: ingress
    image: docker:5000/mma/neurotec-ingress
    imagePullPolicy: Always

kubectl get pods -n my_namespace

NAME            READY   STATUS    RESTARTS   AGE
ingress         1/1     Running   1          11d

The pod is a server that listens on port 8080.

I also have a service defined that exports the pod to the outside:

apiVersion: v1
kind: Service
metadata:
  name: ingress
  labels:
    app: ingress
spec:
  ports:
  - port: 8080
    protocol: TCP
  selector:
    app: ingress
  type: LoadBalancer

kubectl describe service -n my_namespace ingress

Name:              ingress
Namespace:         my_namespace
Labels:            app=ingress
Selector:          app=ingress
Type:              LoadBalancer
IP:                10.104.95.96
Port:              <unset>  8080/TCP
TargetPort:        8080/TCP
Endpoints:         10.16.1.232:8080

I now want to send message to the server from my local computer. First thing I want to do is to make sure that its IP address is reachable. However, a simple host command returns errors:

host 10.16.1.232
Host 10.16.1.232 not found: 3(NXDOMAIN)

host ingress.my_namespace.nt // .nt is company's prefix
Host ingress.my_namespace not found: 3(NXDOMAIN)

and if I try to run telepresence it also returns an error:

Looks like there's a bug in our code. Sorry about that!

Traceback (most recent call last):
  File "/usr/bin/telepresence/telepresence/cli.py", line 135, in crash_reporting
    yield
  File "/usr/bin/telepresence/telepresence/main.py", line 65, in main
    remote_info = start_proxy(runner)
  File "/usr/bin/telepresence/telepresence/proxy/__init__.py", line 138, in start_proxy
    run_id=run_id,
  File "/usr/bin/telepresence/telepresence/proxy/remote.py", line 144, in get_remote_info
    runner, deployment_name, deployment_type, run_id=run_id
  File "/usr/bin/telepresence/telepresence/proxy/remote.py", line 91, in get_deployment_json
    return json.loads(output)["items"][0]
IndexError: list index out of range

Question: how to reach a server on kubernetes from your local machine?

-- mercury0114
host
kubernetes
service

1 Answer

4/3/2020

I think you should use service type: LoadBalancer. (not ClusterIP)

from kubernetes documentation

  • ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType.
  • LoadBalancer: Exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

Below is my sample loadbalaner service file. I exposed port 80 to the internet and map port 80 to pod with label run: my-web-portal port 8000.

apiVersion: v1
kind: Service
metadata:
  name: my-web-portal-svc
  labels:
    run: my-web-portal-svc
spec:
  ports:
  - port: 80
    targetPort: 8000
    protocol: TCP
    name: port-80
  selector:
    run: my-web-portal
  type: LoadBalancer

and here is my deployments yml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-web-portal
spec:
  selector:
    matchLabels:
      run: my-web-portal
  replicas: 1

  template:
    metadata:
      labels:
        run: my-web-portal

    spec:
      containers:
      - name: backend
        image: xxxxxx-backend

to get endpoints, I run command

kubectl describe service my-web-portal-svc

Command will print out the endpoints which used to connect to your pods

Type:                     LoadBalancer
IP:                       10.100.108.141
LoadBalancer Ingress:     xxxxxxxxxxxxxx.us-west-2.elb.amazonaws.com

Leave comments if you have any questions.

-- Piyapan Poomsirivilai
Source: StackOverflow