Skip to content

Kubernetes

jbellver99 edited this page May 24, 2021 · 2 revisions

In this section we are going to look everything about Kubernetes. We will see what Kubernetes is, and explain how to install it into our machines in order to deploy our jump-the-queue application.

What is Kubernetes?

Kubernetes is a portable and extensible opensource platform used to administrate workloads and services. Kubernetes facilitates automation and the declarative configuration.

Kubernetes has various characteristics and it can be seen as a: container platform, a microservices platform or as a cloud portable platform.

We are going to be using Kubernetes for the same purposes as Docker, it can be said that in the end we are getting the same results as we did with Docker, but it is important to know about other ways to deploy our application.

ℹ️ It is important to know about Kubernetes as it is the standard for managing containers in many devops environments such as AWS and Azure.

Installing Kubernetes in our machine

There are many ways to install Kubernetes in our computer, but we are going to do this with our Docker Desktop application.

First we are going to look at a few things that we need in order to get started. The first thing is to look if we have Hyper-V enabled, we can check this by going into: Control Panel > Programs > Turn Windows features on or off > Tick the Hyper-V section. Now enter into the Docker Desktop application and head over to settings and into the Kubernetes section, and tick the "Enable Kubernetes" box and click on apply.

Now we are going to install kubectl. Click on the Link to download the latest version of kubectl.

ℹ️ kubectl is the command line utility you use to operate and manage Kubernetes.

Leave the downloaded file wherever you desire, but remember the path where it is in order to finish its configuration. Now head over to: Control Panel > Search for "Environment" > Edit environment variables for your account a new window will pop-up, now double click on the "PATH" variable and add a new one pointing to where your kubectl.exe is located, you should end up with something like this:

After doing this we are ready to start our work with Kubernetes.

ℹ️ You can check that everything is installed correctly by typing kubectl in your command line.

Deploying our Jump The Queue app

As we did before with Docker, we are going to need to create two different files, one for our Angular side and one for our Java side. Lets get started with our Angular side.

Angular deployment

First of all we will need to push our Docker image into the Docker Hub repository.

⚠️ This is not the best way to do it, but we will explain this in the next section.

Open your terminal and head into your Angular project folder C:\...\workspaces\main\jumpthequeue\angular and execute the following order:

docker tag jump-the-queue/angular:latest jbellver99/jumpthequeue-angular:latest.

What we are doing here is tagging the image we had of our angular project with our Docker Hub repository, now we are going to push this image into the Docker Hub:

docker push jbellver99/jumpthequeue-angular:latest.

ℹ️ jbellver99 refers to my repository, you should put there your Docker Hub repository.

Now that we have pushed our image into the Docker Hub repository lets create the deployment file for Kubernetes. In the same directory add a new file called angular-deployment.yaml and add the following code:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: angular-deployment
  labels:
    app: jump-the-queue-angular
spec:
  selector:
    matchLabels:
      app: jump-the-queue-angular
  template:
    metadata:
      labels:
        app: jump-the-queue-angular
    spec:
      containers:
      - name: jump-the-queue-angular
        image: jbellver99/jumpthequeue-angular
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80

What we are doing here is creating a deployment called jump-the-queue-angular which is based on the image that we just pushed into our repository jbellver99/jumpthequeue-angular.

Now inside your console execute the following order:

kubectl create -f angular-deployment.yaml

Everything should work and you should be able to access your Angular application by entering to the following URL: http://localhost:80.

ℹ️ To check if an error occurred or if the container is running you can execute the following order in your command line: kubectl get pods.

Java deployment

Now we are going to do the same thing we did before but with our Java side.

ℹ️ Remember to tag your Docker image and push it into you docker Hub repository.

Inside C:\...\workspaces\main\java add a new file called java-deployment.yaml with the following code:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-deployment
  labels:
    app: jump-the-queue-java
spec:
  selector:
    matchLabels:
      app: jump-the-queue-java
  template:
    metadata:
      labels:
        app: jump-the-queue-java
    spec:
      containers:
      - name: jump-the-queue-java
        image: jbellver99/jumpthequeue-java
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8081

Just like we did before, this is going to create a deployment called jump-the-queue-java based on our jbellver99/jumpthequeue-java image. After doing this just execute in your terminal:

kubectl create -f java-deployment.yaml

Pulling images from a private repository

As it was mentioned before, pushing the images into the Docker Hub repository is not a good idea as the images here become public for everyone and it could be the case that you do not want share with everyone your images, so we are going to use a private repository to store our images and pull them from there when needed.

Remember that in a previous chapter we talked about what is Nexus and we created a repository where we uploaded our Docker images. Now we are going to use this repository in our deployment files. First lets start our Nexus container, we should not need to configure anything as we already did this in previous chapters, and then we are going to change slightly our deployment files (angular-deployment.yaml & java-deployment.yaml).

What we need to change here are just the images, we can enter our Nexus jump-the-queue repository and see that our images are called angular and java , but to specify from where we are pulling them we will need to add localhost:8090/ as it is the URL where our Docker images are stores in our repository, the new files should look like this:

angular-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: angular-deployment
  labels:
    app: jump-the-queue-angular
spec:
  selector:
    matchLabels:
      app: jump-the-queue-angular
  template:
    metadata:
      labels:
        app: jump-the-queue-angular
    spec:
      containers:
      - name: jump-the-queue-angular
        image: localhost:8090/angular
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80

java-deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-deployment
  labels:
    app: jump-the-queue-java
spec:
  selector:
    matchLabels:
      app: jump-the-queue-java
  template:
    metadata:
      labels:
        app: jump-the-queue-java
    spec:
      containers:
      - name: jump-the-queue-java
        image: localhost:8090/java
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8081

We can now check that everything works correctly by executing the orders kubectl create -f angular-deployment.yaml and kubectl create -f java-deployment.yaml and accessing the corresponding urls in our web browser.

Creating a reverse proxy

Lastly, we are going to create a reverse proxy just as we did in the Docker chapter, but this time using Kubernetes.

We will need to create two yaml files in our reverse proxy folder (C:\...\workspaces\main\jumpthequeue\reverse-proxy). One of this files is going to create the deployments for both Angular and Java side and the services for these deployments(jump-the-queue.yaml) and the other one is going to create our reverse proxy, linking each service to a specific path (reverse-proxy.yaml).

Our jump-the-queue.yaml file is going to be similar to our angular-deployment.yaml and java-deployment.yaml, but this time both deployments will be in the same file, and we will also create two services (one for each deployment) which we will use for the reverse proxy. The file should look like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: angular
spec:
  selector:
    matchLabels:
      app: angular
  template:
    metadata:
      labels:
        app: angular
    spec:
      containers:
      - name: angular
        image: localhost:8090/angular
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80
      imagePullSecrets:
        - name: nexus-cred
---
apiVersion: v1
kind: Service
metadata:
  name: angular-svc
spec:
  selector:
    app: angular
  ports:
  - port: 80
    targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java
spec:
  selector:
    matchLabels:
      app: java
  template:
    metadata:
      labels:
        app: java
    spec:
      containers:
      - name: java
        image: localhost:8090/java
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8081
      imagePullSecrets:
        - name: nexus-cred
---
apiVersion: v1
kind: Service
metadata:
  name: java-svc
spec:
  selector:
    app: java
  ports:
  - port: 8081
    targetPort: 8081

Now lets create our reverse-proxy.yaml file, which will be of type ingress. What an ingress does is expose HTTP and HTTPS routes from outside the cluster to services within the cluster. The code of the file should look similar to this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: reverse-proxy
  labels:
      name: reverse-proxy
spec:
  rules:
  - host: localhost
    http:
      paths:
      - pathType: Prefix
        path: /
        backend:
          service:
            name: angular-svc
            port: 
              number: 80
      - pathType: Prefix
        path: /api
        backend:
          service:
            name: java-svc
            port:
              number: 8081

What we are doing here is specifying that when the route http://localhost is accessed you should be redirected into the Angular service that has been created and when the route is http://localhost/api you should be redirected into the Java service.

Now head into your console and execute kubectl create -f jump-the-queue.yaml and kubectl create -f reverse-proxy, and check that everything works correctly by accessing http://localhost to see the Angular side of the app and http://localhost/api to see the Java side.