Bootstrap FreeKB - OpenShift - Create ClusterIP Service
OpenShift - Create ClusterIP Service

Updated:   |  OpenShift articles

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

A route or an ingress object will provide a URL such as hello-world.apps.openshift.example.com which is used to route a request onto a service, which is then routed onto a pod, and then to the container in the pod, and finally to the application running in the container.

 

Like this.

 

The oc create service clusterip command can be used to create a ClusterIP service that will be used to route a request onto a pod.

oc create service clusterip <service name> --tcp <service port>:<pod port>

 

In this example, a ClusterIP service is created that will be used to route a request onto the pod listening on ports 8080 and 8443.

~]$ oc create service clusterip my-service --tcp 8080:8080 --tcp 8443:8443
service/my-service created

 

Or, a JSON or YAML file that contains key value pairs used to create an object, such as a config map, deployment, a project, a pod, a route, a secret, a service, et cetera. These files are known as templates. The oc explain command can be used to get the list of keys that can be used in the JSON or YAML template file.

oc explain service

 

And then more details on each key can be displayed.

oc explain service.spec

 

For example, let's say you have a YAML file named service.yml that contains the following markup.

Notice "kind: Service" and clusterIP, which means this template will be used to create a ClusterIP service.

apiVersion: v1
kind: Service
metadata:
  name: my-service     
spec:  
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  - port: 8443
    protocol: TCP
    targetPort: 8443

 

The oc apply or oc create command with the -f or --filename option can be used to add a service using the template JSON or YAML file.

The oc replace command can be used to replace a service using a new or updated template JSON or YAML file.

The oc edit command can be used to update a service template YAML file.

oc create --filename service.yml

 

The oc get services command should show the service has been created.

~]$ oc get services
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)              AGE
my-service     ClusterIP   172.30.255.64   <none>        8080/TCP,8443/TCP    23s

 

The oc describe service command can be used to display more details for the service. Notice here that Endpoints are 10.128.44.55:8080 and 10.128.44.55:8443. This means the service was successfully setup to route requests onto the container with IP address 10.128.44.55.

When a pod is restarted, the pod will get a new private (internal) IP address. The service will automatically detect the pods new private IP address so that requests continue to be routed from the service to the pod. 

~]$ oc describe service my-service
Name:              my-service
Namespace:         my-project
Labels:            app=my-service
Annotations:       <none>
Selector:          app=my-app
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                172.30.255.64
IPs:               172.30.255.64
Port:              8080/TCP
TargetPort:        8080/TCP
Endpoints:         10.128.44.55:8080
Port:              8443/TCP
TargetPort:        8443/TCP
Endpoints:         10.128.44.55:8443
Session Affinity:  None
Events:            <none>

 

A service establishes a static DNS name that is used to forward a request onto a pod and the service will always point to the private IP address and port being used by the pod.

The oc exec and curl command can be used to submit a request from a pod in the currently selected namespace to the service and the request should get routed onto the pod.

~]# oc exec pod/hello-world-1-9mzm2 -- curl my-service:8080 -v
* Connected to my-service (172.30.179.246) port 8080 (#0)
> Host: my-service:8080
< HTTP/1.1 200
Hello World

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter faad87 in the box below so that we can be sure you are a human.