Containers, Kubernetes and DevOps for an Old as Dirt Developer, Part 2 of 6

Table Of Content

Part 1: Intro to containers, Kubernetes and DevOps

Part 2: Getting started (setting up my dev environment)

Part 3: My first app –  .net core, vscode, WSL, containerize and deploy to local k8s AND debug it while running in k8s??? GTFOH

Part 4: Deploy to k8s in the cloud (Azure Kubernetes Service – AKS) and debug from VSCode

Part 5: Set up CI/CD pipeline for Kubernetes using DevOps best practices

Part 6: Debugging generic Kubernetes Clusters with Draft (coming soon)

Getting Started

The first thing I needed to do was set up a development environment. Since I was going to do container dev, I knew I could do this on Windows, Mac or Linux. At the moment i started this adventure, I only had access to my Windows machine and of course Azure. I figured I would first start with creating a dev environment for Windows and after playing around with that for a bit, I’d recreate everything on Linux. For this blog, we’ll just look at Windows and we’ll circle back around to Linux later in the series.

No matter the OS, the pieces I needed were going to be the same. I needed Docker for containers. I needed some sort of Kubernetes cluster locally. I needed Helm to do container package management. And finally Draft, so Draft could make my development cycle easier by handling all the kubernetes/helm nonsense for me. And logically, I would need to install everything in this order. Piece of cake right? How hard could this be? lol….

The first time I did this, I installed Minikube as my local Kubernetes cluster. However, Minikube was being wonky so instead of Minikube, I used Docker Edge, which includes Kubernetes and everything worked smoothly.

Setting up my windows dev environment

Install Chocolatey

Chocolatey is a package manager for Windows and it makes life easier when you have to install a bunch of stuff. Go to https://chocolatey.org and just follow the install directions. Or

  1. Bring up an Admin power shell console
  2.  Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1’))

Install Docker Edge

Installing Docker Edge for windows is straight forward with chocolatey.

Turn on Hyper-V

Docker does require hyper-v to be turned on so let’s do that.

  1. From the start search text field thing, I typed in Turn Windows Features on or off
  2. Make sure Hyper-V is selected

Install Docker Edge

  1. From and Admin powershell command prompt, type in cinst docker-for-windows –pre –y
  2. After the install, docker doesn’t launch automatically, so from start, look for Docker for windows and launch it
  3. Window should pop up with Docker is now up and running
  4. Now we need to enable Kubernetes in the settings for Docker Edge, right click the little whale in the task bar and select Settings
  5. Click Kubernetes, check Enable Kubernetes and Show system containers (advanced) and click Apply
  6. This takes a while to run so kick back and relax for a bit.

 

Configure kubectl to point to this Kubernetes cluster

If you are installing this and haven’t used kubectl (the kubernetes client command line tool), it should be automatically configured to connect to your Kubernetes cluster in Docker Edge. Earlier, I installed and configured Minikube so I need change the context so kubectl is pointing to this Kubernetes cluster.

  1. From an Admin powershell console, kubectl config get-contexts

    Notice I already installed minikube and it’s currently configured to use minikube

  2. kubectl config use-context docker-for-desktop

 

Install Helm

  1. From Admin power shell, cinst kubernetes-helm
  2. helm init

Install Draft

At the time of this blog post, Draft wasn’t working for windows so i decided to use WSL (Windows Subsystem for Linux) and install Draft for Linux. This also meant that I had to configure my WSL environment to connect to my Kubernetes cluster from within WSL. So I had to install kubectl in WSL and then configure it to connect to my Docker Edge Kubernetes instance on windows.

When I first published this blog, I wrote a bunch of steps to install and configure kubectl in WSL. @nunixtech pointed out a MUCH easier way to do this so I updated my blog with his simple steps! I kept the old steps around for posterity. Also,  if you need to tweak the paths in the config if you happen to use Minikube, those old steps might be useful. But using Docker Edge, here is the much simpler way.

 

Copy and tweak kubectl config files to Temp

First, I got my configuration files for kubectl ready by copying it to my temp directory and then tweaking the line endings for unix.

  1. From Admin powershell, copy $env:UserProfile/.kube/config $env:TEMP/kubectl-config
  2. Get-ChildItem -File -Filter *-config | % { $x = get-content -raw -path $_.fullname
    ; $x -replace “rn”,”`n” | set-content -path $_.fullname }

Install and configure kubectl in WSL

Next I opened up WSL, installed kubect. Then, from WSL, i tweaked the config files in my windows temp directory by normalizing the path character, moved the files from my windows temp directory into the the correct .kube and .minikube directory. Then configured kubectl (in WSL) to connect to my windows Minikube

Next I opened up WSL, installed kubectl. After that,  I needed to configure the kubectl from within WSL to connect to my Docker Edge Kubernetes instance. Because the configuration for Docker Edge didn’t require any paths to certificates (that would require me to tweak those paths for linux and to copy those certs over to WSL), I could just create and linked ~/.kube directory to my .kube directory on the windows side (C:/User/abel/.kube).

  1. From WSL, install kubectl, make it executable and then move it to /usr/local/bin
    curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
  1. cd to windows temp directory, tweak the temp config files by normalizing directory paths
    cd /mnt/$(cmd.exe /c echo\|set /p=%TEMP% | sed --expression='s|\\|/|g' | sed --expression='s|^\([A-Za-z]\):|\L\1|')
  2. create a .kube in the home directory
    mkdir ~/.kube
  3. move configuration files to the right location in ~/
    mv kubectl-config ~/.kube/config
  4. Convert windows paths to unix format
    sed -i 's|\\|/|g' ~/.kube/config
    sed -i 's|\([ "]\)\([A-Za-z]\):|\1/mnt/\L\2|' ~/.kube/config
  5. Create and link ~/.kube to the .kube directory on the windows side (this is the step @nunixtech  suggested. See how step 6 replaces 2-5)
    ln -s /mnt/$(cmd.exe /c echo\|set /p=%USERPROFILE% | sed 's/^./\L&\E/' | tr -d ':' | tr '\\' '/')/.kube ~/.kube
  6. Configure kubectl to connect to Docker Edge Kubernetes
    kubectl config use-context docker-for-desktop
  7. Make sure kubectl is configured correctly by launching sample app
    kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.4 --port=8080
  8. Make sure deployment is working
    kubectl get deployment
    kubectl get pod
  9. Expose this service to the outside world
    kubectl expose deployment hello-minikube --type=NodePort
    kubectl get service hello-minikube
  10. Test everything by going back to windows, and browsing to localhost:<routed port>, so in my case, localhost:32048

 

Install Draft in WSL

  1. First make sure docker expose daemon on tcp://localhost:2375 without TLS by showing the hidden running icons and right clicking docker and selecting settings
  2. Make sure the Expose daemon checkbox is checked
  3. From WSL
    export DOCKER_HOST=tcp://0.0.0.0:2375
  4. curl -LO https://azuredraft.blob.core.windows.net/draft/draft-v0.14.1-linux-amd64.tar.gz
  5. tar -xvzf draft-v0.14.1-linux-amd64.tar.gz
  6. sudo mv linux-amd64/draft /usr/local/bin
  7. rm –r linux-amd64/
  8. rm draft-v0.14.1-linux-amd64.tar.gz
  9. draft init

Conclusion

If you followed all the steps in this blog, this should create your development environment for container based apps on a windows machine. We installed and configured Docker Edge (Docker + Kubernetes) on windows, Helm on windows, and Draft on WSL (because at this time, Draft is not working on windows) and configured Draft to connect to your kubernetes instance on windows. All the tooling involved in containers based development is in place and we are now ready to start writing our first container based app.  In the next installment of this series, I will walk you through creating your first container based app and how to use all these tools in your development workflow.

And finally, if you found all these instructions to set up your Windows dev environment insane, you will be happy to know that Jessica Deen (@jldeen), Steven Murawski (@StevenMurawski) and I are creating a simple install script that will install and configure all this nonsense for you.

Next > My first app – Creating the asp.net core app, run in WSL, debug from VSCode