Skip to content

Instantly share code, notes, and snippets.

@k4mrul
Last active May 2, 2025 09:23
Show Gist options
  • Save k4mrul/a8e0753d90a9b6e07cd1844aae94d47d to your computer and use it in GitHub Desktop.
Save k4mrul/a8e0753d90a9b6e07cd1844aae94d47d to your computer and use it in GitHub Desktop.
cloud init script with kubernetes, helm, kubectl
#cloud-config
package_update: true
packages:
- bash-completion
- make
- g++
- jq
- fzf
- kubectx
write_files:
- path: /root/setup.sh
permissions: '0755'
content: |
#!/bin/bash
set -e
ARCH=$(dpkg --print-architecture)
# Install yq
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq && chmod +x /usr/bin/yq
# Install kubectl
wget -q "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/${ARCH}/kubectl"
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
rm kubectl
# Enable bash completion for kubectl
echo "source /usr/share/bash-completion/bash_completion" >> /home/ubuntu/.bashrc
echo "complete -F __start_kubectl k" >> /home/ubuntu/.bashrc
echo "alias k=kubectl" >> /home/ubuntu/.bashrc
# Upgrade k0s
curl --proto '=https' --tlsv1.2 -sSf https://get.k0s.sh | sudo sh
# Install kubernetes
k0s config create > /root/k0s.yaml
## we will setup cilium cni
sed -i 's/provider: kuberouter/provider: custom/' /root/k0s.yaml
## disable kubeproxy (maybe not needed but necessary for laaaarge cluster) for cilium handle routing with eBPF
yq eval '.spec.network.kubeProxy.disabled = true' -i /root/k0s.yaml
## Install k8s
k0s install controller --enable-worker --no-taints -c /root/k0s.yaml
k0s start
sleep 120
mkdir -p /home/ubuntu/.kube/
k0s kubectl config rename-context Default $HOSTNAME
yq e ".users[0].name = \"$(hostname)\"" -i /var/lib/k0s/pki/admin.conf
yq e '.contexts[].context.user = "'$(hostname)'"' -i /var/lib/k0s/pki/admin.conf
yq e '(.clusters[].name, .contexts[].context.cluster) |= "'$(hostname)'"' -i /var/lib/k0s/pki/admin.conf
k0s kubeconfig admin > /home/ubuntu/.kube/config
chown ubuntu:ubuntu /home/ubuntu/.kube/ -R
# Install Helm
HELM_RELEASE=$(curl -L -s -H 'Accept: application/json' https://github.com/helm/helm/releases/latest | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/')
tar -xvz -C /tmp/ -f <(wget -q -O - https://get.helm.sh/helm-${HELM_RELEASE}-linux-${ARCH}.tar.gz)
install -o root -g root -m 0755 /tmp/linux-${ARCH}/helm /usr/local/bin/helm
# Install Cilium CLI
CILIUM_CLI_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/cilium-cli/main/stable.txt)
CLI_ARCH=amd64
if [ "$(uname -m)" = "aarch64" ]; then CLI_ARCH=arm64; fi
curl -L --fail --remote-name-all https://github.com/cilium/cilium-cli/releases/download/${CILIUM_CLI_VERSION}/cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
sha256sum --check cilium-linux-${CLI_ARCH}.tar.gz.sha256sum
sudo tar xzvfC cilium-linux-${CLI_ARCH}.tar.gz /usr/local/bin
rm cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
# Install Go
#wget -q https://go.dev/dl/go1.24.2.linux-${ARCH}.tar.gz
#tar -xvf go1.24.2.linux-${ARCH}.tar.gz
#mv go /usr/local
#echo "export GOROOT=/usr/local/go" >> /etc/bash.bashrc
#echo "export PATH=\$GOPATH/bin:\$GOROOT/bin:\$PATH" >> /etc/bash.bashrc
#ln -sf /usr/local/go/bin/go /bin/go
# Install Docker
#curl -fsSL get.docker.com -o get-docker.sh
#sudo sh get-docker.sh
#sudo systemctl start docker
#sudo systemctl enable docker
# Add ubuntu user to docker group
#sudo usermod -aG docker ubuntu
runcmd:
- [ bash, /root/setup.sh ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment