Skip to content

Instantly share code, notes, and snippets.

@h-mochizuki
Last active June 17, 2024 23:50
Show Gist options
  • Select an option

  • Save h-mochizuki/1ced4fafc7ec2f0f752c0830181ae5c0 to your computer and use it in GitHub Desktop.

Select an option

Save h-mochizuki/1ced4fafc7ec2f0f752c0830181ae5c0 to your computer and use it in GitHub Desktop.
WSL2のUbuntuにk8sをインストールする際のスクリプト。 .bashrc から呼ぼう。
#!/bin/bash
# Docker インストール
type -f docker > /dev/null && {
docker -v
} || {
(
echo "[info] 'docker' is not found. start install..."
sudo apt-get update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl enable docker
sudo systemctl start docker
) && {
echo "[info] 'docker' is installed."
} || {
echo "[err] failed to install docker" >&2
return 1
}
}
cat /etc/group | grep docker | grep $USER > /dev/null || {
echo "[info] add user to docker-group"
sudo usermod -aG docker $USER
}
type -t docker-compose > /dev/null || {
alias docker-compose='docker compose'
}
# kind(k8sを動かすための土台) インストール
type -t kind > /dev/null && {
kind version
} || {
(
kind_version=$(curl -Ls https://github.com/kubernetes-sigs/kind/tags \
| grep "kind/releases/tag" \
| sed -e 's/^.*>v/v/' -e 's/<.*//g' \
| sed '/^\s*$/d' \
| grep -v alpha \
| head -n 1)
echo "[info] install 'kind(${kind_version})'"
cd /tmp
# For AMD64 / x86_64
[ $(uname -m) = x86_64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/${kind_version}/kind-linux-amd64
# For ARM64
[ $(uname -m) = aarch64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/${kind_version}/kind-linux-arm64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
echo "[info] 'kind(${kind_version})' is insatalled"
) || {
echo "[err] failed to install kind(${kind_version})" >&2
return 1
}
}
[[ "$(kind get clusters 2>/dev/null | wc -c)" -ne 0 ]] || kind create cluster
# k8s インストール
type -t kubectl > /dev/null && {
kubectl version
} || (
{
k8s_version="$(curl -L -s https://dl.k8s.io/release/stable.txt)"
echo "[info] install k8s(${k8s_version})"
cd /tmp
curl -LO "https://dl.k8s.io/release/${k8s_version}/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
} && {
echo "[info] k8s(${k8s_version}) is insatalled"
} || {
echo "[err] failed to install k8s(${k8s_version})" >&2
return 1
}
)
# k9s(k8sの各情報をGUIで見られるようにする) インストール
type -t k9s > /dev/null || (
{
echo "[info] install k9s"
brew install derailed/k9s/k9s
k9s_version="$(k9s version --short | head -n 1 | awk '{print $2}')"
} && {
echo "[info] k9s(${k9s_version}) is installed"
} || {
echo "[err] failed to install k9s" >&2
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment