Skip to content →

Windows setup for working effectively with K8s

Introduction

This blog post references how I set up my Microsoft Windows 10 developer machine to work effectively with Kubernetes (K8s). Please feel free to comment on any tools or techniques you find missing.

Ironically the first tool I am setting up on Windows is the Windows Subsystem for Linux.

Windows Subsystem for Linux

Ubuntu on Windows

Window Subsystem for Linux (WSL) makes it possible to run a Linux distro “in” Windows. I have never been a big fan of PowerShell or the terminal in Windows, but I like the rest of the Windows developer ecosystem. With WSL, we get the superior bash and Linux terminal without leaving Windows. The best of both worlds.

Go to Install WSL | Microsoft Docs and follow the instructions. I highly recommend WSL 2 over WSL 1. It is way faster. To see the complete list of changes between versions 1 and 2, go to Comparing WSL 1 and WSL 2 | Microsoft Docs. Set up a WSL development environment | Microsoft Docs introduces a list of resources to customize the look and feel.

Windows Terminal

Windows Terminal is way better than the MS-DOS or Powershell terminals. Go to An overview on Windows Terminal | Microsoft Docs to get an overview and install it. I install it through the Microsoft Store. In that way, it is easier to keep it up to date.

Windows Terminal

Be sure to run

sudo apt-get update
sudo apt-get upgrade

at regular intervals to get the latest packages if you are using a Debian distro. The other Linux distros have similar package managers.

Cascadia font with ligatures

The Cascadia font comes shipped with the new Windows Terminal. If you want, you can enable the version that has ligatures by changing the font in the setting dialog to Cascadia Code.

Terminal apperance

Now you can type != and get the beautiful ≠ symbol instead, for example.

Be sure to set your Linux distro of choice as the default:

Startup setting

DNS lookup issues

I have encountered DNS issues when working with WSL2 and a laptop with VPN software configured. Follow the steps if you have trouble with the DNS lookups.

Turn off /etc/resolv.conf generation

Make sure the /etc/wsl.conf has the following content:

[network]
generateResolvConf = false
Restart the WSL2 virtual machine

Run the following command in Powershell:

wsl --shutdown
Create a custom /etc/resolv.conf
cd /etc
rm resolv.conf
echo "nameserver 1.1.1.1" > resolv.conf

Be sure to add any VPN nameserver before the third line.

Restart the WSL2 virtual machine

Run the following command in Powershell:

wsl --shutdown

Worlds smallest VIM configuration

These are the smallest number of settings that I find useful when quickly editing files in Vim from WSL. Add the following lines in the ~/.vimrc

set list
set expandtab
set shiftwidth=2
set tabstop=2

Azure CLI

I use the Azure CLI for all changes in Azure. Follow the instructions at Install the Azure CLI on Linux | Microsoft Docs to add the package repo and install the CLI on your Linux distro of choice.

Azure CLI tab completion should be enabled out of the box.

Kubectl

Next, we need the primary tool for working with K8s, the Kubectl CLI. Go to Install and Set Up kubectl on Linux | Kubernetes and follow the instructions (Ubuntu is a Debian-based Linux distro).

This is a good reference for working with the Kubectl CLI: https://kubernetes.io/docs/reference/kubectl/cheatsheet/.

Kubectl alias and tab-completion

This step saves you a lot of time.

echo 'source <(kubectl completion bash)' >>~/.bashrc
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -F __start_kubectl k' >>~/.bashrc

Reload the ~/.bashrc into your current shell:

. ~/.bashrc

You can now type k instead of kubectl, and use [tab] to get a list of options, including the objects in your K8s instance.

Namespace and context switch helper

https://github.com/ahmetb has developed some great utilities for Kubectl. We will be installing kubectx and kubens.

sudo apt-get install pkg-config
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
COMPDIR=$(pkg-config --variable=completionsdir bash-completion)
sudo ln -sf ~/.kubectx/completion/kubens.bash $COMPDIR/kubens
sudo ln -sf ~/.kubectx/completion/kubectx.bash $COMPDIR/kubectx
cat << EOF >> ~/.bashrc


#kubectx and kubens
export PATH=~/.kubectx:\$PATH
EOF 

The GitHub – junegunn/fzf: A command-line fuzzy finder is a tool that together with kubectx and kubens will help us choose the context or namespace interactively. Install it by running:

sudo apt-get install fzf

Typing kubectx and kubens is way to long. Let us set up some aliases:

echo "alias kx=kubectx" >> .bashrc
echo "alias kns=kubens" >> .bashrc

We can type kx and kns, get a list of the contexts and namespaces, respectively, and choose an item interactively with fzf.

YAML scaffolding

Use the scaffolding functionality to get going with the K8s objects. If I wanted to create a deployment I would write something like:

k create deployment atest --image=nginx --replicas=3 --dry-run=client -o yaml > atest.yaml

The file atest.yaml would look like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: atest
  name: atest
spec:
  replicas: 3
  selector:
    matchLabels:
      app: atest
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: atest
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

I usually clean it up by removing the creationTimestamp: null, resources: {} and status: {}.

Krew – the kubectl package manager

Next, let us install the package manager for Kubectl: Krew. Go to Installing · Krew (k8s.io) and follow the instructions under macOS/Linux.

Install Stern using Krew

First, update the database over the packages by running:

k krew update

Then we install stern which is a handy utility for getting the logs from all pods matching a set of labels:

k krew install stern

Check out GitHub – wercker/stern: ⎈ Multi pod and container log tailing for Kubernetes for instructions on how to use Stern. Since we installed using the krew the command will be:

k stern {stern command}

Visual Studio Code

Visual Studio Code is a lightweight editor, perfect for editing all of those YAML files when working with K8s. I have not seen the need for a full-blown Integrated Development Editor (IDE) like Visual Studio when working with K8s.

I create git repos and do all git operations from within WSL and bash. Whenever I want to fire up the Visual Studio Code for a directory I simply type the following:

code .

Please be aware of the CRLF vs LF line endings when switching between Linux and Windows. Visual Studio Code has an indicator on the bottom right showing you the files line ending, and enabling you to set it on a pair file basis.

Visual Studio Code – add ons

I try to keep the Visual Studio Code as minimal as possible. Right now the only plugin I install is the VIM emulator: Vim – Visual Studio Marketplace. If you are more of an Emacs developer then please leave…

When adding plugins please be aware that it is different if you do it when having started Visual Studio Code through WSL or from Windows.

Get credentials for an AKS cluster

Type in the terminal:

az aks get-credentials --admin --subscription {subscriptionID} --resource-group {resourcegroup to AKS object} --name {AKS resource name}

If you want to have the credentials as non-admin, drop the —admin flag. Now you should be good to go when working with K8s clusters, namespaces, and troubleshooting.

Published in k8s programming

Comments

Leave a Reply

Your email address will not be published.