k8s配置中心-configmap,Secret
在生產環境中經常會遇到需要修改配置文件的情況,傳統的修改方式不僅會影響到服務的正常運行,而且操作步驟也很繁瑣。為了解決這個問題,kubernetes項目從1.2版本引入了ConfigMap功能,用於將應用的配置信息與程序的分離。這種方式不僅可以實現應用程序被的復用,而且還可以通過不同的配置實現更靈活的功能。在創建容器時,用戶可以將應用程序打包為容器鏡像后,通過環境變量或者外接掛載文件的方式進行配置注入。ConfigMap && Secret 是K8S中的針對應用的配置中心,它有效的解決了應用掛載的問題,並且支持加密以及熱更新等功能,可以說是一個k8s提供的一件非常好用的功能。
創建ConfigMap
# 創建名稱空間
apiVersion: v1
kind: Namespace
metadata:
name: sg-bs
labels:
app: sg-bs
---
# ConfigMap是名稱空間級資源
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: sg-bs
data: # 健 : 值
level: debug
使用ConfigMap
## 使用掛載方式,將配置文件掛載到容器中
# 使用
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-config
spec:
selector:
matchLabels:
app: nginx-config
template:
metadata:
labels:
app: nginx-config
spec:
containers:
- name: nginx
image: nginx
volumeMounts: # 掛載
- mountPath: /etc/nginx/conf.d # 掛載路徑
name: nginx-config-configmap # 存儲卷名字
volumes:
- name: nginx-config
persistentVolumeClaim:
claimName: nginx-config
- name: nginx-config-configmap
configMap:
name: test-configmap # ConfigMap名字
items:
- key: level
path: level # 最終路徑為:/etc/nginx/conf.d/level
subPath參數
# configmap熱更新
## 修改configmap中的文件,可以同步到所有的掛載此configmap的容器中(僅僅同步到容器中),但是如果使用subPath參數,則熱更新失效。
## configMap掛載會直接覆蓋原來的目錄,如果不覆蓋則需要使用subPath參數(subPath參數只能夠針對文件,同時不支持熱更新)
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
default.conf: |
server {
listen 80;
listen [::]:80;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.php;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
index.php: |
<?php
phpinfo();
?>
---
kind: Service
apiVersion: v1
metadata:
name: nginx-config
spec:
ports:
- port: 80
targetPort: 80
nodePort: 30089
selector:
app: nginx-config
type: NodePort
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-config
spec:
selector:
matchLabels:
app: nginx-config
template:
metadata:
labels:
app: nginx-config
spec:
containers:
- name: php
image: alvinos/php:wordpress-v2
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginx-config-configmap
- name: nginx
image: alvinos/nginx:wordpress-v2
volumeMounts:
- mountPath: /usr/share/nginx/html/index.php
name: nginx-config-configmap
subPath: index.php
- mountPath: /etc/nginx/conf.d
name: nginx-config-configmap
volumes:
- name: nginx-config-configmap
configMap:
name: nginx-config
items:
- key: index.php
path: index.php
Secret
Secret解決了密碼、token、密鑰等敏感數據的配置問題,而不需要把這些敏感數據暴露到鏡像或者Pod Spec中。Secret可以以Volume或者環境變量的方式使用。
Secret用來保存敏感數據,保存之前就必須將文件進行base64加密,掛載到pod中,自動解密。
默認使用Opaque類型。
Secret有四種類型:
Service Account :用來訪問Kubernetes API,由Kubernetes自動創建,並且會自動掛載到Pod的/run/secrets/kubernetes.io/serviceaccount目錄中;
Opaque :base64編碼格式的Secret,用來存儲密碼、密鑰等;
kubernetes.io/dockerconfigjson :用來存儲私有docker registry的認證信息
tls類型:訪問證書
官方文檔
https://kubernetes.io/zh/docs/concepts/configuration/secret/
編寫secret清單
kind: Secret
apiVersion: v1
metadata:
name: discuz-mysql-secrer
namespace: discuz-mysql
type: Opaque
data:
passwd: MTIzNDU2Cg== # echo "123456"| base64
使用secret
Secret 可以作為數據卷被掛載,或作為環境變量 暴露出來以供 Pod 中的容器使用。它們也可以被系統的其他部分使用,而不直接暴露在 Pod 內。 例如,它們可以保存憑據,系統的其他部分將用它來代表你與外部系統進行交互。
apiVersion: batch/v1
kind: Job
metadata:
name: test-job
spec:
template:
metadata:
labels:
app: job
deploy: discuz
spec:
restartPolicy: OnFailure # 重啟策略
containers:
- name: mysql
image: mysql:5.7
command:
- "/bin/bash"
- "-c"
- "/usr/bin/mysqldump -uroot -p`cat /mnt/passwd`"
volumeMounts: # 掛載
- mountPath: /opt
name: job-sql-dump
- mountPath: /mnt # 密碼掛載目錄
name: passwd
volumes:
- name: password-secret
secret:
secretName: discuz-mysql-secrer
items:
- key: passwd
path: passwd
