11
votes

I was wondering how pods are accessed when no service is defined for that specific pod. If it's through the environment variables, how does the cluster retrieve these?

Also, when services are defined, where on the master node is it stored?

Kind regards, Charles

4
Have you looked into kubernetes documentation and maybe got some clues?Urosh T.

4 Answers

5
votes
  • If you define a service for your app , you can access it outside the cluster using that service

  • Services are of several types , including nodePort , where you can access that port on any cluster node and you will have access to the service regardless of the actual location of the pod

  • you can access the endpoints or actual pod ports inside the cluster as well , but not outside

  • all of the above uses the kubernetes service discovery

  • There are two type of service dicovery though
  • Internal Service discovery
  • External Service Discovery.

enter image description here

5
votes

You cannot "access" a pods container port(s) without a service. Services are objects that define the desired state of an ultimate set of iptable rule(s).

Also, services, like all other objects, are stored in etcd and maintained through your master(s).

You could however manually create an iptable rule forwarding traffic to the local container port that docker has exposed.

Hope this helps! If you still have any questions drop them here.

4
votes

Just for debugging purposes, you can forward a port from your machine to one in the pod:

kubectl port-forward POD_NAME HOST_PORT:POD_PORT

If you have to access it from anywhere, you should use services, but you got to have a deployment created

Create deployment

kubectl create -f https://raw.githubusercontent.com/kubernetes/website/master/content/en/examples/service/networking/run-my-nginx.yaml

Expose the deployment with a NodePort service

kubectl expose deployment deployment/my-nginx --type=NodePort --name=nginx-service

Then list the services and get the port of the service

kubectl get services | grep nginx-service
1
votes
  • All cluster data is stored in etcd which is a distributed key-value store. If etcd goes down, cluster becomes unstable and no new pods can come up.

  • Kubernetes has a way to access any pod within the cluster. Service is a logical way to access a set of pods bound by a selector. An individual pod can still be accessed irrespective of the service. Further service can be created to access the pods from outside the cluster (NodePort service)