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
9 app: system
15 app: system
23 app: system
27 - name: system-container
29 image: system:1.0-SNAPSHOT
33 - containerPort: 9080
39 name: inventory-deployment
44 app: inventory
50 app: inventory
58 app: inventory
62 - name: inventory-container
64 image: inventory:1.0-SNAPSHOT
68 - containerPort: 9080
74 name: system-service
77 type: NodePort
81 app: system
84 - protocol: TCP
85 port: 9080
86 targetPort: 9080
88 nodePort: 31000
95 name: inventory-service
98 type: NodePort
102 app: inventory
105 - protocol: TCP
106 port: 9080
107 targetPort: 9080
109 nodePort: 32000
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
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
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
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.