ConfigMap用来保存key-value pair配置数据,数据可以在pod容器中挂载. 可以简单理解为Linux系统中的/etc目录,用来存储配置文件的目录。这里以将ConfigMap数据作为容器内配置文件举例. 本例中,
ConfigMap挂载到容器后, 其每一个item会成为挂载目录下的一个文件, key为文件名, 文件内容是key对应的值.
前台UI如下:
添加ConfigMap内容:
点击保存, 将键值对数据以map方式传入后台, 构建ConfigMap对象并进行创建:
构建对象:
/**
* 构建ConfigMap对象
* @param ku8ConfigMap
*/
public void createConfigMap(Ku8ConfigMap ku8ConfigMap) {
k8sAPIService.createConfigMap(
ku8ConfigMap.getNamespace(),
new ConfigMapBuilder()
.withKind("ConfigMap")
.withApiVersion("v1")
.withNewMetadata()
.withName(ku8ConfigMap.getName())
.withNamespace(ku8ConfigMap.getNamespace())
.endMetadata()
.withData(ku8ConfigMap.getData())
.build()
);
}
创建对象:
在kubernetes集群查看ConfigMap创建结果:
查看创建成功的pod:
/**
* 创建ConfigMap
* @param namespace
* @param configMap
* @return
*/
public ConfigMap createConfigMap(String namespace, ConfigMap configMap) {
return getClient().configMaps().inNamespace(namespace).create(configMap);
}
Ku8ConfigMap实体如下:
package com.***.entity;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
@XmlRootElement
public class Ku8ConfigMap implements Serializable {
private static final long serialVersionUID = -2929072646302058679L;
private Integer configMapId;
private String name;
private Integer namespaceId;
private Date createTime;
private Date updateTime;
private Map<String, String> data;
private String namespace;
public Integer getConfigMapId() {
return configMapId;
}
public void setConfigMapId(Integer configMapId) {
this.configMapId = configMapId;
}
public Integer getNamespaceId() {
return namespaceId;
}
public void setNamespaceId(Integer namespaceId) {
this.namespaceId = namespaceId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Map<String, String> getData() {
return data;
}
public void setData(Map<String, String> data) {
this.data = data;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
}
创建Deployment时, 挂载ConfigMap:
后端操作:
// 遍历需要挂载的ConfigMap列表
for (VolumeDto confMap : ku8ConfMapArray) {
ConfigMap f8ConfigMap = k8sAPIService.getConfigMapByName(namespace, confMap.getName());
if (f8ConfigMap == null) {
throw new BusinessException("cant find configMap:" + confMap.getName());
}
for (Container f8Container : f8Containers) { // 遍历需要创建的容器
List<VolumeMount> f8VolumeMounts = f8Container.getVolumeMounts();
if (f8VolumeMounts == null) {
f8VolumeMounts = new ArrayList<VolumeMount>();
}
// 在容器中挂载ConfigMap
VolumeMount f8VolumeMount = new VolumeMountBuilder().withName(confMap.getName()).withMountPath(confMap.getPath()).build();
f8VolumeMounts.add(f8VolumeMount);
f8Container.setVolumeMounts(f8VolumeMounts);
}
// 将ConfigMap挂载为pod的一个volume
List<KeyToPath> f8KeyToPathList = new ArrayList<KeyToPath>();
f8ConfigMap.getData().forEach((k, v) -> f8KeyToPathList.add(new KeyToPathBuilder().withKey(k).withPath(k).build())); // key既为键名称又作为挂载结果的文件名称
Volume f8Volume = new VolumeBuilder().withName(confMap.getName()).withNewConfigMap().withName(confMap.getName()).withItems(f8KeyToPathList).endConfigMap().build();
f8Volumes.add(f8Volume);
}
查看创建成功的pod:
查看
ConfigMap
挂载结果: