kubernetes-配置管理(十一)
Secret
https://kubernetes.io/docs/concepts/configuration/secret/
Secret解决了密码、token、密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者Pod Spec中。Secret可以以Volume或者环境变量的方式使用。
使用kubectl创建secret
[root@k8s-master1 secret]# echo -n 'admin' > ./username.txt
[root@k8s-master1 secret]# echo -n '1f2d1e2e67df' > ./password.txt
[root@k8s-master1 secret]# kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
secret/db-user-pass created
查看secret信息
[root@k8s-master1 secret]# kubectl get secret
NAME TYPE DATA AGE
db-user-pass Opaque 15s
default-token-7vs6s kubernetes.io/service-account-token 6d23h
registry-pull-secret kubernetes.io/dockerconfigjson 5d3h
sslexample-foo-com kubernetes.io/tls 66m
[root@k8s-master1 secret]# kubectl describe secret/db-user-pass
Name: db-user-pass
Namespace: default
Labels: <none>
Annotations: <none> Type: Opaque Data
====
password.txt: bytes
username.txt: bytes
使用yaml文件创建secret
[root@k8s-master1 secret]# echo -n 'admin' | base64
YWRtaW4=
[root@k8s-master1 secret]# echo -n '1f2d1e2e67df' | base64
MWYyZDFlMmU2N2Rm
[root@k8s-master1 secret]# vim secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm [root@k8s-master1 secret]# kubectl create -f secret.yaml
secret/mysecret created
Pod 可以通过 Volume 的方式使用 Secret
[root@k8s-master1 secret]# vim secret-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-secret
spec:
containers:
- name: pod-secret
image: busybox
args:
- /bin/sh
- -c
- sleep ;touch /tmp/healthy;sleep
volumeMounts:
- name: foo
mountPath: "/etc/foo"
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret [root@k8s-master1 secret]# kubectl apply -f secret-vol.yaml
pod/pod-secret created
进入容器查看
[root@k8s-master1 secret]# kubectl exec -it pod-secret sh
/ # ls /etc/foo/
password username/ # cat /etc/foo/username
admin/ #
/ # cat /etc/foo/password
1f2d1e2e67df/ #
以 Volume 方式使用的 Secret 支持动态更新:Secret 更新后,容器中的数据也会更新。
[root@k8s-master1 secret]# vim secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWt3OG4zbDQ4Yg== [root@k8s-master1 secret]# kubectl apply -f secret.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
secret/mysecret configured
[root@k8s-master1 secret]# kubectl exec -it pod-secret sh/ # cat /etc/foo/password
1kw8n3l48b/ #
/ #
Pod 可以通过 环境变量 的方式使用 Secret
[root@k8s-master1 secret]# vim secret-env.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-secret-env
spec:
containers:
- name: pod-secret-env
image: busybox
args:
- /bin/sh
- -c
- sleep ;touch /tmp/healthy;sleep
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password [root@k8s-master1 secret]# kubectl apply -f secret-env.yaml
pod/pod-secret-env created
[root@k8s-master1 secret]# kubectl exec -it pod-secret-env sh
/ # echo $SECRET_USERNAME
admin
/ # echo $SECRET_PASSWORD
1kw8n3l48b
通过环境变量 SECRET_USERNAME 和 SECRET_PASSWORD 成功读取到 Secret 的数据。
需要注意的是,环境变量读取 Secret 很方便,但无法支撑 Secret 动态更新。
Secret 可以为 Pod 提供密码、Token、私钥等敏感数据;对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap
ConfigMap
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/
configmap是让配置文件从镜像中解耦,让镜像的可移植性和可复制性。许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。这些配置信息需要与docker image解耦,你总不能每修改一个配置就重做一个image吧?ConfigMap API给我们提供了向容器中注入配置信息的机制,ConfigMap可以被用来保存单个属性,也可以用来保存整个配置文件或者JSON二进制大对象。
configmap的创建
命令创建configmap
[root@k8s-master1 configmap]# kubectl create configmap nginx-config --from-literal=nginx_port= --from-literal=server_name=myapp.magedu.com
configmap/nginx-config created
[root@k8s-master1 configmap]# kubectl get cm
NAME DATA AGE
nginx-config 8s
[root@k8s-master1 configmap]# kubectl describe cm nginx-config
Name: nginx-config
Namespace: default
Labels: <none>
Annotations: <none> Data
====
nginx_port:
---- server_name:
----
myapp.magedu.com
Events: <none>
通过 --from-file:每个文件内容对应一个信息条目。
[root@k8s-master1 configmap]# vim www.conf
server {
server_name myapp.magedu.com;
listen ;
root /data/web/html;
}
[root@k8s-master1 configmap]# kubectl create configmap nginx-www --from-file=./www.conf
configmap/nginx-www created
[root@k8s-master1 configmap]# kubectl get cm
NAME DATA AGE
nginx-config 16m
nginx-www 8s
[root@k8s-master1 configmap]# kubectl get cm nginx-www -o yaml
apiVersion: v1
data:
www.conf: |
server {
server_name myapp.magedu.com;
listen ;
root /data/web/html;
}
kind: ConfigMap
metadata:
creationTimestamp: "2018-12-26T03:49:22Z"
name: nginx-www
namespace: default
resourceVersion: ""
selfLink: /api/v1/namespaces/default/configmaps/nginx-www
uid: 3add1507-08c1-11e9-ad5d-000c2977dc9c
使用configmap
环境变量方式注入到pod
[root@k8s-master1 configmap]# vim pod-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: "cluster admin"
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort:
env:
- name: NGINX_SERVER_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name [root@k8s-master1 configmap]# kubectl apply -f pod-configmap.yaml
pod/pod-cm- created
[root@k8s-master1 configmap]# kubectl exec -it pod-cm- -- /bin/sh
/ # echo $NGINX_SERVER_PORT / # echo $NGINX_SERVER_NAME
myapp.magedu.com
修改端口,可以发现使用环境变化注入pod中的端口不会根据配置的更改而变化
[root@k8s-master1 configmap]# kubectl edit cm nginx-config
configmap/nginx-config edited
[root@k8s-master1 configmap]# kubectl exec -it pod-cm- -- /bin/sh
/ # echo $NGINX_SERVER_PORT
存储卷方式挂载configmap:
Volume 形式的 ConfigMap 也支持动态更新
[root@k8s-master1 configmap]# vim pod-configmap-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: "cluster admin"
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort:
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/config.d/
readOnly: true
volumes:
- name: nginxconf
configMap:
name: nginx-config [root@k8s-master1 configmap]# kubectl apply -f pod-configmap-vol.yaml
pod/pod-cm- created
[root@k8s-master1 configmap]# kubectl exec -it pod-cm- -- /bin/sh
# cd /etc/nginx/config.d/
# ls
nginx_port server_name
# cat server_name
myapp.magedu.com
以nginx-www配置nginx
[root@k8s-master1 configmap]# vim pod-configmap-ngx.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-cm-
namespace: default
labels:
app: myapp
tier: frontend
annotations:
magedu.com/created-by: "cluster admin"
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort:
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/conf.d/
readOnly: true
volumes:
- name: nginxconf
configMap:
name: nginx-www [root@k8s-master1 configmap]# kubectl apply -f pod-configmap-ngx.yaml
pod/pod-cm- created
[root@k8s-master1 configmap]# kubectl exec -it pod-cm- -- /bin/sh
/ # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # ls
www.conf
/etc/nginx/conf.d # cat www.conf
server {
server_name myapp.magedu.com;
listen ;
root /data/web/html;
}
kubernetes-配置管理(十一)的更多相关文章
- K8S学习笔记之Kubernetes 配置管理 ConfigMap
0x00 概述 很多情况下我们为某一应用做好镜像,当我们想修改其中的一些参数的时候,就变得比较麻烦,又要重新制作镜像,我们是不是有一种方式,让镜像根据不同的场景调用我们不同的配置文件呢,那我们就需要用 ...
- Kapitan 通用terraform&& kubernetes 配置管理工具
Kapitan 是一个通用的配置管理工具,可以帮助我们管理terraform .kubernetes 以及其他的配置. Kapitan 自生基于jsonnet 开发,对于我们日常进行软件的部署(tf以 ...
- Kubernetes 配置管理 ConfigMap(十二)
目录 一.背景 二.创建 ConfigMap 2.1.通过 --from-literal 2.2.通过 --from-file 2.3.通过--from-env-file 2.4.YAML 配置文件 ...
- Kubernetes 配置管理
ConfigMap(可变配置管理) 对于应用的可变配置在 Kubernetes 中是通过一个 ConfigMap 资源对象来实现的,我们知道许多应用经常会有从配置文件.命令行参数或者环境变量中读取一些 ...
- kubernetes实战(十一):k8s使用openLDAP统一认证
1.基本概念 为了方便管理和集成jenkins,k8s.harbor.jenkins均使用openLDAP统一认证. 2.部署openLDAP 此处将openLDAP部署在k8s上,openLDAP可 ...
- Kubernetes 配置管理 Dashboard(十三)
目录 一.安装配置 1.1 下载 镜像 1.2.安装 1.3.修改 NodePort 二.配置授权 Kubernetes 所有的操作我们都是通过命令行工具 kubectl 完成的.为了提供更丰富的用户 ...
- Kubernetes第十一章--部署微服务电商平台
- kubernetes 学习资料
谷歌大神详解 Kubernetes 配置管理最佳方法 https://www.kubernetes.org.cn/3031.html all in on kubernetes https://gith ...
- 026.[转] 基于Docker及Kubernetes技术构建容器云平台 (PaaS)
[编者的话] 目前很多的容器云平台通过Docker及Kubernetes等技术提供应用运行平台,从而实现运维自动化,快速部署应用.弹性伸缩和动态调整应用环境资源,提高研发运营效率. 本文简要介绍了与容 ...
- k8s API sample
Declarative API k8s: cluster-api Introduction to Kubernetes Cluster-API Project Declarative Manageme ...
随机推荐
- NodeJS什么都能做,为什么还要JAVA?
这张图看起来简单而且很好理解,但没尝试过,会有很多疑问. SPA模式中,后端已供了所需的数据接口,view前端已经可以控制,为什么要多加NodeJS这一层? 多加一层,性能怎么样? 多加一层,前端的工 ...
- CF447B DZY Loves Strings 贪心
DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter ...
- thinkphp5使用QueryList实现采集功能
QueryList是基于phpQuery的 1.下载`QueryList.php`和`phpQuery.php`这两个文件. 2.在`extend`下新建`QL`目录. 3.将下载好的`QueryLi ...
- Eclipse中新建Maven Web项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
在maven web项目中的index.jsp中的错误信息如下: The superclass "javax.servlet.http.HttpServlet" was not f ...
- RabbitMQ基础概念及使用
RabbitMQ RabbitMQ是什么? RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.MQ全称为Message Q ...
- java 使用 spirng 监控 cpu 使用 状态。。。。
首先..使用 sigar ,sigar 使用 要 注意区分 是 web 还是 本地..最好 在 WEB-INF 下 复制 dll 文件..因为WEB-INF 不会被压缩... try { //如果是 ...
- 什么是obj文件?
百度百科: 程序编译时生成的中间代码文件.目标文件,一般是程序编译后的二进制文件,再通过链接器(LINK.EXE)和资源文件链接就成可执行文件了.OBJ只给出了程序的相对地址,而可执行文件是绝对地址. ...
- GUI的最终选择 Tkinter(三):Checkbutton组件和Radiobutton组件、LabelFrame组件
Checkbutton组件 Checkbutton组件就是常见的多选按钮,而Radiobutton则是单选按钮 from tkinter import * root = Tk() v = IntVar ...
- Java面向对象_对象数组
今天学习了对象数组,写点东西总结一下.废话不多说,啥是对象数组呢? 对象数组的概念是这么讲的,对象数组就是数组里的每个元素都是类的对象,赋值时先定义对象,然后将对象直接赋给数组. 举个例子,使用对象数 ...
- 美国一家科技公司毕威拓(Pivotal)规定员工在早上9点06分准时上班,以提高工作效率。
美国一家科技公司毕威拓(Pivotal)规定员工在早上9点06分准时上班,以提高工作效率. 据<英国广播公司BBC>报道,美国科技公司毕威拓(Pivotal)的员工每天都要在9点06分准时 ...