30 Creating Services by Exposing Ports
30 Creating Services by Exposing Ports
30 Creating Services by Exposing Ports
In this lesson, we will explore how to create Kubernetes Services by exposing ports.
• Creating ReplicaSets
• Exposing a Resource
• Other Types of Services
• ClusterIP
• LoadBalancer
• ExternalName
Creating ReplicaSets #
Before we dive into services, we should create a ReplicaSet similar to the one
we used in the previous chapter. It’ll provide the Pods we can use to
demonstrate how Services work.
cat svc/go-demo-2-rs.yml
...
- name: db
image: mongo:3.3
command: ["mongod"]
args: ["--rest", "--httpinterface"]
ports:
- containerPort: 28017
protocol: TCP
...
We customized the command and the arguments so that MongoDB exposes
the REST interface. We also defined the containerPort . Those additions are
needed so that we can test that the database is accessible through the Service.
We created the ReplicaSet and retrieved its state from Kubernetes. The output
is as follows.
You might need to wait until both replicas are up-and-running. If, in your
case, the READY column does not yet have the value 2 , please wait for a while
and get the state again. We can proceed after both replicas are running.
Exposing a Resource #
We can use the kubectl expose command to expose a resource as a new
Kubernetes Service. That resource can be a Deployment, another Service, a
ReplicaSet, a ReplicationController, or a Pod. We’ll expose the ReplicaSet since
it is already running in the cluster.
Line 3: The port that should be exposed is 28017 (the port MongoDB
interface is listening to).
As a result, the target port will be exposed on every node of the cluster to the
outside world, and it will be routed to one of the Pods controlled by the
ReplicaSet.
ClusterIP #
ClusterIP (the default type) exposes the port only inside the cluster. Such a
port would not be accessible from anywhere outside. ClusterIP is useful
when we want to enable communication between Pods and still prevent any
external access.
LoadBalancer #
The LoadBalancer type is only useful when combined with cloud provider’s
load balancer.
ExternalName #
ExternalName maps a service to an external address (e.g., kubernetes.io ).
In the next lesson, we will go through the sequential breakdown of the process
of Service creation.