[英]Inject code/files directly into a container in Kubernetes on Google Cloud Engine
How can I inject code/files directly into a container in Kubernetes on Google Cloud Engine, similar to the way that you can mount host files / directories with Docker, eg如何将代码/文件直接注入 Google Cloud Engine 上 Kubernetes 的容器中,类似于使用 Docker 挂载主机文件/目录的方式,例如
docker run -d --name nginx -p 443:443 -v "/nginx.ssl.conf:/etc/nginx/conf.d/default.conf"
Thanks谢谢
It is possible to use ConfigMaps to achieve that goal:可以使用 ConfigMaps 来实现该目标:
The following example mounts a mariadb configuration file into a mariadb POD:以下示例将 mariadb 配置文件挂载到 mariadb POD 中:
ConfigMap配置映射
apiVersion: v1
data:
charset.cnf: |
[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8
[mysqld]
#
# * Character sets
#
# Default is Latin1, if you need UTF-8 set all this (also in client section)
#
character-set-server = utf8
collation-server = utf8_unicode_ci
kind: ConfigMap
metadata:
name: mariadb-configmap
MariaDB deployment MariaDB 部署
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mariadb
labels:
app: mariadb
spec:
replicas: 1
template:
metadata:
labels:
app: mariadb
version: 10.1.16
spec:
containers:
- name: mariadb
image: mariadb:10.1.16
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mariadb
key: rootpassword
volumeMounts:
- name: mariadb-data
mountPath: /var/lib/mysql
- name: mariadb-config-file
mountPath: /etc/mysql/conf.d
volumes:
- name: mariadb-data
hostPath:
path: /var/lib/data/mariadb
- name: mariadb-config-file
configMap:
name: mariadb-configmap
It is also possible to use subPath feature that is available in kubernetes from version 1.3, as stated here .也可以使用子路径的功能,为1.3版本kubernetes可用,说明这里。
I'm not sure you can do that exactly.我不确定你能做到这一点。 Kubernetes does things quite differently than docker, and isn't really ideal for interacting with the 'host' you are probably used to with docker.
Kubernetes 的工作方式与 docker 完全不同,并且对于与您可能已经习惯使用 docker 的“主机”进行交互来说并不是真正理想的选择。
A few alternative possibilities come to mind.我想到了一些替代的可能性。 First, and probably least ideal but closest to what you are asking, would be to add the file after the container is running, either by adding
commands
or args
to the pod spec, or using kubectl exec
and echo'ing the contents into the file.首先,可能是最不理想但最接近您要求的是在容器运行后添加文件,方法是向 pod 规范添加
commands
或args
,或者使用kubectl exec
并将内容回显到文件中. Second would be to create a volume where that file already exists, eg create a GCE or EBS disk, add that file, and then mount the file location (read-only) in the container's spec.第二种是在该文件已经存在的地方创建一个卷,例如创建一个 GCE 或 EBS 磁盘,添加该文件,然后在容器的规范中安装文件位置(只读)。 Third, would be to create a new docker image where that file or other code already exists.
第三,是在该文件或其他代码已经存在的地方创建一个新的 docker 镜像。
For the first option, the kubectl exec
would be for one-off jobs, it isn't very scalable/repeatable.对于第一个选项,
kubectl exec
将用于一次性作业,它的可扩展性/可重复性不是很好。 Any creation/fetching at runtime adds that much overhead to the start time for the container, so I normally go with the third option, building a new docker image whenever the file or code changes.运行时的任何创建/获取都会为容器的启动时间增加大量开销,因此我通常采用第三个选项,每当文件或代码更改时构建新的 docker 映像。 The more you change it, the more you'll probably want a CI system (like drone) to help automate the process.
你改变的越多,你就越可能需要一个 CI 系统(比如无人机)来帮助自动化这个过程。
Add a comment if I should expand any of these options with more details.如果我应该用更多细节扩展这些选项中的任何一个,请添加评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.