一、Secret:
1、概念
用来保存密码。token,敏感的K8S资源
这类数据可以直接存放在镜像中,但是放在Secret中可以更方便的控制,减少暴露的风险
Secret:保存加密的信息
2、Secret类型:
docker-registry:存储docker仓库的认证信息,以及docker的组件认证信息(私有)
generic:是Secret的默认模式,类型Opaque,base64加密编码的Secret,存储用户自定义的米密码、秘钥等
tls:用于存储证书和私钥(保存http信息)
系统自建的:kubernetes.io/service-account-token用来访问系统的apiserver,pod会默认使用这个kubernetes.io/service-account-token创建的Secret和apiserver通信。自动挂载到pod的/run/Secret/kubernetes.io/serviceaccount
3、pod如何来引用Secret:
三种方式:
- 挂载的方式,Secret挂载到pod中的一个或者多个容器上的卷里面
- 把Secret作为容器的环境变量
- 集群拉取镜像时使用docker-registry。使用Secret可以实现免密登录
4、创建Secret的方式:
4.1、陈述式创建:
#创建Secret,指定文件提取信息
kubectl create secret generic mysecret --from-file=/opt/username.txt --from-file=/opt/passwd.txt
generic默认类型,Opequel加密类型
--from-file=/opt/username.txt
从指定获取需要加密的信息
4.2、声明式创建:
要先将资源转换格式
apiVersion: v1
kind: Secret
metadata:name: mysecret1
type:Opaque
data:username: dXNlcm5hbWUudHh0Cg==passwd: cGFzc3dkLnR4dAo=
5、Secret使用:
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: nginximage: nginx:1.22volumeMounts:- name: secretsmountPath: "/etc/secret"readOnly: falsevolumes:- name: secretssecret:secretName: mysecret1
kubectl create secret generic mysecret2 --from-file=/etc/passwd --from-file=/etc/shadow
保存的内容是加密的内容,但是容器内部可以直接解密引用
作为环境变量传参到容器内部:
apiVersion: v1
kind: Pod
metadata:name: mypod1
spec:containers:- name: nginximage: nginx:1.22env:- name: USERvalueFrom:secretKeyRef:name: mysecret1key: passwd- name: USER1valueFrom:secretKeyRef:name: mysecret1key: username
#我给nginx1.22这个容器里面传了两个环境变量USRE和USER1,这两个变量的值从Secret来,分别是两条mysecret1中的加密信息
docker-registry
存储docker仓库的认证信息,以及docker的组件认证信息(私有)
kubectl create secret docker-registry myharbor --docker-server=20.0.0.66 --docker-username=admin --docker-password=123456
apiVersion: v1
kind: Pod
metadata:name: mypod2
spec:containers:- name: nginximage: hub.test.com/library/nginx:v1imagePullSecrets:- name: myharbor
6、总结
Secret的三种方式:
创建可以时陈述式创建也可以时声明式创建
引用方式:
挂载使用(最常用)
设定环境变量
docker-Registry
二、configmap:
1、概述
保存的是不需要加密的信息。configmap是1.2之后引入的功能,应用程序会从配置文件,命令参数,以及环境变量中读取配置信息
将保存的信息传给容器内部
configmap在创建容器中,给他注入我们需要的配置信息。既可以是单个的属性也可以是整个容器的配置文件
2、configmap的创建方式:
陈述式和声明式
#从指定文件创建,可以是一个也可以是多个文件
kubectl create configmap game --from-file=/opt/configmap/aa.txt --from-file=/opt/configmap/bb.txtkubectl get cm
kubectl describe cm game
使用字面值创建:
#直接在命令行定义好值和变量
kubectl create configmap game1 --from-literal=koeda=shuai --from-literal=kang=nb
声明式:
apiVersion: v1
kind: ConfigMap
metadata:name: game
data:koeda: shuaikang: niubi
configmap是以键值对的形式保存
3、pod中如何使用configmap
apiVersion: v1
kind: Pod
metadata:name: mypod2
spec:containers:- name: nginx1image: nginx:1.22env:- name: USER1valueFrom:configMapKeyRef:name: game1key: kang- name: USER2valueFrom:configMapKeyRef:name: game1key: koeda
数据卷使用configmap:
kubectl create configmap nginx-con --from-file=/opt/nginx-configmap/nginx.conf
vim nginx.conf
worker_processes 2;
events {worker_connections 1024;
}
http {default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 8081;server_name localhost;charset utf-8;location / {root html;index index.html index.php;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}
}
}
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx1labels:app: nginx1spec:replicas: 3selector:matchLabels:app: nginx1template:metadata:labels:app: nginx1spec:containers:- name: nginx1image: nginx:1.22ports:- containerPort: 8081volumeMounts:- name: nginx-configmountPath: /etc/nginx/- name: nginx-mountmountPath: /usr/share/nginx/htmlvolumes:- name: nginx-configconfigMap:name: nginx-con- name: nginx-mounthostPath:path: /opt/htmltype: DirectoryOrCreate
我们通过数据卷的形式,将文件传到pod容器内部
去对应节点上,将html下创建index.html
访问测试404
因为是yum安装的所以访问目录时/usr/share/nginx/html,而不是默认的root
想要在容器运行的情况下更改配置文件,就要用到configmap的热备份
configmap的热更新:
在pod运行的情况下对configmap的配置信息进行修改,直接生效(反映到容器当中)
#热更新就是进去给nginx-con的配置文件
kubectl edit cm nginx-con
因为挂载,所以更改之后容器内的文件会自动更改
文件更新之后,需要重启服务才能能生效
configmap的热更新是不会触发pod的滚动更新机制(deployment)
热更新之后,容器中的配置文件改变,但是生效要重启服务
version/config来触发滚动更新机制
kubectl patch deployments.apps nginx1 --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20240116"}}}}}'
重启之后再访问:
4、总结:
Secret:主要作用就是保存加密文件,主要的使用方式就是挂载方式
configmapMap:把配置信息传给容器,主要方式也是挂载方式
configmap的热更新,热更新可以直接反应到容器的内部,也不会触发pod的更新机制,如果不是需要重启的配置,都可以直接生效
需要重启的,可以通过version/config来触发滚动更新机制
所谓的更新就是把配置信息重新传到容器内,重启也是一样
configmap在工作中在工作中经常是把配置文件传给容器
configmap是以键值对形式保存的非加密的信息