--- title: Tanzu Kubernetes GridでgVisorを使用するメモ tags: ["Kubernetes", "vSphere", "TKG", "Tanzu", "Cluster API", "ytt", "gVisor", "containerd"] categories: ["Dev", "CaaS", "Kubernetes", "TKG", "vSphere"] date: 2021-09-05T13:22:59Z updated: 2021-09-05T13:25:56Z --- Tanzu Kubernetes Gridで[gVisor](https://gvisor.dev)を使えるように設定するメモ。 gVisorを使うには, Worker Nodeに https://gvisor.dev/docs/user_guide/install のドキュメントの通り、 * runsc * containerd-shim-runsc-v1 をインストールし、 https://gvisor.dev/docs/user_guide/containerd/quick_start/ のドキュメントの通り、`/etc/containerd/config.toml`を設定する必要があります。 また、RuntimeClassの定義も必要です。 これらをTanzu Kubernetes Cluster作成時に自動で行うために [ytt](https://carvel.dev/ytt/) のOverlayを作成します。 `$HOME/.tanzu/tkg/providers/infrastructure-vsphere/ytt/vsphere-overlay.yaml` に次の設定を記述または追記します。 ```yaml #@ load("@ytt:overlay", "overlay") #@ load("@ytt:data", "data") #@overlay/match by=overlay.subset({"kind":"KubeadmConfigTemplate"}) --- spec: template: spec: preKubeadmCommands: #@overlay/append - curl -s https://storage.googleapis.com/gvisor/releases/release/latest/x86_64/runsc > runsc #@overlay/append - curl -s https://storage.googleapis.com/gvisor/releases/release/latest/x86_64/containerd-shim-runsc-v1 > containerd-shim-runsc-v1 #@overlay/append - chmod a+rx runsc containerd-shim-runsc-v1 #@overlay/append - mv runsc containerd-shim-runsc-v1 /usr/local/bin #@overlay/append - echo " [plugins.\"io.containerd.grpc.v1.cri\".containerd.runtimes.runc]" >> /etc/containerd/config.toml #@overlay/append - echo " runtime_type = \"io.containerd.runc.v2\"" >> /etc/containerd/config.toml #@overlay/append - echo " [plugins.\"io.containerd.grpc.v1.cri\".containerd.runtimes.runsc]" >> /etc/containerd/config.toml #@overlay/append - echo " runtime_type = \"io.containerd.runsc.v1\"" >> /etc/containerd/config.toml #@overlay/append - systemctl restart containerd --- apiVersion: addons.cluster.x-k8s.io/v1alpha3 kind: ClusterResourceSet metadata: name: #@ "{}-gvisor-runtimeclass".format(data.values.CLUSTER_NAME) labels: cluster.x-k8s.io/cluster-name: #@ data.values.CLUSTER_NAME spec: strategy: "ApplyOnce" clusterSelector: matchLabels: tkg.tanzu.vmware.com/cluster-name: #@ data.values.CLUSTER_NAME resources: - name: #@ "{}-gvisor-runtimeclass".format(data.values.CLUSTER_NAME) kind: Secret --- apiVersion: v1 kind: Secret metadata: name: #@ "{}-gvisor-runtimeclass".format(data.values.CLUSTER_NAME) type: addons.cluster.x-k8s.io/resource-set stringData: value: | apiVersion: node.k8s.io/v1beta1 kind: RuntimeClass metadata: name: gvisor handler: runsc ``` この設定を加えて`tanzu cluster create`を実行し、クラスタを作成します。 RuntimeClassが設定されていることを確認します。 ``` $ kubectl get runtimeclass NAME HANDLER AGE gvisor runsc 14m ``` gVisorを有効にしたPodを作成します。 ```yaml cat < 8m15s v1.20.5+vmware.2 192.168.11.48 192.168.11.48 VMware Photon OS/Linux 4.19.189-5.ph3 containerd://1.4.4 $ kubectl -n tkg-system exec kapp-controller-7776659df6-msz8v -- uname -a Linux rapsberry-control-plane-p6dr2 4.19.189-5.ph3 #1-photon SMP Thu May 13 16:00:29 UTC 2021 x86_64 ```