kubernetes集群编排——k8s存储(configmap,secrets)

2023-11-08 02:12

本文主要是介绍kubernetes集群编排——k8s存储(configmap,secrets),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

configmap

字面值创建

kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2kubectl get cmkubectl describe cm my-config

通过文件创建

kubectl create configmap my-config-2 --from-file=/etc/resolv.confkubectl describe cm my-config-2

通过目录创建

mkdir testcp /etc/passwd test/cp /etc/fstab  test/ls test/

kubectl create configmap my-config-3 --from-file=testkubectl describe cm my-config-3

通过yaml文件创建

vim cm1.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: cm1-config
data:db_host: "172.25.0.250"db_port: "3306"
kubectl apply -f cm1.yamlkubectl describe cm cm1-config

使用configmap设置环境变量

vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:name: pod1
spec:containers:- name: pod1image: busyboxcommand: ["/bin/sh", "-c", "env"]env:- name: key1valueFrom:configMapKeyRef:name: cm1-configkey: db_host- name: key2valueFrom:configMapKeyRef:name: cm1-configkey: db_portrestartPolicy: Never
kubectl apply -f pod1.yamlkubectl logs pod1

kubectl delete  pod pod1
vim pod2.yaml
apiVersion: v1
kind: Pod
metadata:name: pod2
spec:containers:- name: pod2image: busyboxcommand: ["/bin/sh", "-c", "env"]envFrom:- configMapRef:name: cm1-configrestartPolicy: Never
kubectl apply -f pod2.yamlkubectl logs pod2

kubectl delete  pod pod2

使用conigmap设置命令行参数

vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:name: pod3
spec:containers:- name: pod3image: busyboxcommand: ["/bin/sh", "-c", "echo $(db_host) $(db_port)"]envFrom:- configMapRef:name: cm1-configrestartPolicy: Never
kubectl apply -f pod3.yamlkubectl logs  pod3

kubectl delete  pod pod3

通过数据卷使用configmap

vim pod4.yaml
apiVersion: v1
kind: Pod
metadata:name: pod4
spec:containers:- name: pod4image: busyboxcommand: ["/bin/sh", "-c", "cat /config/db_host"]volumeMounts:- name: config-volumemountPath: /configvolumes:- name: config-volumeconfigMap:name: cm1-configrestartPolicy: Never
kubectl apply -f pod4.yamlkubectl logs  pod4

kubectl delete pod pod4

configmap热更新

vim nginx.conf
server {listen       8000;server_name  _;location / {root /usr/share/nginx/html;index  index.html index.htm;}
}
kubectl create configmap nginxconf --from-file=nginx.confkubectl describe cm nginxconf

vim my-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: my-nginx
spec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginxvolumeMounts:- name: config-volumemountPath: /etc/nginx/conf.dvolumes:- name: config-volumeconfigMap:name: nginxconf
kubectl apply -f my-nginx.yamlkubectl get pod -o wide

kubectl exec my-nginx-85fb986977-87dff -- cat /etc/nginx/conf.d/nginx.conf

curl 10.244.219.17:8000

编辑cm,修改端口

kubectl edit  cm nginxconf

kubectl exec my-nginx-85fb986977-87dff -- cat /etc/nginx/conf.d/nginx.conf

修改cm后,过上几秒配置信息会同步到容器,但是容器内运行的服务并不会加载生效,需要手动刷新

方式一:(推荐)

kubectl delete  pod my-nginx-85fb986977-87dff

方式二:(手动触发版本更新,会新建一个replicaset)

kubectl patch deployments.apps my-nginx --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20231103"}}}}}'
kubectl get pod -o wide

curl 10.244.106.133

secrets

从文件创建

echo -n 'admin' > ./username.txtecho -n 'westos' > ./password.txtkubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txtkubectl get secrets db-user-pass -o yaml

编写yaml文件

echo -n 'admin' | base64echo -n 'westos' | base64

vim mysecret.yaml
apiVersion: v1
kind: Secret
metadata:name: mysecret
type: Opaque
data:username: YWRtaW4=			#必须编码后的值password: d2VzdG9z
kubectl apply -f mysecret.yamlkubectl get secrets mysecret -o yaml

将Secret挂载到Volume中

vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:name: mysecret
spec:containers:- name: nginximage: nginxvolumeMounts:- name: secretsmountPath: "/secret"readOnly: truevolumes:- name: secretssecret:secretName: mysecret
kubectl apply  -f pod1.yamlkubectl get pod

kubectl exec  mysecret -- ls /secret

kubectl delete  -f pod1.yaml

向指定路径映射 secret 密钥

vim pod2.yaml
apiVersion: v1
kind: Pod
metadata:name: mysecret
spec:containers:- name: nginximage: nginxvolumeMounts:- name: secretsmountPath: "/secret"readOnly: truevolumes:- name: secretssecret:secretName: mysecretitems:- key: usernamepath: my-group/my-username
kubectl apply -f pod2.yamlkubectl exec  mysecret -- cat /secret/my-group/my-username

kubectl delete  -f pod2.yaml

将Secret设置为环境变量

vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:name: secret-env
spec:containers:- name: pod3image: busyboxcommand: ["/bin/sh", "-c", "env"]env:- name: SECRET_USERNAMEvalueFrom:secretKeyRef:name: mysecretkey: username- name: SECRET_PASSWORDvalueFrom:secretKeyRef:name: mysecretkey: passwordrestartPolicy: Never
kubectl apply -f pod3.yamlkubectl logs secret-env

存储docker registry的认证信息

kubectl create secret docker-registry myregistrykey --docker-server=reg.westos.org --docker-username=admin --docker-password=westos --docker-email=hjl@westos.org

新建私有仓库

vim pod4.yaml
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: game2048image: reg.westos.org/westos/game2048imagePullSecrets:- name: myregistrykey
kubectl apply -f pod4.yamlkubectl  get pod

推荐把registrykey绑定到sa,这样yaml文件中就可以不用指定,更加安全。

kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "myregistrykey"}]}'kubectl describe sa default

这篇关于kubernetes集群编排——k8s存储(configmap,secrets)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/367334

相关文章

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

k8s中实现mysql主备过程详解

《k8s中实现mysql主备过程详解》文章讲解了在K8s中使用StatefulSet部署MySQL主备架构,包含NFS安装、storageClass配置、MySQL部署及同步检查步骤,确保主备数据一致... 目录一、k8s中实现mysql主备1.1 环境信息1.2 部署nfs-provisioner1.2.

k8s admin用户生成token方式

《k8sadmin用户生成token方式》用户使用Kubernetes1.28创建admin命名空间并部署,通过ClusterRoleBinding为jenkins用户授权集群级权限,生成并获取其t... 目录k8s admin用户生成token创建一个admin的命名空间查看k8s namespace 的

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

k8s搭建nfs共享存储实践

《k8s搭建nfs共享存储实践》本文介绍NFS服务端搭建与客户端配置,涵盖安装工具、目录设置及服务启动,随后讲解K8S中NFS动态存储部署,包括创建命名空间、ServiceAccount、RBAC权限... 目录1. NFS搭建1.1 部署NFS服务端1.1.1 下载nfs-utils和rpcbind1.1

Redis高性能Key-Value存储与缓存利器常见解决方案

《Redis高性能Key-Value存储与缓存利器常见解决方案》Redis是高性能内存Key-Value存储系统,支持丰富数据类型与持久化方案(RDB/AOF),本文给大家介绍Redis高性能Key-... 目录Redis:高性能Key-Value存储与缓存利器什么是Redis?为什么选择Redis?Red

k8s容器放开锁内存限制问题

《k8s容器放开锁内存限制问题》nccl-test容器运行mpirun时因NCCL_BUFFSIZE过大导致OOM,需通过修改docker服务配置文件,将LimitMEMLOCK设为infinity并... 目录问题问题确认放开容器max locked memory限制总结参考:https://Access

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

Redis中哨兵机制和集群的区别及说明

《Redis中哨兵机制和集群的区别及说明》Redis哨兵通过主从复制实现高可用,适用于中小规模数据;集群采用分布式分片,支持动态扩展,适合大规模数据,哨兵管理简单但扩展性弱,集群性能更强但架构复杂,根... 目录一、架构设计与节点角色1. 哨兵机制(Sentinel)2. 集群(Cluster)二、数据分片

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I