在cmd命令行终端或powershell中操作
PS C:\Users\cpf> wsl
提示:
适用于 Linux 的 Windows 子系统没有已安装的分发版。
可以通过访问 Microsoft Store 来安装分发版:
https://aka.ms/wslstore
PS C:\Users\cpf> wsl --help
获取命令使用帮助
PS C:\Users\cpf> wsl --set-default-version 2
提示:
有关与 WSL 2 的主要区别的信息,请访问 https://aka.ms/wsl2
操作成功完成。
PS C:\Users\cpf> wsl --update
提示:
正在安装: 适用于 Linux 的 Windows 子系统
已安装 适用于 Linux 的 Windows 子系统。
PS C:\Users\cpf> wsl
提示:
适用于 Linux 的 Windows 子系统没有已安装的分发。
使用‘wsl.exe --list --online’列出可用的分发
和 ‘wsl.exe --install ’进行安装。
PS C:\Users\cpf> wsl --list --online
无法从'https://raw.githubusercontent.com/microsoft/WSL/master/distributions/DistributionInfo.json'提取列表分发。无法解析服务器的名称或地址
Error code: Wsl/WININET_E_NAME_NOT_RESOLVED
注:科学上网后
PS C:\Users\cpf> wsl --list --online
提示如下:
以下是可安装的有效分发的列表。
使用‘wsl.exe --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
适用于 Linux 的 Windows 子系统已安装。
以下是可安装的有效分发的列表。
使用‘wsl.exe --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 -d Ubuntu
正在安装: Ubuntu
已安装 Ubuntu。
正在启动 Ubuntu...
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sP6OBe7U-1684921594607)(Windows主机中构建适用于K8S Operator开发环境.assets/image-20230209123449122.png)]
PS C:\Users\cpf> wsl --install -d Ubuntu-22.04
正在安装: Ubuntu 22.04 LTS
[==========================68.0%======== ]
PS C:\Users\cpf> wsl -l
提示如下:
适用于 Linux 的 Windows 子系统分发:
Ubuntu (默认)
PS C:\Users\cpf> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2
PS C:\Users\cpf> wsl --set-default Ubuntu
操作成功完成。
输入子系统名称,直接启动一个子系统,此步骤不是必须的。
PS C:\Users\cpf> Ubuntu
To run a command as administrator (user "root"), use "sudo " .
See "man sudo_root" for details.
msb@DESKTOP-OO4CNVS:~$
关闭所有
PS C:\Users\cpf> wsl --shutdown
关闭指定发行版本
PS C:\Users\cpf> wsl -t Ubuntu
查看是否停止
PS C:\Users\cpf> wsl -l -v
NAME STATE VERSION
* Ubuntu Stopped 2
卸载已安装
PS C:\Users\cpf> wsl --unregister Ubuntu
https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe?utm_source=docker&utm_medium=webreferral&utm_campaign=dd-smartbutton&utm_location=module
msb@DESKTOP-OO4CNVS:~$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
输出:
Cloning from https://github.com/moovweb/gvm.git to /home/msb/.gvm
No existing Go versions detected
Installed GVM v1.0.22
Please restart your terminal session or to get started right away run
`source /home/msb/.gvm/scripts/gvm
需要退出Ubuntu终端,再登录即可有gvm命令
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.
msb@DESKTOP-OO4CNVS:~$ sudo apt-get install bison gcc make
或
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
默认可以不用安装,直接把docker desktop中附加过来使用即可。
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
官方文档
https://www.jetbrains.com/help/go/how-to-use-wsl-development-environment-in-product.html#local_project
由于我们已经安装并配置好golang了,所以从这里开始就可以
https://www.jetbrains.com/help/go/how-to-use-wsl-development-environment-in-product.html#create_project_for_wsl
golang的一些环境信息可以使用此命令查看
填写项目位置
\\wsl$\Ubuntu\home\zhshl\aiops
填写GOROOT
\\wsl$\Ubuntu\home\.gvm\gos\go1.20.3
报如下错误时:
package test is not in GOROOT
root@DESKTOP-OO4CNVS:~# go env
输出如下内容:
root@DESKTOP-OO4CNVS:~# go env
GO111MODULE="" 需要修改位置
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
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
输出内容如下:
GO111MODULE="on" 修改位置
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
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
Kind是Kubernetes In Docker的缩写,顾名思义,看起来是把k8s放到docker的意思。没错,kind创建k8s集群的基本原理就是:提前准备好k8s节点的镜像,通过docker启动容器,来模拟k8s的节点,从而组成完整的k8s集群。需要注意,kind创建的集群仅可用于开发、学习、测试等,不能用于生产环境。
创建、启动k8s集群非常快速,资源消耗较低。
支持创建多节点的k8s集群,包括高可用模式。
kind 支持 Linux, macOS and Windows
它是CNCF认证的k8s集群安装方式之一
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IVDCSvZg-1684921594611)(Windows主机中构建适用于K8S Operator开发环境.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
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-740SQvVx-1684921594611)(Windows主机中构建适用于K8S Operator开发环境.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"]
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 101s v1.25.3
kind-worker2 Ready 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"
也可以创建多套K8S集群
使用–name 指定集群名称
使用kubectl config use-context 集群上下文名称即可切换集群。