最近有個項目須要考慮安全容器,所以先調研下 kata contaienr,嚐嚐鮮node
Kata Container 是兩個現有的開源項目合併:Intel Clear Containers和Hyper runV。linux
Intel Clear Container項目的目標是經過英特爾®虛擬化技術(VT)解決容器內部的安全問題,而且可以將容器做爲輕量級虛擬機(VM)啓動,提供了一個可選的運行時間,專一於性能(<100ms啓動時間),可與Kubernetes 和Docker 等經常使用容器環境互操做。英特爾®Clear Container代表,安全性和性能沒必要是一個折衷,將硬件隔離的安全性與容器的性能能夠兼得。ios
hyper runV優先於技術無關支持許多不一樣的CPU架構和管理程序。nginx
使用Docker輕量級的容器時,最大的問題就是會碰到安全性的問題,其中幾個不一樣的容器能夠互相的進行攻擊,若是把這個內核給攻掉了,其餘全部容器都會崩潰。若是使用KVM等虛擬化技術,會完美解決安全性的問題,但響應的性能會受到必定的影響。git
單單就Docker來講,安全性能夠歸納爲兩點:github
因此安全性問題90%以上能夠歸結爲隔離性問題。而Docker的安全問題本質上就是容器技術的安全性問題,這包括共用內核問題以及Namespace還不夠完善的限制:shell
固然,鏡像自己不安全也會致使安全性問題。api
咱們都知道 Docker 等一類的容器使用 Cgroup 進行資源限制,使用 linux 的 namespace 機制對容器進行隔離,但在實際運行中,還是由宿主機向容器直接提供網絡、存儲、計算等資源,雖然性能損失很小,但存在必定的安全問題。安全
Kata Containers是一個開放源代碼社區,致力於經過輕量級虛擬機來構建安全的容器運行時,這些虛擬機的感受和性能相似於容器,可是使用硬件虛擬化技術做爲第二防護層,能夠提供更強的工做負載隔離。bash
kata 底層實現了Intel Clear Containers的最佳部分與Hyper.sh RunV合併,並進行了擴展,以包括對主要架構的支持,包括x86_64以外的AMD64,ARM,IBM p系列和IBM z系列。
Kata Containers還支持多個虛擬機管理程序,包括QEMU,NEMU和Firecracker,並與其餘容器化項目集成。
QEMU 一種虛擬機,與KVM類似,但KVM 是硬件輔助的虛擬化技術,主要負責 比較繁瑣的 CPU 和內存虛擬化,而 Qemu 則負責 I/O 虛擬化
kata 其實是經過建立輕量級虛擬機實現容器之間的資源隔離,再在虛擬機中運行容器運行時,這樣就使容器在專用內核中運行,提供網絡,I / O和內存的隔離,並能夠經過虛擬化VT擴展利用硬件強制隔離。提供安全性的同時,其仍然由很高的性能。
kata 包含的主要組件有兩部分:
在 Docker 引擎中,Docker 調用 containerd,containerd 經過 shim 調用 runc建立底層的容器,(以前在 深刻理解container 篇中詳細描述過此過程),若將kata 集成到 Docker 中時,containerd 則調用 kata 提供的 kata-shim
去建立 kata 容器運行時。
值得提的一點是,kata 如今正在努力推行 shimv2 接口,shimv2 主要影響了 K8s 調用 CRI 建立 pod 和容器。
藉助shimv2,Kubernetes可使用每一個Pod一個shimv2 而不是2N + 1 個shim(每一個容器和Pod沙箱自己的容器shim和kata shim)
容器進程由 agent 生成,agent 在虛擬機內部做爲守護進程運行。
kata-agent使用 VIRTIO 串行或 VSOCK 接口在宿主機中運行gRPC服務器,QEMU在宿主機中經過暴露 socket 文件向外部提供連接。kata-runtime 使用 gRPC 協議與 agent 進行通訊。該協議容許運行時將容器管理命令發送到 agent。該協議還用於在容器和管理引擎(例如Docker Engine)之間傳輸I/O流(stdout,stderr,stdin)。也就是說在容器中,包括 init 進程在內,全部與容器交互的命令以及 IO 均會經過 VIRTIO 或 VSOCK。
硬件支持
首先節點須要支持如下四種任意一種cpu虛擬化技術
若是部署在VMware虛擬機中,須要在宿主機開啓 嵌套虛擬化的功能,開啓步驟見連接 https://blog.51cto.com/114348...
軟件依賴
執行如下腳本安裝
source /etc/os-release sudo yum -y install yum-utils ARCH=$(arch) BRANCH="${BRANCH:-master}" sudo -E yum-config-manager --add-repo "http://download.opensuse.org/repositories/home:/katacontainers:/releases:/${ARCH}:/${BRANCH}/CentOS_${VERSION_ID}/home:katacontainers:releases:${ARCH}:${BRANCH}.repo" sudo -E yum -y install kata-runtime kata-proxy kata-shim
注意:我在安裝時,kata 剛剛發佈了 1.11 alpha0 版本,此版本中依賴的 qume-vanilla 版本爲4.1.1+git.99c5874a9b
,可是 repo 源中只有4.1.0+git.9e06029aea-6.1.x86_64
,所以在執行腳本以前,須要設置 BRANCH 環境變量:export BRANCH='stable-1.10'
安裝完成以後,執行命令 kata-runtime kata-check
檢查系統是否支持運行 kata runtime
WARN[0000] modprobe insert module failed: modprobe: FATAL: Module vhost_vsock not found. arch=amd64 error="exit status 1" module=vhost_vsock name=kata-runtime pid=21060 source=runtime ERRO[0000] kernel property not found arch=amd64 description="Host Support for Linux VM Sockets" name=vhost_vsock pid=21060 source=runtime type=module System is capable of running Kata Containers System can currently create Kata Containers
這裏的error 有多是我使用了VMware 嵌套虛擬化致使的,暫且放置。
首先啓用 containerd
systemctl enable containerd
修改 containerd 的配置文件,/etc/containerd/config.toml
,官網默認配置見連接:https://github. com/containerd/cri/blob/master/docs/config.md
須要注意的是:
k8s.io/pause-amd64:3.2
containerd 1.3.2 版本之前,不支持非安全的 registry,也就是說拉鏡像時使用的必須是 https 協議,自 己搭私有倉庫的同窗,能夠升級到 1.3.3,而且增長條目
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."your registry".tls] insecure_skip_verify = true
修改 kubelet 啓動參數
$ sudo mkdir -p /etc/systemd/system/kubelet.service.d/ $ cat << EOF | sudo tee /etc/systemd/system/kubelet.service.d/0-containerd.conf [Service] Environment="KUBELET_EXTRA_ARGS=--container-runtime=remote --runtime-request-timeout=15m --container-runtime-endpoint=unix:///run/containerd/containerd.sock" EOF
重啓 containerd 和 kubelet
systemctl daemon-reload systemctl start containerd systemctl restart kubelet
K8s 使用 Kata 有兩種方式:
首先在 containerd 的配置文件中,增長條目
# "plugins.cri.containerd.untrusted_workload_runtime" is a runtime to run untrusted workloads on it. [plugins.cri.containerd.untrusted_workload_runtime] # runtime_type is the runtime type to use in containerd e.g. io.containerd.runtime.v1.linux runtime_type = "io.containerd.kata.v2"
若使用的 kata 版本較低,不支持 shimV2,則使用條目
[plugins.cri.containerd.untrusted_workload_runtime] # runtime_type is the runtime type to use in containerd e.g. io.containerd.runtime.v1.linux runtime_type = "io.containerd.runtime.v1.linux" # runtime_engine is the name of the runtime engine used by containerd. runtime_engine = "/usr/bin/kata-runtime"
而後重啓 containerd systemctl daemon-reload & systemctl restart containerd
K8s 資源須要增長 annotation
annotations: io.kubernetes.cri.untrusted-workload: "true"
例如:
apiVersion: v1 kind: Pod metadata: name: nginx-untrusted annotations: io.kubernetes.cri.untrusted-workload: "true" spec: nodeName: k8s3 containers: - name: nginx image: nginx
這種模式下,相關軟件版本需求以下:
在 Containerd 配置文件中配置相關條目
[plugins.cri.containerd] no_pivot = false [plugins.cri.containerd.runtimes] [plugins.cri.containerd.runtimes.runc] runtime_type = "io.containerd.runc.v1" [plugins.cri.containerd.runtimes.runc.options] NoPivotRoot = false NoNewKeyring = false ShimCgroup = "" IoUid = 0 IoGid = 0 BinaryName = "runc" Root = "" CriuPath = "" SystemdCgroup = false [plugins.cri.containerd.runtimes.kata] runtime_type = "io.containerd.kata.v2" [plugins.cri.containerd.runtimes.katacli] runtime_type = "io.containerd.runc.v1" [plugins.cri.containerd.runtimes.katacli.options] NoPivotRoot = false NoNewKeyring = false ShimCgroup = "" IoUid = 0 IoGid = 0 BinaryName = "/usr/bin/kata-runtime" Root = "" CriuPath = "" SystemdCgroup = false
在 K8s 中建立 RuntimeClass
apiVersion: node.k8s.io/v1beta1 # RuntimeClass is defined in the node.k8s.io API group kind: RuntimeClass metadata: name: kata handler: kata # 這裏與containerd配置文件中的 [plugins.cri.containerd.runtimes.{handler}] 匹配
建立pod
apiVersion: v1 kind: Pod metadata: name: kata-nginx spec: runtimeClassName: kata containers: - name: nginx image: nginx ports: - containerPort: 80
經過kata-runtime list
能夠查看建立出來的 container
ID PID STATUS BUNDLE CREATED OWNER 5a512bfa19cd166c6cc6411dd7e1d20cc41490d0f2df064adab7871c9656a058 -1 running /run/containerd/io.containerd.runtime.v2.task/k8s.io/5a512bfa19cd166c6cc6411dd7e1d20cc41490d0f2df064adab7871c9656a058 2020-02-26T09:49:43.743162591Z #0 432628537c5d5606707e225869846542c68cb7406992dcf55a4b48a494f333b8 -1 running /run/containerd/io.containerd.runtime.v2.task/k8s.io/432628537c5d5606707e225869846542c68cb7406992dcf55a4b48a494f333b8 2020-02-26T09:49:47.928499707Z #0
若是喜歡,請關注個人公衆號,或者查看個人博客 http://packyzbq.gitee.io 我會不定時的發送我本身的學習記錄,你們互相學習交流哈~
![]()