Kubernetes and OpenShift Workshop
Image Source: https://cloudowski.com/articles/honest-review-of-openshift-4

Kubernetes and OpenShift Workshop

This marks my final article on OpenShift4.X series of articles which I started writing in December 2019. This final article is designed as a complete hands-on virtual workshop on Kubernetes and OpenShift. If you have access to an OpenShift4 cluster and 20 minutes to spare, let's dive in!

Kubernetes Refresher

A Kubernetes cluster consists of a set of worker machines, called nodes, that run containerized applications. The worker node(s) host the Pods that are the components of the application workload. The control plane manages the worker nodes and the Pods in the cluster. In production environments, the control plane usually runs across multiple computers and a cluster usually runs multiple nodes, providing fault-tolerance and high availability. The kube-api-server, or API Server, in short, is the brain of the Kubernetes cluster. In this lab, we'll be executing various Kubernetes commands/operations by using the powerful kubectl CLI which will talk to the API Server. It is possible to access the API Server via REST calls or officially supported client libraries but kubectl CLI is the most common and preferred way. [image and text reference]

No alt text provided for this image

Begin by checking your access to the Kubernetes/OpenShift cluster. If you have an OpenShift cluster, you can execute the following commands there as well.

kubectl version --short

If the above command returns the Kubernetes version, you're good to go. You're free to use any managed Kubernetes service or local Kubernetes installation for this workshop.

Deploying a microservices app on Kubernetes

The following command creates a Kubernetes Deployment called dewans-app from dewandemo/authors image (which is on Docker Hub).

kubectl create deployment dewans-app --image=index.docker.io/dewandemo/authors:v1

One of the powerful feature of Kubernetes is the ability to scale your deployment up or down. The following command scales up dewans-app deployment to three repliacas.

kubectl scale deployment dewans-app --replicas=3

Execute the following command to view the running pods:

kubectl get pods

If you don't have any previous pods running on your system, you should see three pods running.

Self-healing of Kubernetes

From the output of the last command you ran, you should see a list like below:

dewans-app-cf48574d-6cssh      1/1     Running   0          10m
dewans-app-cf48574d-c97hr      1/1     Running   0          9m4s

dewans-app-cf48574d-pqssh      1/1     Running   0          9m4s

Delete one of the pods (make sure to use your own pod name for the following command):

kubectl delete pods dewans-app-cf48574d-6cssh

After a few moments, execute the following command and you should still see three running pods.

kubectl get pods

Kubernetes deleted one of the pods but the deployment ensures a replica of three at all times so another pod was spun up.

Updating the deployment with a newer version of the image

kubectl set image deployment/dewans-app authors=index.docker.io/dewandemo/authors:v2

This is how easily you can update a deployment to use a newer (or older) image.

Deploying a Java microservices app on OpenShift

Step 1: Download the OpenShift CLI tool from this link.

Step 2: Go to your OpenShift Web Console and click on top-right corner under your profile name. Click "Copy login command". Click "Display Token" from the following page and use this to log in to your OpenShift cluster from the terminal of your computer.

Step 3: You need an OpenShift project, this is simply put equivalent to a Kubernetes namespace plus OpenShift security. A project is similar to a namespace in Kubernetes with added security. It allows a community of users to organize and manage their content in isolation from other communities.

oc new-project <your-project-name>

Step 4: OpenShift provides various options to deploy an application. For this workshop, I'll choose the simplest way, which is to deploy directly from a docker image.

oc new-app dewandemo/liberty-rest:dev

This is a Java OpenLiberty microservices application with one REST endpoint.

Once we deploy the app, we need to expose a route to the app so that the app is accessible from the public internet. An OpenShift route is a way to expose a service by giving it an externally-reachable hostname.

oc expose svc/liberty-rest

That's it! Your Java microservices application is now deployed. To ensure that the deployment pod is completed, execute the following command and observe the status.

oc get pods
No alt text provided for this image

Step 5: Switch to Developer perspective and click on Topology view. Ensure that you switch to the correct project name from the drop-down menu. Click on the arrow

No alt text provided for this image

which will open your running application on a new window. This is the OpenLiberty landing page from the base docker image. To reach the application home page, add "/LibertyProject" at the end of the URL. You should be seeing something like the following image.

No alt text provided for this image

Step 6: There's a REST endpoint for this application that shows various system properties. Append "/LibertyProject/System/properties" at the end of the URL and you should be seeing something like the following output (I used a JSON viewer).

No alt text provided for this image

Step 7: Monitoring and logging are made easy on OpenShift. Under Developer perspective, from Topology view, click on the application icon and you'll see the running pod(s) and various resources such as Builds, Services and Routes.

No alt text provided for this image

Click on View Logs to see detailed logs like the following for your application.

No alt text provided for this image

Step 8: Clean-up--> Once done, switch to the Administrator perspective and click on Home --> Projects and find your project name from the list. From the three vertical dots on the last column, click Delete Project. Confirm the name by typing it in and click Delete.

No alt text provided for this image

That's a wrap for this workshop, for this article, and my series on OpenShift4. I started publishing these articles with the sole intent to learn OpenShift myself and share my learning along the way. The journey and experience have been a blessed one and I thank you for your continuous support, feedback and inspiration along the way.





Dewan A.

Principal Developer Advocate @ Harness | ??: dewanahmed.com

4 年

Resources I'm using: 1. learn.openshift.com 2. RedHat Developers youtube channel and docs

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了