Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucebentley/fbce61ad5b6f0e4de53c7d632dbb8d03 to your computer and use it in GitHub Desktop.
Save brucebentley/fbce61ad5b6f0e4de53c7d632dbb8d03 to your computer and use it in GitHub Desktop.
Setup your Windows 10 & WSL development environment(s) so that you're able to use all the various Kubernetes tooling.

Setting Up The Kubernetes Tooling On Windows 10 WSL

Nowadays software is build mostly targeting Linux. Life is great as a Mac OS user since we can pretty much install everything through brew. Unfortunately thing start going pear shape when we have Windows 10 machines as part of the team and the shared tooling just stops working.

The following post describes how to setup your Windows 10 & WSL development environment(s) so that you're able to use all the various Kubernetes tooling.

STEP 1:

Set up an Ubuntu WSL machine as described below:

Enable WSL and Ubuntu 16.04 on Windows 10

STEP 2:

Install the Kube and Docker tools on Ubuntu WSL:

Installing Kubernetes and Docker tools on Windows 10 Ubuntu WSL

STEP 3:

Connect Kubectl and Docker to the Windows side of things:

Connecting Docker cli and Kubectl to Docker Desktop for Windows

STEP 4:

Celebrate! 🎉

Connecting Docker CLI & Kubectl To Docker Desktop For Windows

Install Kubectl, Docker, Helm, etc.

View details here: https://gist.github.com/xynova/87beae35688476efb2ee290d3926f5bb

Setup Docker

References:

  1. Open Docker settings dialog.

  2. Under general, tick the Expose daemon on tcp://localhost2375 option so that we can connect to Docker Daemon from the WSL Ubuntu VM. You can later explore options on how to secure the connection even further if you so desire: https://hub.docker.com/r/stefanscherer/dockertls-windows/.

  3. Make sure you share your C drive so that you can run containers with volumes

  4. Definitely give Docker Daemon more memory. Maybe 6GB or more?

  5. Enable Kubernetes (if you are not planning to use Minikube). Just tick the Enable Kubernetes option.

IMPORTANT: Make sure you select the docker-for-desktop context through the Docker Desktop for Windows menu so that the .kube/config file appropriately configured.

Configure Docker cli

Back in your WSL ubuntu session configure the cli to talk to the Docker Daemon:

echo "export DOCKER_HOST=tcp://localhost:2375" >> ~/.bashrc && source ~/.bashrc

Make sure connectivity to the Docker daemon work:

docker run --rm -ti hello-world

Configure Ubuntu to mount windows drives at /c or /e instead of /mnt/c or /mnt/e. The options = "metadata" line is not necessary but it will fix folder and file permissions on WSL mounts so everything isn’t 777 all the time within the WSL mounts:

cat << EOF | sudo tee /etc/wsl.conf
[automount]
root = /
options = "metadata"
EOF

NOTE: The previous action might take some time to propagate so a full Windows log-off or restart is required.

Once back, enter an ubuntu session:

ubuntu

# ubuntu@windows10:~$

Validate drives were mounted at the root:

ls "/c/Program Files"

Now lets run a sample container to make sure volumes are working

curl https://www.w3.org/Test/test > /c/index.html
docker run -ti --rm -p 2015:2015 -v /c/index.html:/srv/index.html abiosoft/caddy

You can now now browse to http://localhost:2015/ and should see a Test Dataset page.

Configure Kubectl

Find your user directory on windows

ls /c/users

Create a soft link to the .kube/config. Make sure you replace <YOUR_USER> with your username on windows:

mkdir -p ~/.kube
ln -sf /c/users/<YOUR_USER>/.kube/config ~/.kube/config

Now you should be able to connect to Kubernetes on Docker for Windows

kubectl cluster-info

# Kubernetes master is running at https://localhost:6445
# KubeDNS is running at https://localhost:6445/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
# 
# To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Test kubectx

kubectx

# docker-for-desktop

Test kubens

kubens kube-system

# Context "docker-for-desktop" modified.
# Active namespace is "kube-system".

kubectl get pods

# NAME                                         READY   STATUS    RESTARTS   AGE
# etcd-docker-for-desktop                      1/1     Running   1          2h
# kube-apiserver-docker-for-desktop            1/1     Running   1          2h
# kube-controller-manager-docker-for-desktop   1/1     Running   1          2h
# kube-dns-86f4d74b45-z7n5h                    3/3     Running   3          2h
# kube-proxy-6568r                             1/1     Running   1          2h
# kube-scheduler-docker-for-desktop            1/1     Running   1          2h

Enable WSL & Ubuntu 20.04 On Windows 10

References:

Install and setup

Open Powershell in Administrator mode and enable WSL (Needs a computer restart):

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Download Ubutnu 20.04 (20.04 has been patched for over a year):

Invoke-WebRequest -Uri https://aka.ms/wsl-ubuntu-1604 -OutFile Ubuntu.appx -UseBasicParsing

Open Powershell in Administrator mode and install the downloaded package:

Add-AppxPackage .\Ubuntu.appx

Now type the following in on the terminal:

ubuntu

# Installing, this may take a few minutes...

Setup your default user

The first user account you create is automatically (https://aka.ms/wslusers) configured with a few special attributes:

  • It is your default user -- it signs-in automatically on launch.
  • It is Linux administrator (a member of the sudo group) by default. Type in a Linux username and password for the user.

NOTE: You will need this password to perform sudo commands (dont loose it):

# Installation successful!
# Please create a default UNIX user account. The username does not need to match your Windows username.
# For more information visit: https://aka.ms/wslusers
# Enter new UNIX username:

ubuntu

# Enter new UNIX password:
password123

# Retype new UNIX password:
password123

# passwd: password updated successfully
# Default UNIX user set to: ubuntu
# To run a command as administrator (user "root"), use "sudo <command>".
# See "man sudo_root" for details.

You are now in an ubuntu session:

# ubuntu@windows10:~$

Having to enter a password when executing sudo commands is a pain so lets remove that. Note that this is based on the default user being named ubuntu.

sudo su

# [sudo] password for ubuntu:

cat<<EOF > /etc/sudoers.d/ubuntu && chmod 0440 /etc/sudoers.d/ubuntu
ubuntu ALL=(ALL) NOPASSWD:ALL
EOF

Managing distro

You can manage the Ubuntu instance from a Powershell session:

ubuntu.exe /?

# Launches or configures a linux distribution.
# 
# Usage:
#     <no args>
#       - Launches the distro's default behavior. By default, this launches your default shell.
# 
#     run <command line>
#       - Run the given command line in that distro, using the default configuration.
#       - Everything after `run ` is passed to the linux LaunchProcess call.
# 
#     config [setting [value]]
#       - Configure certain settings for this distro.
#       - Settings are any of the following (by default)
#         - `--default-user <username>`: Set the default user for this distro to <username>
# 
#     clean
#       - Uninstalls the distro. The appx remains on your machine. This can be
#         useful for "factory resetting" your instance. This removes the linux
#         filesystem from the disk, but not the app from your PC, so you don't
#         need to redownload the entire tar.gz again.
# 
#     help
#       - Print this usage message.

You can also enter an ubuntu shell from other terminal emulators like Powershell or cmd by typing ubuntu:

ubuntu

# root@windows10:~#

You can also change the default user if you want to change it from your previously created one:

ubuntu config --default-user ubuntu

Installing Kubernetes & Docker Tools On Windows 10 Ubuntu WSL

Enter a WSL ubuntu session

View details here: https://gist.github.com/xynova/87beae35688476efb2ee290d3926f5bb

From a Powershell session:

ubuntu

# ubuntu@windows10:~$

Install some useful tools

Install some utilities

sudo apt-get update -y && sudo apt-get install -y \
bash-completion \
nmap \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

Get Kubectl

References:

Download the latest release with the command:

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

Make the kubectl binary executable and move it into the utilities path::

chmod +x ./kubectl && sudo mv ./kubectl /usr/local/bin/kubectl

Add the completion script to the /etc/bash_completion.d directory.

kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null

Force the creation of a temporary .kube config directory

kubectl config set-cluster fake --server=https://5.6.7.8 --insecure-skip-tls-verify
kubectl config set-credentials nobody 
kubectl config set-context fake --cluster=fake --namespace=default --user=nobody

Get Helm

References:

Install helm

curl -LO https://git.io/get_helm.sh
chmod 700 get_helm.sh
./get_helm.sh

Add the completion script to the /etc/bash_completion.d directory.

helm completion bash | sudo tee /etc/bash_completion.d/helm > /dev/null

Install helm plugins: helm-github, helm-tiller, helm-restore

helm init --client-only
helm plugin install https://github.com/sagansystems/helm-github.git
helm plugin install https://github.com/rimusz/helm-tiller
helm plugin install https://github.com/maorfr/helm-restore

Get Kubectx and Kubens

References:

Get the kubectx script

curl -O https://raw.githubusercontent.com/ahmetb/kubectx/master/kubectx \
&& chmod +x kubectx \
&& sudo mv kubectx /usr/local/bin/

Get the kubens script

curl -O https://raw.githubusercontent.com/ahmetb/kubectx/master/kubens \
&& chmod +x kubens \
&& sudo mv kubens /usr/local/bin/

Add the completion script to the /etc/bash_completion.d directory.

BASE_URL=https://raw.githubusercontent.com/ahmetb/kubectx/master/completion
curl ${BASE_URL}/kubectx.bash | sudo tee /etc/bash_completion.d/kubectx > /dev/null
curl ${BASE_URL}/kubens.bash | sudo tee /etc/bash_completion.d/kubens > /dev/null

Get Docker cli and Docker Compose

References:

Remove older versions of Docker

sudo apt-get remove docker docker-engine docker.io containerd runc

Add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88

Add Docker's stable repository

sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

Install Docker cli

sudo apt-get update -y && sudo apt-get install docker-ce-cli 

Update pip, install it if not present

pip --version || sudo apt-get install python-pip

Install Docker Compose

pip install --user docker-compose

Add the completion script to the /etc/bash_completion.d directory.

curl https://raw.githubusercontent.com/docker/compose/1.24.0/contrib/completion/bash/docker-compose \
| sudo tee /etc/bash_completion.d/docker-compose > /dev/null

Connecting Docker cli and Kubectl to Docker Desktop for Windows

View details here: https://gist.github.com/xynova/7d18ccb44eb52a38b5b51c0922e06cc7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment