Skip to content

Instantly share code, notes, and snippets.

@cmwylie19
Last active May 4, 2022 21:04
Show Gist options
  • Select an option

  • Save cmwylie19/655202bd0fc4a66359abcf860f6c090c to your computer and use it in GitHub Desktop.

Select an option

Save cmwylie19/655202bd0fc4a66359abcf860f6c090c to your computer and use it in GitHub Desktop.
Routing to Kubernetes services of type ExternalName in OpenShift 4.10

Test ExternalName Service across Namespaces

We will create two namespaces, mesh, which is where the service mesh resides, and the blue namespace where our apps reside.

We will create a service in the mesh namespace called mesh, and a corresponding service of type ExternalName in the blue namespace called mesh-external, representing the mesh service in the mesh namespace.

Setup namespaces

kubectl create ns mesh 
kubectl create ns blue

Create the Mesh Pod/Service in the Mesh Namespace

kubectl run mesh --image=nginx:alpine --port=80 -n mesh 
kubectl expose pod/mesh -n mesh --type=LoadBalancer # important because the Service itself needs an IP

Create the ExternalName Service in the Blue Namespace

kubectl apply -f -<<EOF
kind: Service
apiVersion: v1
metadata:
  name: mesh-external
  namespace: blue
spec:
  type: ExternalName
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  externalName: mesh.mesh.svc.cluster.local
EOF

Create the curler pod

kubectl run curler --image=nginx:alpine -n blue

Curl against the externalName service

kubectl exec -it curler -n blue -- curl mesh-external

output

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Wait & Create Route

# Wait for IP to be present
until [ -n "$(kubectl get svc/mesh -n mesh -o jsonpath='{.status.loadBalancer.ingress[0].ip}')" ]; do
    sleep 7
done

kubectl apply -f -<<EOF
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: mesh-external
  namespace: blue
spec:
  host: $(kubectl get svc mesh -n mesh -ojsonpath='{.status.loadBalancer.ingress[0].ip}')
  port:
    targetPort: http
  to:
    kind: Service
    name: mesh-external
EOF

Curl against OpenShift Route

curl $(kubectl get route mesh-external -n blue -ojsonpath='{.spec.host}')

output

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Clean Up

kubectl delete po,svc mesh --force -n mesh --grace-period=0

kubectl delete svc mesh-external -n blue --force --grace-period=0
kubectl delete pod/curler -n blue --force --grace-period=0
kubectl delete route --all -n blue

kubectl delete ns blue
kubectl delete ns mesh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment