Navigate to the guide-istio-intro/start
directory and run the following command to build the application locally.
Next, run the docker build
commands to build the container image for your application:
docker build -t system:1.0-SNAPSHOT .
The command builds a Docker image for the system
microservice.
The -t
flag in the docker build
command allows the Docker image to be labeled (tagged) in the name[:tag]
format.
The tag for an image describes the specific image version.
If the optional [:tag]
tag is not specified, the latest
tag is created by default.
You can verify that this image was created by running the following command:
You’ll see an image called system:1.0-SNAPSHOT
listed in a table similar to the output.
REPOSITORY TAG IMAGE ID CREATED SIZE
system 1.0-SNAPSHOT 8856039f4c42 9 minutes ago 745MB
istio/proxyv2 1.20.3 7a3aaffcf645 3 weeks ago 347MB
istio/pilot 1.20.3 4974b5b22dcc 3 weeks ago 261MB
icr.io/appcafe/open-liberty kernel-slim-java11-openj9-ubi d6ef646493e1 8 days ago 729MB
To deploy the system
microservice to the Kubernetes cluster, use the following command to deploy the microservice.
kubectl apply -f system.yaml
You can see that your resources are created:
gateway.networking.istio.io/sys-app-gateway created
service/system-service created
deployment.apps/system-deployment-blue created
deployment.apps/system-deployment-green created
destinationrule.networking.istio.io/system-destination-rule created
1apiVersion: networking.istio.io/v1alpha3
2kind: Gateway
3metadata:
4 name: sys-app-gateway
5spec:
6 selector:
7 istio: ingressgateway
8 servers:
9 - port:
10 number: 80
11 name: http
12 protocol: HTTP
13 hosts:
14 - "example.com"
15 - "test.example.com"
16---
17apiVersion: v1
18kind: Service
19metadata:
20 name: system-service
21 labels:
22 app: system
23spec:
24 ports:
25 - port: 9090
26 name: http
27 selector:
28 app: system
29---
30apiVersion: apps/v1
31kind: Deployment
32metadata:
33 name: system-deployment-blue
34spec:
35 replicas: 1
36 selector:
37 matchLabels:
38 app: system
39 version: blue
40 template:
41 metadata:
42 labels:
43 app: system
44 version: blue
45 spec:
46 containers:
47 - name: system-container
48 image: system:1.0-SNAPSHOT
49 ports:
50 - containerPort: 9090
51---
52apiVersion: apps/v1
53kind: Deployment
54metadata:
55 name: system-deployment-green
56spec:
57 replicas: 1
58 selector:
59 matchLabels:
60 app: system
61 version: green
62 template:
63 metadata:
64 labels:
65 app: system
66 version: green
67 spec:
68 containers:
69 - name: system-container
70 image: system:1.0-SNAPSHOT
71 ports:
72 - containerPort: 9090
73---
74apiVersion: networking.istio.io/v1alpha3
75kind: DestinationRule
76metadata:
77 name: system-destination-rule
78spec:
79 host: system-service
80 subsets:
81 - name: blue
82 labels:
83 version: blue
84 - name: green
85 labels:
86 version: green
View the system.yaml
file. It contains two deployments
, a service
, a gateway
, and a destination rule
. One of the deployments is labeled blue
and the second deployment is labeled green
. The service points to both of these deployments. The Istio gateway is the entry point for HTTP requests to the cluster. A destination rule is used to apply policies post-routing, in this situation it is used to define service subsets that can be specifically routed to.
1apiVersion: networking.istio.io/v1alpha3
2kind: VirtualService
3metadata:
4 name: system-virtual-service
5spec:
6 hosts:
7 - "example.com"
8 gateways:
9 - sys-app-gateway
10 http:
11 - route:
12 - destination:
13 port:
14 number: 9090
15 host: system-service
16 subset: blue
17 weight: 100
18 - destination:
19 port:
20 number: 9090
21 host: system-service
22 subset: green
23 weight: 0
24---
25apiVersion: networking.istio.io/v1alpha3
26kind: VirtualService
27metadata:
28 name: system-test-virtual-service
29spec:
30 hosts:
31 - "test.example.com"
32 gateways:
33 - sys-app-gateway
34 http:
35 - route:
36 - destination:
37 port:
38 number: 9090
39 host: system-service
40 subset: blue
41 weight: 0
42 - destination:
43 port:
44 number: 9090
45 host: system-service
46 subset: green
47 weight: 100
View the traffic.yaml
file. It contains two virtual services. A virtual service defines how requests are routed to your applications. In the virtual services, you can configure the weight, which controls the amount of traffic going to each deployment. In this case, the weights should be 100 or 0, which corresponds to which deployment is live.
Deploy the resources defined in the traffic.yaml
file.
kubectl apply -f traffic.yaml
You can see that the virtual services have been created.
virtualservice.networking.istio.io/system-virtual-service created
virtualservice.networking.istio.io/system-test-virtual-service created
You can check that all of the deployments are available by running the following command.
The command produces a list of deployments for your microservices that is similar to the following output.
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
system-deployment-blue 1 1 1 1 1m
system-deployment-green 1 1 1 1 1m
After all the deployments are available, you will make a request to version 1 of the deployed application. As defined in the system.yaml
, file the gateway
is expecting the host to be example.com
. However, requests to example.com
won’t be routed to the appropriate IP address. To ensure that the gateway routes your requests appropriately, ensure that the Host header is set to example.com
. For instance, you can set the Host
header with the -H
option of the curl
command.
Make a request to the service by running the following curl
command.
curl -H "Host:example.com" -I http://localhost/system/properties
If the curl
command is unavailable, then use Postman. Postman enables you
to make requests using a graphical interface. To make a request with Postman, enter http://localhost/system/properties
into the URL bar. Next, switch to the Headers
tab and add a header with key of Host
and value of example.com
.
Finally, click the blue Send
button to make the request.
curl -H "Host:example.com" -I http://localhost/system/properties
If the curl
command is unavailable, then use Postman. Postman enables you
to make requests using a graphical interface. To make a request with Postman, enter http://localhost/system/properties
into the URL bar. Next, switch to the Headers
tab and add a header with key of Host
and value of example.com
.
Finally, click the blue Send
button to make the request.
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
curl -H "Host:example.com" -I http://`minikube ip`:$INGRESS_PORT/system/properties
You’ll see a header called x-app-version
along with the corresponding version.
x-app-version: 1.0-SNAPSHOT