企业网站建设参考资料,长沙网络优化推广,崂山区建设局网站,网站首页标题怎么设置这里写目录标题 K8S配置资源管理一.Secret1.介绍2.Secret 有四种类型3.创建 Secret4.使用方式 二.ConfigMap1.介绍2.创建 ConfigMap3.Pod 中使用 ConfigMap4.用 ConfigMap 设置命令行参数5.通过数据卷插件使用ConfigMap6.ConfigMap 的热更新7.ConfigMap 更新后滚动更新 Pod K8S… 这里写目录标题 K8S配置资源管理一.Secret1.介绍2.Secret 有四种类型3.创建 Secret4.使用方式 二.ConfigMap1.介绍2.创建 ConfigMap3.Pod 中使用 ConfigMap4.用 ConfigMap 设置命令行参数5.通过数据卷插件使用ConfigMap6.ConfigMap 的热更新7.ConfigMap 更新后滚动更新 Pod K8S配置资源管理
一.Secret
1.介绍
Secret 是用来保存密码、token、密钥等敏感数据的 k8s 资源这类数据虽然也可以存放在 Pod 或者镜像中但是放在 Secret 中是为了更方便的控制如何使用数据并减少暴露的风险。
2.Secret 有四种类型
●kubernetes.io/service-account-token由 Kubernetes 自动创建用来访问 APIServer 的 SecretPod 会默认使用这个 Secret 与 APIServer 通信 并且会自动挂载到 Pod 的 /run/secrets/kubernetes.io/serviceaccount 目录中; ●Opaque base64 编码格式的 Secret用来存储用户自定义的密码、密钥等默认的 Secret 类型; ●kubernetes.io/dockerconfigjson 用来存储私有 docker registry 的认证信息。 ●kubernetes.io/tls 用来存储 TLS 证书和私钥信息。
Pod 需要先引用才能使用某个 secretPod 有 3 种方式来使用 secret ●作为挂载到一个或多个容器上的卷 中的文件。 ●作为容器的环境变量。 ●由 kubelet 在为 Pod 拉取镜像时使用。
应用场景凭据 https://kubernetes.io/docs/concepts/configuration/secret/
3.创建 Secret
1、用kubectl create secret命令创建Secret
echo -n zhangsan username.txt
echo -n abc1234 password.txtkubectl create secret generic mysecret --from-fileusername.txt --from-filepassword.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: noneType: OpaqueDatapassword.txt: 7 bytes
username.txt: 8 bytes
//get或describe指令都不会展示secret的实际内容这是出于对数据的保护的考虑
2、内容用 base64 编码创建Secret
echo -n zhangsan | base64
emhhbmdzYW4Kecho -n abc1234 | base64
YWJjMTIzNAovim secret.yaml
apiVersion: v1
kind: Secret
metadata:name: mysecret1
type: Opaque
data:username: emhhbmdzYW4Kpassword: YWJjMTIzNAokubectl 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: YWJjMTIzNAousername: emhhbmdzYW4K
kind: Secret
metadata:creationTimestamp: 2021-05-24T09:11:18Zname: mysecret1namespace: defaultresourceVersion: 45641selfLink: /api/v1/namespaces/default/secrets/mysecret1uid: fffb7902-bc6f-11eb-acba-000c29d88bba
type: Opaque4.使用方式 1、将 Secret 挂载到 Volume 中以 Volume 的形式挂载到 Pod 的某个目录下
vim secret-test.yaml
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: nginximage: nginxvolumeMounts:- name: secretsmountPath: /etc/secretsreadOnly: 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 2、将 Secret 导出到环境变量中
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: passwordenvFrom:- secretRef:name: mysecret1kubectl 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
1.介绍
与Secret类似区别在于ConfigMap保存的是不需要加密配置的信息。 ConfigMap 功能在 Kubernetes1.2 版本中引入许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制ConfigMap 可以被用来保存单个属性也可以用来保存整个配置文件或者JSON二进制大对象。 应用场景应用配置
2.创建 ConfigMap
1、使用目录创建
mkdir /opt/configmap/vim /opt/configmap/game.config
enemy.typesaliens,monsters
player.maximum-lives5 vim /opt/configmap/ui.config
color.goodpurple
color.badyellow
allow.textmodetruels /opt/configmap/
game.config
ui.configkubectl create configmap game-config --from-file/opt/configmap/
//--from-file 指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对键的名字就是文件名值就是文件的内容kubectl get cm
NAME DATA AGE
game-config 2 10skubectl get cm game-config -o yaml
apiVersion: v1
data:game.config: |enemy.typesaliens,monstersplayer.maximum-lives5 ui.config: |color.goodpurplecolor.badyellowallow.textmodetrue
kind: ConfigMap
metadata:creationTimestamp: 2021-05-25T06:49:18Zname: game-confignamespace: defaultresourceVersion: 87803selfLink: /api/v1/namespaces/default/configmaps/game-configuid: 541b5302-bd25-11eb-acba-000c29d88bba2、使用文件创建
只要指定为一个文件就可以从单个文件中创建 ConfigMap
--from-file 这个参数可以使用多次即可以使用两次分别指定上个实例中的那两个配置文件效果就跟指定整个目录是一样的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-23、使用字面值创建
使用文字值创建利用 --from-literal 参数传递配置信息该参数可以使用多次格式如下
kubectl create configmap special-config --from-literalspecial.howvery --from-literalspecial.typegoodkubectl get configmaps special-config -o yaml
apiVersion: v1
data:special.how: very #key-value 结构special.type: good
kind: ConfigMap
metadata:creationTimestamp: 2021-05-25T06:59:37Zname: special-confignamespace: defaultresourceVersion: 88610selfLink: /api/v1/namespaces/default/configmaps/special-configuid: c4f45936-bd26-11eb-acba-000c29d88bbakubectl delete cm --all
kubectl delete pod --all
3.Pod 中使用 ConfigMap
1、使用 ConfigMap 来替代环境变量
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的创建
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 33s
kubectl logs pod-test
KUBERNETES_SERVICE_PORT443
KUBERNETES_PORTtcp://10.0.0.1:443
HOSTNAMEpod-test
SHLVL1
SPECIAL_HOW_KEYvery #赋值变量 SPECIAL_HOW_KEY 的值为 special-config 的 special.how: very
HOME/root
SPECIAL_TYPE_KEYgood #赋值变量 SPECIAL_TYPE_KEY 的值为 special-config 的 special.type: good
KUBERNETES_PORT_443_TCP_ADDR10.0.0.1
PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT443
KUBERNETES_PORT_443_TCP_PROTOtcp
log_levelINFO #引入 env-config 的变量 log_level: INFO
KUBERNETES_PORT_443_TCPtcp://10.0.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS443
KUBERNETES_SERVICE_HOST10.0.0.1
PWD/
4.用 ConfigMap 设置命令行参数 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
5.通过数据卷插件使用ConfigMap
在数据卷里面使用 ConfigMap就是将文件填入数据卷在这个文件中键就是文件名键值就是文件内容
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 6.ConfigMap 的热更新
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.yaml
kubectl 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: 93616selfLink: /api/v1/namespaces/default/configmaps/log-configuid: 1b8115de-bd2f-11eb-acba-000c29d88bba//等大概10秒左右使用该 ConfigMap 挂载的 Volume 中的数据同步更新
kubectl exec -it my-nginx-76b6489f44-6dwxh -- cat /etc/config/log_level
DEBUG7.ConfigMap 更新后滚动更新 Pod
更新 ConfigMap 目前并不会触发相关 Pod 的滚动更新可以通过在 .spec.template.metadata.annotations 中添加 version/config 每次通过修改 version/config 来触发滚动更新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 不会同步更新。
●使用该 ConfigMap 挂载的 Volume 中的数据需要一段时间实测大概10秒才能同步更新。