向容器传递命令行参数
为每个容器配置环境变量
通过特殊的卷将配置文件挂载到容器中
向容器传递命令行参数在Kubernetes中定义容器时,镜像的ENTRYPOINT和CMD都可以被覆盖(但是在Docker中,镜像的ENTRYPOINT是不能覆盖的)。仅需在容器定义中设置command和args的值。
构建一个镜像loopechodate.sh:接收一个时间间隔的参数,追加输出当前时间到 /tmp/a.txt
#! /bin/sh trap "exit" SIGINT echo "interval is : $1" while : do echo -e "$(date)" >> /tmp/a.txt sleep $1 doneDockerfile
FROM alpine COPY loopechodate.sh /bin/ ENTRYPOINT ["/bin/loopechodate.sh"] CMD ["5"]构建、推送
-> [root@kube0.vm] [~] docker build -t registry.cn-hangzhou.aliyuncs.com/orzi/loopechodate . -> [root@kube0.vm] [~] docker push registry.cn-hangzhou.aliyuncs.com/orzi/loopechodate 运行Pod # cat config-cli.yaml apiVersion: v1 kind: Pod metadata: name: config-cli spec: containers: - name: config-cli image: registry.cn-hangzhou.aliyuncs.com/orzi/loopechodate args: ["2"] # 间隔时间2s创建这个pod,然后查看一下logs
-> [root@kube0.vm] [~] k create -f config-cli.yaml pod/config-cli created -> [root@kube0.vm] [~] k exec -it config-cli cat /tmp/a.txt Sun May 24 15:49:37 UTC 2020 Sun May 24 15:49:39 UTC 2020 为容器设置环境变量Kubernetes中通过容器的env属性定义环境变量,采用$(VAR)语法在环境变量值中引用其他变量。
定义了两个环境变量FIRST_VAR,和引用了FIRST_VAR的SECOND_VAR。
# config-env.yaml apiVersion: v1 kind: Pod metadata: name: config-env spec: containers: - name: config-env image: nginx:alpine env: - name: FIRST_VAR value: "Hello" - name: SECOND_VAR value: "$(FIRST_VAR) World!"创建查看
-> [root@kube0.vm] [~] k create -f config-env.yaml pod/config-env created -> [root@kube0.vm] [~] k exec config-env env PATH=http://www.likecs.com/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=config-env FIRST_VAR=Hello SECOND_VAR=Hello World! ....... ConfigMapConfigMap本质上是一个键值对,值可以使短字面量,也可以是文件
创建ConfigMapkubectl create configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run] [options]
kubectl create命令支持从字面量(--from-literal),文件、目录(--from-file)、以及环境变量文件(--from-env-file)创建。而且不同选项可以合并,但是--from-env-file不能与--from-literal和--from-file一起指定。
下面是使用字面量、文件、目录合并创建。
-> [root@kube0.vm] [~] k create configmap mycm --from-literal=interval=3 --from-file=Dockerfile --from-file=http://www.likecs.com/root/configdir configmap/mycm created -> [root@kube0.vm] [~] k describe cm mycm Name: mycm Namespace: default Labels: <none> Annotations: <none> Data ==== Dockerfile: ---- FROM alpine COPY loopechodate.sh /bin/ ENTRYPOINT ["/bin/loopechodate.sh"] CMD ["5"] a.txt: ---- this is configdir/a.txt b.txt: ---- this is configdir/b.txt interval: ---- 3 Events: <none>使用环境变量文件创建
-> [root@kube0.vm] [~] echo -e "env1=1111\nenv2=2222" | tee test.env env1=1111 env2=2222 -> [root@kube0.vm] [~] k create cm envcm --from-env-file=test.env configmap/envcm created -> [root@kube0.vm] [~] k describe cm envcm Name: envcm Namespace: default Labels: <none> Annotations: <none> Data ==== env1: ---- 1111 env2: ---- 2222 Events: <none> 传递ConfigMap条目作为环境变量定义一个Pod,引用了mycm中的两个key。
# config-env-cm.yaml apiVersion: v1 kind: Pod metadata: name: config-env-cm spec: containers: - name: config-env-cm image: nginx:alpine env: - name: INTERVAL valueFrom: configMapKeyRef: # 引用configMap中的内容 name: mycm # configMap的名字 key: interval # 引用哪个键 - name: ATXT valueFrom: configMapKeyRef: name: mycm key: a.txt创建查看
-> [root@kube0.vm] [~] k create -f config-env-cm.yaml pod/config-env-cm created -> [root@kube0.vm] [~] k exec config-env-cm env PATH=http://www.likecs.com/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=config-env-cm INTERVAL=3 ATXT=this is configdir/a.txt .... 一次性传递ConfigMap所有条目作为环境变量 # config-env-cmall.yaml apiVersion: v1 kind: Pod metadata: name: config-env-cmall spec: containers: - name: config-env-cmall image: nginx:alpine envFrom: - prefix: CONFIG_ configMapRef: name: mycm创建查看
-> [root@kube0.vm] [~] k create -f config-env-cmall.yaml pod/config-env-cmall created -> [root@kube0.vm] [~] k exec config-env-cmall env PATH=http://www.likecs.com/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=config-env-cmall CONFIG_interval=3 CONFIG_Dockerfile=FROM alpine COPY loopechodate.sh /bin/ ENTRYPOINT ["/bin/loopechodate.sh"] CMD ["5"] CONFIG_a.txt=this is configdir/a.txt CONFIG_b.txt=this is configdir/b.txt ........ 传递ConfigMap条目作为命令行参数