Understanding services in kubernetes?

7/9/2019

I am studying services in k8s from here

I have created service without selector and with one endpoint. What I am trying to do is I have installed apache and it's running on port 80. I have created a node port service on port 31000. Now this service should redirect ip:31000 to ip:80 port.

It is doing for internal ip of service but not on external ip.

my-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
  - protocol: TCP
    port: 9376
    targetPort: 80
    nodePort: 31000
  type: NodePort

my-endpoint.yaml

apiVersion: v1
kind: Endpoints
metadata:
  name: my-service
subsets:
  - addresses:
      - ip: <IP>
    ports:
      - port: 80

Output for kubectl get service -o wide

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE   SELECTOR
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          53m   <none>
my-service   NodePort    10.111.205.207   <none>        9376:31000/TCP   30m   <none>
-- TechChain
devops
docker
kubernetes
kubernetes-service

2 Answers

7/10/2019

First thing is, you need to run a pod inside your cluster then assign the ip of that pod inside the Endpoints yaml with port, because services exposes the pods to within or outside the cluster, we must use either selector or the address of the pod so that service can attach it self to particular pod.

apiVersion: v1
kind: Endpoints
metadata:
  name: my-service
subsets:
  - addresses:
      - ip: <ip address of the pod>
    ports:
      - port: <port of the pod>

One more thing use Statefulset in place of Deployment to run pods.

-- Arun Salaria
Source: StackOverflow

7/9/2019

There are multiple types of services which can give you different levels of access:

ClusterIP - can access the service from another pod ( only inside the Kubernetes Cluster)

NodePort - can access the service from another pod, and from the machine that it is running the Kubernetes cluster

LoadBalancer - can access the service from outside the Kubernetes cluster ( using an external IP )

LoadBalancers are great when you have a TCP level connection. If you have a higher level connection ( http ) you can also use Ingress + NodePort.

-- BogdanSucaciu
Source: StackOverflow