There is also option for you to define a service with no pod selector.
and then manually map the Service to the network address and port where it’s running, by adding an Endpoint object manually.
Example for your reference :
Created two pods of type nginx
$ kubectl get all -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-one 1/1 Running 0 4m56s 192.168.58.199 k8s-node02 <none> <none>
pod/nginx-two 1/1 Running 0 4m50s 192.168.85.193 k8s-node01 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 29m <none>
Create two service using below yamls, Note no Pod selector field used on yaml below
service1.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-one-service
spec:
ports:
- protocol: TCP
port: 80
service2.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-two-service
spec:
ports:
- protocol: TCP
port: 80
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 32m
nginx-one-service ClusterIP 10.102.230.78 <none> 80/TCP 7m16s
nginx-two-service ClusterIP 10.98.86.67 <none> 80/TCP 6m56s
Describe the service and no end point are mapped since we gave no selector.
ubuntu@k8s-master:~$ kubectl describe service nginx-one-service
Name: nginx-one-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.102.230.78
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
ubuntu@k8s-master:~$ kubectl describe service nginx-two-service
Name: nginx-two-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.98.86.67
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: <none>
Session Affinity: None
Events: <none>
Now you can choose to map the end point manually using below yamls.
endpoint1.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: nginx-one-service
subsets:
- addresses:
- ip: 192.168.85.193
ports:
- port: 80
endpoint2.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: nginx-two-service
subsets:
- addresses:
- ip: 192.168.85.193
ports:
- port: 80
Now get endpoint on creation
$ kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 131.160.188.46:6443 35m
nginx-one-service 192.168.58.199:80 5m30s
nginx-two-service 192.168.85.193:80 4m59s
and list the servie and endpoint should be mapped as below
ubuntu@k8s-master:~$ kubectl describe service nginx-one-service
Name: nginx-one-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.102.230.78
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 192.168.58.199:80
Session Affinity: None
Events: <none>
ubuntu@k8s-master:~$ kubectl describe service nginx-two-service
Name: nginx-two-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.98.86.67
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 192.168.85.193:80
Session Affinity: None
Events: <none>