Now that your Docker images are built, deploy them using a Kubernetes resource definition.
A Kubernetes resource definition is a yaml file that contains a description of all your deployments, services, or any other resources that you want to deploy. All resources can also be deleted from the cluster by using the same yaml file that you used to deploy them.
Create the Kubernetes configuration file in the start
directory.
kubernetes.yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4 name: system-deployment
5
6 labels:
7
8
9 app: system
10
11spec:
12 selector:
13 matchLabels:
14
15 app: system
16
17 template:
18 metadata:
19
20 labels:
21
22
23 app: system
24
25 spec:
26 containers:
27 - name: system-container
28
29 image: system:1.0-SNAPSHOT
30
31 ports:
32
33 - containerPort: 9080
34
35---
36apiVersion: apps/v1
37kind: Deployment
38metadata:
39 name: inventory-deployment
40
41 labels:
42
43
44 app: inventory
45
46spec:
47 selector:
48 matchLabels:
49
50 app: inventory
51
52 template:
53 metadata:
54
55 labels:
56
57
58 app: inventory
59
60 spec:
61 containers:
62 - name: inventory-container
63
64 image: inventory:1.0-SNAPSHOT
65
66 ports:
67
68 - containerPort: 9080
69
70---
71apiVersion: v1
72kind: Service
73metadata:
74 name: system-service
75spec:
76
77 type: NodePort
78
79 selector:
80
81 app: system
82
83 ports:
84 - protocol: TCP
85 port: 9080
86 targetPort: 9080
87
88 nodePort: 31000
89
90
91---
92apiVersion: v1
93kind: Service
94metadata:
95 name: inventory-service
96spec:
97
98 type: NodePort
99
100 selector:
101
102 app: inventory
103
104 ports:
105 - protocol: TCP
106 port: 9080
107 targetPort: 9080
108
109 nodePort: 32000
110
This file defines four Kubernetes resources. It defines two deployments and two services. A Kubernetes deployment is a resource that controls the creation and management of pods. A service exposes your deployment so that you can make requests to your containers. Three key items to look at when creating the deployments are the labels
, image
, and containerPort
fields. The labels
is a way for a Kubernetes service to reference specific deployments. The image
is the name and tag of the Docker image that you want to use for this container. Finally, the containerPort
is the port that your container exposes to access your application. For the services, the key point to understand is that they expose your deployments. The binding between deployments and services is specified by labels — in this case the app
label. You will also notice the service has a type of NodePort
. This means you can access these services from outside of your cluster via a specific port. In this case, the ports are 31000
and 32000
, but port numbers can also be randomized if the nodePort
field is not used.
Run the following commands to deploy the resources as defined in kubernetes.yaml:
kubectl apply -f kubernetes.yaml
When the apps are deployed, run the following command to check the status of your pods:
You’ll see an output similar to the following if all the pods are healthy and running:
NAME READY STATUS RESTARTS AGE
system-deployment-6bd97d9bf6-4ccds 1/1 Running 0 15s
inventory-deployment-645767664f-nbtd9 1/1 Running 0 15s
You can also inspect individual pods in more detail by running the following command:
You can also issue the kubectl get
and kubectl describe
commands on other Kubernetes resources, so feel free to inspect all other resources.
Next you will make requests to your services.
The default host name for Docker Desktop is localhost
.
The default host name for minikube is 192.168.99.100. Otherwise it can be found using the minikube ip
command.
Then, run the curl
command or visit the following URLs to access your microservices, substituting the appropriate host name:
The first URL returns system properties and the name of the pod in an HTTP header called X-Pod-Name
. To view the header, you may use the -I
option in the curl
when making a request to http://[hostname]:31000/system/properties
. The second URL adds properties from the system-service
endpoint to the inventory Kubernetes Service. Visiting http://[hostname]:32000/inventory/systems/[kube-service]
in general adds to the inventory depending on whether kube-service
is a valid Kubernetes service that can be accessed.