---
# System prepended metadata

title: WSL2 + docker-desktop
tags: [Windows]

---

---
title: WSL2 + docker-desktop
tags: Windows
---

# WSL2 + docker-desktop
> [name=陳信安]
> [time=TUE, OCT 06, 2020 10:03 AM]

---

# Agenda
* 何謂WSL
* 安裝WSL + 升級WSL2
* Windows Terminal OR Hyper
* zsh + oh my zsh + powerlevel10k
* 常用插件 + 優化
* docker-desktop + kubectl + kubectx + linuxbrew + htop + gcloud sdk

---

## 何謂WSL

---

WSL全名Windows Subsystem for Linux，意思是Windows的linux子系統
主要是讓開發人員無需安裝虛擬機就可以在Windows上運行Linux環境

---

## 安裝WSL + 升級WSL2

---

首先開啟Powershell(系統管理員)並執行以下命令：
##### 備註：Windows作業系統版本必須至少大於或等於Windows 10 2004
安裝WSL功能
```
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
```
![](https://i.imgur.com/5goG75D.png)

---

### 安裝WSL2功能
```
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
```
![](https://i.imgur.com/D7RvVIC.png)

---

執行 Win + R 輸入appwiz.cpl，點擊"開啟或關閉Windows功能"
![](https://i.imgur.com/dYOLKQ1.png)

---

勾選 Windows子系統Linux版 與 虛擬機器平台後重新開機
![](https://i.imgur.com/sPaOyiG.png)

---

### 安裝Linux內核

目前為止我們只是開啟了WSL2的功能，接下來安裝Linux系統內核。
[Linux kernel載點](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)
執行此命令

```
wsl --set-default-version 2
```

之後安裝的Linux版本都會安裝在WSL2中

---

### 安裝Linux 18.04 or 20.04

##### 備註：Linux建議安裝 20.04版本(較穩定)

點擊開始 -> 輸入Microsoft store -> 開啟
![](https://i.imgur.com/vwtHZ7X.png)

---

右上角搜尋Ubuntu
![](https://i.imgur.com/BkPVvNT.png)

---

選擇安裝Ubuntu 20.04 LTS
![](https://i.imgur.com/jptcs11.png)

---

安裝之後第一次打開Linux會初始化，待初始化完畢後進行設定帳號與密碼

設定完畢後執行重開機

---

接下來執行```wsl -l -v```看到Version已經是2。到這裡WSL算是完成安裝
![](https://i.imgur.com/zhAZui3.png)

---

## Windows Terminal Or Hyper(擇一)

---

Windows Terminal是Microsoft推出一款全新的Windows終端機，可以把目前Windows上的CMD、Powershell與WSL 統一使用一套終端機，透過不同的分頁去使用不同的終端機

---

Hyper是Electron推出，主要特色是擁有大量的插件、主題等等擴展性強大。

---

### Hyper[載點](https://hyper.is/#installation)

![](https://i.imgur.com/3aXTVN9.png)

---

### 主題切換
![](https://i.imgur.com/UGYW0JH.png)

---

### 安裝Windows terminal

點擊開始 -> 輸入Microsoft store -> 開啟
![](https://i.imgur.com/vwtHZ7X.png)

---

右上角搜尋Windows terminal
![](https://i.imgur.com/lwIFbWe.png)


---

選擇Windows terminal
![](https://i.imgur.com/dFYzkON.png)

---

完成畫面
![](https://i.imgur.com/OdGHjye.png)

---

## zsh + oh my zsh + powerlevel10k

---

Linux世界裡最常接觸到的就是指令，大多數的Linux版本預設帶的都是bash，但bash缺少了好看的介面與功能。如果您也是追求好看的介面那就不能錯過zsh並使用oh my zsh框架套上powerlevel10k主題 

---

### 安裝zsh

進入 Ubuntu 20.04後先進行系統更新再執行下列指令
```=
sudo apt-get install zsh -y
```
將zsh設成預設的Shell
```=
chsh -s /usr/bin/zsh
```

重新載人
```=
source ~/.zshrc
```

---

### 安裝oh my zsh

使用curl或wget下載 Oh My Zsh 並執行
* curl
```=
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```
* wget
```=
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
```

---

### 安裝 powerlevel10k

在安裝powerlevel10k前首先下載待會用到的字型，字型下載後安裝到Windows系統裡（C:\Windows\fonts)
- [MesloLGS NF Regular.ttf](https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Regular.ttf)
- [MesloLGS NF Bold.ttf](https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Bold.ttf)
- [MesloLGS NF Italic.ttf](https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Italic.ttf)
- [MesloLGS NF Bold Italic.ttf](https://github.com/romkatv/dotfiles-public/raw/master/.local/share/fonts/NerdFonts/MesloLGS%20NF%20Bold%20Italic.ttf)

---

![](https://i.imgur.com/VPMtXPT.png)

---

powerlevel10k

```=
git clone https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
```

編輯~/.zshrc檔案 
```=
vi ~/.zshrc
```

---

修改成ZSH_THEME="powerlevel10k/powerlevel10k" 與新增POWERLEVEL9K_MODE="awesome-patched"
![](https://i.imgur.com/cipL1of.png)

---

預設使用WSL，將guid複製至defaultProfile

![](https://i.imgur.com/Cx1XiTf.png)

---

使用WSL開啟Ubuntu
Windows terminal點選右上角的設定
![](https://i.imgur.com/zHTMCLe.png)

---

增加下面2段參數至設定檔
    
```
"commandline": "wsl.exe -d Ubuntu-20.04",
"fontFace": "MesloLGS NF",
```
![](https://i.imgur.com/LG3idMy.png)

---

重啓Windows terminal後開始設定風格

---

###### 備註：如需重新設定可以執行`p10k configure`
![](https://i.imgur.com/tBjm9cA.png)

---

### 快捷鍵
`Ctrl + Shift + T` 打開新標籤頁
`Ctrl + Shift + 1` 進入配置文件中定義的第一個環境（Ctrl + Shift + 2進入第二個，以此類推）
`Ctrl + Shift + W` 關閉分屏
`Alt + Shift + -` 橫向分屏
`Alt + Shift + +` 縱向分屏
`Ctrl + +` 放大
`Ctrl + -` 縮小
`Ctrl + 0` 恢復默認縮放比例


---

## 常用插件 + 優化

---

### 常用插件

zsh-syntax-highlighting（語法高亮）
```=
git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
```

zsh-autosuggestions（自動建議）
```=
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
```
###### 備註：如要提高高反差於.zshrc加入
```=
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#ff00ff,bg=cyan,bold,underline"
```

---

zsh-completions（自動補全）
```=
git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins/zsh-completions
```

---

zsh-completions插件還需把`autoload -U compinit && compinit`增加到.zshrc。輸入以下指令
```=
[ -z "`grep "autoload -U compinit && compinit" ~/.zshrc`" ] && echo "autoload -U compinit && compinit" >> ~/.zshrc
```

---

到這就剩最後一步了！將要啓用的插件增加到.zshrc。
```=
vi ~/.zshrc 新增
plugins=(git zsh-syntax-highlighting zsh-autosuggestions zsh-completions docker)

or

sed -i '/^plugins=/c\plugins=(git zsh-syntax-highlighting zsh-autosuggestions zsh-completions docker)' ~/.zshrc
```

---

## 優化

### 限制Windows terminal最大内存

在C:\Users\（使用者名稱）底下 新增.wslconfig 檔案 輸入以下

---

[wsl2]
memory=4GB
swap=8GB
localhostForwarding=true

![](https://i.imgur.com/yIZx5iW.png)

---

## docker + kubectl + kubectx + linuxbrew

---

### 安裝docker

首先到docker官網下載[docker-desktop](https://www.docker.com/get-started)
##### 備註：因目前docker-desktop最新版(2.4.0.0)與K8S版本(1.18)有相容性問題，建議先退版安裝[2.3.0.5版本](https://docs.docker.com/docker-for-windows/release-notes/)



![](https://i.imgur.com/JOgzLnp.png)

---

安裝完成後，進入Settings的General把 Use the WSL2 based engine選項打勾
![](https://i.imgur.com/F3yLlXo.png)

---

再進入Resources的WSL INTEGRATION把Enable integration with my default WSL distro打勾與點選Enable integration with additional distros
![](https://i.imgur.com/KYsNuR2.png)

---

輸入docker ps -a查看是否安裝成功
![](https://i.imgur.com/tbyBk48.png)

###### 備註：如要安裝K8S請將Kubernetes中Enable Kubernetes打勾
![](https://i.imgur.com/T8oOlAz.png)

---

### linuxbrew
linuxbrew相當於Linux下的apt-get是一個安裝包工具，可以安裝各種軟體
[linuxbrew官網](https://docs.brew.sh/Homebrew-on-Linux)

```=
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
```
---

安裝後會出現以下步驟及提示，按照提示操作即可

```=
==> Next steps:
- Run `brew help` to get started
- Further documentation:
    https://docs.brew.sh
- Install the Homebrew dependencies if you have sudo access:
    sudo apt-get install build-essential
    See https://docs.brew.sh/linux for more information
- Add Homebrew to your PATH in /home/zoechao/.zprofile:
    echo 'eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)' >> /home/zoechao/.zprofile
    eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
- We recommend that you install GCC:
    brew install gcc
```

---

```=
sudo apt-get install build-essential
```
![](https://i.imgur.com/iRUTWrx.png)

---
##### 備註：留意使用者名稱
```=
echo 'eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)' >> /home/(使用者名稱)/.zshrc

eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
```

![](https://i.imgur.com/xy9MtSW.png)

---

```=
brew install gcc
```

![](https://i.imgur.com/DkdgcLl.png)

---

### kubectl
主要是用來針對kubernetes集群運行命令
```=
brew install kubectl
```

---

### kubectx
簡易切換kubernetes集群
```=
brew install kubectx
```

---

安裝後輸入kubectx會帶出使用環境
![](https://i.imgur.com/S6NT5kM.png)

---

執行後會列出所有的kubernetes集群
![](https://i.imgur.com/4hv7QIw.png)

---

切換kubernetes集群
```=
kubectx <集群name>
```
![](https://i.imgur.com/lFXZ4G1.png)

---

### kustomize
kustomize 是 SIG-CLI 的一個子項目，其目的是為 K8s 提供一種可以重複使用配置檔的管理工具。
如果成功安裝linuxbrew，可直接執行brew的指令去安裝
```=
brew install kustomize
```
---

### htop
查看linux系統效能最常用的指令是top。但功能比較陽春與顯示資訊不足，這時侯就可以安裝htop來取代內建的top
```=
brew install htop
```

---

![](https://i.imgur.com/ke8ggbW.png)

---

說明：
PID：程序編號
USER：開啟程序的使用者名稱
PRI：Linux kernal排程優先順序,數值從0（最高優先權）到139（最低優先權）
NI：Niceness，數值從-20（最高優先權）到19（最低優先權）
VIRT：虛擬記憶體用量
RES：常駐記憶體用量
SHR：共享記憶體用量

---

S:程序狀態
- S：可中斷的睡眠
- D：不可中斷的睡眠狀態（例如IO）
- R：執行中或可執行
- T：工作停止
- t：除錯中斷
- Z：Zombie Process(殭屍程序)

---

CPU%：CPU使用率
MEM%：記憶體使用率
TIME+：程序執行時間
Comamnd：執行程序的命令

---

### figlet
使用指令創造出文字標題
```=
brew install figlet
```
輸入figlet docker(自定義文字)

![](https://i.imgur.com/bQeCzH8.png)

---

### gcloud sdk
gcp console
```=
curl https://sdk.cloud.google.com | bash
```
---

點擊ENTER
![](https://i.imgur.com/4a7I6Xa.png)

---

輸入Y進行更新
![](https://i.imgur.com/lm1HdPH.png)

---

點擊ENTER
![](https://i.imgur.com/n9FtIcN.png)

---
將下列命令加入~./zshrc
```=
# google cloud sdk path
if [ -d $HOME/.google-cloud-sdk ]; then export PATH="$HOME/.google-cloud-sdk/bin:$PATH"; fi
```

![](https://i.imgur.com/yUmLZZj.png)

