diff --git a/.gitignore b/.gitignore index 997ca2f..4bc910d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -.vagrant \ No newline at end of file +.vagrant +kubernetes/ +etcd*/ +cluster.config \ No newline at end of file diff --git a/README.md b/README.md index 6f069cf..9c8d132 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,62 @@ # Introduction This repository is intended for demo-ing the manual install of kubernetes's components on both master and worker nodes. -It should be able to get you to a working single master kubernetes setup on a set of vagrant boxes + +It should be able to get you to a working single master (insecure) kubernetes setup on a set of VMs + +```plantuml +@startuml +database etcd [ + etcd +] + +package "master-node" { + [api-server] -> etcd + + [kubelet] --> [api-server] : watch + [kubelet] --> [container-runtime] : run & watch + + [scheduler] --> [api-server] : watch + [scheduler] --> [api-server] : apply + + [controller-manager] --> [api-server] : watch + [controller-manager] --> [api-server] : apply +} + +package "worker-node-1" { + [kubelet ] --> [api-server] : watch + [kubelet ] --> [container-runtime ] : run & watch + +} + +package "worker-node-2" { + [kubelet ] --> [api-server] : watch + [kubelet ] --> [container-runtime ] : run & watch +} + +@enduml +``` + # prerequisites - vagrant - the scp vagrant plugin : `vagrant plugin install vagrant-scp` - [the GNU parallel CLI](https://www.gnu.org/software/parallel/) +- [jq](https://stedolan.github.io/jq/) # setup -- start the vms +- run `vagrant up` to start the vms. This will create a master node and 2 worker nodes on your host's network + +- run `./scripts/show_cluster_config | tee cluster.config` + +- copy the cluster configuration to the nodes: ```sh -vagrant up +./scripts/copy_file_to_nodes cluster.config +``` + +- install the jq CLI on the nodes so they can read the config +```sh +./scripts/run_script_on_nodes install_jq_cli ``` - setup a container runtime @@ -21,5 +66,19 @@ vagrant up - download kubernetes ```sh -./scripts/run_script_on_nodes download_node_binaries +./scripts/download_kubernetes_binaries $(cat cluster.config | jq -r ".kubernetes_version") ./kubernetes +``` +- download etcd +```sh +./scripts/download_etcd_binaries $(cat cluster.config | jq -r ".etcd3_version") ./etcd3 +``` + +- copy kubelet & kube-proxy on the worker nodes +```sh +./scripts/copy_file_to_nodes ./kubernetes/workers worker +``` + +- copy kubelet, proxy, apiserver, scheduler and native controllers binaries to the master nodes +```sh +./scripts/copy_file_to_nodes ./etcd3 master ``` \ No newline at end of file diff --git a/Vagrantfile b/Vagrantfile index 4e3a914..a53709e 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -8,6 +8,8 @@ Vagrant.configure("2") do |config| config.vm.box = "debian/stretch64" config.vm.box_version = "= 9.9.1" + config.vm.network "private_network", type: "dhcp" + # greet from every configured VM, revealing its hostname config.vm.provision "shell", inline: "echo Hello from \$HOSTNAME" diff --git a/scripts/copy_file_to_nodes b/scripts/copy_file_to_nodes new file mode 100755 index 0000000..787c0ea --- /dev/null +++ b/scripts/copy_file_to_nodes @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +SCRIPTS_DIR=$(dirname $0) +FILE_TO_COPY=$(realpath "$1") +NODE_NAMES="$($SCRIPTS_DIR/node_names $2)" + +echo "will copy $FILE_TO_COPY to nodes $(echo $NODE_NAMES | xargs)" + +echo "" +echo "" +echo "" +echo "" + +read -n 1 -s -r -p "Press any key to continue..." +echo "" +echo "" + +parallel vagrant scp $FILE_TO_COPY {}:~/ ::: $NODE_NAMES \ No newline at end of file diff --git a/scripts/download_etcd_binaries b/scripts/download_etcd_binaries new file mode 100755 index 0000000..f0a9c71 --- /dev/null +++ b/scripts/download_etcd_binaries @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +ETCD3_RELEASE_VERSION=$1 +OUTPUT_DIR=$(realpath "$2") + +tmp_dir=$(mktemp -d) + +pushd "$tmp_dir" &> /dev/null + +curl -sL "https://github.com/etcd-io/etcd/releases/download/$ETCD3_RELEASE_VERSION/etcd-$ETCD3_RELEASE_VERSION-linux-arm64.tar.gz" | tar -zxf - + +mkdir -p $OUTPUT_DIR +mv etcd-$ETCD3_RELEASE_VERSION-linux-arm64/etcd $OUTPUT_DIR/ +mv etcd-$ETCD3_RELEASE_VERSION-linux-arm64/etcdctl $OUTPUT_DIR/ + +popd &> /dev/null + +rm -rf $tmp_dir \ No newline at end of file diff --git a/scripts/download_kubernetes_binaries b/scripts/download_kubernetes_binaries new file mode 100755 index 0000000..5bb290e --- /dev/null +++ b/scripts/download_kubernetes_binaries @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +# v1.15.0 +K8S_RELEASE_VERSION=$1 +OUTPUT_DIR=$(realpath "$2") + +tmp_dir=$(mktemp -d) + +pushd "$tmp_dir" &> /dev/null + +container_id=$(docker create gcr.io/google-containers/hyperkube:$K8S_RELEASE_VERSION) +docker cp $container_id:/hyperkube ./hyperkube +docker rm -f $container_id &> /dev/null + +mkdir -p $OUTPUT_DIR/workers +cp hyperkube $OUTPUT_DIR/workers/kubelet +cp hyperkube $OUTPUT_DIR/workers/proxy +cp hyperkube $OUTPUT_DIR/workers/kubectl + +mkdir -p $OUTPUT_DIR/masters +cp hyperkube $OUTPUT_DIR/masters/kubelet +cp hyperkube $OUTPUT_DIR/masters/proxy +cp hyperkube $OUTPUT_DIR/masters/kubectl +cp hyperkube $OUTPUT_DIR/masters/scheduler +cp hyperkube $OUTPUT_DIR/masters/controller-manager +cp hyperkube $OUTPUT_DIR/masters/cloud-controller-manager +cp hyperkube $OUTPUT_DIR/masters/apiserver +popd &> /dev/null + +rm -rf $tmp_dir \ No newline at end of file diff --git a/scripts/download_node_binaries b/scripts/download_node_binaries deleted file mode 100755 index 57be7d1..0000000 --- a/scripts/download_node_binaries +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env bash -curl -sL https://github.com/kubernetes/kubernetes/releases/download/v1.15.0/kubernetes.tar.gz | tar zxvf - \ No newline at end of file diff --git a/scripts/install_container_runtime b/scripts/install_container_runtime index 31cb8a4..3d37b47 100755 --- a/scripts/install_container_runtime +++ b/scripts/install_container_runtime @@ -13,4 +13,12 @@ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - sudo add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" -sudo apt-get install -y --allow-unauthenticated docker-ce docker-ce-cli containerd.io \ No newline at end of file +sudo apt-get install -y --allow-unauthenticated docker-ce docker-ce-cli containerd.io + +sudo systemctl enable docker + +sleep 5 + +sudo systemctl start docker + +sudo usermod -aG docker $USER \ No newline at end of file diff --git a/scripts/install_jq_cli b/scripts/install_jq_cli new file mode 100755 index 0000000..0cd8ae7 --- /dev/null +++ b/scripts/install_jq_cli @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +sudo apt-get update -y + +sudo apt-get install -y jq \ No newline at end of file diff --git a/scripts/node_ip_addresses b/scripts/node_ip_addresses new file mode 100755 index 0000000..689f9e7 --- /dev/null +++ b/scripts/node_ip_addresses @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +SCRIPTS_DIR=$(dirname $0) + +for vagrant_box in $($SCRIPTS_DIR/node_names $1) +do + ip_address=$(vagrant ssh $vagrant_box -c "hostname -I | cut -d' ' -f2" 2>/dev/null) + echo "$vagrant_box $ip_address" +done + diff --git a/scripts/run_script_on_nodes b/scripts/run_script_on_nodes index 02fe07e..8d695f9 100755 --- a/scripts/run_script_on_nodes +++ b/scripts/run_script_on_nodes @@ -1,7 +1,8 @@ #!/usr/bin/env bash SCRIPTS_DIR=$(dirname $0) SCRIPT_NAME=$1 -NODE_NAMES="$($SCRIPTS_DIR/node_names $2)" +NODE_NAMES_FILTER=$2 +NODE_NAMES="$($SCRIPTS_DIR/node_names $NODE_NAMES_FILTER)" cat $SCRIPTS_DIR/$SCRIPT_NAME @@ -15,8 +16,10 @@ echo "" echo "" # copy script over -chmod u+x "$SCRIPTS_DIR/$SCRIPT_NAME" -parallel vagrant scp "$SCRIPTS_DIR/$SCRIPT_NAME" "{}:~/" ::: $NODE_NAMES +pushd $SCRIPTS_DIR &> /dev/null +chmod u+x "$SCRIPT_NAME" +yes | ./copy_file_to_nodes "$SCRIPT_NAME" "$NODE_NAMES_FILTER" &> /dev/null +popd &> /dev/null # remotely run the script parallel vagrant ssh {} -c "\~/$SCRIPT_NAME" ::: $NODE_NAMES \ No newline at end of file diff --git a/scripts/show_cluster_config b/scripts/show_cluster_config new file mode 100755 index 0000000..994c8af --- /dev/null +++ b/scripts/show_cluster_config @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +KTHW_KUBERNETES_VERSION=v1.15.0 +KTHW_ETCD3_VERSION=v3.3.13 + +cat <