目錄
ConfigMap :
參考文檔:k8s -- ConfigMap - 簡(jiǎn)書(shū) (jianshu.com)? ? K8S ConfigMap使用 - 知乎 (zhihu.com)
ConfigMap的作用類型:
可以作為卷的數(shù)據(jù)來(lái)源:使用 ConfigMap 來(lái)配置 Redis | Kubernetes
可以基于文件創(chuàng)建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes
可以基于目錄創(chuàng)建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes
為什么需要使用ConfigMap呢?
使用 ConfigMap 來(lái)配置 Redis
參考網(wǎng)址:使用 ConfigMap 來(lái)配置 Redis | Kubernetes
配置拓?fù)鋱D:
步驟:
nginx配置文件投射:(ConfigMap 基于文件創(chuàng)造)
Secret
參考文檔:Secret | Kubernetes
例子:使用 Secret 安全地分發(fā)憑證:使用 Secret 安全地分發(fā)憑證 | Kubernetes
ConfigMap :
ConfigMap理解為一個(gè)容器,存放數(shù)據(jù)的地方,里面存放鍵值對(duì)的數(shù)據(jù)或者是文件類型,里面存放的數(shù)據(jù)是明文的,不是加密
ConfigMap顧名思義,是用于保存配置數(shù)據(jù)的鍵值對(duì),可以用來(lái)保存單個(gè)屬性,也可以保存配置文件。Secret可以為Pod提供密碼、Token、私鑰等敏感數(shù)據(jù);對(duì)于一些非敏感數(shù)據(jù),比如應(yīng)用的配置信息,則可以使用ConfigMap。
Secret理解為一個(gè)容器,存放數(shù)據(jù)的地方,在內(nèi)存里,里面存放敏感的數(shù)據(jù),例如密碼,密鑰,token等,需要進(jìn)行加密,數(shù)據(jù)可以是鍵值對(duì)或者文件
參考文檔:k8s -- ConfigMap - 簡(jiǎn)書(shū) (jianshu.com)? ? K8S ConfigMap使用 - 知乎 (zhihu.com)
ConfigMap的作用類型:
可以作為卷的數(shù)據(jù)來(lái)源:使用 ConfigMap 來(lái)配置 Redis | Kubernetes
可以基于文件創(chuàng)建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes
可以基于目錄創(chuàng)建 ConfigMap:配置 Pod 使用 ConfigMap | Kubernetes
kubectl create configmap game-config --from-file=configure-pod-container/configmap/
為什么需要使用ConfigMap呢?
使用k8s部署應(yīng)用,當(dāng)你將應(yīng)用配置寫(xiě)進(jìn)代碼中,就會(huì)存在一個(gè)問(wèn)題,更新配置時(shí)也需要打包鏡像,configmap可以將配置信息和docker鏡像解耦。
使用微服務(wù)架構(gòu)的話,存在多個(gè)服務(wù)共用配置的情況,如果每個(gè)服務(wù)中單獨(dú)一份配置的話,那么更新配置就很麻煩,使用configmap可以友好的進(jìn)行配置共享。
其次,configmap可以用來(lái)保存單個(gè)屬性,也可以用來(lái)保存配置文件。
基于文件創(chuàng)造ConfigMap拓?fù)鋱D
?
使用 ConfigMap 來(lái)配置 Redis
參考網(wǎng)址:使用 ConfigMap 來(lái)配置 Redis | Kubernetes
配置拓?fù)鋱D:
步驟:
首先創(chuàng)建一個(gè)配置模塊為空的 ConfigMap:
cat <<EOF >./example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-config
data:
redis-config: ""
EOF
應(yīng)用上面創(chuàng)建的 ConfigMap 以及 Redis pod 清單:
[root@master configmap]# kubectl apply -f example-redis-config.yaml
configmap/example-redis-config created
[root@master configmap]# wget https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml
配置文件?redis-pod.yaml:
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis:5.0.4
command:
- redis-server
- "/redis-master/redis.conf" #redis啟動(dòng)的時(shí)候加載配置文件redis.conf
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: example-redis-config
items:
- key: redis-config #讀取key redis-config的內(nèi)容
path: redis.conf #寫(xiě)到容器里的redis.conf配置文件里去
啟動(dòng)redis-pod.yaml文件
[root@master configmap]# kubectl apply -f redis-pod.yaml
pod/redis created
[root@master configmap]#
檢查創(chuàng)建的對(duì)象:
[root@master configmap]# kubectl get pod/redis configmap/example-redis-config
NAME READY STATUS RESTARTS AGE
pod/redis 1/1 Running 0 3m20s
NAME DATA AGE
configmap/example-redis-config 1 5m2s
[root@master configmap]#
使用?kubectl exec
?進(jìn)入 pod,運(yùn)行?redis-cli
?工具檢查當(dāng)前配置:
[root@master configmap]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379>
查看?maxmemory
:
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
127.0.0.1:6379>
同樣,查看?maxmemory-policy
:
127.0.0.1:6379> CONFIG GET maxmemory-policy
它也應(yīng)該顯示默認(rèn)值?noeviction
:
1) "maxmemory-policy"
2) "noeviction"
應(yīng)用更新的 ConfigMap:
向?example-redis-config
?ConfigMap 添加一些配置:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-config
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
[root@master configmap]# kubectl apply -f example-redis-config-2.yaml
configmap/example-redis-config configured
[root@master configmap]# kubectl get cm
NAME DATA AGE
example-redis-config 1 15m
kube-root-ca.crt 1 31h
[root@master configmap]#
確認(rèn) ConfigMap 已更新:
[root@master configmap]# kubectl describe configmap/example-redis-config
Name: example-redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru
BinaryData
====
Events: <none>
[root@master configmap]#
重新啟動(dòng) Pod 才能從關(guān)聯(lián)的 ConfigMap 中獲取更新的值。?
[root@master configmap]# kubectl delete pod redis
pod "redis" deleted
[root@master configmap]# ls
example-redis-config-2.yaml example-redis-config.yaml redis-pod.yaml
[root@master configmap]# kubectl apply -f redis-pod.yaml
pod/redis created
[root@master configmap]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
redis 1/1 Running 0 15s 10.244.1.18 node1 <none> <none>
test 1/1 Running 0 28h 10.244.2.7 node2 <none> <none>
[root@master configmap]#
現(xiàn)在,最后一次重新檢查配置值:
[root@master configmap]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory #查看 maxmemory:
1) "maxmemory"
2) "2097152"
127.0.0.1:6379>
#現(xiàn)在,它應(yīng)該返回更新后的值 2097152 大概2mb
127.0.0.1:6379> CONFIG GET maxmemory-policy # 同樣,maxmemory-policy 也已更新:
1) "maxmemory-policy"
2) "allkeys-lru"
127.0.0.1:6379>
#現(xiàn)在它反映了期望值 allkeys-lru:
刪除創(chuàng)建的資源,清理你的工作:
kubectl delete pod/redis configmap/example-redis-config
nginx配置文件投射:(ConfigMap 基于文件創(chuàng)造)
===
[root@scmaster nginx]# cat nginx/nginx.conf
worker_processes 4;
events {
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@scmaster nginx]# kubectl create configmap wyt-nginx-1 --from-file=nginx.conf
[root@scmaster nginx]# cat nginx.yaml
kind: Deployment
metadata:
name: wyt-nginx
spec:
replicas: 2
apiVersion: apps/v1
kind: Deployment
metadata:
name: wyt-nginx
spec:
replicas: 3
selector:
matchLabels:
app: wyt-nginx
template:
metadata:
labels:
app: wyt-nginx
spec:
containers:
- name: nginx
image: "nginx:latest"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: wyt-nginx-config
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: wyt-nginx-config
configMap:
name: wyt-nginx-1
items:
- key: nginx.conf
path: nginx.conf
[root@scmaster nginx]#
Secret
Secret 是存儲(chǔ)諸如密碼或密鑰之類的敏感數(shù)據(jù)的對(duì)象 --》相當(dāng)于一個(gè)密碼箱
Secret 對(duì)象類型用來(lái)保存敏感信息,例如密碼、OAuth 令牌和 SSH 密鑰。?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-608571.html
Kubernetes Secret 默認(rèn)情況下存儲(chǔ)為 base64-編碼的、非加密的字符串。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-608571.html
參考文檔:Secret | Kubernetes
例子:使用 Secret 安全地分發(fā)憑證:使用 Secret 安全地分發(fā)憑證 | Kubernetes
到了這里,關(guān)于Kubernetes ConfigMap - Secret - 使用ConfigMap來(lái)配置 Redis的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!