Make sure you are logged in
oc whoami
or
kubectl config current-context
and you have setup the project/namespace
oc new-project tutorial
or
kubectl create namespace tutorial
kubectl config set-context $(kubectl config current-context) --namespace=tutorial
oc adm policy add-scc-to-user privileged -z default -n tutorial
Then clone the git repository
git clone https://github.com/redhat-developer-demos/istio-tutorial
cd istio-tutorial
Start deploying the microservice projects, starting with customer
Make sure istioctl
is in your PATH
:
$ istioctl version
Version: 1.0.2
GitRevision: d639408fded355fb906ef2a1f9e8ffddc24c3d64
User: root@66ce69d4a51e
Hub: gcr.io/istio-release
GolangVersion: go1.10.1
BuildStatus: Clean
Note
|
Your very first Docker build will take a bit of time as it downloads all the layers. Subsequent rebuilds of the Docker image, updating only the microservice layer will be very fast. |
cd customer/java/springboot
mvn clean package
docker build -t example/customer .
docker images | grep customer
Now let’s deploy the customer pod with its sidecar
oc apply -f <(istioctl kube-inject -f ../../kubernetes/Deployment.yml) -n tutorial
oc create -f ../../kubernetes/Service.yml -n tutorial
or
kubectl apply -f <(istioctl kube-inject -f ../../kubernetes/Deployment.yml) -n tutorial
kubectl create -f ../../kubernetes/Service.yml -n tutorial
cd customer/java/springboot
oc new-app --name=customer --context-dir=customer/java/springboot -e JAEGER_SERVICE_NAME=customer JAEGER_ENDPOINT=http://jaeger-collector.istio-system.svc:14268/api/traces JAEGER_PROPAGATION=b3 JAEGER_SAMPLER_TYPE=const JAEGER_SAMPLER_PARAM=1 JAVA_OPTIONS='-Xms128m -Xmx256m -Djava.net.preferIPv4Stack=true' fabric8/s2i-java~https://github.com/redhat-developer-demos/istio-tutorial -o yaml > customer.yml
oc apply -f <(istioctl kube-inject -f customer.yml) -n tutorial
oc delete svc/customer -n tutorial; oc create -f ../../kubernetes/Service.yml -n tutorial
oc logs bc/customer -f -n tutorial
Since the customer
service is the one our users will interact with, let’s add an OpenShift Route that exposes that endpoint.
oc expose service customer -n tutorial
oc get route -n tutorial
oc get pods -w -n tutorial
or
kubectl get route -o=jsonpath='{.items[0].spec.host}'
kubectl get pods -w
Important
|
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 customer-tutorial.$(minishift ip).nip.io
You should see the following error because the services preference
and recommendation
are not yet deployed.
customer => I/O error on GET request for "http://preference:8080": preference; nested exception is java.net.UnknownHostException: preference
Also review the logs
stern customer -c customer
You should see a stacktrace containing this cause:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://preference:8080": preference; nested exception is java.net.UnknownHostException: preference
Back to the main istio-tutorial directory
cd ../../..
cd preference/java/springboot
mvn clean package
docker build -t example/preference:v1 .
docker images | grep preference
oc apply -f <(istioctl kube-inject -f ../../kubernetes/Deployment.yml) -n tutorial
oc create -f ../../kubernetes/Service.yml
or
kubectl apply -f <(istioctl kube-inject -f ../../kubernetes/Deployment.yml) -n tutorial
kubectl create -f ../../kubernetes/Service.yml
cd preference/java/springboot
oc new-app -l app=preference,version=v1 --name=preference-v1 --context-dir=preference/java/springboot -e JAEGER_SERVICE_NAME=preference JAEGER_ENDPOINT=http://jaeger-collector.istio-system.svc:14268/api/traces JAEGER_PROPAGATION=b3 JAEGER_SAMPLER_TYPE=const JAEGER_SAMPLER_PARAM=1 JAVA_OPTIONS='-Xms128m -Xmx256m -Djava.net.preferIPv4Stack=true' fabric8/s2i-java~https://github.com/redhat-developer-demos/istio-tutorial -o yaml > preference.yml
oc apply -f <(istioctl kube-inject -f preference.yml) -n tutorial
oc delete svc/preference-v1 -n tutorial; oc create -f ../../kubernetes/Service.yml -n tutorial
oc logs bc/preference-v1 -f -n tutorial
oc get pods -w -n tutorial
or
kubectl get pods -w
Wait until the status is Running
and there are 2/2
pods in the Ready
column. To exit, press Ctrl+C
curl customer-tutorial.$(minishift ip).nip.io
It will respond with an error since the service recommendation
is not yet deployed.
Note
|
We could make this a bit more resilient in a future iteration of this tutorial |
customer => 503 preference => I/O error on GET request for "http://recommendation:8080": recommendation; nested exception is java.net.UnknownHostException: recommendation
and check out the logs
stern preference -c preference
You should see a stacktrace containing this cause:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://recommendation:8080": recommendation; nested exception is java.net.UnknownHostException: recommendation
Back to the main istio-tutorial directory
cd ../../..
Important
|
The tag v1 at the end of the image name matters. We will be creating a v2 version of recommendation later in this tutorial. Having both a v1 and v2 version of the recommendation code will allow us to exercise some interesting aspects of Istio’s capabilities.
|
cd recommendation/java/vertx
mvn clean package
docker build -t example/recommendation:v1 .
docker images | grep recommendation
oc apply -f <(istioctl kube-inject -f ../../kubernetes/Deployment.yml) -n tutorial
oc create -f ../../kubernetes/Service.yml -n tutorial
oc get pods -w
or
kubectl apply -f <(istioctl kube-inject -f ../../kubernetes/Deployment.yml) -n tutorial
kubectl create -f ../../kubernetes/Service.yml
kubectl get pods -w
cd recommendation/java/vertx
oc new-app -l app=recommendation,version=v1 --name=recommendation-v1 --context-dir=recommendation/java/vertx JAVA_OPTIONS='-Xms128m -Xmx256m -Djava.net.preferIPv4Stack=true' fabric8/s2i-java~https://github.com/redhat-developer-demos/istio-tutorial -o yaml > recommendation.yml
oc apply -f <(istioctl kube-inject -f recommendation.yml) -n tutorial
oc delete svc/recommendation-v1 -n tutorial; oc create -f ../../kubernetes/Service.yml -n tutorial
oc logs bc/recommendation-v1 -f -n tutorial
Wait until the status is Running
and there are 2/2
pods in the Ready
column. To exit, press Ctrl+C
curl customer-tutorial.$(minishift ip).nip.io
it should now return
customer => preference => recommendation v1 from '99634814-sf4cl': 1
and you can monitor the recommendation
logs with
stern recommendation -c recommendation
Back to the main istio-tutorial
directory
cd ../../..
When you wish to change code (e.g. editing the .java files) and wish to "redeploy", simply:
cd {servicename}/java/{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 .
oc get pods -o jsonpath='{.items[*].metadata.name}' -l app={servicename}
oc get pods -o jsonpath='{.items[*].metadata.name}' -l app={servicename},version=v1
oc delete pod -l app={servicename},version=v1
or
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
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
oc describe deployment {servicename} | grep Replicas
or
kubectl describe deployment {servicename} | grep Replicas