作者介绍:简历上没有一个精通的运维工程师。请点击上方的蓝色《运维小路》关注我,下面的思维导图也是预计更新的内容和当前进度(不定时更新)。
我们上一章介绍了Docker基本情况,目前在规模较大的容器集群基本都是Kubernetes,但是Kubernetes涉及的东西和概念确实是太多了,而且随着版本迭代功能在还增加,笔者有些功能也确实没用过,所以只能按照我自己的理解来讲解。
我们上一小节,介绍了3种方式创建cm,这里用了单独一节创建资源,实际上前面2种是所有资源都通用的方法,最后也那种只适合少量的资源。
下面我们将介绍如何来使用创建的configmap。
ENV方式注入
1.复用历史configmap
2.pod使用configmap
apiVersion: v1
kind: Pod
metadata:
name: my-pod-configmap
spec:
containers:
- name: my-container-cm
image: 192.168.31.43:5000/nginx
envFrom:
- configMapRef:
name: my-config
这个等效于使用docker run 使用-e注入环境变量,但是在实际k8s的运维中很少会用这个方法,一般都是使用文件方式注入。而且环境变量如果太多,会影响服务器性能(真实生产经验)。
文件注入
1.先准备一个nginx配置文件
# 全局配置,这位是一个测试文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# 配置事件模块
events {
worker_connections 1024;
}
http {
# 配置HTTP模块
# 设置日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 配置访问日志
access_log /var/log/nginx/access.log main;
# MIME类型配置
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 配置gzip压缩
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_proxied any;
gzip_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component;
gzip_vary on;
# 配置服务端
server {
listen 80;
server_name example.com;
# 配置根目录
root /var/www/html;
# 配置静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
# 配置代理
location /api/ {
proxy_pass http://127.0.0.1:12080;
}
}
}
2.使用文件创建configmap
[root@master01 cm]# kubectl create cm nginx-config \
--from-file=nginx.conf
configmap/nginx-config created
3.pod使用configmap
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx-container
image: 192.168.31.43:5000/nginx
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
但是使用这种方式,如果配置文件错误,可能会导致容器启动失败,需要注意。
运维小路
一个不会开发的运维!一个要学开发的运维!一个学不会开发的运维!欢迎大家骚扰的运维!
关注微信公众号《运维小路》获取更多内容。