- DevOps with Kubernetes
- Hideto Saito Hui Chuan Chloe Lee Cheng Yang Wu
- 603字
- 2025-04-04 15:00:00
Preparing the environment
Firstly, kubectl has to be installed. In major Linux distributions (such as Ubuntu or CentOS), you can just search for and install the package named kubectl via the package manager. In macOS, we can choose to use Homebrew (https://brew.sh/) to install it. Homebrew is a useful package manager in macOS. We can easily install Homebrew via the /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" command. Then, we can run brew install kubernetes-cli to install kubectl via Homebrew.
Let's now start to provision a Kubernetes cluster. The easiest way to do this is to run minikube (https://github.com/kubernetes/minikube), which is a tool to run Kubernetes on a single-node locally. It can be run on Windows, Linux, and macOS. In the following example, we'll run on macOS. Minikube will launch a VM with Kubernetes installed. We'll then be able to interact with it via kubectl.
Note that minikube isn't suitable for production or any heavy-load environment. It has some limitations due to its single node nature. We'll learn how to run a real cluster in Chapter 10, Kubernetes on AWS, Chapter 11, Kubernetes on GCP, and Chapter 12, Kubernetes on Azure, instead.
Before installing minikube, we have to install some dependencies first. Minikube's official GitHub repository (https://github.com/kubernetes/minikube) lists the dependencies and drivers for the different platforms. In our case, we're using VirtualBox (https://www.virtualbox.org/) as the driver in macOS. You're free to use other drivers; visit the preceding minikube GitHub link to find more options.
After downloading and installing VirtualBox from its official website, we're ready to go. We can install minikube via brew cask install minikube:
// install minikube
# brew cask install minikube
==> Tapping caskroom/cask
==> Linking Binary 'minikube-darwin-amd64' to '/usr/local/bin/minikube'.
...
minikube was successfully installed!
After minikube is installed, we can start the cluster via the minikube start command. This will launch a Kubernetes cluster locally. At the time of writing, the default Kubernetes version that minikube v0.30.0 supports is v.1.12.0. You also can add the --kubernetes-version argument to specify the particular Kubernetes version you'd like to run. For example, assume we want to run a cluster with the version v1.12.1:
// start the cluster (via minikube v0.30.0)
#
Starting local Kubernetes v1.12.1 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Downloading kubeadm v1.12.1
Downloading kubelet v1.12.1
Finished Downloading kubeadm v1.12.1
Finished Downloading kubelet v1.12.1
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.
This will then proceed to start a VM named minikube in VirtualBox and set up the cluster via kubeadm, a Kubernetes provisioning tool. It'll also set up kubeconfig, which is a configuration file to define the context and authentication settings of the cluster. With kubeconfig, we're able to switch to different clusters via the kubectl command. We could use the kubectl config view command to see the current settings in kubeconfig:
apiVersion: v1
# cluster and certificate information
clusters:
- cluster:
certificate-authority: /Users/chloelee/.minikube/ca.crt
server: https://192.168.99.100:8443
name: minikube
# context is the combination of cluster, user and namespace
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {} # user information
users:
- name: minikube
user:
client-certificate: /Users/chloelee/.minikube/client.crt
client-key: /Users/chloelee/.minikube/client.key
Here, we're currently using the minikube context. The context is a combination of the authentication information and the cluster connection information. You could use kubectl config use-context $context to forcibly switch the context if you have more than one context.