內(nèi)容管理系統(tǒng)開源佛山網(wǎng)站建設(shè)十年樂云seo
一,secret
????????Secret 是用來保存密碼、token、密鑰等敏感數(shù)據(jù)的 k8s 資源,這類數(shù)據(jù)雖然也可以存放在 Pod 或者鏡像中,但是放在 Secret 中是為了更方便的控制如何使用數(shù)據(jù),并減少暴露的風(fēng)險(xiǎn)。
?有三種類型:
1,kubernetes.io/service-account-token:由 Kubernetes 自動(dòng)創(chuàng)建,用來訪問 APIServer 的 Secret,Pod 會(huì)默認(rèn)使用這個(gè) Secret 與 APIServer 通信, 并且會(huì)自動(dòng)掛載到 Pod 的 /run/secrets/kubernetes.io/serviceaccount 目錄中;
2,Opaque :base64 編碼格式的 Secret,用來存儲(chǔ)用戶自定義的密碼、密鑰等,默認(rèn)的 Secret 類型;
3,kubernetes.io/dockerconfigjson :用來存儲(chǔ)私有 docker registry 的認(rèn)證信息。
Pod 需要先引用才能使用某個(gè) secret,Pod 有 3 種方式來使用 secret:
●作為掛載到一個(gè)或多個(gè)容器上的卷 中的文件。
●作為容器的環(huán)境變量。
●由 kubelet 在為 Pod 拉取鏡像時(shí)使用。
具體訪問Secrets | Kubernetes
二,創(chuàng)建 Secret
echo -n 'zhangsan' > username.txt
echo -n 'abc1234' > password.txtkubectl create secret generic mysecret --from-file=username.txt --from-file=password.txtkubectl get secrets
NAME TYPE DATA AGE
default-token-8pqp6 kubernetes.io/service-account-token 3 3d1h
mysecret Opaque 2 51skubectl describe secret mysecret
Name: mysecret
Namespace: default
Labels: <none>
Annotations: <none>Type: OpaqueData
====
password.txt: 7 bytes
username.txt: 8 bytes
//get或describe指令都不會(huì)展示secret的實(shí)際內(nèi)容,這是出于對(duì)數(shù)據(jù)的保護(hù)的考慮
2,內(nèi)容用 base64 編碼,創(chuàng)建Secret
echo -n zhangsan | base64
emhhbmdzYW4K=echo -n abc1234 | base64
YWJjMTIzNAo==vim secret.yaml
apiVersion: v1
kind: Secret
metadata:name: mysecret1
type: Opaque
data:username: emhhbmdzYW4K=password: YWJjMTIzNAo==kubectl create -f secret.yaml kubectl get secrets
NAME TYPE DATA AGE
default-token-8pqp6 kubernetes.io/service-account-token 3 3d1h
mysecret Opaque 2 43m
mysecret1 Opaque 2 6skubectl get secret mysecret1 -o yaml
apiVersion: v1
data:password: YWJjMTIzNAo==username: emhhbmdzYW4K=
kind: Secret
metadata:creationTimestamp: 2021-05-24T09:11:18Zname: mysecret1namespace: defaultresourceVersion: "45641"selfLink: /api/v1/namespaces/default/secrets/mysecret1uid: fffb7902-bc6f-11eb-acba-000c29d88bba
type: Opaque
//使用方式?
1、將 Secret 掛載到 Volume 中,以 Volume 的形式掛載到 Pod 的某個(gè)目錄下
vim secret-test.yaml
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: nginximage: nginxvolumeMounts:- name: secretsmountPath: "/etc/secrets"readOnly: truevolumes:- name: secretssecret:secretName: mysecretkubectl create -f secret-test.yamlkubectl get pods
NAME READY STATUS RESTARTS AGE
seret-test 1/1 Running 0 16skubectl exec -it seret-test bash# cd /etc/secrets/# ls
password.txt username.txt# vi password.txt # vi username.txt
vim secret-test1.yaml
apiVersion: v1
kind: Pod
metadata:name: mypod1
spec:containers:- name: nginximage: nginxenv:- name: TEST_USERvalueFrom:secretKeyRef:name: mysecret1key: username- name: TEST_PASSWORDvalueFrom:secretKeyRef:name: mysecret1key: passwordkubectl apply -f secret-test1.yaml kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod1 1/1 Running 0 77skubectl exec -it mypod bash# echo $TEST_USER
zhangsan# echo $TEST_PASSWORD
abc1234
//ConfigMap
與Secret類似,區(qū)別在于ConfigMap保存的是不需要加密配置的信息。
ConfigMap 功能在 Kubernetes1.2 版本中引入,許多應(yīng)用程序會(huì)從配置文件、命令行參數(shù)或環(huán)境變量中讀取配置信息。Con?gMap API 給我們提供了向容器中注入配置信息的機(jī)制,Con?gMap 可以被用來保存單個(gè)屬性,也可以用來保存整個(gè)配置文件或者JSON二進(jìn)制大對(duì)象。
應(yīng)用場(chǎng)景:應(yīng)用配置
//創(chuàng)建 ConfigMap
1、使用目錄創(chuàng)建0000000000000000000000000
mkdir /opt/configmap/vim /opt/configmap/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30vim /opt/configmap/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNicels /opt/configmap/
game.properties
ui.propertieskubectl create configmap game-config --from-file=/opt/configmap/
//--from-file 指定在目錄下的所有文件都會(huì)被用在 Con?gMap 里面創(chuàng)建一個(gè)鍵值對(duì),鍵的名字就是文件名,值就是文件的內(nèi)容kubectl get cm
NAME DATA AGE
game-config 2 10skubectl get cm game-config -o yaml
apiVersion: v1
data:game.properties: |enemies=alienslives=3enemies.cheat=trueenemies.cheat.level=noGoodRottensecret.code.passphrase=UUDDLRLRBABASsecret.code.allowed=truesecret.code.lives=30ui.properties: |color.good=purplecolor.bad=yellowallow.textmode=truehow.nice.to.look=fairlyNice
kind: ConfigMap
metadata:creationTimestamp: 2021-05-25T06:49:18Zname: game-confignamespace: defaultresourceVersion: "87803"selfLink: /api/v1/namespaces/default/configmaps/game-configuid: 541b5302-bd25-11eb-acba-000c29d88bba
2、使用文件創(chuàng)建?
只要指定為一個(gè)文件就可以從單個(gè)文件中創(chuàng)建 Con?gMap
--from-file 這個(gè)參數(shù)可以使用多次,即可以使用兩次分別指定上個(gè)實(shí)例中的那兩個(gè)配置文件,效果就跟指定整個(gè)目錄是一樣的
kubectl create configmap game-config-2 --from-file=/opt/configmap/game.properties --from-file=/opt/configmap/ui.propertieskubectl get configmaps game-config-2 -o yamlkubectl describe cm game-config-2
3、使用字面值創(chuàng)建?
使用文字值創(chuàng)建,利用 --from-literal 參數(shù)傳遞配置信息,該參數(shù)可以使用多次,格式如下
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=good
kubectl get configmaps special-config -o yaml
apiVersion: v1
data:special.how: very #key-value 結(jié)構(gòu)special.type: good
kind: ConfigMap
metadata:creationTimestamp: 2021-05-25T06:59:37Zname: special-confignamespace: defaultresourceVersion: "88610"selfLink: /api/v1/namespaces/default/configmaps/special-configuid: c4f45936-bd26-11eb-acba-000c29d88bbakubectl delete cm --all
kubectl delete pod --all//Pod 中使用 Con?gMap
1、使用 Con?gMap 來替代環(huán)境變量
vim env.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: special-confignamespace: default
data:special.how: veryspecial.type: good
---
apiVersion: v1
kind: ConfigMap
metadata:name: env-confignamespace: default
data:log_level: INFOkubectl create -f env.yaml kubectl get cm
NAME DATA AGE
env-config 1 6s
special-config 2 6s//Pod的創(chuàng)建
vim test-pod.yaml
apiVersion: v1
kind: Pod
metadata:name: test-pod
spec:containers:- name: busyboximage: busybox:1.28.4command: [ "/bin/sh", "-c", "env" ]env:- name: SPECIAL_HOW_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.how- name: SPECIAL_TYPE_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.typeenvFrom:- configMapRef:name: env-configrestartPolicy: Neverkubectl create -f test-pod.yamlkubectl get pods
NAME READY STATUS RESTARTS AGE
pod-test 0/1 Completed 0 33skubectl logs pod-test
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.0.0.1:443
HOSTNAME=pod-test
SHLVL=1
SPECIAL_HOW_KEY=very #賦值變量 SPECIAL_HOW_KEY 的值為 special-config 的 special.how: very
HOME=/root
SPECIAL_TYPE_KEY=good #賦值變量 SPECIAL_TYPE_KEY 的值為 special-config 的 special.type: good
KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
log_level=INFO #引入 env-config 的變量 log_level: INFO
KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.0.0.1
PWD=/
2、用 Con?gMap 設(shè)置命令行參數(shù)?
vim test-pod2.yaml
apiVersion: v1
kind: Pod
metadata:name: test-pod2
spec:containers:- name: busyboximage: busybox:1.28.4command: - /bin/sh- -c- echo "$(SPECIAL_HOW_KEY) $(SPECIAL_TYPE_KEY)"env:- name: SPECIAL_HOW_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.how- name: SPECIAL_TYPE_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.typeenvFrom:- configMapRef:name: env-configrestartPolicy: Neverkubectl create -f test-pod2.yamlkubectl get pods
NAME READY STATUS RESTARTS AGE
test-pod2 0/1 Completed 0 34skubectl logs test-pod2
very good
3、通過數(shù)據(jù)卷插件使用Con?gMap?
在數(shù)據(jù)卷里面使用 Con?gMap,就是將文件填入數(shù)據(jù)卷,在這個(gè)文件中,鍵就是文件名,鍵值就是文件內(nèi)容
vim test-pod3.yaml
apiVersion: v1
kind: Pod
metadata:name: test-pod3
spec:containers:- name: busyboximage: busybox:1.28.4command: [ "/bin/sh", "-c", "sleep 36000" ]volumeMounts:- name: config-volumemountPath: /etc/configvolumes:- name: config-volumeconfigMap:name: special-configrestartPolicy: Neverkubectl create -f test-pod3.yaml kubectl get pods
NAME READY STATUS RESTARTS AGE
test-pod3 1/1 Running 0 5skubectl exec -it test-pod3 sh# cd /etc/config/# ls
special.how special.type# vi special.how # vi special.type //Con?gMap 的熱更新
vim test-pod4.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: log-confignamespace: default
data:log_level: INFO
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:name: my-nginx
spec:replicas: 1template:metadata:labels:run: my-nginxspec:containers:- name: my-nginximage: nginxports:- containerPort: 80volumeMounts:- name: config-volumemountPath: /etc/configvolumes:- name: config-volumeconfigMap:name: log-configkubectl apply -f test-pod5.yamlkubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-76b6489f44-6dwxh 1/1 Running 0 46skubectl exec -it my-nginx-76b6489f44-6dwxh -- cat /etc/config/log_level
INFOkubectl edit configmap log-config
apiVersion: v1
data:log_level: DEBUG #INFO 修改成 DEBUG
kind: ConfigMap
metadata:annotations:kubectl.kubernetes.io/last-applied-configuration: |{"apiVersion":"v1","data":{"log_level":"DEBUG"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"log-config","namespace":"default"}} #INFO 修改成 DEBUGcreationTimestamp: 2021-05-25T07:59:18Zname: log-confignamespace: defaultresourceVersion: "93616"selfLink: /api/v1/namespaces/default/configmaps/log-configuid: 1b8115de-bd2f-11eb-acba-000c29d88bba//等大概10秒左右,使用該 ConfigMap 掛載的 Volume 中的數(shù)據(jù)同步更新
kubectl exec -it my-nginx-76b6489f44-6dwxh -- cat /etc/config/log_level
DEBUG//ConfigMap 更新后滾動(dòng)更新 Pod
更新 ConfigMap 目前并不會(huì)觸發(fā)相關(guān) Pod 的滾動(dòng)更新,可以通過在 .spec.template.metadata.annotations 中添加 version/config ,每次通過修改 version/config 來觸發(fā)滾動(dòng)更新kubectl patch deployment my-nginx --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20210525" }}}}}'kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-665dd4dc8c-j4k9t 0/1 ContainerCreating 0 4s
my-nginx-76b6489f44-6dwxh 0/1 Terminating 0 10mkubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-665dd4dc8c-j4k9t 1/1 Running 0 74sPS:更新 ConfigMap 后:
●使用該 ConfigMap 掛載的 Env 不會(huì)同步更新。
●使用該 ConfigMap 掛載的 Volume 中的數(shù)據(jù)需要一段時(shí)間(實(shí)測(cè)大概10秒)才能同步更新。