头图

With the popularity of cloud-native technologies such as containerization, microservices, service grids, service orchestration, and DevOps, we have to keep up with the times, so how do we need to get in the car? At this point, we need a Kubernetes tool that is easy to run locally. You can easily create a stand-alone Kubernetes cluster in a virtual machine on your laptop, so that we can use Kubernetes for daily development and learning. Then let us easily build a more realistic K8s environment.

Tool recommendation

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

The goal of using any of the above tools can quickly run a locally learned Kubernetes cluster, and my personal favorite is Kind.

Build K8s cluster

Let's try Kind and Minikube to create a stand-alone Kubernetes cluster.

Install kubectl

No matter which tool you use, you must first install the kubectl kubectl command after installing the Kind, Minikube and other environments.

Use Kind to create a K8s cluster

kind is a tool for running local Kubernetes clusters using Docker container “nodes”.

Install kind

Kind provides a variety of installation methods, and supports the following methods:

Here will be installed in the Linux environment in the way of Installing From Release Binaries:

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 the installation environment

🐋️ ~ kind get clusters
kind

Use Minikube to create a K8s cluster

Install minikube

Choose the installation method in 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 the domestic network environment, using the following command will automatically use minikube Cloud services to support the environment configuration of , refer to 161bdfe8bb814e https://developer.aliyun.com/article/221687

minikube start --image-mirror-country='cn'
# minikube delete

Verify the installation environment

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

Start the K8s console, refer to https://minikube.sigs.k8s.io/docs/handbook/dashboard/

minikube dashboard
# or
minikube dashboard --url

View minikube list of supported extensions, reference 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"}

# 查看当前指向的 k8s 环境,kind/minikube 安装时会自动修改 kubectl 配置
🐋️ ~ kubectl config current-context
kind-kind

# 如本地有多个 k8s 环境,可手动切换
🐋️ ~ kubectl config use-context minikube
Switched to context "minikube".

# 检查服务器节点
🐋️ ~ 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

# 查看 k8s 集群信息
🐋️ ~ 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'.

K8s first experience

Quick taste

# 启动单实例 nginx
🐋️ ~ kubectl create deployment nginx-depl --image=nginx

# 查看运行的实例
🐋️ ~ kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
nginx-depl-5c8bf76b5b-zw8ms   1/1     Running   0          70s

# 转发一个本地 8080 端口到 Pod 80 端口
🐋️ ~ kubectl port-forward nginx-depl-5c8bf76b5b-zw8ms 8080:80

# 本地访问
🐋️ ~ curl 127.0.0.1:8080

# 清除实例
🐋️ ~ kubectl delete deployment nginx-depl

Small scale chopper

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

Excuting an order:

# 一键发布,启动实例
🐋️ ~ kubectl apply -f .

# 调试 Pod
🐋️ ~ kubectl describe pod nginx
🐋️ ~ kubectl port-forward nginx 8080:80

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

# 清除实例
🐋️ ~ kubectl delete -f .

Precautions

  • Need to install kubectl
  • Pod can be accessed locally through Port-Forward, only in the local debugging environment, such as curl 127.0.0.1:8080
  • When using the Service reverse proxy, you need to use the IP of the K8s cluster for access, and use kubectl get nodes -o wide view the IP of the K8s cluster
  • Service is a reverse proxy mechanism provided by K8s, responsible for reverse routing + load balancing
  • NodePort is a type of Service, which can expose Service to the external network
  • NodePort range 30000~32767
  • Label is a labeling mechanism of K8s
  • Selector is the routing and positioning mechanism in K8s
  • Use Kind or Minikube to deploy a K8s cluster. Node runs on a container instead of a host. When using a Service reverse proxy, the kube-proxy only takes effect in the node container. Use docker exec -it kind-control-plane bash verify this, instead of directly mapping it on the host.

K8s troubleshooting guide

a-visual-guide-on-troubleshooting-kubernetes-deployments

Reference link


y0ngb1n
50 声望3 粉丝

求关注ヾ(◍°∇°◍)ノ゙