Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build a local K8s development environment

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to build the local K8s development environment". In the daily operation, I believe many people have doubts about how to build the local K8s development environment. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to build a local K8s development environment"! Next, please follow the editor to study!

1. Dependency check

We need to check before we start.

1. Whether the Docker environment has been installed

Users who have not already installed Mac/Windows can install Docker Desktop directly.

WSL2 is recommended for Windows users

two。 Whether the Golang environment has been installed, the Go version is better > = 1.15

If not, you can refer to the official documentation for installation

Use Kind to build a local development environment 2.1 installation

If there is a local Golang environment, you can directly execute the following command to install it.

GO111MODULE= "on" go get sigs.k8s.io/kind@v0.10.0 & & kind create cluster

If you do not want to install through the source code, you can view the official documentation and install the compiled binaries directly.

Execute the following command to output the version of kind, which means that it is installed.

❯ kind version kind v0.10.0 go1.16 linux/amd642.2 to create a K8s cluster

Use the following command to create a simple single-node K8s cluster

Node create clutser

The creation time of the cluster is related to your network environment and the performance of the machine. Kind will pull the kindest/node image on docker hub, which is about 400m in size. When prompted below, the cluster has been created.

❯ kind create cluster Creating cluster "kind"... ✓ Ensuring node image (kindest/node:v1.20.2)? ✓ Preparing nodes? ✓ Writing configuration? ✓ Starting control-plane? ️✓ Installing CNI? ✓ Installing StorageClass? Set kubectl context to "kind-kind" You can now use your cluster with: kubectl cluster-info-context kind-kind Thanks for using kind!?

We can use kind get clusters to get the list of clusters we have created. Kind supports the creation of multiple clusters. If the cluster name is not specified manually, the default name is kind. We can also use-- name to specify the cluster name when creating the cluster as follows.

Kind create cluster-name mohuishou

You can see that both of our clusters have been created.

❯ kind get clusters kind mohuishou

Now we only need one cluster, so we can delete the cluster we just created using kind delete clusters mohuishou first.

2.3 using clusters

Let's take a look at whether all the common kubectl commands work, and try to deploy a simple web service.

View the cluster information ❯ kubectl cluster-info-- context kind-kind Kubernetes master is running at https://127.0.0.1:41801 KubeDNS is running at https://127.0.0.1:41801/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

You can see our K8s master address and dns address. Note that in general, we only create one cluster and we don't need to specify-- context cluster name, but this is basically a necessary command when creating multiple clusters. If you do not add this parameter, you may report the following error

The connection to the server localhost:8080 was refused-did you specify the right host or port?

This is because the default apiserver address of the kubectl connection is localhost:8080, but our apiserver address is not this, so the error is reported.

Why should we just use-- context cluster-name?

This is because kind will modify the configuration of $HOME/.kube/config when creating a cluster, and automatically write the cluster's apiserver address, certificate and other related information into it.

This parameter should be added to every command. What if it's troublesome?

We can use kubectl config use-context context-name to set it. After setting it up, we will not have to add the parameter-context every time. At the same time, we can also query which cluster we are currently operating by default through kubectl config current-context.

View the list of cluster nodes

We can find that we are deploying a single-node v1.20.2 cluster.

❯ kubectl get no NAME STATUS ROLES AGE VERSION kind-control-plane Ready control-plane,master 20m v1.20.2 deploy a Nginx service

Use the following code to create a nginx.yml file, and then use kubectl apply-f nginx.yml to complete the deployment

ApiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers:-name: nginx image: nginx:1.14.2 ports:-containerPort: 80

View the status of the current deployment

❯ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 0/3 3 0 39s

View the status of pod

❯ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-deployment-66b6c48dd5- 2s5cb 1/1 Running 0 84s nginx-deployment-66b6c48dd5- 8wf8b 1/1 Running 0 84s nginx-deployment-66b6c48dd5-zc6vd 1/1 Running 0 84s

Since we do not expose the service, we cannot directly access the corresponding service. We can use the port forwarding function provided by kubectl to forward traffic locally to the K8s cluster.

❯ kubectl port-forward nginx-deployment-66b6c48dd5-2s5cb 30080 Forwarding from 127.0.0.1 Forwarding from 30080-> 80 Forwarding from [:: 1]: 30080-> 80 Handling connection for 30080

When we visit http://localhost:30080, we will find this familiar nginx interface.

Image-20210424210804343

Here we can find that our cluster is ready for use.

2.4 Advanced use

2.4.1 create a multi-node cluster

Kind creates a single-node cluster by default. We can create a highly available cluster by modifying the configuration. We create a cluster with three master nodes and two worker nodes.

Kind create cluster-name mohuishou-ha-config kind-ha.yml

Kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes:-role: control-plane-role: worker-role: worker

You can verify it by looking at the node.

❯ kubectl-- context kind-mohuishou-ha get no NAME STATUS ROLES AGE VERSION mohuishou-ha-control-plane Ready control-plane,master 16m v1.20.2 mohuishou-ha-control-plane2 Ready control-plane,master 14m v1.20.2 mohuishou-ha-control-plane3 Ready control-plane Master 13m v1.20.2 mohuishou-ha-worker Ready 5m52s v1.20.2 mohuishou-ha-worker2 Ready 5m52s v1.20.2

2.4.2 Load Image

Generally speaking, when we deploy an application within K8s, we need to push the container image to the image repository first, which will be relatively troublesome when developing locally. Especially when the image is relatively large, there will be two round-trip network consumption. To solve this problem, we can use the kind load image feature to directly create the image.

Kind load docker-image my-custom-image-0 my-custom-image-1-name kind

Pit removal Guide: load image succeeded, but deployment pod reported an error

Failed to pull image "controller:latest": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/controller:latest": failed to resolve reference "docker.io/library/controller:latest": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed

This question has stuck me for a long time, but it's still a bit of a pit.

1. Enter the node to check whether the image exists.

Get the node name

▶ kubectl get nodes NAME STATUS ROLES AGE VERSION kind-control-plane Ready control-plane,master 2d1h v1.20.2

Enter the terminal

Docker exec-it kind-control-plane bash

Check to see if the image exists. The cluster created by kind uses containerd, so we use the crictl command to get

Crictl img | grep controller docker.io/library/controller latest 421cbf77618ba 72.1MB

two。 If the image exists, as we have seen above, check the yaml file that we deployed at this time

Kubectl-n node-pool-operator-system get deployment-o yaml | grep imagePullPolicy

Whether it exists: imagePullPolicy: Always. If we do not change the yaml, this configuration will default to this configuration. This configuration will cause the image to be pulled from the image warehouse every time, and change it to imagePullPolicy: IfNotPresent.

3. If the image does not exist: check whether the cluster-name is specified. If there are multiple clusters, it may not be loaded into the cluster we want. Just add-- name cluster-name.

Summary

Kind actually uses kubeadm when creating a cluster, so you can also modify the configuration of kubeadm to modify the default image address, node tag stains and other information. In addition, you can also configure configurations such as PV/PVC CNI plug-ins. If necessary, you can consult the official documentation of kind.

However, it should be noted that kind does not support adding nodes to a running cluster. If you need a multi-node cluster, you have to plan the number of nodes in advance.

At this point, the study on "how to build a local K8s development environment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report