With the popularity of cloud native technologies such as containerization, microservices, service grid, service choreography, DevOps, etc., we need to keep up with The Times, so how do we get on board? At this time you need an easy to run in the local and Kubernetes tools, you can easily create a standalone version of Kubernetes cluster on the virtual machine on your laptop, easy for us to use Kubernetes for daily development and learning. So let’s easily build a more realistic K8s environment.

Tools recommended

For local experiments, various Kubernetes implementations can also be used to run Kubernetes clusters, such as

  • Kind (kind.sigs.k8s.io/)
  • Minikube (minikube.sigs.k8s.io/docs/)
  • MicroK8s (microk8s.io/)
  • Online experience K8s (labs.play-with-k8s.com/)
  • Dockerized (github.com/y0ngb1n/doc…). To recommend my personal project, welcome Star

The goal with any of the above tools is to quickly run a locally learned Kubernetes cluster, of which my personal favorite is Kind.

Build a K8s cluster

Create Kubernetes cluster by Kind and Minikube.

First installationkubectl

Kubectl Kubernetes command line tool must be installed correctly. Otherwise, kubectl command cannot be executed after Kind, Minikube and other environments are installed.

  • Installation documentation: kubernetes. IO/useful/docs/tas…

Create a K8s cluster using Kind

Kind is a tool for running local Kubernetes clusters using Docker Container “Nodes”.

The installationkind

Kind provides a variety of installation methods, including the following:

  • On macOS via Homebrew
  • On macOS via MacPorts
  • On Windows via Chocolatey
  • Installing From Release Binaries
  • Installing From Source

In Linux, the installation will be done by Installing From Release Binaries:

The 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
Copy the code

Create a K8s cluster

kind create cluster
# kind delete cluster
Copy the code

Checking the Installation Environment

🐋️ ~ kind get clusters
kind
Copy the code

Create a K8s cluster using Minikube

The installationminikube

Installation, choose a different environment. Reference 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
Copy the code

Domestic network environment, use the following command will automatically use ali cloud services to support minikube environment configuration, reference developer.aliyun.com/article/221…

minikube start --image-mirror-country='cn'
# minikube delete
Copy the code

Checking the Installation Environment

🐋️ ~ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
Copy the code

Start K8s console, reference minikube. Sigs. K8s. IO/docs/handbo…

minikube dashboard
# or
minikube dashboard --url
Copy the code

Check minikube support the expansion of the list, reference minikube. The sigs. K8s. IO/docs/handbo…

minikube addons list
Copy the code

Verify the 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"}

Kubectl configuration will be automatically modified when Kind/Minikube is installed
🐋️ ~ kubectl config current-context
kind-kind

# If there are multiple K8S environments locally, you can manually switch
🐋️ ~ kubectl config use-context minikube
Switched to context "minikube".

Check the server node🐋️ ~ 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# Check 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'.
Copy the code

At the beginning of K8s experience

The rapid early adopters

# start singleton nginx
🐋️ ~ kubectl create deployment nginx-depl --image=nginx

# View a running instance
🐋️ ~ kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
nginx-depl-5c8bf76b5b-zw8ms   1/1     Running   0          70s

# forward a local port 8080 to port Pod 80
🐋️ ~ kubectl port-forward nginx-depl-5c8bf76b5b-zw8ms 8080:80

# local access🐋 ️ ~ curl 127.0.0.1:8080# delete instance
🐋️ ~ kubectl delete deployment nginx-depl
Copy the code

A profound

nginx-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
    - name: nginx
      image: nginx
Copy the code

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
Copy the code

Execute command:

# One-click publish, start the instance🐋️ ~ kubectl apply-f.# debugging Pod
🐋️ ~ kubectl describe pod nginx
🐋️ ~ kubectl port-forward nginx 8080:80

# debugging Service
🐋️ ~ kubectl describe svc nginx-svc
🐋️ ~ kubectl port-forward service/nginx-svc 8080:80

# delete instance🐋️ ~ kubectl delete-f.Copy the code

Matters needing attention

  • The installation must be correctkubectl
  • Pod can be accessed locally through port-forward, only in the local debugging environment, for exampleThe curl 127.0.0.1:8080
  • If the Service reverse proxy is used, you need to use the IP address of the K8s cluster for accesskubectl get nodes -o wideView the IP address of the K8s cluster
  • Service is a mechanism for K8s to provide reverse proxy, which is responsible for reverse routing and load balancing
  • NodePort is a type of Service that can be exposed to the Internet
  • NodePort range30000 ~ 32767
  • Label is a Label mechanism of K8s
  • Selector is the routing location mechanism in K8s
  • In a K8s cluster deployed by Kind or Minikube, node runs on a container instead of a host. When using Service reverse proxy, kube-proxy only takes effect in the node containerdocker exec -it kind-control-plane bashValidate this, rather than mapping directly to the host

K8s Troubleshooting Guide

Refer to the link

  • Kubernetes. IO/useful/docs/tas…
  • Kind. The sigs. K8s. IO/docs/user/q…
  • Developer.aliyun.com/article/221…
  • Learnk8s. IO/troubleshoo…
  • b23.tv/2yDbtP9
  • Youtube.com/playlist?li…