Skip to content

Instantly share code, notes, and snippets.

@pjbgf
Last active November 11, 2019 18:39
Show Gist options
  • Save pjbgf/a8e02c81a9af62af3ca9b06196d84c70 to your computer and use it in GitHub Desktop.
Save pjbgf/a8e02c81a9af62af3ca9b06196d84c70 to your computer and use it in GitHub Desktop.
Script to automate the creation of development environment for Kubernetes in Ubuntu
#!/bin/bash
set -e
set -o pipefail
set -x
GO_VERSION="1.13.4"
TARGET_GO_PATH="/home/$(whoami)/go"
KUBERNETES_PATH="$TARGET_GO_PATH/src/k8s.io/kubernetes"
# you can change this with your fork
KUBE_SOURCE_REPO="https://github.com/kubernetes/kubernetes.git"
CRIO_SOURCE_REPO="https://github.com/cri-o/cri-o.git"
# This script automates the setup of a development environment for the kubernetes project.
# The manual steps can be found at https://github.com/kubernetes/community/blob/master/contributors/devel/development.md
install_os_dependencies() {
sudo apt update
# optional to upgrade all out of date packages
sudo apt upgrade -y
sudo apt install -y \
gcc \
make \
docker.io \
software-properties-common
}
install_kubectl() {
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/kubectl
}
install_golang() {
echo "Installing go $GO_VERSION"
wget https://dl.google.com/go/go$GO_VERSION.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go$GO_VERSION.linux-amd64.tar.gz
rm go$GO_VERSION.linux-amd64.tar.gz
echo "GOPATH=$TARGET_GO_PATH"
mkdir -p "$TARGET_GO_PATH"
echo "Updating ~/.profile to add go in PATH and to set GOPATH"
echo export PATH="\$PATH:/usr/local/go/bin" >> ~/.profile
echo export GOPATH="$TARGET_GO_PATH" >> ~/.profile
}
project_clone() {
echo "cloning kubernetes to $KUBERNETES_PATH"
mkdir -p "$KUBERNETES_PATH"
cd $KUBERNETES_PATH
git clone $KUBE_SOURCE_REPO $KUBERNETES_PATH
}
install_etcd() {
echo "Installing etcd"
hack/install-etcd.sh
echo "Updating ~/.profile to add etcd to PATH"
echo export PATH="\$PATH:$KUBERNETES_PATH/third_party/etcd" >> ~/.profile
}
project_install_dependencies() {
install_etcd
}
install_crio_dependencies() {
# Add the PPA repository containing dependencies
sudo apt-add-repository ppa:projectatomic/ppa -y
sudo apt update -qq
sudo apt install -y \
btrfs-tools \
containers-common \
libassuan-dev \
libdevmapper-dev \
libglib2.0-dev \
libc6-dev \
libgpgme11-dev \
libgpg-error-dev \
libseccomp-dev \
libsystemd-dev \
libselinux1-dev \
pkg-config \
go-md2man \
cri-o-runc \
libudev-dev \
software-properties-common \
socat \
conntrack
install_crio_conmon
}
install_crio_from_source() {
CRIO_REPO_PATH="$TARGET_GO_PATH/src/github.com/cri-o/cri-o"
mkdir -p $CRIO_REPO_PATH
git clone $CRIO_SOURCE_REPO $CRIO_REPO_PATH
cd $CRIO_REPO_PATH
PATH=$PATH:/usr/local/go/bin make BUILDTAGS='seccomp apparmor'
# ensure that GO is in sudo's PATH
sudo PATH=$PATH:/usr/local/go/bin make install
sudo PATH=$PATH:/usr/local/go/bin make install.config
}
install_crio_plugins() {
# https://github.com/cri-o/cri-o/blob/master/contrib/cni/README.md
PLUGINS_REPO_PATH="$TARGET_GO_PATH/src/github.com/containernetworking/plugins"
git clone https://github.com/containernetworking/plugins $PLUGINS_REPO_PATH
cd $PLUGINS_REPO_PATH
git checkout v0.8.1
PATH=$PATH:/usr/local/go/bin ./build_linux.sh
sudo mkdir -p /opt/cni/bin
sudo cp bin/* /opt/cni/bin/
}
install_crio_conmon() {
CONMON_REPO_PATH="$TARGET_GO_PATH/src/github.com/containers/conmon"
mkdir -p $CONMON_REPO_PATH
git clone https://github.com/containers/conmon $CONMON_REPO_PATH
cd $CONMON_REPO_PATH
make
sudo make install
}
configure_crio() {
# whitelist container registries
cat <<-EOF > registries.conf
[registries.search]
registries = ['quay.io', 'docker.io', 'k8s.gcr.io']
[registries.insecure]
registries = []
[registries.block]
registries = []
EOF
sudo mv registries.conf /etc/containers/
# Configure CNI plugins
cat <<-EOF > 10-mynet.conf
{
"cniVersion": "0.4.0",
"name": "mynet",
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "10.22.0.0/16",
"routes": [
{ "dst": "0.0.0.0/0" }
]
}
}
EOF
cat <<-EOF > 99-loopback.conf
{
"cniVersion": "0.4.0",
"name": "lo",
"type": "loopback"
}
EOF
sudo mkdir -p /etc/cni/net.d/
sudo mv 10-mynet.conf /etc/cni/net.d/
sudo mv 99-loopback.conf /etc/cni/net.d/
# set log_level to info
sudo sed -i "s/log_level = \"error\"/log_level = \"info\"/g" /etc/crio/crio.conf
sudo sed -i "s/k8s.io\/pause:3.1/docker.io\/paulinhu\/pause-amd64:3.1/g" /etc/crio/crio.conf
# instead of using the pause image above, you can build the local image with:
# cd ~/go/src/k8s.io/kubernetes/build/pause
# make container
# then you would need to push it to a registry you have whitelisted on registries.conf
}
crio_message() {
echo "Run CRIO manually:
/usr/local/bin/crio
Or set up a systemd unit file with:
sudo make install.systemd
And let systemd take care of running CRI-O:
sudo systemctl daemon-reload
sudo systemctl enable crio
sudo systemctl start crio
To run a local kubernetes using CRIO use:
API_HOST=0.0.0.0 CONTAINER_RUNTIME=remote CONTAINER_RUNTIME_ENDPOINT=\"unix:///var/run/crio/crio.sock\" KUBELET_FLAGS=\"--pod-infra-container-image=docker.io/paulinhu/pause-amd64:3.1\" $KUBERNETES_PATH/hack/local-up-cluster.sh"
}
install_crio() {
# manual steps: https://github.com/cri-o/cri-o/blob/master/tutorials/setup.md
install_crio_dependencies
install_crio_from_source
install_crio_plugins
configure_crio
crio_message
}
project_message() {
echo "project is setup! you should be able to build (make) and run unit tests (make test) straight from $KUBERNETES_PATH"
echo "to run a local cluster use: API_HOST=0.0.0.0 $KUBERNETES_PATH/hack/local-up-cluster.sh"
}
add_current_user_to_docker() {
sudo usermod -aG docker $(whoami)
newgrp docker
}
setup() {
install_os_dependencies
install_golang
install_kubectl
project_clone
project_install_dependencies
project_message
if [ "$1" = "--crio" ]; then
install_crio
fi
add_current_user_to_docker
cd
}
setup $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment