首页 > 其他分享 >K8s的环境变量

K8s的环境变量

时间:2023-02-22 10:56:47浏览次数:54  
标签:name fieldPath mysql pod K8s MY 环境变量 metadata

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"   # 自定义变量,后面要加 ""

案例

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

    $ vim pod-var.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或其他的详细信息

    $  kubectl  get pods
    NAME         READY   STATUS    RESTARTS   AGE
    probe-demo   1/1     Running   0          123m
                  
    $ 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>
    
  • 启动服务

    $ kubectl apply -f pod-var.yaml 
    pod/pod-envars created
    
  • 进入容器看看,变量是否可以使用

    $ 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
    

部署一个mysql

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: 10.0.0.9:4000/mymysql:5.7.39
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3306
          #这里引用就是自定义环境变量
          env:
            - name: MYSQL_DATABASE
              value: "wp"
            - name: MYSQL_USER
              value: "wp"
            - name: MYSQL_PASSWORD
              value: "111111"
            - name: MYSQL_ROOT_PASSWORD
              value: "111111"
          volumeMounts:
            - name: mysql-db
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-db
          persistentVolumeClaim:
            claimName: mysql-pvc

总结

在使用k8s 配置文件传入变量的时候,需要注意在变量上,整数或者字符串需要使用 单引号或双引号,否则或报错

标签:name,fieldPath,mysql,pod,K8s,MY,环境变量,metadata
From: https://www.cnblogs.com/guangdelw/p/17143569.html

相关文章

  • k8s介绍与重要概念
    1、k8s介绍Kubernetes(k8s)是Google开源的容器集群管理系统。在Docker技术的基础上,为容器化的应用提供部署运行、资源调度、服务发现和动态伸缩等一系列完整功能,提高了大......
  • K8S部署&DevOps
    一、k8s集群部署1、k8s快速入门1)、简介Kubernetes简称k8s。是用于自动部署,扩展和管理容器化应用程序的开源系统。中文官网:https://kubernetes.io/zh/中文社区:https......
  • k8s服务端二进制部署-kubectl
    本文章是 k8s二进制高可用集群部署 的分支。详细步骤请参考目录。二进制下载地址压缩包下载:https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG......
  • k8s多节点二进制部署以及Dashboard UI
    一、多Maser集群架构的了解Kubernetes作为容器集群系统,通过健康检查+重启策略实现了Pod故障自我修复能力,通过调度算法实现将Pod分布式部署,并保持预期副本数,根据Node失效......
  • macOS永久添加环境变量
    1vim~/.bash_profile修改文件,假如要添加的环境变量为/usr/local/pgsql/bin,则在末尾添加一行:1exportPATH=/usr/local/pgsql/bin:$PATH依次按下esc、:、w、q四个......
  • k8s 控制平面地址修改
    修改kubeadm-configconfigmapkubectleditcm-nkube-systemkubeadm-config对应的位置dataClusterConfigurationcontrolPlaneEndpoint修改kube-proxyc......
  • springboot读取配置信息,环境变量的方法
    前提配置文件一般是值resources目录下的application.properties或application.yml,其中保存着配置信息代码中实现配置注入的方法使用@Value注解@Value("${test.msg}")......
  • 修复 K8s SSL/TLS 漏洞(CVE-2016-2183)指南
    作者:老Z,中电信数智科技有限公司山东分公司运维架构师,云原生爱好者,目前专注于云原生运维,云原生领域技术栈涉及Kubernetes、KubeSphere、DevOps、OpenStack、Ansible等。前......
  • 修复 K8s SSL/TLS 漏洞(CVE-2016-2183)指南
    作者:老Z,中电信数智科技有限公司山东分公司运维架构师,云原生爱好者,目前专注于云原生运维,云原生领域技术栈涉及Kubernetes、KubeSphere、DevOps、OpenStack、Ansible等。......
  • k8s之keepalive+nginx高可用集群部署
    1、安装nginx、keepalivedyuminstallnginxkeepalived-y2、更新nginx配置文件stream{log_formatmain'$remote_addr$upstream_addr-[$time_local]$status$up......