With the popularity of cloud-native technologies such as containerization, microservices, service mesh, service orchestration, and DevOps, we need to keep up with the times. So how do we get on board? At this point, we need a tool that is easy to run locally and works with Kubernetes, allowing us to easily create a standalone Kubernetes cluster in a virtual machine on your laptop, facilitating our daily development and learning with Kubernetes. Now, let's easily set up a more realistic K8s environment.
Tool Recommendations#
For local experimentation, various Kubernetes implementations can be used to run Kubernetes clusters, such as:
- Kind (https://kind.sigs.k8s.io/)
- Minikube (https://minikube.sigs.k8s.io/docs/)
- MicroK8s (https://microk8s.io/)
- Online K8s Experience (https://labs.play-with-k8s.com/)
- Dockerized (https://github.com/y0ngb1n/dockerized) Recommended my personal project, welcome to Star
The goal of using any of the above tools is to quickly run a local learning Kubernetes cluster, among which my personal favorite is Kind.
Setting Up K8s Cluster#
Next, let's try creating a standalone Kubernetes cluster with Kind and Minikube.
First, Install kubectl
#
Regardless of which tool you use, you need to correctly install the kubectl
Kubernetes command-line tool first; otherwise, you won't be able to execute kubectl
commands after installing Kind, Minikube, and other environments.
- Installation Documentation: https://kubernetes.io/zh/docs/tasks/tools/#kubectl
Create K8s Cluster Using Kind#
kind is a tool for running local Kubernetes clusters using Docker container “nodes”.
Install kind
#
Kind provides various installation methods, supporting the following:
- On macOS via Homebrew
- On macOS via MacPorts
- On Windows via Chocolatey
- Installing From Release Binaries
- Installing From Source
Here, we will install it in a Linux environment using the Installing From Release Binaries method:
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
chmod +x ./kind
# mv ./kind /some-dir-in-your-PATH/kind
Create K8s Cluster#
kind create cluster
# kind delete cluster
Verify Installation Environment#
🐋️ ~ kind get clusters
kind
Create K8s Cluster Using Minikube#
Install minikube
#
Choose the installation method for different environments, refer to https://minikube.sigs.k8s.io/docs/start/
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
In a domestic network environment, use the following command to automatically use Alibaba Cloud services to support the minikube
environment configuration, refer to https://developer.aliyun.com/article/221687
minikube start --image-mirror-country='cn'
# minikube delete
Verify Installation Environment#
🐋️ ~ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
Start the K8s dashboard, refer to https://minikube.sigs.k8s.io/docs/handbook/dashboard/
minikube dashboard
# or
minikube dashboard --url
View the list of extensions supported by minikube
, refer to https://minikube.sigs.k8s.io/docs/handbook/deploying/
minikube addons list
Verify K8s Cluster#
🐋️ ~ kubectl version
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.1", GitCommit:"5e58841cce77d4bc13713ad2b91fa0d961e69192", GitTreeState:"clean", BuildDate:"2021-05-12T14:18:45Z", GoVersion:"go1.16.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.3", GitCommit:"c92036820499fedefec0f847e2054d824aea6cd1", GitTreeState:"clean", BuildDate:"2021-10-27T18:35:25Z", GoVersion:"go1.16.9", Compiler:"gc", Platform:"linux/amd64"}
# Check the current pointed k8s environment; kind/minikube installation will automatically modify kubectl configuration
🐋️ ~ kubectl config current-context
kind-kind
# If there are multiple k8s environments locally, you can switch manually
🐋️ ~ kubectl config use-context minikube
Switched to context "minikube".
# Check server nodes
🐋️ ~ kubectl get no
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 36m v1.22.3
🐋️ ~ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
minikube Ready control-plane,master 15m v1.22.3 192.168.49.2 <none> Ubuntu 20.04.2 LTS 5.4.0-42-generic docker://20.10.8
# View k8s cluster information
🐋️ ~ kubectl cluster-info
Kubernetes control plane is running at https://192.168.49.2:8443
CoreDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
First Experience with K8s#
Quick Taste#
# Start a single instance of nginx
🐋️ ~ kubectl create deployment nginx-depl --image=nginx
# View running instances
🐋️ ~ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-depl-5c8bf76b5b-zw8ms 1/1 Running 0 70s
# Forward a local port 8080 to Pod port 80
🐋️ ~ kubectl port-forward nginx-depl-5c8bf76b5b-zw8ms 8080:80
# Access locally
🐋️ ~ curl 127.0.0.1:8080
# Clean up the instance
🐋️ ~ kubectl delete deployment nginx-depl
A Small Trial#
nginx-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
nginx-svc.yml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
ports:
- name: http
port: 80
targetPort: 80
nodePort: 31080
selector:
app: nginx
type: NodePort
Execute the command:
# One-click publish, start the instance
🐋️ ~ kubectl apply -f .
# Debug Pod
🐋️ ~ kubectl describe pod nginx
🐋️ ~ kubectl port-forward nginx 8080:80
# Debug Service
🐋️ ~ kubectl describe svc nginx-svc
🐋️ ~ kubectl port-forward service/nginx-svc 8080:80
# Clean up the instance
🐋️ ~ kubectl delete -f .
Notes#
- You need to correctly install
kubectl
first - Port-Forward allows local access to Pod, limited to local debugging environments, such as
curl 127.0.0.1:8080
- When using Service for reverse proxy, you need to access using the K8s cluster's IP, use
kubectl get nodes -o wide
to view the K8s cluster's IP - Service is a mechanism provided by K8s for reverse proxy, responsible for reverse routing + load balancing
- NodePort is a type of Service that can expose the Service to the external network
- NodePort range is
30000~32767
- Label is K8s's labeling mechanism
- Selector is the routing selection mechanism in K8s
- K8s clusters deployed using Kind or Minikube run nodes based on containers rather than the host machine; when using Service for reverse proxy, kube-proxy only takes effect in the node container, verify this by using
docker exec -it kind-control-plane bash
, not directly mapped on the host machine
K8s Troubleshooting Guide#