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.
4 name: system-deployment
6 app: system
10 app: system
14 app: system
17 - name: system-container
18 image: system:1.0-SNAPSHOT
20 - containerPort: 9080
25 name: inventory-deployment
27 app: inventory
31 app: inventory
35 app: inventory
38 - name: inventory-container
39 image: inventory:1.0-SNAPSHOT
41 - containerPort: 9080
46 name: system-service
48 type: NodePort
50 app: system
52 - protocol: TCP
53 port: 9080
54 targetPort: 9080
55 nodePort: 31000
60 name: inventory-service
62 type: NodePort
64 app: inventory
66 - protocol: TCP
67 port: 9080
68 targetPort: 9080
69 nodePort: 32000
This file defines four Kubernetes resources. It defines two deployments and two services. A Kubernetes deployment is a resource responsible for controlling 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
containerPort fields. The
label 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 for purposes of accessing 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 the use of 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 will be
32000, but it 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
The default host name for minikube is 192.168.99.100. Otherwise it can be found using the
minikube ip command.
curl 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
system-service 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.