K8s:Configmap
Configmap
英语翻译:配置映射
作用:把配置映射到容器中、直接给容器使用。
运行流程:9-54-33M
1、非机密性信息,通常来说都是配置信息
vim /etc/my.cnf #MySQL的配置信息
nginx.conf #nginx的配置信息
2、通过configmap给pod中的容器服务提供配置文件、配置文件以挂载到容器中使用。
3、通过一种方法、给容器提供配置的一种手段、解决配置和镜像的耦合关系。
4、configmap的资源存也是存储在etcd
实现方式
将pod指定资源对象为configmap(通常默认和pod在同一个namespace)
使用场景
1、配置文件映射方式
2、变量传递给后端pod(通过configmap给后端pod定义全局变量)
3、传递命令行参数
注意事项:
1、创建pod之前必须先定义好configmap(先创建configmap)
2、必须在同一个namespace
3、小于1M的配置文件
命令
kubectl get configmap
kubectl edit configmap configmap名称
###(直接编辑、不建议、下次启动时候就丢失了、类似于在docker容器内修改内容后重启docker)
kubectl get cm -A
实验
1、文件映射(用的最多)
###创建时候指定资源
###1、configmap
###2、deployment
###3、service
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
default: |
server {
listen 80;
server_name www.mysite.com; ###公司网站域名
index index.html index.php index.htm;
location / { ###请求转换目录
root /data/nginx/html;
if (!-e $request_filename) {
rewrite ^/(.*) /index.html last;
}
}
}
---
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: ng-deploy-80
template:
metadata:
labels:
app: ng-deploy-80
spec:
containers:
- name: ng-deploy-80
image: nginx:1.20.0
ports:
- containerPort: 80
volumeMounts:
- mountPath: /data/nginx/html
name: nginx-static-dir
- name: nginx-config
mountPath: /etc/nginx/conf.d ###此挂载点可以修改
volumes:
- name: nginx-static-dir
nfs:
server: 10.0.0.206
path: /data/k8sdata/nginx
- name: nginx-config
configMap:
name: nginx-config
items:
- key: default
path: mysite.conf
---
apiVersion: v1
kind: Service
metadata:
name: ng-deploy-80
spec:
ports:
- name: http
port: 81
targetPort: 80
nodePort: 30019
protocol: TCP
type: NodePort
selector:
app: ng-deploy-80
修改挂载点
注意:修改nginx挂载点、默认的配置文件include提前打镜像时候都是写好的、或者更改镜像使用
1、修改挂载点
mountPath: /etc/nginx/conf.d ###此挂载点可以修改
修改为:
mountPath: /data/nginx/conf.d
kubectl exec -it nginx-deployment-68d4cdf8c5-wfn8r bash
2、进入容器修改nginx文件、让其生效(主配置文件:/etc/nginx/nginx.conf )
不使用编辑器、不下载vim、直接用sed交互式插入倒数第二行一行内容
sed -i '/include \/etc\/nginx\/conf.d\/\*\.conf;/a \ include /data/nginx/conf.d/*.conf;' /etc/nginx/nginx.conf
3、直接在10.0.0.206NFS共享存储添加配置文件index.html
echo "www.jigaobo.com and nginx_web" > index.html
最后访问
1、端口是30019、使用端口访问肯定不行
2、使用haproxy或者阿里云的SLB映射
3、访问www.jigaobo.com

浙公网安备 33010602011771号