Deploy Microservices

Deploy customer

Make sure you are logged in

kubectl config current-context

and you have setup the project/namespace

kubectl create namespace tutorial
kubectl config set-context $(kubectl config current-context) --namespace=tutorial

Start deploying the microservice projects, starting with customer

Make sure istioctl is in your PATH:

istioctl version
client version: 1.4.6
control plane version: 1.4.6
data plane version: 1.4.6 (2 proxies)

Deploy Customer

You will deploy docker images that were previously built for this tutorial. If you want to build customer using Quarkus visit: Build Customer
You will deploy docker images that were previously built for this tutorial. If you want to build customer using Spring Boot visit: Build Customer Spring Boot

If you have not built the images on your own then let’s deploy the customer pod with its sidecar using the already built images for this tutorial:

kubectl apply -f <(istioctl kube-inject -f customer/kubernetes/Deployment.yml) -n tutorial
kubectl create -f customer/kubernetes/Service.yml -n tutorial

Expose customer

Since the customer service is the one our users will interact with, let’s add a Gateway that exposes that endpoint.

kubectl create -f customer/kubernetes/Gateway.yml -n tutorial
kubectl get pods -w -n tutorial
kubectl get svc istio-ingressgateway -n istio-system
istio-ingressgateway   LoadBalancer   10.101.82.250   <pending>     15020:31582/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15029:30818/TCP,15030:32542/TCP,15031:30106/TCP,15032:32284/TCP,15443:31080/TCP   19m
  • Minikube

  • OpenShift

export INGRESS_HOST=$(minikube ip -p istio)
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')

export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT
export GATEWAY_URL=$(kubectl get route istio-ingressgateway -n istio-system -o yaml | yq r - "spec.host")
If your pod fails with ImagePullBackOff, it’s possible that your current terminal isn’t using the proper Docker Environment. See Setup environment.

Wait until the status is Running and there are 2/2 pods in the Ready column. To exit, press Ctrl+C

Then test the customer endpoint

curl $GATEWAY_URL/customer

You should see the following error because the services preference and recommendation are not yet deployed.

customer => UnknownHostException: preference

Also review the logs:

stern $(kubectl get pods|grep customer|awk '{ print $1 }'|head -1) -c customer

or

stern "customer-\w" -c customer

You should see a stacktrace containing this cause:

customer-6fc99b7bfd-5st28 customer Caused by: java.net.UnknownHostException: preference

Deploy Preference

You will deploy docker images that were previously built for this tutorial. If you want to build preference with Quarkus visit: Build Preference
You will deploy docker images that were previously built for this tutorial. If you want to build preference with Spring Boot visit: Build Preference Spring Boot

If you have not built the images on your own then let’s deploy the customer pod with its sidecar using the already built images for this tutorial:

kubectl apply -f <(istioctl kube-inject -f preference/kubernetes/Deployment.yml)  -n tutorial
kubectl create -f preference/kubernetes/Service.yml -n tutorial

Wait preference to be deployed

kubectl get pods -w -n tutorial

Wait until the status is Running and there are 2/2 pods in the Ready column. To exit, press Ctrl+C

curl $GATEWAY_URL/customer

It will respond with an error since the service recommendation is not yet deployed.

We could make this a bit more resilient in a future iteration of this tutorial
customer => Error: 503 - preference => UnknownHostException: recommendation

and check out the logs

stern $(kubectl get pods|grep preference|awk '{ print $1 }'|head -1) -c preference

or

stern "preference-\w" -c preference

You should see a stacktrace containing this cause:

preference-v1-898764bdb-hz7s6 preference Caused by: java.net.UnknownHostException: recommendation

Deploy Recommendation

You will deploy docker images that were previously built for this tutorial. If you want to build recommendation with Quarkus visit: Build Recommendation
You will deploy docker images that were previously built for this tutorial. If you want to build recommendation with Spring Boot visit: Build Recommendation Spring Boot

If you have not built the images on your own then let’s deploy the customer pod with its sidecar using the already built images for this tutorial:

kubectl apply -f <(istioctl kube-inject -f recommendation/kubernetes/Deployment.yml) -n tutorial
kubectl create -f recommendation/kubernetes/Service.yml -n tutorial
kubectl get pods -w -n tutorial

Wait recommendation to be deployed

Wait until the status is Running and there are 2/2 pods in the Ready column. To exit, press Ctrl+C

curl $GATEWAY_URL/customer

it should now return

customer => preference => recommendation v1 from '99634814-sf4cl': 1

and you can monitor the recommendation logs with

stern $(kubectl get pods|grep recommendation-v1|awk '{ print $1 }'|head -1) -c recommendation

or

stern "recommendation-v1-\w" -c recommendation-v1

Updating Redeploying Code

When you wish to change code (e.g. editing the .java files) and wish to "redeploy", simply:

cd {servicename}/java/{quarkus|springboot|vertx}
vi src/main/java/com/redhat/developer/demos/{servicename}/{Servicename}{Controller|Verticle}.java

Make your changes, save it and then:

mvn clean package
docker build -t example/{servicename}:v1 .
kubectl get pods -o jsonpath='{.items[*].metadata.name}' -l app={servicename}
kubectl get pods -o jsonpath='{.items[*].metadata.name}' -l app={servicename},version=v1
kubectl delete pod -l app={servicename},version=v1 -n tutorial

Why the delete pod?

Based on the Deployment configuration, Kubernetes/OpenShift will recreate the pod, based on the new docker image as it attempts to keep the desired replicas available

kubectl describe deployment {servicename} -n tutorial{namespace-suffix} | grep Replicas