ConfigMap介绍
参考地址:https://kubernetes.io/zh-cn/docs/concepts/configuration/configmap/
我们在部署应用的时候, 许多应用程序会从配置文件或环境变量中读取所需的配置信息,如果程序较多且分散在集群的不同节点上,那么更新配置就很麻烦, 所以k8s引入了Configmap资源对象, 提供了向容器中注入配置信息的机制,让程序和配置文件解耦,以便实现配置的统一管理。
注意,ConfigMap以键值对的形式保存一些非机密性数据,并不适合存储敏感信息,如密码。比较常见的使用方式是当作程序的配置文件使用,当需要对配置文件批量更新时,这样会更加的方便。
使用ConfigMap的限制条件:
ConfigMap需要在Pod启动前创建出来,并且只有当ConfigMap和Pod处于同一空间时,才可以被 Pod引用;
ConfigMap在设计上不是用来保存大量数据的,在ConfigMap中保存的数据不可超过1M。
ConfigMap案例
案例1:通过命令方式创建ConfigMap来存储Nginx虚拟web主机配置文件。
#准备配置文件
tee /opt/default.conf <<EOF
server {
listen 80;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
EOF#创建ConfigMap,可通过 kubectl create cm -h 获取帮助
kubectl create configmap ngx-default --from-file=/opt/default.conf -n test
#查看ConfigMap
kubectl get cm -n test
kubectl describe cm ngx-default -n test创建Nginx的Pod,并使用Volume将ConfigMap挂载到nginx容器中
# cat http-nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-nginx
namespace: test
spec:
selector:
matchLabels:
app: deploy-nginx
template:
metadata:
labels:
app: deploy-nginx
spec:
volumes:
- name: ngx-default #卷名称自定义
configMap: #卷类型是configMap
name: ngx-default #configmap的名称
containers:
- name: nginx
image: nginx:1.18.0
ports:
- containerPort: 80
volumeMounts: #挂载到容器
- name: ngx-default #挂载的卷名称(上边定义的卷的名称)
mountPath: /etc/nginx/conf.d #挂载到容器的路径
---
apiVersion: v1
kind: Service
metadata:
name: svc-nginx
namespace: test
spec:
type: ClusterIP
clusterIP: None
selector:
app: deploy-nginx
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: http-nginx
namespace: test
annotations:
#指定spec下方的rules的path可以使用正则表达式,如果我们没有使用正则表达式,此项则可不使用
#ingressclass.kubernetes.io/is-default-class: "true"
#指定控制器的类别为nginx
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: web.nginx.com
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: svc-nginx
port:
number: 80#创建Pod
kubectl create -f http-nginx.yml
#查看Pod信息
kubectl get pod -o wide -n test
NAME READY STATUS RESTARTS IP
deploy-nginx-65c8d4bb9b-nvl6n 1/1 Running 0 172.16.85.238#进入容器查看文件内容
kubectl exec -it deploy-nginx-65c8d4bb9b-nvl6n /bin/bash -n test
cat /etc/nginx/conf.d/default.conf提示:如果你想挂载的容器目录下有其他文件,默认会覆盖目录下其他文件,可以通过subPath属性挂载,subPath的作用是将configMap作为文件挂载到容器中而不覆盖挂载目录下的文件。
#准备nginx.conf配置文件
tee /opt/nginx.conf <<EOF
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
EOF#创建ConfigMap
kubectl create configmap ngx-conf --from-file=/opt/nginx.conf -n testPod挂载ConfigMap
# cat http-nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-nginx
namespace: test
spec:
selector:
matchLabels:
app: deploy-nginx
template:
metadata:
labels:
app: deploy-nginx
spec:
volumes:
- name: ngx-default
configMap:
name: ngx-default
- name: ngx-conf #卷名称自定义
configMap: #卷类型是configMap
name: ngx-conf #configmap的名称
containers:
- name: nginx
image: nginx:1.18.0
ports:
- containerPort: 80
volumeMounts:
- name: ngx-default
mountPath: /etc/nginx/conf.d
- name: ngx-conf #挂载的卷名称(上边定义的卷的名称)
mountPath: /etc/nginx/nginx.conf #挂载到容器的路径
subPath: nginx.conf #指定文件名
---
apiVersion: v1
kind: Service
metadata:
name: svc-nginx
namespace: test
spec:
type: ClusterIP
clusterIP: None
selector:
app: deploy-nginx
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: http-nginx
namespace: test
annotations:
#指定spec下方的rules的path可以使用正则表达式,如果我们没有使用正则表达式,此项则可不使用
#ingressclass.kubernetes.io/is-default-class: "true"
#指定控制器的类别为nginx
kubernetes.io/ingress.class: nginx
spec:
rules:
- host: web.nginx.com
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: svc-nginx
port:
number: 80#创建Pod
kubectl create -f http-nginx.yml
#查看Pod信息
kubectl get pod -n test
NAME READY STATUS
deploy-nginx-5c56dbf6bb-jxxrp 1/1 Running
#进入容器查看
kubectl exec -it deploy-nginx-5c56dbf6bb-jxxrp /bin/bash -n test
ls /etc/nginx/ConfigMap更新
通过Volume挂载到容器内部时,当ConfigMap发生变化时,容器内部具备自动更新的能力,但是通过subPath挂载的ConfigMap不会自动更新。
通过ConfigMap更新Nginx端口为8080
kubectl edit cm ngx-default -n test
apiVersion: v1
data:
nginx.conf: |
server {
listen 8080; # 改为8080
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}#查看Pod
kubectl get pod -n test
NAME READY STATUS
deploy-nginx-5c56dbf6bb-jxxrp 1/1 Running
#进入容器验证配置更新
kubectl exec -it deploy-nginx-5c56dbf6bb-jxxrp /bin/bash -n test
cat /etc/nginx/conf.d/default.conf
server {
listen 8080;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}访问Pod验证端口:curl 172.16.85.241:8080
访问是8080端口发现失败,但从pod中查询nginx的配置文件,发现已经更改为8080,说明ConfigMap热更新已生效,这时就需要手动触发滚动更新才能再次加载nginx的配置文件。
这个命令是在 .spec.template.metadata 中添加 annotations注解,每次通过修改注解来触发滚动更新。
kubectl edit deploy deploy-nginx -n test
...
template:
metadata:
annotations:
configmap: "20230608"
...访问Pod验证端口:curl 172.16.85.242:8080
Secret介绍
参考地址:https://kubernetes.io/zh-cn/docs/concepts/configuration/secret/
Secret与ConfigMap类似,区别在于ConfigMap存储的数据是明文,而Secret存储的数据是密文。所以Secret可以用来存储和管理一些敏感数据,比如账号、密码、token,ssh密钥、证书等敏感内容。
Secret常用3种类型:
Opaque //用来存储密码、密钥,需要通过base64编码格式进行编码。
docker-registry //用来存储私有私有仓库的认证信息。
tls //用于存储证书和私钥文件。
Secret案例
案例:使用Opaque类型来存储MySQL root密码。
#Opaque类型需要将明文密码进行base64编码
echo -n 123456 | base64
MTIzNDU2提示: echo -n 不换行输出,如果不使用-n,就会把换行符也作为字符当做密码使用,会一直报错进不去MySQL。
创建Opaque类型Secret
# cat secret-mysql.yml
apiVersion: v1
kind: Secret
metadata:
name: secret-mysql
namespace: test
type: Opaque #指定类型(不指定默认也是Opaque类型)
data:
pass: MTIzNDU2 #数据以键值对方式定义kubectl create -f secret-mysql.yml
kubectl get secret -test
kubectl describe secret secret-mysql -n test创建MySQL的Pod,并通过环境变量的方式传递给Pod使用。
# cat mysql.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-mysql
namespace: test
spec:
selector:
matchLabels:
app: deploy-mysql
template:
metadata:
labels:
app: deploy-mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD #MySQL内置环境变量
valueFrom:
secretKeyRef:
name: secret-mysql #对应创建的secret名字
key: pass #在secret中定义的key名称kubectl apply -f mysql.yml
kubectl get pod -n test
NAME READY STATUS
deploy-mysql-7f9ffb4598-zxvw2 1/1 Running#进入容器验证密码是否可用
kubectl exec -it deploy-mysql-7f9ffb4598-zxvw2 /bin/bash -n test
#查看MySQL环境变量
env | grep MYSQL_ROOT_PASSWORD
#进入数据库
mysql -p123456Secret更新
#将明文密码进行base64编码
echo -n 123456789 | base64
MTIzNDU2Nzg5 #编辑Secret
kubectl edit secret secret-mysql -n test
apiVersion: v1
data:
pass: MTIzNDU2Nzg5 #修改密码
...动触发滚动更新
kubectl edit deploy deploy-mysql -n test
...
template:
metadata:
annotations:
secret: "20230608"
...#查看Pod
kubectl get pod -n test
NAME READY STATUS
deploy-mysql-5c887cbc58-4djbz 1/1 Running#进入容器验证密码是否可用
kubectl exec -it deploy-mysql-5c887cbc58-4djbz /bin/bash -n test
#查看MySQL环境变量
env | grep MYSQL_ROOT_PASSWORD
#进入数据库
mysql -p123456789
评论区