Cluster IP


Use oc explain to see available keys/fields for a service manifest.

oc explain service --recursive=true
oc explain service.spec.ports --recursive=true

Expose the myfirstreplicaset ReplicaSet to the cluster with a ClusterIP service.

Create the ClusterIP Service object manifest.

cat > service-clusterip.yaml <<EOF 
kind: Service
apiVersion: v1
  name: myfirstservice
  namespace: myproject
  type: ClusterIP
    app: myfirstapp
  - protocol: TCP
    port: 8080
    targetPort: 80

Create the ClusterIP Service.

oc create -f service-clusterip.yaml

You could have also used the kubectl expose command to create the service.

# oc expose replicaset myfirstreplicaset --name=myfirstservice --port 8080 --target-port 80

Confirm the ClusterIP service was successfully created.

oc get svc

Creating a service automatically generates an Endpoints object that manages pod IP addresses.

oc get endpoints

View the manifest for the Service resource.

oc get svc myfirstservice -o yaml

Fetch the ClusterIP IP address.

CLUSTERIP=`oc get svc myfirstservice -o jsonpath='{.spec.clusterIP}'`

Run a separate pod to verify the ClusterIP is accessible in the cluster.

oc run --image=busybox busybox --restart=Never -- sleep 6000
oc exec -it busybox -- wget -qO - http://$CLUSTERIP:8080

Service Environment Variables

All services are advertised as variables within the Pod. The downside to variables is that they do not automatically update when new services are created. You must restart the pod to see new variables.

oc exec -it busybox -- env

Connecting to a ClusterIP Service

Verify that the service can be accessed using the IP address specified in the environment variable.

oc exec -it busybox -- /bin/sh

DNS - ClusterIP

Verify that the service can be accessed with a hostname using DNS.

cat /etc/resolv.conf

Verify that the service can be accessed using the IP address specified in the environment variable.

wget -qO - myfirstservice:8080

You can also use the FQDN for the service

wget -qO - myfirstservice.myproject.svc.cluster.local:8080

Exit out of the pod

Clean Up

Let’s delete the ClusterIP service

oc delete svc myfirstservice

Delete the replicaset

oc delete replicaset myfirstreplicaset