LoginSignup
1
0

More than 1 year has passed since last update.

コピペで作るKubernetes on gVisor

Posted at

はじめに

この記事は @inductor さんのブログ記事:

kubeadmで作ったKubernetesクラスターでcontainerd + gVisorのRuntimeClassを動かすぞ2021年エディション~~~~

のパクリ を全面的に参考にしつつ、コマンドをCopy & Pasteしても動くようにしたものです。追加の情報はないので何をやっているのか知りたい方はオリジナルの記事を読んでください。sudoの使い方を知っている人、root権限で作業するタイプの人はこの記事ではなくオリジナルの記事で十分です。

ぶっちゃけた話、元記事からコピペしたときにsudoを補うのが面倒になったから、自分用のメモとして残しているだけです。

コピペ用コマンド

  • containerdのインストール
    (変更なし)
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# Setup required sysctl params, these persist across reboots.
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

# Apply sysctl params without reboot
sudo sysctl --system

# (Install Docker CE)
## Set up the repository:
### Install packages to allow apt to use a repository over HTTPS
apt-get update && apt-get install -y \
  apt-transport-https ca-certificates curl software-properties-common gnupg2

# Add Docker's official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key --keyring /etc/apt/trusted.gpg.d/docker.gpg add -

# Add the Docker apt repository:
add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

## Install containerd
sudo apt-get update && sudo apt-get install -y containerd.io
  • containerd-shim-runsc-v1のインストール
    (変更なし)
## Install gVisor shim for containerd
(
  set -e
  ARCH=$(uname -m)
  URL=https://storage.googleapis.com/gvisor/releases/release/latest/${ARCH}
  wget ${URL}/runsc ${URL}/runsc.sha512 \
    ${URL}/containerd-shim-runsc-v1 ${URL}/containerd-shim-runsc-v1.sha512
  sha512sum -c runsc.sha512 \
    -c containerd-shim-runsc-v1.sha512
  rm -f *.sha512
  chmod a+rx runsc containerd-shim-runsc-v1
  sudo mv runsc containerd-shim-runsc-v1 /usr/local/bin
)
  • containerdの設定を変更する
    (変更なし)
sudo mkdir -p /etc/containerd
sudo containerd config default > /etc/containerd/config.toml

if grep -q SystemdCgroup "/etc/containerd/config.toml"; then
  echo "Config found, skip rewriting..."
else
  sed -i -e "/^          \[plugins\.\"io\.containerd\.grpc\.v1\.cri\"\.containerd\.runtimes\.runc\.options\]$/a\            SystemdCgroup \= true\n        \[plugins\.\"io\.containerd\.grpc\.v1\.cri\"\.containerd\.runtimes\.runsc\]\n          runtime_type \= \"io\.containerd\.runsc\.v1\"" /etc/containerd/config.toml
  sed -i -e 's/shim_debug \= false/shim_debug \= true/g' /etc/containerd/config.toml
fi
  • Kubernetes向けのセットアップとパッケージのインストール
# Set Kubernetes kernel params
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.overcommit_memory = 1
vm.panic_on_oom = 0
kernel.panic = 10
kernel.panic_on_oops = 1
kernel.keys.root_maxkeys = 1000000
kernel.keys.root_maxbytes = 25000000
EOF
sudo sysctl --system

# Install kubeadm 
sudo apt-get update -y && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update -y
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
  • kubeadm用コンフィグの設定とクラスターの作成
# Set kubeadm config
cat > ~/init_kubelet.yaml <<EOF
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
bootstrapTokens:
# DO NOT USE THE STATIC TOKEN IN PRODUCTION
- token: "9a08jv.c0izixklcxtmnze7"
  description: "kubeadm bootstrap token"
  ttl: "24h"
nodeRegistration:
  criSocket: "/var/run/containerd/containerd.sock"
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: "systemd"
protectKernelDefaults: true
EOF

# Create a Kubernetes cluster
sudo kubeadm init --config init_kubelet.yaml
  • クラスターアクセス用にkubeconfigの取得をしてCNIでCiliumを入れる
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# Setup CNI
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
helm repo add cilium https://helm.cilium.io/
helm install cilium cilium/cilium --namespace kube-system

# Untaint master node to schedule workloads
kubectl taint nodes --all node-role.kubernetes.io/master-

これ以降は元記事とおなじです

  • RuntimeClassの設定をしてワークロードを動かしてみる
cat <<EOF | kubectl apply -f -
apiVersion: node.k8s.io/v1beta1
kind: RuntimeClass
metadata:
  name: gvisor
handler: runsc
EOF

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: nginx-gvisor
spec:
  runtimeClassName: gvisor
  containers:
  - name: nginx
    image: nginx
EOF

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
EOF
kubectl get pod nginx-gvisor -o wide
kubectl get pod nginx -o wide
  • 動作確認
kubectl exec -it nginx-gvisor -- dmesg
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0