Achilleus
Achilleus

Reputation: 1944

Exposing Cluster ips and how services work in kubernetes services?

I am trying to understand more about on how services work in kubernetes. Consider this sample yaml file as an example.

apiVersion: v1
kind: Service
metadata:
  name: schemaregistry #1
  labels:
    name: kafka #2
    app: demo  #3
spec:
  ports:
  - port: 4000 #4
    name: landoopkafkasr #5
    targetPort: 8081 #6
  selector:
    name: landoopkafka #7
    app: demo #8

I realize that the name(#7) and the app(#8) are the things that are scanned for in the pods to match the service exposed on the targetPort exposed on that landoopkafka(#7) pod.Please correct me if I am wrong.

Basically, my understanding is that this service will expose port 8081 of the pod landoopkafka

My question was what is the significance of #1,#2,#3 and #4?

And also if I have to access port 8081 of the Pod landoopkafka from a different pod B in the k8 cluster, how do I access it?

Thank you so much.

Upvotes: 0

Views: 240

Answers (2)

Vikram Hosakote
Vikram Hosakote

Reputation: 3684

#1 is the name of the service itself seen in the output of kubectl show services.

#2 and #3 are the labels of the service that can be matched/selected by other kubernetes resources and used to list the service using:

kubectl get service -l name=kafka and

kubectl get service -l app=demo

#4 is the port 4000 exposed by the service which can be used to access the backend pods.

And also if I have to access port 8081 of the Pod landoopkafka from a different pod B in the k8 cluster, how do I access it?

Service discovery in kubernetes is done using DNS (Kube-dns). So, a different pod pod B in the cluster can access the pod landoopkafka using the name of the service as:

schemaregistry:4000 (name of the service:port exposed).

Upvotes: 0

Shahriar
Shahriar

Reputation: 13804

You can get basic information about Kubernetes Service from these

To tell you in short

Lets have a look into Pod

apiVersion: v1
kind: Pod
metadata:
  name: landoopkafka
  labels:
    name: landoopkafka
    app: demo
spec:
  containers:
  - name: kafka
    image: kafka:1.7.9
    ports:
    - containerPort: 8081

This Pod can be targeted by a Service is (usually) determined by a Label Selector

Now, you Service

apiVersion: v1
kind: Service
metadata:
  name: schemaregistry #1
  labels:
    name: kafka #2
    app: demo  #3
spec:
  ports:
  - port: 4000 #4
    name: landoopkafkasr #5
    targetPort: 8081 #6
  selector:
    name: landoopkafka #7
    app: demo #8

Here, following is the label selector to match Pod

  selector:
    name: landoopkafka #7
    app: demo #8

Here,

  • metadata.name (1) Name is primarily intended for creation idempotence and configuration definition. Its a required field.
  • metadata.labels (2,3) Map of string keys and values that can be used to organize and categorize. This labels can be used to list Service.
  • spec.ports[*].port (4) The port that will be exposed by this service. (If you want to access Pod using this Service, you need to use this port, not the port of your Pod)
  • spec.ports[*].name (5) The name of this port within the service
  • spec.ports[*].targetPort (6) Number or name of the port to access on the pods targeted by the service. (If you do not provide this, port will be used as targetPort)
  • spec.selector (7,8) Route service traffic to pods with label keys and values matching this selector. (This Map will be matched with Pod labels)

According to my examples provided, if you want to access Pod landoopkafka, you can use Service DNS as

schemaregistry:4000

Your Service is exposed with port 4000 (#4) and these traffic will be routed to 8081 (#6) port of the Pod

Upvotes: 1

Related Questions