コンテナ内で発生する環境変数
1 configmapの名前nginxの-設定を作成し、変数名nginx_portの値が80であり、変数名の値はSERVER_NAME www.test.comあります
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=www.test.com
2.確認してください
kubectl get cm nginx-config -o yaml #其中data就是环境变量
kubectl describe cm nginx-config
3.ポッドを作成し、定義された環境変数の上に引用しました
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80
env:
- name: NGINX_SERVER_PORT #pod容器中的环境变量名字
valueFrom:
configMapKeyRef:
name: nginx-config #configmap的名字
key: nginx_port #configmap中定义的key
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name
4.確認してください
ConfigMap volumeMountを使用することにより
1.設定ファイルを作成します。
cat >www.conf <<EOF
server {
server_name www.test.com;
listen 80;
root /app/webroot/;
}
EOF
2. configmapを作成し、値としてコンテンツを提出、あなたがキーをカスタマイズすることができ、あなたはまた、キーとしてファイル名を使用することができます
方式一: kubectl create configmap nginx-file --from-file=./www.conf
方式二:kubectl create configmap nginx-file --from-file=www.conf=./www.conf
3.展開リソースを作成します。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.11
ports:
- containerPort: 80
volumeMounts: #挂载
- name: nginxconfig #调用下面定义的存储卷, 名字要一致
mountPath: /etc/nginx/conf.d/ #pod容器目录
readOnly: true #只读
volumes: #创建一个存储卷
- name: nginxconfig #定义存储卷名字
configMap: #定义存储卷类型为configMap
name: nginx-file #引用名字叫nginx-file的configmap,
4.確認してください
configmap制限を使用します
1.ConfigMap必须在Pod之前创建
2.ConfigMap受Namespace限制, 只有处于相同的NameSpace中的Pod才可以引用它