What is the URL to this Kubernetes service/pod/docker thing

8/23/2020

I need to hard code the address of a couchDB instance to another server in my kubernetes cluster. I'm not super familiar with kubernetes but I know that IP will change each time the cluster is rebuilt or the pod is rebuilt. So I can't use that.

What is the URL to this kubernetes service/what should I hard code into my Server Docker Image so it will alway find the CouchDB server in the system. I think it will be in this format

<service-name>.<namespace>.svc.cluster.local:<service-port>

Picture of the interface

# YAML for launching the server
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kino-couch
  labels:
    app: kino-couch
spec:
  serviceName: orderer
  # Single instance of the Orderer Pod is needed
  replicas: 1
  selector:
    matchLabels:
      app: kino-couch
  template:
    metadata:
      labels:
        app: kino-couch
    spec:
      containers:
      - name: kino-couch
        ports:
        - containerPort: 5984
        # Image used
        image: dpacchain/development:dpaccouch

enter image description here

If "wget 172.17.0.2:5984" works what should "172.17.0.2" be replaced with

The following is not correct

wget kino-couch-0.couch-service.default.svc.cluster.local:5984

wget kino-couch-0.couch-service.default.svc.cluster.local:5984

wget kino-couch-0.kino-couch.default.svc.cluster.local:5984

wget kino-couch-0.kino-couchdb.default.svc.cluster.local:5984

wget kino-couch-0.kino-couchdb.svc.cluster.local:5984

-- Some Guy Trying To Do Math
kubernetes

1 Answer

8/23/2020

For StatefulSet you need to create a Headless service to be responsible for the network identity of the Pods proving stable DNS entries. Notice clusterIP: None in below example.

apiVersion: v1
kind: Service
metadata:
  name: couch-service
  labels:
    app: kino-couch
spec:
  ports:
  - port: 5984
  clusterIP: None
  selector:
    app: kino-couch

The statefulset need to refer to the above service in serviceName. So the statefulset yaml would look like below

# YAML for launching the server
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kino-couch
  labels:
    app: kino-couch
spec:
  serviceName: couch-service
  # Single instance of the Orderer Pod is needed
  replicas: 1
  selector:
    matchLabels:
      app: kino-couch
  template:
    metadata:
      labels:
        app: kino-couch
    spec:
      containers:
      - name: kino-couch
        ports:
        - containerPort: 5984
        # Image used
        image: dpacchain/development:dpaccouch

Then as a client you can access it using couch-service.<namespace>.svc.cluster.local:5984 to connect to a any of the CouchDB pods.

If you want to connect to a specific pod then use kino-couch-0.couch-service.<namespace>.svc.cluster.local:5984. This is typically needed for connecting the couchDB pods between themselves to create a cluster.

-- Arghya Sadhu
Source: StackOverflow