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