In order to run a Docker container in Kubernetes you need to define a pod. The aim of this video is to show how deploy and run a containerized Go microservice using Minikube on a local developer machine.
- [Instructor] Hi and welcome back to Cloud Native Go. My name is Leander Reimer and I'm happy to be your host. For this video is about deploying our Go microservice to our locally running Kubernetes. So in this video we're going to take a look at how can write a simple YAML descriptor for a pod. I'm going to show you how you how you can connect to this pod through port forwarding. Going to work with labels and we're going to label our pods, display the labels and assign labels to the pods.
I'm going to show you how can use namespaces to organize the pods and then finally we will be stopping and removing out pods. So this is a lot of ground to cover. Let's get started. Now in the previous video I've shown you that we use Minikube, okay? And one thing you should be doing now is issue minikube docker-env. Basically what we need to do is switch out local Docker environment and point it, well to the Docker environment of our Minikube.
So if you have not done it you need to issue this command here, okay? And after this one's been evaluated we're working against the docker registry within our Minikube VM. Do docker images and so we see here there are a lot of Kubernetes images and see here, I've already prepared the image of our Cloud Native Go microservice.
Okay be sure that you're also working against the right cluster, so kube control cluster info should tell you yeah, that you're working against your local Minikube instance. Okay so let's open our editor, right. So the easiest way to deploy something to Kubernetes is via YAML or JSON files. So create a pod for our container, create a simple YAML file, okay? So this kind here tells that we want to create something pod manifest.
Specify the name metadata and maybe some labels. Now comes the interesting bit, the spec. This acts like a blueprint, okay? So now in here we specify the list of containers this pod consists of. The image, we're specifying well, the image of our little microservice we've built previously.
So container port 8080, protocol TCP. So this should be it. So let's switch to our console again. And now we use the kube control CLI, minus F, then we pass it the YAML file we just created. So you see the pod cloud native go has been created, okay? So what we can do is, we can use kube control get pods to get a list of all the pods that are running in the system.
So you see here there's one cloud native go pod running, status running, no restarts, age 24 seconds, and it's ready. To get some more detailed information you can use the kube control describe command. So kube control describe pod cloud native go. So you see this is all the information that's been created, okay? So here's the name, we're in the namespace default.
You see here the following node it's running on. You see the labels we assigned, the status is running, we see the IP address of this pod and a few other things, okay? So it's ready, it's initialized, the pod is scheduled, everything's good. Okay, so how can we access the service running within this pod? Well we use port forwarding for this. Kube control port-forward, cloud-native-go is the pod name and maybe we forward port 8080 of our pod to port 8080 on localhost.
Now this command now is blocking so what we can do again is open up our REST client, localhost 8080 API ports, we're going to send this and hurray, here's the response for all our books from our book API, from our Go microservice running within Kubernetes. Okay so let's start fresh.
What else do we have? Well, labels. Well first of all you can get the pods and show all the labels for them. Usually they're not shown. With this command for example this pattern you can print on certain environment variables or certain labels. For this one for example you can at runtime attach additional labels to a pod.
So kube control label pod cloud native go hello equals world, yes. Just to show you I'm going to attach an additional label to our pod. You see this one works. You want to display the label, kube control get pods, see here an additional label attached. So of course you can also override existing labels.
Like this kube control label pod cloud native go environment equals prod, minus minus overwrite. And we display the labels again, there you go the env is now prod. Okay so we got labels covered. So next thing are namespaces. So namespaces are a means to divide and to group things together logically, right? So think of the namespace like a package in Java or Gomi.
So let's display your available namespaces at the moment. So you have a namespace called default and a namespace called kube-system. So let's see what's in the kube-system namespace. So those are kube-dns, kubernetes-dashboard, you see here the kube-addon-manager-minikube. So those are pods running in this namespace.
So if we want to create our own namespace, what we can do is kube control create, like this. Now and if you're interested in well what does this file look like we just created? That was a YAML file again and it's very, very simple. So see kind namespace, and the metadata specifies the name for this one.
So what we now can do is kube control create minus F, create the pod again but this time we're going to create pod in the namespace cloud-native-go, okay? And because namespace is separate well, names, well if you say kube control get pods we see our cloud-native-pod in the default namespace.
Whereas with this one we see all the pods in the cloud-native-go namespace, okay? So we have two pods with the same name but running in individual namespaces. Now the good thing about namespaces is if you delete the namespace, kube control delete then everything within this namespace will also be deleted. So be careful deleting namespaces okay? Especially the default namespace.
As you see here you're not warned, it will just delete everything. So we also delete the pod we created initially and that's that. So we're done for this video. Now in the next video I'm going to show you how you can implement the deployment and service descriptors for our Go microservice so we can deploy those within Kubernetes.
All right, hope to see you then, bye bye.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Implementing Go HTTP Server
- JSON marshalling and unmarshalling of Go structs
- Implementing a simple REST API
- Using Docker workflows and commands
- Building a naïve Docker image
- Running a containerized Go microservice
- Kubernetes architecture and concepts
- Deploying a Go microservice to Kubernetes
- Implementing service descriptors
- Performing rolling updates