Service

This follows the creation of the Deployment in the previous chapter

Make sure you are in the correct namespace:

kubectl config set-context --current --namespace=myspace

Make sure you have Deployment:

kubectl get deployments
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
quarkus-demo-deployment   3/3     3            3           8m33s

Make sure you have RS:

kubectl get rs
NAME                                 DESIRED   CURRENT   READY   AGE
quarkus-demo-deployment-5979886fb7   3         3         3       8m56s

Make sure you have Pods:

kubectl get pods
NAME                                       READY   STATUS    RESTARTS   AGE
quarkus-demo-deployment-5979886fb7-c888m   1/1     Running   0          9m17s
quarkus-demo-deployment-5979886fb7-gdtnz   1/1     Running   0          9m17s
quarkus-demo-deployment-5979886fb7-grf59   1/1     Running   0          9m17s

Create a Service

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
  name: the-service
spec:
  selector:
    app: quarkus-demo
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: LoadBalancer
EOF
watch kubectl get services
NAME          TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
the-service   LoadBalancer   172.30.103.41   <pending>     80:31974/TCP     4s

Wait until you see an external IP assigned.

On Minikube without an Ingress controller, <pending> will not become a real external IP. Optional: Setup Minikube Ingress
NAME    TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)          AGE
myapp   LoadBalancer   172.30.103.41   34.71.122.153   8080:31974/TCP   44s
  • Minikube

  • Hosted

IP=$(minikube ip -p devnation)
PORT=$(kubectl get service/the-service -o jsonpath="{.spec.ports[*].nodePort}")

If using a hosted Kubernetes cluster like OpenShift then use curl and the EXTERNAL-IP address with port 8080 or get it using kubectl:

IP=$(kubectl get service the-service -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
PORT=$(kubectl get service the-service -o jsonpath="{.spec.ports[*].port}")
If you are in AWS, you need to get the hostname instead of ip.
IP=$(kubectl get service the-service -o jsonpath="{.status.loadBalancer.ingress[0].hostname}")

Curl the Service:

curl $IP:$PORT

Results:

Supersonic Subatomic Java with Quarkus quarkus-demo-deployment-5979886fb7-grf59:1
"5979886fb7-grf59" is part of the unique id for the pod. The .java code uses System.getenv().getOrDefault("HOSTNAME", "unknown");

Ingress

cat <<EOF | kubectl apply -f -
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: myingress
spec:
  rules:
  - host: stuff-myspace.apps.gcp.burrsutter.dev
    http:
      paths:
      - path: /
        backend:
          serviceName: the-service
          servicePort: 80
EOF
curl stuff-myspace.apps.gcp.burrsutter.dev
Supersonic Subatomic Java with Quarkus quarkus-demo-deployment-5979886fb7-gdtnz:2

OpenShift Route

Delete the manually created Ingress to avoid any naming collisions. An OpenShift Route leverages ha-proxy for its default Ingress.

kubectl delete ingress myingress
oc expose service the-service
kubectl get routes
NAME          HOST/PORT                                     PATH   SERVICES      PORT   TERMINATION   WILDCARD
the-service   the-service-myspace.apps.gcp.burrsutter.dev          the-service   8080                 None

Then make a request to the service:

curl the-service-myspace.apps.gcp.burrsutter.dev
Supersonic Subatomic Java with Quarkus quarkus-demo-deployment-5979886fb7-gdtnz:3

Use jq to pull out the data elements you need for scripting

kubectl get route the-service -o json > myroutes.json

Copy and paste contents into jqplay.org

brew install jq
kubectl get route the-service -o json | jq '.spec.host'
"the-service-myspace.apps.gcp.burrsutter.dev"