Call rest api from different namespace in kubernetes

8/13/2020

I deployed a REST API application YAML in Kubernetes and I tried to access that API from another namespace. But it shows error. How to access the rest API from a different namespace. Below is my deployment YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: configuration
  labels:
    app: configuration
  namespace: restapi
spec:
  replicas: 1
  selector:
    matchLabels:
      app: configuration
  template:
    metadata:
      labels:
        app: configuration
    spec:
      containers:
      - name: configuration
        image: global.azurecr.io/config:1
        env:
        - name: AzureFunctionsJobHost__functions__0
          value: configuration
        envFrom:
        - secretRef:
            name: configuration
      imagePullSecrets:
      - name: pull

URL for API calls from other namespace is "http://configuration.restapi:80/api/configuration" I tried with .restapi in my url but its not working. I can call rest API in the same namespace.

-- Arun Thomas
kubernetes

2 Answers

8/13/2020

You can always do a simple test to check if you can reach from different namespace.

Lets say you don't have a service.

Then you can reach the pod directly using the ip like below.

kubectl run dnstest --image=busybox:1.28 --restart=Never --rm -ti -- nslookup 10-36-0-2.default.pod
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      10-36-0-2.default.pod
Address 1: 10.36.0.2
pod "dnstest" deleted

And if you expose the service like below

kubectl expose deployment configuration --port 80 -n restapi

Then the result of the test is below.

kubectl run dnstest --image=busybox:1.28 --restart=Never --rm -ti -- nslookup configuration.restapi
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      configuration.restapi
Address 1: 10.105.110.174 configuration.restapi.svc.cluster.local
pod "dnstest" deleted

You can use configuration.restapi or configuration.restapi.svc or configuration.restapi.svc.cluster.local in an standard kubernetes environment.

-- Rohit
Source: StackOverflow

8/13/2020

Assuming configuration.restapi is the name of the service you're trying to access and is the name you would use within that namespace, you would use "configuration.restapi.othernamespace.svc.cluster.local".

-- Paul Becotte
Source: StackOverflow