Build K8S application development environment based on win 10
1. wsl subsystem installation
Operate in cmd command line terminal or powershell
1.1 Confirm the Windows operating system version
1.2 Enable wsl function
1.3 wsl configuration
PS C:\Users\cpf>wsl hint: There are no installed distributions of Windows Subsystem for Linux. The distribution can be installed by visiting the Microsoft Store: https://aka.ms/wslstore
PS C:\Users\cpf> wsl --help Get command usage help
PS C:\Users\cpf> wsl --set-default-version 2 hint: For information on key differences from WSL 2, visit https://aka.ms/wsl2 The operation completed successfully.
PS C:\Users\cpf> wsl --update hint: Installing: Windows Subsystem for Linux The Windows Subsystem for Linux is installed.
PS C:\Users\cpf>wsl hint: Windows Subsystem for Linux does not have an installed distribution. Use 'wsl.exe --list --online' to list available distributions and 'wsl.exe --install <Distro>' to install.
PS C:\Users\cpf> wsl --list --online Unable to fetch list distribution from 'https://raw.githubusercontent.com/microsoft/WSL/master/distributions/DistributionInfo.json'. Could not resolve the name or address of the server Error code: Wsl/WININET_E_NAME_NOT_RESOLVED
Note: After the science goes online
PS C:\Users\cpf> wsl --list --online Tips are as follows: Below is a list of valid distributions that can be installed. Use 'wsl.exe --install <Distro>' to install. NAME FRIENDLY NAME Ubuntu Ubuntu Debian Debian GNU/Linux kali-linux Kali Linux Rolling SLES-12 SUSE Linux Enterprise Server v12 SLES-15 SUSE Linux Enterprise Server v15 Ubuntu-18.04 Ubuntu 18.04 LTS Ubuntu-20.04 Ubuntu 20.04 LTS Ubuntu-22.04 Ubuntu 22.04 LTS OracleLinux_8_5 Oracle Linux 8.5 OracleLinux_7_9 Oracle Linux 7.9
PS C:\Users\cpf> wsl --install The Windows Subsystem for Linux is installed. Below is a list of valid distributions that can be installed. Use 'wsl.exe --install <Distro>' to install. NAME FRIENDLY NAME Ubuntu Ubuntu Debian Debian GNU/Linux kali-linux Kali Linux Rolling SLES-12 SUSE Linux Enterprise Server v12 SLES-15 SUSE Linux Enterprise Server v15 Ubuntu-18.04 Ubuntu 18.04 LTS Ubuntu-20.04 Ubuntu 20.04 LTS Ubuntu-22.04 Ubuntu 22.04 LTS OracleLinux_8_5 Oracle Linux 8.5 OracleLinux_7_9 Oracle Linux 7.9
Install a distribution PS C:\Users\cpf> wsl --install -d Ubuntu Installing: Ubuntu Ubuntu is installed. Starting Ubuntu...
[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-sP6OBe7U-1684921594607) (Building on a Windows host is suitable for the K8S Operator development environment.assets/image-20230209123449122.png) ]
PS C:\Users\cpf> wsl --install -d Ubuntu-22.04 Installing: Ubuntu 22.04 LTS [=============================68.0%======== ]
PS C:\Users\cpf> wsl -l Tips are as follows: Windows Subsystem for Linux distribution: Ubuntu (default)
PS C:\Users\cpf> wsl -l -v NAME STATE VERSION * Ubuntu Stopped 2
PS C:\Users\cpf> wsl --set-default Ubuntu The operation completed successfully.
Enter the subsystem name to start a subsystem directly, this step is not necessary. PS C:\Users\cpf> Ubuntu To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. msb@DESKTOP-OO4CNVS:~$
Close all PS C:\Users\cpf> wsl --shutdown Close specific release version PS C:\Users\cpf> wsl -t Ubuntu Check if stop PS C:\Users\cpf> wsl -l -v NAME STATE VERSION * Ubuntu Stopped 2
Uninstall already installed PS C:\Users\cpf> wsl --unregister Ubuntu
2. docker-desktop installation
2.1 Download
https://desktop.docker.com/win/main/amd64/Docker Desktop Installer.exe?utm_source=docker &utm_medium=webreferral&utm_campaign=dd-smartbutton&utm_location=module
2.2 Installation and Setup
run docker desktop
3. Golang installation
3.1 Install gvm
msb@DESKTOP-OO4CNVS:~$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer) output: Cloning from https://github.com/moovweb/gvm.git to /home/msb/.gvm No existing Go versions detected Installed GVMv1.0.22 Please restart your terminal session or to get started right away run `source /home/msb/.gvm/scripts/gvm
You need to exit the Ubuntu terminal and then log in to have the gvm command
msb@DESKTOP-OO4CNVS:~$ gvm Could not find bison linux: apt-get install bison Could not find gcc linux: apt-get install gcc Could not find make linux: apt-get install make ERROR: Missing requirements.
3.2 golang installation
msb@DESKTOP-OO4CNVS:~$ sudo apt-get install bison gcc make
or
msb@DESKTOP-OO4CNVS:~$ sudo apt-get install curl git mercurial make binutils bison gcc build-essential
msb@DESKTOP-OO4CNVS:~$ gvm listall
msb@DESKTOP-OO4CNVS:~$ gvm install go1.19 -B
msb@DESKTOP-OO4CNVS:~$ gvm list gvm gos (installed) go1.19
msb@DESKTOP-OO4CNVS:~$ gvm use go1.19 Now using version go1.19
msb@DESKTOP-OO4CNVS:~$ gvm list gvm gos (installed) => go1.19
msb@DESKTOP-OO4CNVS:~$ gvm uninstall go1.19
4. kubectl installation
By default, you don’t need to install it, just attach it to docker desktop and use it.
root@DESKTOP-OO4CNVS:~# curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt) /bin/linux/amd64/kubectl"
root@DESKTOP-OO4CNVS:~# chmod +x kubectl
root@DESKTOP-OO4CNVS:~# mv kubectl /usr/local/bin/
root@DESKTOP-OO4CNVS:~# kubectl version
5. goland installation and configuration
Official documentation
https://www.jetbrains.com/help/go/how-to-use-wsl-development-environment-in-product.html#local_project
Since we have installed and configured golang, we can start from here
https://www.jetbrains.com/help/go/how-to-use-wsl-development-environment-in-product.html #create_project_for_wsl
Some environment information of golang can be viewed with this command
Fill in the project location \wsl$\Ubuntu\home\zhshl\aiops
Fill in GOROOT \wsl$\Ubuntu\home\.gvm\gos\go1.20.3
When the following error is reported: package test is not in GOROOT
root@DESKTOP-OO4CNVS:~# go env The output is as follows: root@DESKTOP-OO4CNVS:~# go env GO111MODULE="" needs to modify the location GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOIN SECURE="" GOMODCACHE="/root/.gvm/pkgsets/go1.19/global/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/.gvm/pkgsets/go1.19/global" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/root/.gvm/gos/go1.19" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/root/.gvm/gos/go1.19/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="0" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build4022425319=/tmp/go-build -gno-record-gcc- switches"
go env -w GO111MODULE=on
root@DESKTOP-OO4CNVS:~# go env The output is as follows: GO111MODULE="on" modify location GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOIN SECURE="" GOMODCACHE="/root/.gvm/pkgsets/go1.19/global/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/.gvm/pkgsets/go1.19/global" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/root/.gvm/gos/go1.19" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/root/.gvm/gos/go1.19/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="0" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build524401627=/tmp/go-build -gno-record-gcc- switches"
root@DESKTOP-OO4CNVS:~# vim aiops/test.go root@DESKTOP-OO4CNVS:~# cat aiops/test.go package main import "fmt" func main() {<!-- --> fmt.Println("hello"); }
root@DESKTOP-OO4CNVS:~# go run aiops/test.go hello
6. kind installation
Kind is the abbreviation of Kubernetes In Docker. As the name suggests, it seems to mean putting k8s into docker. That's right, kind's basic principle of creating a k8s cluster is: prepare the image of the k8s node in advance, and start the container through docker to simulate the k8s node to form a complete k8s cluster. It should be noted that the cluster created by kind can only be used for development, learning, testing, etc., and cannot be used in the production environment.
Creating and starting a k8s cluster is very fast and consumes less resources. Supports the creation of multi-node k8s clusters, including high availability mode. kind supports Linux, macOS and Windows It is one of the k8s cluster installation methods certified by CNCF
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-IVDCSvZg-1684921594611) (Building on a Windows host is suitable for the K8S Operator development environment.assets/562987-20220114201905023-573450383. png)]
root@DESKTOP-OO4CNVS:~# go install sigs.k8s.io/kind@latest
root@DESKTOP-OO4CNVS:~# kind version kind v0.17.0 go1.19 linux/amd64
7. Use kind to implement single-node K8S cluster deployment
[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-740SQvVx-1684921594611) (Building on a Windows host is suitable for the K8S Operator development environment.assets/image-20230213155110943.png) ]
kind create cluster --image kindest/node:v1.25.3
Creating cluster "kind" ... ?Ensuring node image (kindest/node:v1.25.3) ?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 Not sure what to do next? Check out https://kind.sigs.k8s.io/docs/user/quick-start/
root@DESKTOP-OO4CNVS:~# kind get clusters kind
root@DESKTOP-OO4CNVS:~# kind delete clusters kind Deleted clusters: ["kind"]
8. Use kind to implement multi-node K8S cluster deployment
root@DESKTOP-OO4CNVS:~# cat cluster.yaml kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: -role: control-plane -role: worker -role: worker
root@DESKTOP-OO4CNVS:~# kind create cluster --config cluster.yaml
Creating cluster "kind" ... ?Ensuring node image (kindest/node:v1.25.3) ? Preparing nodes ? Writing configuration ? Starting control-plane ? ? Installing CNI ? Installing StorageClass ? Joining worker nodes Set kubectl context to "kind-kind" You can now use your cluster with: kubectl cluster-info --context kind-kind Thanks for using kind!
root@DESKTOP-OO4CNVS:~# kubectl get nodes NAME STATUS ROLES AGE VERSION kind-control-plane Ready control-plane 2m5s v1.25.3 kind-worker Ready <none> 101s v1.25.3 kind-worker2 Ready <none> 88s v1.25.3
#vim cluster.yaml kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: -role: control-plane -role: worker -role: worker networking: disableDefaultCNI: true kubeProxyMode: "ipvs"
You can also create multiple sets of K8S clusters
Use –name to specify the cluster name
Use kubectl config use-context cluster context name to switch clusters.