I would strongly suggest taking a glance at the service docs to make sure you're familiar with what is happening:
https://kubernetes.io/docs/concepts/services-networking/service/
A Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them - sometimes called a micro-service.
With that in mind and the guide you're using, note the following:
You can tell this is a Headless Service because the clusterIP is set to “None.” Other than that, it looks exactly the same as any normal Kubernetes Service.
So what you've created is a headless service(no load balancer or exposed IPs)
So instead of the configuration given for a headless service:
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
role: mongo
What you actually want is:
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
name: mongo
spec:
ports:
- protocol: TCP
port: 27017
targetPort: 27017
selector:
role: mongo
Very subtle but you'll notice that the clusterIP
property no longer exists.
I also prefer to specify the protocol, for completeness, even though TCP is the default.