Chapter 5
本节内容: configmap, secret
参数化运行容器
作用
支持在运行docker container时候添加参数
就像启动MySQL container时候,可以设置MYSQL_ROOT_PASSWORD环境变量来改变mysql的root用户密码
参数化方式
Dockerfile中指定ENTRYPOINT和CMD
创建容器时添加参数:
docker run <image> <args>
ENTRYPOINT 之 shell 和 exec 形式的区别
shell形式 —— 如ENTRYPOINT node app.js。
shell会多运行一个shell程序
exec形式 —— 如ENTRYPOINT["node","app.js"]
实践
创建镜像
使用地址创建一个image
运行镜像
使用
docker run -it image-name运行镜像 -> 查看输出或者使用
docker run -it image-name 5-> 查看输出
参数化运行pod
作用
在容器运行是设定一下参数,如mysql的密码
可以配置容器的环境变量,以达到某种效果
实践
在pod的yaml中配置容器的参数
配置容器的环境变量
ConfigMap
作用
将需要配置的东西抽象出来,pod只需要引用configMap,而不需要关心具体的值。
实践
查看configmap:
kubectl get configmap [name -o yaml #输出成yaml]删除configmap:
kubectl delete configmap name使用yaml文件创建configmap:
kubectl create -f configmap.yaml指定KV以创建configmap:
kubectl create configmap myconfigmap --from-literal=foo=bar --from-literal=bar=baz使用配置文件创建configmap:
kubectl create configmap my-config --from-file=ssl.conf#文件名为K,内容为V,文件名有效才能建kubectl create configmap my-config --from-file=key=ssl.conf# 可以指定key
使用文件夹创建configmap:
kubectl create configmap my-config --from-file=configmap-files#文件名为K,内容为V,文件名有效才能建将configmap的value传递给pod
将多个环境变量的值传入容器
让configmap的值在容器启动变量上生效
初始化值到环境变量,在启动参数上引用环境变量
将值为文件内容的configmap挂载到容器内
测试
kubectl port-forward fortune-configmap-volume 8080:80 &curl -H "Accept-Encoding: gzip" -I localhost:8080# 有开启gzip则成功kubectl exec fortune-configmap-volume -c web-server -- ls /etc/nginx/conf.d# 查看挂载文件
configmap 在被更新后会同步文件到pod,但是如果pod不支持重载,那只有新的pod会生效。 同步并不是同步的,所有会不一致的情况。 用items时候不能用subPath 使用subPath挂载时不能接收configMap的更新
Secret
作用
作为一种kube资源,存储敏感的数据,以KV键值存储。
secret存储在内存中,而不是磁盘。
每个pod都会被挂载一个default-token-xx的secret,作为访问kube api-server的凭证,如果在需要访问api-server时。
secret limit 1M
实践
将secret挂载到容器内的目录
创建一个secret:
kubectl create secret generic fortune-https --from-file=https.key --from-file=https.cert --from-file=foo#生成https所需的key,cert,以前有教程创建一个configmap:
kubectl create configmap ssl-configmap-2 --from-file=ssl.conf --from-literal=sleep-interval=10创建pod
测试pod
kubectl port-forward fortune-https 8443:443 &# 将容器端口放出curl https://localhost:8443 -k -v# 在kube中直接访问容器,查看连接握手。
使用secret设置dockerhub账号来拉取私有镜像
创建dockerhub账号的secret:
kubectl create secret docker-registry dockerhub-secret --docker-username=myname --docker-password=mypassword --docker-email=my.email@example.com在pod的yaml中配置拉取镜像时的secret
使用secret设置环境变量 不建议使用,不安全
配置pod yaml中的env值
Last updated