{{announcement.body}}
{{announcement.title}}

How to Deploy a Container to The IBM Cloud Kubernetes Service

DZone 's Guide to

How to Deploy a Container to The IBM Cloud Kubernetes Service

Take a look at how you can deploy to the IBM Cloud Kubernetes service using this tutorial.

· Cloud Zone ·
Free Resource

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

Note: The #BlueCloudMirrorgame @Niklas, @Harald and I made, is available as a 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 here. IBM Cloud documentation contains a great guide for getting started.

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:

  • Setting up the IBM Cloud Kubernetes service
  • Pushing the local container image to IBM Cloud Container Registry
  • Defining the Kubernetes deployment YAML to deploy the container
  • Defining the Kubernetes service YAMLfor access the running Highscores-Service from the internet

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:

  • Kubernetes in IBM Cloud
  • Service inside Kubernetes is responsible for providing access to the scores-service over the internet, using a Loadbalancer
  • Deployment is responsible for container deployment inside the Pod.
  • Pod instantiation with the container image stored in the Container Registry

Setting up The IBM Cloud Kubernetes Service

To set up the cluster, I follow the instructions 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. 

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 the 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 yam ls 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


Get Started With The Deployment YAML Definition

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

  apiVersion: apps/v1 


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 a 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 to Access the Running Highscores-Service

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, deployment, ports, 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.

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.

Topics:
kubernetes ,ibm ,cloud ,containers ,loadbalancer ,yaml

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}