devops:改变思维,让一切更加高效WSL

Windows WSL2开发环境,一点不逊色于Mac

2022-04-29  本文已影响0人  潘晓华Michael
ubutnu on wsl

一直使用Mac来做开发,但没想到现在Windows也这么好用。通过WSL,拥有了Windows酣畅的操作特性,又有了Linux的兼容性。作为开发环境一点不逊色于Mac,甚至更易使用。下面就操练起来吧。

WSL2

1. 安装

控制面板 -> 程序和功能 -> 启用和关闭windows 功能, 勾选如下功能:
- 适用于Linux的Windows子系统
- 虚拟机平台

确定。


功能特性

2. 升级WSL

https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package
下载 Linux 内核更新包 WSL2 Linux kernel update package for x64 machines
,并安装。

设置wsl默认版本为 `2`, PowerShell
```
wsl --set-default-version 2
```
  升级已安装的分发,可使用wsl --set-version, PowerShell
```
wsl --set-version 分发版名称 版本号
```

3. 安装windows terminal

在windows appstore中安装windows terminal

windows terminal

4. 安装Linux系统

在windows appstore中安装 ubuntu 22.04

ubuntu

并在windows terminal 中设置WSL的访问

5. WSL开机启动sshd

通过windows terminal进行ubuntu系统,并配置ssh开机启动

# /etc/init.wsl
#!/bin/bash
/etc/init.d/ssh start

5. 将端口暴露

powershell:
PS C:\Users\mpan> netsh interface portproxy add v4tov4 listenport=22 connectaddress=127.0.0.1 connectport=22 listenaddress=* protocol=tcp

如果需要删除该策略可执行

PS C:\Users\mpan> netsh interface portproxy delete v4tov4 listenport=22 protocol=tcp

zsh

  1. 安装
$ apt install zsh -y
$ chsh -s /bin/zsh
$ zsh
  1. 安装 zinit 和插件
$ bash -c "$(curl --fail --show-error --silent --location https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh)"
  1. 配置zsh
# ~/.zshrc
...
# Load powerlevel10k theme
zinit ice depth"1" # git clone depth
zinit light romkatv/powerlevel10k

# Plugin history-search-multi-word loaded with investigating.
zinit load zdharma-continuum/history-search-multi-word

# Two regular plugins loaded without investigating.
zinit light zsh-users/zsh-autosuggestions
zinit light zdharma-continuum/fast-syntax-highlighting

zinit snippet https://gist.githubusercontent.com/hightemp/5071909/raw/
zinit ice lucid wait='0'
zinit snippet https://github.com/ohmyzsh/ohmyzsh/blob/master/plugins/git/git.plugin.zsh

autoload -Uz compinit && compinit

## 解决粘贴慢的问题
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic # I wonder if you'd need `.url-quote-magic`?
}

pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish

# ~/.bashrc
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$HOME/shtool:$PATH:$HOME/go/bin:/home/mpan/.local/bin"
export BROWSER=wslview
source ${fpath[1]}/_kubectl
unsetopt prompt_cr prompt_sp

alias pbcopy="clip.exe"
alias pbpaste="powershell.exe -command 'Get-Clipboard'|head -n 1"
alias bd='ostr=`pbpaste`; echo ${ostr:0:-1}  | base64 -d'
alias b='ostr=`pbpaste`; echo ${ostr:0:-1} | base64'
alias gpush="git push origin HEAD:refs/for/main"
alias k="kubectl"
alias kcn="kc config set-context current-context --namespace "

Docker

  1. 安装并启动

下载Docker Desktop安装: Install Docker Desktop on Windows | Docker Documentation

  1. 配置

开启 WSL2作为基础引擎

  1. 验证, WSL Ubuntu
$ docker ps

Kubernetes

  1. Docker Desktop设置中开启Kubernetes,即可获得一个单节点的K8s集群。
    如果镜像无法下载,可参考: https://github.com/AliyunContainerService/k8s-for-docker-desktop 预先导入镜像

  2. 可使用其它方式部署K8S/K3S,以kind为例:
    安装 kind

$ curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
$ sudo install kind-linux-amd64 /usr/local/bin/kind

创建集群

$  kind  create cluster --name wsl2-k8s

查看集群

$ kind get clusters

删除集群

$ kind delete cluster --name wsl2-k8s

vscode

  1. 安装 https://code.visualstudio.com/
    下载vscode,并安装
  2. 安装插件
    安装插件 Remote - WSL
  3. 通过远程资源管理器,打开连接WSL的vscode windows


    连接wsl
  4. 在新窗口中选择WSL系统下的目录,开始愉快编码。

小拓展:

  1. 插件:git history, gitlens, live server
  2. 添加快捷键。 命令:清除控制台 绑定键:ctrl + k 当(条件):terminalFocus
上一篇下一篇

猜你喜欢

热点阅读