Kubernetes:通过hostPath挂载的可执行文件失败,发现错误



我试图从我的pod运行docker来启动docker容器(k8s内的docker)。为了从主机获得docker映像,我正在安装docker。sock和docker命令在我的pod通过hostPath。即使我发现文件挂载,我不能执行docker命令,它会失败,命令没有发现错误。

apiVersion: v1
kind: Pod
metadata:
name: alphine
labels:
app: alphine
spec:
containers:
- name: alpine
image: alpine
securityContext:
privileged: true
volumeMounts:
- name: tmp
mountPath: /tmp
- name: docker-sock
mountPath: /var/run/docker.sock
- name: docker-cmd  # docker command from host.
mountPath: /usr/bin/docker
- name: lib64  # for running docker from the host.
mountPath: /lib64
readOnly: true
- name: usr-lib64  # for running docker from the host.
mountPath: /usr/lib64
readOnly: true
command: ["sleep", "infinity"]
volumes:
- name: tmp
emptyDir: {}
- name: docker-cmd
hostPath:
path: /usr/bin/docker
type: File
- name: lib64
hostPath:
path: /lib64
type: Directory
- name: usr-lib64
hostPath:
path: /usr/lib64
type: Directory
- name: docker-sock
hostPath:
path: /var/run/docker.sock
type: Socket

我正在用下面的命令启动minikube

minikube start --memory=16g --cpus=2 --disk-size=10g 
-p mycluster 
--extra-config=apiserver.enable-admission-plugins=PodSecurityPolicy 
--addons=pod-security-policy --wait=all

在Docker环境中也一样。在这里,我将使用下面的命令

启动容器
docker create
--name=mypod
--read-only
--restart=on-failure
-v /usr/bin/docker:/usr/bin/docker:ro
-v /lib64:/lib64:ro
-v /usr/lib64:/usr/lib64:ro
-v /var/run/docker.sock:/var/run/docker.sock
alphine
docker start mypod

我错过了什么吗?任何指示/参考是赞赏的。提前感谢。

minikube集群的主机路径与本地机器的主机路径不同。当您在本地机器上运行docker命令时,它将使用存在文件/目录的机器的hostPath。但是这些文件/目录不存在于minkube集群节点中。

解决方案:

  1. 创建Dockerfile并在构建镜像时复制文件/目录:
FROM alphine:latest
COPY /var/run/docker.sock /var/run/docker.sock
... ...
... ...
CMD ["sleep", "infinity"]
  1. 在您的pod中使用此图像:
apiVersion: v1
kind: Pod
metadata:
name: alphine
labels:
app: alphine
spec:
containers:
- name: your_image
image: your_image:latest
securityContext:
privileged: true

在客户机操作系统中似乎有所不同。我尝试了Ubuntu:latest而不是alpine作为我的客户机映像,并且我能够从我的客户机访问docker命令。

不确定这两个客户操作系统之间是否有安全差异

相关内容

最新更新