How to deploy a container to the IBM Cloud Kubernetes service

I want to describe another way to run the  Highscores-Service of the #BlueCloudMirror game . In the game I use  IBM Cloud Foundry apps, now I want to explore IBM Cloud Kubernetes.

Note: The #BlueCloudMirror game we  @Niklas, @Harald and I made, is available as an Pattern on IBM Developer.

The first step was creating a container for the Highscores-Service,  which I described in my blog post how to build a container.

In this post I deploy my scores-service container to the IBM Cloud Kubernetes service.  This is  not a blueprint for IBM Kubernetes deployments, if you looking for a blueprint,  just take a look hereIBM Cloud documentation contains a great guide for getting started:  How to setup and use IBM Cloud Kubernetes

I share my experiences and observations with you, along with my deployment.

Let’s start with an overview of the topics I touch in this blog post:

Simplified Architecture 

kube-basic-architecture

The above picture shows a simplified architecture for the scores-service inside Kubernetes. You can see the relevant elements for the deployment. If you are interested in the details of  these elements, just click on the linked Kubernetes documentation.

The major elements:

Setting up the IBM Cloud Kubernetes service

To setup the cluster, I  follow the instruction in the IBM Cloud documentation and I choose an existing paid cluster.

Pushing the local container image to IBM Cloud Container Registry

Container images are  pushed to a container registry for better reuse and maintenance. Find out more about: how to manage your container images

To organize my scores-services images, I define a namespace called scores-services inside the  IBM Cloud Container Registry. Maybe I will also create a container for the scores services UI in the future.

Now I push the container image into the IBM Cloud Container Registry, and I will use this  image later in my deployment YAML. Therefore I use following push command:

docker push REGISTRY_NAME/NAMESPACE/IMAGE_NAME
docker push registry.eu-gb.bluemix.net/scores-services/scores-service:v1

Now I verify the result in the IBM Cloud Container Registry and you can see the uploaded container in the screenshot. push-container-registry

Defining the Kubernetes deployment YAML to deploy the Container

Now it is time to define the deployment yaml. The specifications for deployment yamls you can find in the kubernetes documentation. Usually the yaml configuration file contains specifications for deployments and services, but I want to separate these specifications  into different files, to get an better overview and understanding. In the diagram you can see a simplified dependencies overview for deployment, pod, container,  registry and port for deployment. architecture-deployment

Let’s get started with the deployment yaml definition

It starts with the definition of the kind and metadata statement.

  apiVersion: apps/v1  kind: Deployment  metadata: name: scores-service labels: app: scores-service version: "1.0" 

In the spec section, I give the container a name, and the information where the container image can be found in the Container Registry.

spec:
  containers:
  - name: scores-service
    image: "registry.eu-gb.bluemix.net/scores-services/scores-service:v1"

My specification in the yaml of the containerPort depends on the port definition inside my Dockerfile defined with EXPOSE  before. All this is related to my last blog post.

ports:
- name: scores-http
  containerPort: 3000

Simplified I would say,  labels are used in Kubernetes for the identification of elements like pods, for example.

This is full deployment yaml  file:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: scores-service
 labels:
   app: scores-service
   version: "1.0"
spec:
 replicas: 1
 selector:
  matchLabels:
   app: scores-service
 template:
   metadata:
   labels:
    app: scores-service
    version: "1.0"
  spec:
   containers:
   - name: scores-service
     image: "registry.eu-gb.bluemix.net/scores-services/scores-service:v1"
   ports:
   - name: score-http
     containerPort: 3000

To deploy the scores-service in Kubernetes I execute the kubectl apply command .
I learned: this command is usually used for the creation of deployments or services, because it also updates an existing configuration in Kubernetes. Also this command can easily be reused for reapplying configurations in a terminal.

kubectl apply -f scores-services-deployment.yaml

The gif shows the deployment, using the command line and the verification of the deployment in the Kubernetes Dashboard . By the way, the dashboard was directly opened from the IBM Cloud UI.

kube-deployment-gb

Defining the Kubernetes service YAML for access the running Highscores-Service from the internet

Let’s define the Kubernetes service to access the scores-service from the internet. I choose to use the  loadBalancer to configure this in the Kubernetes service.

The following diagram displays a simplified view of the dependencies related to the service, deploymentports and labels.

kube-service

Let’s get started with the service yaml definition

I start the definition of kind Service and metadata for the definition in the yaml file.

apiVersion: v1
kind: Service
  metadata:
    name: scores-service-lb
  labels:
   app: scores-service-lb

With the definition LoadBalancer, IBM Cloud Kubernetes creates an external LoadBalancer to provide access to the scores-service-lb and the service routes to the scores-service pod, which contains the container.

To ensure the right port of the container will be used, the selector and the targetPort defines which port is used for scores-service container in a pods. Port 80 defines the availability on the internet for scores-service-lb.

Note: With this background in mind, take a look at the diagram above again ;-).

apiVersion: v1
kind: Service
metadata:
name: scores-service-lb
labels:
   app: scores-service-lb
spec:
   type: LoadBalancer
   ports:
   - port: 80
     targetPort: 3000
     name: score-http
   selector:
     app: scores-service

Now I execute the kubectl apply command to deploy the scores-service-lb in IBM Kubernetes service.

kubectl apply -f scores-services-service-loadbalancer.yaml

The following gif shows the creation of the service in Kubernetes via the command line and the verification of the service instance in the Kubernetes Dashboard . Also tested in the gif is the access to the scores service UI from the internet is

service-loadbalancer-gb

Now the scores-service runs in Kubernetes and is available with the LoadBalancer IP address and the port 80.

Sample URL: http://loadblancer-ip-address:80

As you can see, my configuration is only for using HTTP to setup using HTTPS this will be another story.

I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

PS: By the way, you can use the IBM Cloud for free, if you simply create an IBM Lite account. Here you only need an e-mail address.

#BlueCloudMirror, #Kubernetes, #Docker #Container, #IBMDeveloper

2 thoughts on “How to deploy a container to the IBM Cloud Kubernetes service

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.