Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smothiki/50269b8a4efdaa6cef94bd3dfef4d8c8 to your computer and use it in GitHub Desktop.
Save smothiki/50269b8a4efdaa6cef94bd3dfef4d8c8 to your computer and use it in GitHub Desktop.
Bay Area Kubernetes Meetup
Copyright 2018 Google LLC.
SPDX-License-Identifier: Apache-2.0
Authors: ianchak@google.com, mtaufen@google.com silviabear@google.com, fbongiovanni@google.com
# Building Kubernetes from Source in Minutes
In this repo you'll find instructions for building kubernetes (k8s) from source in minutes.
You will start a GCP VM, install kubernetes (k8s) build and runtime dependencies, then build k8s from source. Afterward, we'll startup the local build and verify its working.
## Prerequisites
You must have a Google Cloud Account and gcloud installed and setup locally. You can find instructions on installing gcloud and authenticating with google cloud [here](https://cloud.google.com/sdk/docs/quickstarts).
## Starting a GCP VM
```
$ gcloud compute instances create ${USER}-k8s-build \
--image-family centos-7 \
--image-project centos-cloud \
--machine-type n1-standard-2 \
--boot-disk-size=50GB \
--preemptible
```
* -machine-type: n1-standard-2 has 7.5GB RAM and takes about 22m to build k8s, while a n1-standard-8 takes about 11m to build
* --boot-disk-size: default boot disk side (10GB) is insufficient for kubernetes build
* --preemptible (optional)
Full documentation on creating VMs can be found [here](https://cloud.google.com/compute/docs/instances/create-start-instance).
## Installing dependencies and cloning k8s repo
```
$ gcloud compute ssh ${USER}-k8s-build --command="
sudo yum -y update
# install dependencies and docker
sudo yum -y install yum-utils \
net-tools \
device-mapper-persistent-data \
lvm2
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
sudo yum -y install docker-ce
sudo systemctl enable docker
sudo systemctl start docker
# add user, so sudo isn't required for docker commands
sudo usermod -a -G docker ${USER}
# install gcc to build kubernetes
sudo yum -y install gcc
# install git to clone kubernetes
sudo yum -y install git
# cleanup
sudo yum -y clean all
# install golang-11
curl -O https://dl.google.com/go/go1.11.2.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.11.2.linux-amd64.tar.gz
echo 'export PATH=\${PATH}:/usr/local/go/bin' >> ~/.bash_profile
rm -f go1.11.2.linux-amd64.tar.gz
# clone kubernetes
GOPATH_K8S=\${HOME}/go/src/k8s.io/kubernetes
mkdir -p \${GOPATH_K8S}
git clone https://github.com/kubernetes/kubernetes \${GOPATH_K8S}
# install etcd so that we can run build locally
cd \${GOPATH_K8S}
hack/install-etcd.sh
echo 'export PATH=\${GOPATH_K8S}/third_party/etcd:\${PATH}' >> ~/.bash_profile
"
```
## Building k8s/
## Note: this must be run as a separate `gcloud compute ssh` command as previous command, or logout and login again in order for usermod to take effects.
```
$ gcloud compute ssh ${USER}-k8s-build --command="
cd \${HOME}/go/src/k8s.io/kubernetes
# build kubernetes
time make quick-release
"
```
#### Output snippet
```
<snip>
+++ [0707 14:02:12] Docker builds done
+++ [0707 14:03:32] Building tarball: final
+++ [0707 14:03:32] Building tarball: test
real 10m47.515s
user 2m39.393s
sys 0m19.103s
```
On a n1-standard-2 building k8s took 22m, and on a n1-standard-8 building k8s took 11m.
## Starting local build of k8s
```
$ gcloud compute ssh ${USER}-k8s-build --command="
GOPATH_K8S=\${HOME}/go/src/k8s.io/kubernetes
export PATH=\${PATH}:/usr/local/go/bin
export PATH=\${GOPATH_K8S}/third_party/etcd:\${PATH}
cd ${GOPATH_K8S}
# startup local build
hack/local-up-cluster.sh
"
```
#### Output snippet
```
<snip>
Local Kubernetes cluster is running. Press Ctrl-C to shut it down.
<snip>
```
## Verifying local build
To verify that the local build is operational, we'll execute a kubectl command against it.
```
$ gcloud compute ssh ${USER}-k8s-build --command="
export KUBERNETES_PROVIDER=local
export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig
\${HOME}/go/src/k8s.io/kubernetes/cluster/kubectl.sh get nodes
"
```
#### Output snippet
```
NAME STATUS ROLES AGE VERSION
127.0.0.1 Ready <none> 46s v1.12.0-alpha.0.1948+097f300a4d8dd8
```
## Cleaning up by deleting the compute instance
```
$ gcloud compute instances delete ${USER}-k8s-build
```
## Optional: add a unit test in kubernetes source code and run it
## We recommend directly ssh to the VM and do the editing.
cd ${HOME}/go/src/k8s.io/kubernetes
emacs -nw pkg/util/normalizer/normalizer_test.go
// Add your test for normalizer
// Add test target in pkg/util/normalizer/BUILD
# To run test
make test WHAT=./pkg/util/normalizer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment