1.【CKS】 考试之容器运行时类(Runtime Class)
1.1 题目要求
1.2 官网位置
官网搜索:runtime class 找到:容器运行时类(Runtime Class)
1.3 模拟环境搭建
1.3.1 下载gvisor安装包
官方下载
runsc 和containerd-shim-runsc-v1
下载命令:
ARCH=$(uname -m)
URL=https://storage.googleapis.com/gvisor/releases/release/latest/${ARCH}
wget ${URL}/runsc ${URL}/containerd-shim-runsc-v1
chmod a+rx runsc containerd-shim-runsc-v1
sudo mv runsc containerd-shim-runsc-v1 /usr/local/bin
1.3.2 安装到containerd
由于我的集群版本已经是v1.28了,已是containerd
vim /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
################################添加部分#########################
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runsc]
runtime_type = "io.containerd.runsc.v1"
###############################添加部分################################
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
1.3.3 重启containerd
systemctl daemon-reload
systemctl restart containerd
至此gvisor已安装完成
1.3.4 创建命名空间server并添加pod
kubectl create ns server
kubectl run nginx --image=nginx --dry-run=client -o yaml > nginx.yaml
kubectl create -f nginx.yaml -n server
1.4 操作步骤
切记!
切换context
1.4.1 创建 RuntimeClass
apiVersion: node.k8s.io/v1 # RuntimeClass 定义于 node.k8s.io API 组
kind: RuntimeClass
metadata:
name: untrusted # 用来引用 RuntimeClass 的名字
# RuntimeClass 是一个集群层面的资源
handler: runsc # 对应的 CRI 配置的名称
kubectl create -f gvisorRuntimeClass.yaml
1.4.2 创建 Pod 的 RuntimeClass
kubectl get po nginx -n client -oyaml > nginx.yaml
# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: nginx
name: nginx
namespace: client
spec:
runtimeClassName: untrusted #需要添加部分
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
resources: {}
kubectl delete -f nginx.yaml ; kubectl create -f nginx.yaml
1.5 考题更新
需要修改的 Pod 并非直接创建,而是通过 Deployment 创建,所以在指
定的 runtimeClass 的时候,更改的位置和 Pod 不太一样。
# kubectl edit deploy workload1 -n client
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: workload1
name: workload1
namespace: client
spec:
replicas: 1
selector:
matchLabels:
app: workload1
template:
metadata:
labels:
app: workload1
spec:
runtimeClassName: untrusted # 注意更改的位置和 containers 对齐
containers:
- env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: C.UTF-8
image: nginx
name: nginx