Let’s try to setup a simple deploy for our application using Kubernetes. We first need to install all the tooling. To start playing with Kubernetes we’ll need to following:
Minikube: to run a kubernetes cluster locally
A hypervisor in which minikube will run: I’m using virtualbox for my tests.
Kubernetes CLI (kubectl): to talk to our cluster
Kubernetes extension for Visual Studio Code: to help us with the configuration.
You can easily install minikube and kubectl using Chocolatey.
choco install minikube choco install kubernetes-cli
VirtualBox provides an installer and you can easily install the Kubernetes tool for VS Code in the extensions tab.
If you plan to use a local repository you will need to copy the certificate to your “<user>\.minikube\certs” folder to allow you to fetch the images. Once you have everything setup we can start up minikube.
We can create a YAML file ‘newstack-deploy.yaml’ containing our deployment configuration. This will be very easy thanks to the plugin we just installed for VS Code.
In the YAML file start typing Deployment, and you will get a autocomplete option for Kubernetes. Press tab to apply it, and you’ll see a simple blueprint for a Kubernetes deployment.
Replace the default name “myapp” by “web-deploy” and replace “<image>” by [NAS_IP]:[PORT]/newstack_web, or you dockerhub account. Finally choose a port for this container
Next we’ll create a Service, a service allows you to access a deployment as an abstraction. You can configure a deployment to have multiple instances (or pods) of a container.
Simply add tree dashes to start a new configuration block. Start typing Service and tab to get a blueprint for a Service.
For this service we’ll need to add a name, and use a selector to point to the web deployment we just created. We also need to add a type “LoadBalancer” so we can access this service over the network using the IP address of the node.
Finally we need to add the ports configured within the container and expose a port we can use outside of the container “nodePort”. The nodePort has a limited range, you have to choose a value between 30000 and 32767. We’ll use 30002 for our example.
For real-live use you should use Ingres instead of a service, it allows you to have DNS names instead of the IP adress.
apiVersion: v1 kind: Service metadata: name: web-deploy-service spec: selector: app: web-deploy type: LoadBalancer ports: - port: 80 targetPort: 80 nodePort: 30002
Add 3 dashes again to we can add our final deployment, the background service.
Replace the default name “myapp” by “svc-deploy” and replace “<image>” by [NAS_IP]:[PORT]/newstack_svc, or you dockerhub account and choose a port for this container.
We don’t need a service for this deployment since it will communicate with our website using RabbitMQ which is hosted outside of the kubernetes cluster with our SQL database.
Now we are ready to apply our YAML deployment to our cluster. We’ll also get the IP address for our service to we can test our application.
kubectl apply -f .\ newstack-deploy.yaml minikube service --url web-deploy-service
Browsing to the url returned by minikube using the correct api endpoint should return an id. This means our application is running succesfully in our kubernetes cluster.
Source: Medium - Maarten De Wilde
The Tech Platform