• k8s pod注入环境变量


    1. k8s-pod注入环境变量

    • 变量值几种定义方式:

      • 自定义变量值
      • 变量值从Pod属性获取
      • 变量值从Secret、ConfigMap获取
    • 参考yaml文件

      apiVersion: v1
      kind: Pod
      metadata:
        name: pod-envars
      spec:
        containers:
          - name: test
            image: busybox
            command: [ "sh", "-c", "sleep 36000"]
            env:
              - name: MY_NODE_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: spec.nodeName
              - name: MY_POD_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.name
              - name: MY_POD_NAMESPACE
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
              - name: MY_POD_IP
                valueFrom:
                  fieldRef:
                    fieldPath: status.podIP
              - name: ABC
                value: "123456"   # 自定义变量,后面要加 ""
      

    2. 案例

    • 编写一个pod-var.yaml配置文件

      [root@k8s-master yaml]# vim pod-var.yaml 
      [root@k8s-master yaml]# cat pod-var.yaml 
      apiVersion: v1
      kind: Pod
      metadata:
        name: pod-envars
      spec:
        containers:
          - name: test
            image: busybox
            command: [ "sh", "-c", "sleep 36000"]
            env:
              - name: MY_NODE_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: spec.nodeName
              - name: MY_POD_NAME
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.name
              - name: MY_POD_NAMESPACE
                valueFrom:
                  fieldRef:
                    fieldPath: metadata.namespace
              - name: MY_POD_IP
                valueFrom:
                  fieldRef:
                    fieldPath: status.podIP
              - name: ABC
                value: "123456"   # 自定义变量,后面要加 ""
      
    • 我们可以通过kubectl describe 查看一个pod或其他的详细信息

      [root@k8s-master yaml]#  kubectl  get pods
      NAME         READY   STATUS    RESTARTS   AGE
      probe-demo   1/1     Running   0          123m
                    
      [root@k8s-master yaml]# kubectl describe pods probe-demo 
      Name:         probe-demo
      Namespace:    default
      Priority:     0
      Node:         k8s-node1/192.168.0.202
      Start Time:   Sun, 29 Nov 2020 14:44:23 +0800
      Labels:       test=liveness
      Annotations:  cni.projectcalico.org/podIP: 10.244.36.90/32
                    cni.projectcalico.org/podIPs: 10.244.36.90/32
      Status:       Running
      IP:           10.244.36.90
      IPs:
        IP:  10.244.36.90
      Containers:
        web:
          Container ID:   docker://7e79b87e08f9df2097ac0d2363347dd00eb2e40636c22e0d176f670cc1937548
          Image:          nginx
          Image ID:       docker-pullable://nginx@sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
          Port:           80/TCP
          Host Port:      0/TCP
          State:          Running
            Started:      Sun, 29 Nov 2020 14:44:31 +0800
          Ready:          True
          Restart Count:  0
          Liveness:       tcp-socket :80 delay=0s timeout=1s period=10s #success=1 #failure=3
          Readiness:      tcp-socket :80 delay=30s timeout=1s period=10s #success=1 #failure=3
          Startup:        tcp-socket :80 delay=0s timeout=1s period=10s #success=1 #failure=3
          Environment:    <none>
          Mounts:
            /var/run/secrets/kubernetes.io/serviceaccount from default-token-8pppk (ro)
      Conditions:
        Type              Status
        Initialized       True 
        Ready             True 
        ContainersReady   True 
        PodScheduled      True 
      Volumes:
        default-token-8pppk:
          Type:        Secret (a volume populated by a Secret)
          SecretName:  default-token-8pppk
          Optional:    false
      QoS Class:       BestEffort
      Node-Selectors:  <none>
      Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                       node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
      Events:          <none>
      
      
    • 启动服务

      [root@k8s-master yaml]# kubectl apply -f pod-var.yaml 
      pod/pod-envars created
      
      
    • 进入容器看看,变量是否可以使用

      [root@k8s-master yaml]# kubectl exec -it  pod-envars  -- /bin/sh
      / # echo $MY_NODE_NAME
      k8s-node1
      / # echo $MY_POD_NAME
      pod-envars
      / # echo $MY_POD_IP
      10.244.36.91
      
    • 小结:

      1. 在使用k8s 配置文件传入变量的时候,需要注意在变量上,整数或者字符串需要使用 单引号或双引号,否则或报错
  • 相关阅读:
    Prometheus安装配置
    golang常见错误
    golang数组切片map chan 结构体 初始化
    formdata和xwwwformurlencode的区别
    golang reflect反射
    golang 包之flag包
    golang signal信号处理
    golang import
    golang nil
    golang projectlayout
  • 原文地址:https://www.cnblogs.com/scajy/p/15476020.html
Copyright © 2020-2023  润新知