ConfigMap
镜像使用过程中,经常需要利用配置文件、启动脚步等方式来影响容器的运行方式,如果仅有少量配置,我们可以用环境变量的方式进行配置。然而对于一些较为复杂的配置,k8s提供了configmap解决方案
Configmap api 资源存储键/值对配置数据,pod 里面可以使用该数据.
ConfigMap和Secret类似,但是ConfigMap 可以更加方便的处理不包含敏感信息的字符串。
当CconfigMap 以数据卷的形式挂载进Pod的时,这时更新ConfigMap(或删除掉重建ConfigMap),Pod内挂载的配置新会热更新。这时可以增加一些监测配置文件变更脚本,然后reload 对应服务
ConfigMap的API概念上来说是很简单,ConfigMap的类型只是键值组。应当可以从不同角度配置.在一个Pod里面使用ConfigMap 大致三种方式。
1、命令行参数
2、环境变量
3、数据卷类型
变量设置成configMap
[root@k8s-master-001 ~]# kubectl create configmap test --from-literal=a=1 --from-literal=b=2
configmap/test created
[root@k8s-master-001 ~]# kubectl get configmap test -o yaml
apiVersion: v1
data:
a: "1"
b: "2"
kind: ConfigMap
metadata:
creationTimestamp: "2021-06-11T03:25:55Z"
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:data:
.: {}
f:a: {}
f:b: {}
manager: kubectl
operation: Update
time: "2021-06-11T03:25:55Z"
name: test
namespace: kube-system
resourceVersion: "52924439"
selfLink: /api/v1/namespaces/kube-system/configmaps/test
uid: 45cf5e6d-6b2d-43e6-85a5-ae0fe99cb4bf
nginx 配置文件设置configmap
#cat nginx.conf
user apps apps;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log /apps/logs/nginx/nginx_error.log error;
pid /apps/logs/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
#geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;
#include geo.conf;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 80m;
sendfile on;
tcp_nopush on;
client_body_timeout 5;
client_header_timeout 5;
keepalive_timeout 5;
send_timeout 5;
open_file_cache max=65535 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/json application/javascript;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
proxy_temp_path /dev/shm/temp;
proxy_cache_path /dev/shm/cache levels=2:2:2 keys_zone=cache_go:200m inactive=5d max_size=7g;
log_format log_access "$remote_addr" "\t$remote_user" "\t$time_local" "\t$request" "\t$request_time" "\t$upstream_response_time" "\t$status" "\t$body_bytes_sent" "\t$http_referer" "\t$http_user_agent" "\t$http_x_forwarded_for" "\t$host" "\t$hostname" "\tCustomName1" "\t$http_Cdn_Src_Ip" "\t$http_Cdn_Src_Port" "\tCustomName4" "\tCustomName5" "\tCustomName6" "\tCustomName7" "\tCustomName8" "\t-";
#Vhost server
server{
listen 80 ;
server_name _;
#index index.html index.php;
proxy_read_timeout 600s;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
access_log /apps/logs/nginx/access.log log_access;
}
}
# kubectl create configmap nginxconfig --from-file nginx.conf
# kubectl get configmap
# kubectl get configmap -o yaml
在rc配置文件中使用configmap
#cat nginx-rc-configmap.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
labels:
name: nginx
spec:
replicas: 2
selector:
name: nginx
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: docker.io/nginx
volumeMounts:
- name: nginx
mountPath: /apps/conf/nginx/nginx.conf
subPath: nginx.conf
ports:
- containerPort: 80
volumes:
- name: nginx
configMap:
name: nginxconfig
items:
- key: nginx.conf
path: nginx.conf
## kubectl create -f nginx-rc-configmap.yaml
验证nginx配置文件
# kubectl get pod -o wide -n default
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-2kkk7 1/1 Running 0 23m 10.80.27.3 k8s-node-001 <none> <none>
nginx-fp22n 1/1 Running 0 23m 10.80.58.7 k8s-node-002 <none> <none>
# kubectl exec -it nginx-2kkk7 -n default -- cat /apps/conf/nginx/nginx.conf