当前位置: 首页 > news >正文

kubernetes集群编排——k8s存储

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

http://www.lryc.cn/news/216792.html

相关文章:

  • 【软件STM32cubeIDE下H73xx配置串口uart1+中断接收/DMA收发+HAL库+简单数据解析-基础样例】
  • jdk8和jdk9中接口的新特性
  • 1-爬虫-requests模块快速使用,携带请求参数,url 编码和解码,携带请求头,发送post请求,携带cookie,响应对象, 高级用法
  • java商城免费搭建 VR全景商城 saas商城 b2b2c商城 o2o商城 积分商城 秒杀商城 拼团商城 分销商城 短视频商城
  • 【TS篇一】TypeScript介绍、使用场景、环境搭建、类和接口
  • Tuna: Instruction Tuning using Feedback from Large Language Models
  • uni-app 应对微信小程序最新隐私协议接口要求的处理方法
  • PostgreSQL 进阶 - 使用foreign key,使用 subqueries 插入,inner joins,outer joins
  • 【Python 千题 —— 基础篇】地板除计算
  • 【随手记】np.random.choice()函数
  • 2003-2022年地级市-财政收支明细数据(企业、个人所得税、科学、教育、医疗等)
  • 影响服务器正常使用的有哪些因素
  • NLP学习笔记:使用 Python 进行NLTK
  • 突破性技术!开源多模态模型—MiniGPT-5
  • IntelliJ IDEA快捷键sout不生效
  • 用C++QT实现一个modbus rtu通讯程序框架
  • Python如何设置下载第三方软件包的国内镜像站服务器的地址
  • ChatGLM3-6B详细安装过程记录(Linux)
  • python的类
  • 前端 用HTML,CSS, JS 写一个简易的音乐播放器
  • 自定义QChartView实现鼠标放在图表时,显示鼠标位置坐标值(x,y)
  • antv/g6 交互与事件及自定义Behavior
  • MongoDB根据时间范围查询
  • 大数据Doris(十五):Doris表的字段类型
  • 文本批量处理,一键转换HTML文件编码,释放您的繁琐工作!
  • 硬件工程师到底可以从哪些方面提升自己?
  • 论文辅助笔记:t2vec models.py
  • R语言如何写一个爬虫代码模版
  • 鸿运主动安全云平台任意文件下载漏洞复习
  • CMake基础【学习笔记(八)】