使用K8s的ConfigMap服务
目录
前言
我在前两篇文章里,介绍如何搭建Dashboard以及K8s的架构和实现原理,这篇文章,我么尝试使用ConfigMap创建Redis服务。
创建Redis服务
由于当前集群的安装的版本与官方的不一致,按照官方最新的教程来实现的话,可能会出现异常问题。在这里,我们尝试用kubectl的1.12版本来使用ConfigMap创建Redis服务。
首先创建configmap目录,存放我们的配置文件。
mkdir configmap
cat <<EOF >>./redis-config
maxmemory 2mb
maxmemory-policy allkeys-lru
EOF
在这里,我们定义redis的两个配置选项:最大内存容量为2MB,超过容量的策略为,首先尝试删除最近使用较少的(LRU)键,以便为添加的新数据腾出空间。
创建ConfigMap,名称为example-redis-config,配置文件为configmap/redis-config。
kubectl create configmap example-redis-config --from-file=configmap/redis-config
检查创建的configmap配置文件
kubectl get configmap example-redis-config -o yaml
apiVersion: v1
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
creationTimestamp: 2019-07-14T06:16:30Z
name: example-redis-config
namespace: default
resourceVersion: "2565685803"
selfLink: /api/v1/namespaces/default/configmaps/example-redis-config
uid: eb5f8b0d-a5fe-11e9-b0be-32a10a974892
新建redis-pod.yaml配置文件
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: example-redis-config
items:
- key: redis-config
path: redis.conf
创建pod
kubectl create -f configmap/redis-pod.yaml
pod/redis created
检查创建的pods
kuberctl get pods
NAME READY STATUS RESTARTS AGE
redis 1/1 Running 0 21s
验证创建的服务
kubectl exec -it redis redis-cli
127.0.0.1:6379> CONFIG get maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> CONFIG get maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"
127.0.0.1:6379>
常用命令
比如创建服务kubectl create configmap命令,其中是要分配给ConfigMap的名称,是从中提取数据的目录,文件或文字值。
kubectl create configmap <map-name> <data-source>