1. configMap描述信息
- ConfigMap功能在Kubernetes1.2版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。ConfigMap API给我们提供了向容器中注入配置信息的机制,ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象
2. ConfigMap的创建
2.1. 使用目录创建
[root@master1 ~]
gamp.properties ui.properties
[root@master1 ~]
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat=noGoodRotten
[root@master1 ~]
color.good=purple
coclor.bad=yellow
allow.textmode=true
[root@master1 ~]
configmap/game-config created
--from-file
指定在目录下的所有文件都会被用在configmap里面创建一个键值对,键的名字就是文件名,值是文件内容。
- game-config是创建configmap的名称,可以自定义
2.1.1. 查看创建是否成功:
[root@master1 ~]
NAME DATA AGE
game-config 2 4m55s
kube-root-ca.crt 1 2d
[root@master1 ~]
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
ui.properties:
----
color.good=purple
coclor.bad=yellow
allow.textmode=true
gamp.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat=noGoodRotten
Events: <none>
2.2. 使用文件创建:
- 只要指定一个文件就可以从单个文件中创建configmap
[root@master1 ~]
color.good=purple
coclor.bad=yellow
allow.textmode=true
[root@master1 ~]
configmap/ui-configmap created
2.2.1. 查看创建是否成功
[root@master1 ~]
NAME DATA AGE
game-config 2 12m
kube-root-ca.crt 1 2d
ui-configmap 1 9s
[root@master1 ~]
apiVersion: v1
data:
ui.properties: |
color.good=purple
coclor.bad=yellow
allow.textmode=true
kind: ConfigMap
metadata:
creationTimestamp: "2023-06-19T07:41:04Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:ui.properties: {}
manager: kubectl-create
operation: Update
time: "2023-06-19T07:41:04Z"
name: ui-configmap
namespace: default
resourceVersion: "89376"
uid: d4ebc140-c6d8-4b14-98fb-9062dfb4f0e4
2.3. 使用字面值创建:
- 使用文字值创建,利用
--from-literal
参数传递配置信息,该参数可以使用多次,格式如下:
[root@master1 ~]
configmap/special-config created
2.3.1. 查看是否创建成功:
[root@master1 ~]
NAME DATA AGE
game-config 2 19m
kube-root-ca.crt 1 2d
special-config 2 5s
ui-configmap 1 7m18s
[root@master1 ~]
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: "2023-06-19T07:48:17Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:special.how: {}
f:special.type: {}
manager: kubectl-create
operation: Update
time: "2023-06-19T07:48:17Z"
name: special-config
namespace: default
resourceVersion: "90117"
uid: a1016677-5f24-41c3-b681-b6ad0209085e
2.4. 使用yaml文件方式创建
apiVersion: v1
kind: ConfigMap
metadata:
name: name-configmap
data:
name1: andy
name2: bob
name3: lucy
[root@master1 configmap]
2.4.1. 查看是否创建成功
[root@master1 configmap]
NAME DATA AGE
game-config 2 132m
kube-root-ca.crt 1 2d2h
name-configmap 3 15s
nginx-config 1 95m
special-config 2 112m
ui-configmap 1 119m
[root@master1 configmap]
Name: name-configmap
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
name1:
----
andy
name2:
----
bob
name3:
----
lucy
Events: <none>
[root@master1 configmap]
apiVersion: v1
kind: ConfigMap
metadata:
name: name-configmap
data:
name1: andy
name2: bob
name3: lucy
3. 创建nginx并把nginx的配置文件存放在configmap
3.1. 创建configmap
[root@master1 ~]
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
//创建configmap
[root@master1 ~]
//查看是否创建成功:
[root@master1 ~]
NAME DATA AGE
game-config 2 38m
kube-root-ca.crt 1 2d
nginx-config 1 69s
special-config 2 18m
ui-configmap 1 25m
3.2. 用deployment创建nginx并指定nginx-config
vim nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
spec:
type: ClusterIP
selector:
app: nginx
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
[root@master1 yaml]
[root@master1 yaml]
NAME READY STATUS RESTARTS AGE
nginx-deployment-75dfc89886-8sbxf 1/1 Running 0 7m42s
nginx-deployment-75dfc89886-r4bvn 1/1 Running 0 7m42s
nginx-deployment-75dfc89886-t2664 1/1 Running 0 7m42s
[root@master1 yaml]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.10.0.1 <none> 443/TCP 2d2h
nginx-service ClusterIP 10.10.37.239 <none> 80/TCP 8m7s