Tutorial: Tutorial de Kubernetes de Shang Silicon Valley (entrada de K8 a competente)_bilibili_bilibili
Notas tomadas del capítulo del video: Capítulo 7 Ingreso de servicio
tema
Muchas aplicaciones dependen de cierta información de configuración durante su inicialización o tiempo de ejecución. La mayoría de las veces, es necesario ajustar los valores establecidos por los parámetros de configuración. ConfigMap es el método que utiliza Kubernetes para inyectar datos de configuración en los pods de aplicaciones.
Puede entenderse como un centro de gestión de archivos de configuración
notas
Crear desde el directorio
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# cat config_map_path/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# cat config_map_path/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# kubectl describe cm jjh-test-config
Name: jjh-test-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
BinaryData
====
Events: <none>
复制代码
Crear desde archivo
$ kubectl create configmap game-config-2 --from-file=docs/user- guide/configmap/kubectl/game.properties
$ kubectl get configmaps game-config-2 -o yaml
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# kubectl create configmap jjh-test-config-fromfile --from-file=./config_map_path/game.properties
configmap/jjh-test-config-fromfile created
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu/p37_config_map# kubectl describe cm jjh-test-config-fromfile
Name: jjh-test-config-fromfile
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
BinaryData
====
Events: <none>
复制代码
Crear con valor literal
$ kubectl create configmap special-config --from-literal=special. how=very --from- literal=special.type=charm
$ kubectl get configmaps special-config -o yaml
复制代码
Practica usando configmap
uso en entorno
- lista de mapas de puertos
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
---
apiVersion: v1
kind: ConfigMap
metadata:
name: env-config
namespace: default
data:
log_level: INFO
复制代码
- manifiesto de la vaina
apiVersion: v1
kind: Pod
metadata:
name: portmap-test-pod
spec:
containers:
- name: test-container
image: wangyanglinux/myapp:v1
command: [ "/bin/sh", "-c", "env" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
envFrom:
- configMapRef:
name
复制代码
- Ver resultados de configuración
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu# kubectl registra portmap-test-pod|grep -E "SPECIAL_LEVEL_KE|SPECIAL_TYPE_KEY" SPECIAL_TYPE_KEY=charm SPECIAL_LEVEL_KEY=muy
uso de parámetros de línea de comando de pod
apiVersion: v1
kind: Pod
metadata:
name: portmap-test-pord-cmd
spec:
containers:
- name: test-
image: wangyanglinux/myapp:v1
command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
restartPolicy: Never
复制代码
portmapConfig está montado bajo volumen
- Lista de recursos
apiVersion: v1
kind: Pod
metadata:
name: portmap-test-pod-volume
spec:
containers:
- name: test-container
image: wangyanglinux/myapp:v1
command: [ "/bin/sh", "-c", "sleep 360; cat /etc/config/special.how" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never
复制代码
- Aplíquelo, luego verifique la configuración
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu# kubectl apply -f p38_portmap_pod_volume.yaml
pod/portmap-test-pod-volume created
root@jjh-k8s-demo-master:~/k8s_yaml/bzhan_shangguigu# kubectl exec portmap-test-pod-volume -- cat /etc/config/special.how
very
复制代码
La ventaja de exportar la configuración del mapa de configuración al archivo en el pod es que admite la activación de actualizaciones continuas con un retraso de aproximadamente 10 s. Si está en env, no hay actualización continua