09 Approaching Cloud Native How to Set Up a Neat Kubernetes Environment Locally

09 Approaching Cloud-Native How to Set Up a Neat Kubernetes Environment Locally #

Hello, I’m Chrono.

In the previous “Getting Started” chapter, we learned about container technology represented by Docker and made sufficient preparations. Today, let’s take a look at what container orchestration is, what Kubernetes is, and how to set up a compact and complete Kubernetes environment on your own computer. Let’s get closer to cloud native together.

What is Container Orchestration #

The core concepts of container technology are containers, images, and repositories. With these three basic elements, we can easily package and distribute applications, realizing the dream of “develop once, run anywhere”.

However, when we become proficient in container technology and confidently attempt to implement it on a large scale in a server cluster, we will find that container technology only solves a small part of the deployment work in operations and maintenance. The complexity of real production environments is much higher, requiring more than just basic installation. There are various requirements such as service discovery, load balancing, status monitoring, health checks, scaling, application migration, and high availability.

Image

Although container technology has ushered in the era of cloud native, it has taken only a small step forward. It is unable to go any further because it is no longer just about isolating one or two processes, but about isolating countless processes and solving the super problem of their communication and collaboration. The level of difficulty can be said to increase exponentially.

The management and scheduling work on top of these containers is referred to as “container orchestration”.

Container orchestration may sound quite high-end, but once you understand it, you will find that it is not that mysterious. In our last lesson, when we used Docker to deploy a WordPress website, ordering and setting up the sequence of running Nginx, WordPress, and MariaDB containers with their respective IP addresses was a basic form of “container orchestration”. However, this was a manual operation and relatively primitive.

For a few containers on a single machine, manual orchestration and scheduling can still be manageable. But when the scale reaches hundreds of servers and thousands of containers, handling the complex relationships between them must rely on computers. The de facto standard currently used for scheduling and management by computers is the protagonist of our column: Kubernetes.

What is Kubernetes #

Nowadays, when people talk about containers, they often mention Docker. However, even before Docker, Google was already using similar technologies internally (cgroups, developed by Google and contributed to the Linux kernel), just not under the name “containers”.

As the largest search engine in the world, Google has a massive cluster of servers. In order to improve resource utilization and deployment efficiency, Google developed a cluster application management system called Borg, which supports the operation of the entire company at its core.

In 2014, Google’s internal system needed to “upgrade” from Borg to Omega, and as is customary, Google published a research paper on the topic.

Because Google had previously suffered losses when publishing research papers on MapReduce, BigTable, and GFS (Yahoo’s Hadoop had already dominated the market), Google decided to take advantage of the attention Docker was receiving and simultaneously publish a research paper while rewriting the Borg system, originally written in C++, in the Go language. This is how Kubernetes was born.

Due to the strong technical foundation and high level of expertise gained from Borg’s over ten years of production environment experience, Kubernetes stirred up a sensation immediately after it was released. In 2015, Google, in collaboration with the Linux Foundation, established the Cloud Native Computing Foundation (CNCF) and donated Kubernetes as its seed project.

With the backing of two prominent families, Google and Linux, along with a tolerant and open community, Kubernetes quickly attracted many industry elites. In just two years, it defeated its competitors Apache Mesos and Docker Swarm, making it the sole dominant player in this field.

So, what can Kubernetes do for us?

In simple terms, Kubernetes is a production-grade container orchestration platform and cluster management system. It can not only create and schedule containers but also monitor and manage servers. It harnesses the collective intelligence of large companies like Google and open source communities, enabling small and medium-sized companies to easily operate and maintain a massive number of computing nodes - in other words, to have the ability of “cloud computing”.

What is minikube #

Kubernetes is generally run on large-scale computing clusters, with strict management, which poses a certain obstacle for individuals. How can you learn and use it without an actual operating environment?

Fortunately, Kubernetes takes this into account and provides some tools for quickly setting up a Kubernetes environment. Two of the recommended tools on the official website (https://kubernetes.io/zh/docs/tasks/tools/) are kind and minikube, both of which can run a complete Kubernetes environment on your local machine.

Let me share my personal thoughts on these two tools for your reference.

kind is based on Docker, meaning “Kubernetes in Docker”. It has fewer features and is easier to use, which also means it runs faster and is easier to get started with. However, it lacks many standard Kubernetes features, such as the dashboard and network plugins, and is difficult to customize. Therefore, I think it is more suitable for experienced Kubernetes users for quick development testing, but not ideal for learning and research.

Another reason for not choosing kind is that its name conflicts with the kind field in Kubernetes YAML configurations, which can confuse beginners and disrupt learning.

Now let’s look at minikube. As the name suggests, it is a “mini” version of Kubernetes. Since its release in 2016, it has been actively developed and maintained, keeping up with the latest Kubernetes versions while also being compatible with older versions (up to the previous 6 minor versions).

The biggest feature of minikube is its “small and beautiful” nature. The executable file is less than 100MB and the running image is only about 1GB. In such a small space, it integrates most of the features of Kubernetes. It not only has core container orchestration features but also rich plugins, such as the dashboard, GPU, Ingress, Istio, Kong, Registry, and more. Overall, it is very comprehensive.

Therefore, I recommend choosing minikube to learn Kubernetes in this column.

How to set up a minikube environment #

minikube supports the three mainstream platforms: Mac, Windows, and Linux. You can find detailed installation instructions on its official website (https://minikube.sigs.k8s.io). However, in our case, we will only use Linux on a virtual machine.

The latest version of minikube is 1.25.2, which supports Kubernetes version 1.23.3. Therefore, we will choose it as our learning tool for the beginner’s tutorial.

Minikube is not included in the system’s default apt/yum software repository, so we need to find the installation package online ourselves. However, since it is developed in Go language and is essentially a binary file without additional dependencies, the installation process is very simple and only requires curl or wget to download it.

The official minikube website provides installation commands for various systems, usually just two steps: download and copy. However, you need to pay attention to the hardware architecture of your computer. For Intel chips, choose the one with the “amd64” suffix, and for Apple M1 chips, choose the one with the “arm64” suffix. Choosing the wrong version may result in the inability to run due to different CPU instruction sets.

Image

I have also copied the installation commands for Linux systems from the official website here, so you can directly copy and install them:

# Intel x86_64
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

# Apple arm64
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-arm64

sudo install minikube /usr/local/bin/

After the installation, you can execute the minikube version command to check its version number and verify if the installation is successful:

minikube version

Image

However, minikube can only set up a Kubernetes environment. To operate Kubernetes, another dedicated client tool called “kubectl” is required.

kubectl plays a role similar to the tool “docker” that we used when learning container technology. It is also a command-line tool that communicates with the Kubernetes backend service to forward our commands to Kubernetes and perform container and cluster management functions.

kubectl is an independent project separate from Kubernetes and minikube. Therefore, it is not included in minikube, but minikube provides a simplified way to install it. You only need to execute the following command:

minikube kubectl

It will download the kubectl matching the current Kubernetes version and store it in the internal directory (e.g., .minikube/cache/linux/arm64/v1.23.3). Then, we can use it to issue commands to Kubernetes.

Therefore, in the minikube environment, we will use two clients: minikube to manage the Kubernetes cluster environment, and kubectl to operate the actual Kubernetes functionality. Compared to Docker, this is a bit more complicated.

I have created a simple diagram of the minikube environment to help you understand their relationship.

Image

Testing the minikube Environment #

After completing the previous steps, we can now run minikube on our local machine and create a Kubernetes experimental environment.

Using the command minikube start will pull the image from Docker Hub and start a cluster using the latest version of Kubernetes. However, to ensure consistency in the experimental environment, we can add an additional parameter --kubernetes-version to explicitly specify the Kubernetes version.

Here, I’m using “1.23.3” as the version, and the start command is:

minikube start --kubernetes-version=v1.23.3

Image

(The startup process uses lively emojis, which may be intended to make it more user-friendly. If you don’t like it, you can adjust the settings to disable it.)

Now the Kubernetes cluster is running on our local machine. You can use the commands minikube status and minikube node list to check the cluster’s status:

minikube status
minikube node list

Image

From the screenshot, you can see that there is currently only one node in the Kubernetes cluster named “minikube,” and its type is “Control Plane.” It contains three services: host, kubelet, and apiserver. The IP address is 192.168.49.2.

You can also use the command minikube ssh to log in to this node. Although it is virtual, it is similar to interacting with a physical machine:

Image

With the cluster set up, we can now use kubectl to operate and get a preliminary understanding of Kubernetes, the container orchestration system. The simplest command is, of course, to view the version:

kubectl version

However, this command cannot be used directly because the kubectl provided with minikube has a slight limitation in form and requires the minikube prefix and a -- before the command. It should be used like this:

minikube kubectl -- version

To avoid this small inconvenience, I recommend using the “alias” function of Linux to create an alias for it in the current user’s .bashrc file, like this:

alias kubectl="minikube kubectl --"

In addition, kubectl also provides command auto-completion. You should add “kubectl completion” as well:

source <(kubectl completion bash)

Now, we can use kubectl happily:

Image

Next, let’s run an Nginx application in Kubernetes. The command is similar to Docker, using run, but with a slight difference in form. We need to specify the image using --image, and Kubernetes will automatically pull and run it:

kubectl run ngx --image=nginx:alpine

This involves a very important concept in Kubernetes: a Pod. You can temporarily understand it as a container dressed in a vest. To view the list of Pods, use the kubectl get pod command, which is similar to docker ps:

Image

After executing the command, you can see that there is a Pod named ngx running in the Kubernetes cluster, indicating that our single-node minikube environment has been successfully set up.

Summary #

Alright, today we first learned about the concept of container orchestration and the history of Kubernetes. Then, we installed minikube and kubectl on a Linux virtual machine and ran a simple but complete Kubernetes cluster, achieving our “first intimate contact” with cloud-native.

So, what exactly is cloud-native? There is a clear definition on CNCF, but I think it’s too academic. I don’t want to mechanically repeat it, so I’ll share my own layman’s understanding.

The so-called “cloud” now refers to Kubernetes, so “cloud-native” means that the development, deployment, and operations of applications should all align with Kubernetes. Technologies like containers, microservices, declarative APIs, etc. are used to ensure that the entire lifecycle of an application can be smoothly carried out in a Kubernetes environment without any additional conditions.

In other words, “cloud-native” means being a “native” in Kubernetes, rather than being an “immigrant” migrated from another environment.

Finally, let’s summarize today’s content:

  1. Container technology only solves the problems of packaging and installation of applications, and it is helpless in the face of complex production environments. The solution is container orchestration, which can organize and manage the relationships between various application containers, allowing them to work together smoothly.
  2. Kubernetes originated from Google’s internal Borg system and is now the de facto standard in the field of container orchestration. minikube can build a complete Kubernetes environment on your local machine and is suitable for learning and research.
  3. Operating Kubernetes requires using the command-line tool kubectl, which is the only way to interact with a Kubernetes cluster.
  4. The usage of kubectl is similar to Docker, and it can also pull images and run them. However, it operates not just simple containers, but Pods.

Also, I want to mention the official website of Kubernetes (https://kubernetes.io/zh/). It provides very detailed documentation, including concept explanations, getting started tutorials, reference manuals, and more. The most valuable part is that it has a fully translated Chinese version, so we can read it without any language barriers. I hope you can take the time to visit the website and obtain the latest official knowledge.

Follow-up Assignment #

Finally, it’s time for the follow-up assignment. I have two questions for you to think about:

  1. How do you understand container orchestration and Kubernetes? What problems should they be able to solve?
  2. What do you think is the difference between Kubernetes and Docker?

Feel free to leave a comment and join the discussion. If you find it helpful, please share it with your friends for further study. See you in the next class.