First we need to
-
Here in Rancher labs we use golang for most of our projects so, first we need to install golang on your machine. You can download the golang binaries from https://golang.org/dl/ and extract it into usr/local as root, to create a go tree in /usr/local/go
sudo tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
-
After you install docker you need to change user permissions to be able to run docker commands.
sudo apt install docker.io
sudo usermod -aG docker ${USER}
newgrp docker
-
-
k3d The big advantage of k3d, besides the speed of k3s, is that you can create a multi-cluster setup locally. The downside is that you won't have the full K8's feature set, see the k3s repo for more info on that. The readme has install instructions, note that you'll have to point Rancher at the custom kube config since it lives in a non-standard location. use the install script to grab the latest release:
wget:
wget -q -O - https://raw.githubusercontent.com/rancher/k3d/master/install.sh | bash
curl:
curl -s https://raw.githubusercontent.com/rancher/k3d/master/install.sh | bash
-
cd to where you want to create your workspace, which will include your projects and make a workspace folder that contains of 3 sub-folders,
here we workspace is created on $HOME
directory
- src : where you'll write your code
- pkg : where liberary or 3rd party package will be
- bin : where will be the build files of your projects
cd
mkdir -p <workspace_name>{,/bin,/pkg,/src}}
mkdir -p <workspace_name>/src/github.com/rancher
-
The GOPATH environment variable lists places to look for Go code. So it has to be set to your workspace using the following commands to ensure that it's set for everytime you login to your machine.
vim ~/.profile
Add the followingexport GOPATH=$HOME/<workspace_name> export PATH=$HOME/<workspace_name>/bin:$PATH:/usr/local/go/bin
now you can test your environment by running any go code file.
Most of us at Rancher labs use GoLand, and the rest of this document will be written assuming you are as well, but there are several options
For ubuntu users GoLand is available on SnapStore.
Now with your environments set up, clone the repo of k3s into your workspace using the following command after you fork it in your own repo in order to avoid accidentally creating a branch in the rancher/k3s repo.
cd <workspace_name>/src/github.com/rancher
git clone [email protected]:<github_user_name>/k3s.git
Also clone other dependencies for k3s when needed like:
- helm-controller
- system-upgrade-controller
- containerd
- cri
- flannel
- kubernetes
- wrangler
- kine
- dynamiclistener
in a similar manner, you can clone any of these liberaries, for example:
cd <workspace_name>/src/github.com/rancher
git clone [email protected]:<github_user_name>/kine.git
git clone [email protected]:<github_user_name>/helm-controller.git
Then into the k3s directory you add a remote of the original k3s repo
cd k3s
git remote add upstream https://github.com/rancher/k3s
now check remote git remote -v
and they should be like
origin [email protected]:MonzElmasry/k3s.git (fetch)
origin [email protected]:MonzElmasry/k3s.git (push)
upstream https://github.com/rancher/k3s (fetch)
upstream https://github.com/rancher/k3s (push)
and you fetch the latest changes :
git fetch upstream
go the latest commit from upstream/master branch :
git checkout upstream/master
create new branch from this commit :
git checkout -b new_branch
In k3s, there's two opetions to run your project, either to run the makefile
make -f Makefile
or add the configuration to goland to point at the ci script
, at the top right corner , click on Add Configuration
Set the script path to /rancher/k3s/scripts/ci
and the working directory to k3s project location.
The following commands will copy your binaries to /usr/local/bin and install k3s from the binaries instead of downloading from github releases
sudo cp dist/artifacts/k3s /usr/local/bin
curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_DOWNLOAD=true sh -
Finally if you need to uninstall k3s, you can call the uninstall script located in /usr/local/bin
k3s-uninstall.sh