一、配置容器的环境变量
1.1 编写yaml文件
如下,编写pod_env.yaml文件,内容如下,通过env关键字来定义环境变量,环境变量通过name,value对来设置,如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: pod-env
namespace: dev
labels:
user: redrose2100
spec:
containers:
- name: nginx
image: nginx:1.17.1
env:
- name: "username"
value: "admin"
- name: "password"
value: "admin123"
1.2 创建资源
使用如下命令创建资源
[root@master pod]# kubectl apply -f pod_env.yaml
namespace/dev created
pod/pod-env created
[root@master pod]#
1.3 进入容器查看环境变量
使用如下命令进入容器,并打印环境变量username和password,如下,均与设置一致
[root@master pod]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-env 1/1 Running 0 3m
[root@master pod]# kubectl exec pod-env -n dev -it -c nginx /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pod-env:/# echo $username
admin
root@pod-env:/# echo $password
admin123
root@pod-env:/#
1.4 删除资源
使用如下命令删除资源
[root@master pod]# kubectl delete -f pod_env.yaml
namespace "dev" deleted
pod "pod-env" deleted
[root@master pod]#