kubernetes configMap secret

2023-05-16

配置容器化应用的方式

  • 自定义命令行参数
  • 把配置文件直接焙进镜像
  • 环境变量
    • cloud native的应用程序一般可直接通过环境变量加载配置
    • 通过entrypoint脚本来预处理变量
  • 存储卷

configMap(配置中心)

pod从configMap中读取配置关联到pod

创建configMap

kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.wuxingge.com

查看

kubectl get configmaps
kubectl describe configmaps nginx-config

cat www.conf

server {
	server_name myapp.wuxingge.com;
	listen 80;
	root /data/web/html/;
}
kubectl create configmap nginx-www --from-file=./www.conf

编辑

kubectl edit configmaps nginx-www

在这里插入图片描述

挂载存储卷中的部分键值

  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-config-files
      items:
      - key: myserver.conf
        path: myserver.conf
        mode: 0644
      - key: myserver-gzip.cfg
        path: myserver-compression.cfg
  • key:要引用的键名称,必选字段。
  • path:对应的键于挂载点目录中生成的文件的相对路径,可以不同于键名称,必选字段
  • mode:文件的权限模型,可用范围为0到0777

pod-configmap.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-1
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    wuxingge.com/created-by: "cluster admin"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    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

pod-configmap-2.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-2
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    wuxingge.com/created-by: "cluster admin"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxconf
      mountPath: /etc/nginx/config.d/
      readOnly: true
  volumes:
  - name: nginxconf
    configMap:
      name: nginx-config

pod-configmap-3.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-3
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    wuxingge.com/created-by: "cluster admin"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxconf
      mountPath: /etc/nginx/conf.d/
      readOnly: true
  volumes:
  - name: nginxconf
    configMap:
      name: nginx-www
  • 无论是装载所有文件还是部分文件,挂载点目录下原有的文件都会被隐藏。
  • vol-umeMounts字段中使用的subPath字段,可以支持用户从存储卷挂载单个文件或单个目录而非整个存储卷
    volumeMounts:
    - name: ngxconfig
      mountPath: /etc/nginx/conf.d/myserver.conf
      subPath: myserver.conf
      readOnly: true
    - name: ngxconfig
      mountPath: /etc/nginx/conf.d/myserver-status.cfg
      subPath: myserver-status.cfg
      readOnly: true  
  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-config-files
  • 使用ConfigMap资源为容器应用提供配置的优势之一在于其支持容器应用动态更新其配置
  • 用户直接更新ConfigMap对象,而后由容器应用重载其配置文件即可

nginx使用configmap挂载单个配置文件

vi nginx-cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  namespace: default
data:
  nginx.conf: |-
    user  nginx;
    worker_processes  auto;
    
    error_log  /var/log/nginx/error.log notice;
    pid        /var/run/nginx.pid;
    
    
    events {
        use epoll;
        worker_connections 65535;
        accept_mutex off;
        multi_accept off;
    }
    
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }

vi nginx-dp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-test
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-test
  template:
    metadata:
      labels:
        app: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: nginx:alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-conf
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
          readOnly: true
      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-conf
          items:
          - key: nginx.conf
            path: nginx.conf
            mode: 0644
---
apiVersion: v1   
kind: Service
metadata:
  name: nginx-test
  namespace: default
spec:
  selector:
    app: nginx-test
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 80

secret

创建secret

kubectl create secret generic mysql-root-password --from-literal=password=MyP@ss123

查看

kubectl get secrets mysql-root-password -o yaml
apiVersion: v1
data:
  password: TXlQQHNzMTIz
kind: Secret
metadata:
  creationTimestamp: "2019-11-30T08:26:57Z"
  name: mysql-root-password
  namespace: default
  resourceVersion: "711777"
  selfLink: /api/v1/namespaces/default/secrets/mysql-root-password
  uid: 2c259780-134b-11ea-a76c-000c29b4d624
type: Opaque
echo TXlQQHNzMTIz |base64 -d
MyP@ss123

pod-secret-1.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-secret-1
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    wuxingge.com/created-by: "cluster admin"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    env:
    - name: MYSQL_ROOT_PASSWORD
      valueFrom:
        secretKeyRef:
          name: mysql-root-password
          key: password

密钥证书

(umask 077; openssl genrsa -out nginx.key 2048)
openssl req -new -x509 -key nginx.key -out nginx.crt -subj /C=CN/ST=Beijing/L=Beijing/O=DevOps/CN=www.ilinux.io
kubectl create secret tls nginx-ssl --key=./nginx.key --cert=./nginx.crt
  containers:
  - image: nginx:alpine
    name: web-server
    volumeMounts:
    - name: nginxcert
      mountPath: /etc/nginx/ssl/
      readOnly: true  
  volumes:
  - name: nginxcert
    secret:
      secretName: nginx-ssl

docker仓库 secret

创建docker仓库 secret

kubectl -n default create secret docker-registry docker-registry-key --docker-server=x.x.x.x:5000 --docker-username=xxx --docker-password=xxx

k8s 拉取私有仓库镜像

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: dev
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
      namespace: dev
    spec:
      imagePullSecrets:
      - name: docker-registry-key
      containers:
      - name: nginx
        image: x.x.x.x:5000/nginx:20230202v1
        ports:
        - containerPort: 80
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

kubernetes configMap secret 的相关文章

随机推荐

  • ubuntu中aptitude工具的意思

    aptitude xff1a xff08 Debian系统的包管理工具 xff09 aptitude与 apt get 一样 xff0c 是 Debian 及其衍生系统中功能极其强大的包管理工具 与 apt get 不同的是 xff0c a
  • 什么是微服务

    一 微服务介绍 1 什么是微服务 在介绍微服务时 xff0c 首先得先理解什么是微服务 xff0c 顾名思义 xff0c 微服务得从两个方面去理解 xff0c 什么是 34 微 34 什么是 34 服务 34 xff0c 微 狭义来讲就是体
  • 三、Docker:命令

    其他文章 xff1a 一 Docker xff1a 概述 二 Docker xff1a 安装 三 Docker xff1a 命令 四 Docker xff1a 可视化管理 五 Docker xff1a 镜像 xff08 image 六 Do
  • mysql group by 报错Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated c

    当使用group by的语句中 xff0c select后面跟的列 xff0c 在group by后面没有时 xff0c 会报以下错误 xff1a Expression 2 of SELECT list is not in GROUP BY
  • Opencv快速入门(C++版),新手向

    Opencv快速入门 C 43 43 版 xff09 前言1 图像的读取与显示所使用的API接口 xff1a 代码演示 xff1a 2 图像色彩空间转换所使用的API接口 xff1a 代码演示 xff1a 3 图像对象的创建与赋值所使用的A
  • 前台解析jwt token 前后端分离 ant design pro

    前言 在如今得环境下 xff0c 越来越多得项目采用微服务 xff0c 前后端分离项目 优点在于同时开发 xff0c 分开部署 缺点在于需要约定的太多 xff0c 导致前后端联调产生分歧 就标题而言 xff0c 解决前端antd 接收后台返
  • win10 双击启动nacos报错 Unable to start web server...... Unable to start embedded Tomcat

    1 遇到的问题 win10双击启动nacos报错 2 分析 从启动cmd开始查看 发现 启动模式为集群模式 定位成功 3 解决 修改startup中启动模式 重新启动 成功
  • IDEA 远程debugger SpringBoot项目 超赞!!!

    如题哦 xff0c 项目发布到服务器上后 xff0c 每天被不同的bug所困扰 强大的idea超出你的想象 xff0c 强大到可以远程debugger xff0c 就和在本地一样一样的 进入正题 前提概要 线上即服务器代码必须与本地一致 x
  • git提交时 # Please enter the commit message for your changes. Lines starting # with ‘#‘ will be ignored

    问题 xff1a Please enter the commit message for your changes Lines starting with 39 39 will be ignored and an empty message
  • canal 修改配置信息后监听不到mysql数据并报错can‘t find start position for example

    原由 xff1a 数据库地址变化 canal 需要修改监听 问题 xff1a 修改配置信息后重启canal 但并无监听到数据库信息变化 分析 xff1a canal 与数据库之间断层 xff0c 导致信息传输失败 解决 xff1a xff0
  • AI那点事儿

    从古至今 xff0c 改朝换代 一代崛起 xff0c 就标志着一代的灭亡 AI的兴起 xff0c 让无数程序梦想客死他乡 无论是学者还是技术科研者 xff0c 无一不在说 xff0c AI的时代到了 然而 xff0c 我们扣心自问 xff0
  • win7 配置JDK环境变量

    第一步 xff1a 安装jdk 8u101 windows x64 exe xff0c 路径为默认路径 xff0c 一直下一步直到完成安装 安装最好不要修改安装路径 xff0c 防止自己找不到 第二步 xff1a 设置环境变量 xff1a
  • 完整的搭建内网穿透ngrok详细教程(有图有真相)

    如上 网上找到的都是不稳定的 还不如自己搭建一个 去问度娘了 xff0c 发现了一堆 好吧 xff0c 那就动手开干吧 准备工作 xff08 其实也是硬性条件 xff09 xff1a 1 服务器一台 2 备案域名一个 xff08 好多都说可
  • lsyncd-实时同步(镜像)守护程序

    E mail 1226032602 64 qq com 官方文档 https axkibe github io lsyncd https github com axkibe lsyncd 简介 Lsyncd使用文件系统事件接口 xff08
  • Dockerfile

    docker安装 yum span class token function install span y yum utils device mapper persistent data lvm2 span class token func
  • c51单片机学习笔记-LED闪烁编程

    目的 xff1a 使LED灯闪烁 xff0c 需循环让 D1 指示灯先亮一会后熄灭 xff0c 因此只需编写一个循环函数 xff0c 专门在那循环运行即可实现延时功能 编译软件 xff1a keil5 过程 1 书写延时函数 函数名 xff
  • 网络管理命令-nmcli

    网络管理工具 iproute 软件包包括 ip ss 命令 net tools软件包包括 ifconfig route netstat命令 ip 命令相当于之前的 ifconfig route ss 命令相当于之前的 netstat nmt
  • nginx

    本文作者 五行哥 QQ 1226032602 E mail 1226032602 64 qq com web服务器种类 apache nginx tomcat resin Lighttpd IIS WebLogic Jetty Node j
  • kubernetes ingress

    https kubernetes io docs concepts services networking ingress 负载均衡软件 NginxTraefikEnvoy https github com kubernetes ingre
  • kubernetes configMap secret

    配置容器化应用的方式 自定义命令行参数把配置文件直接焙进镜像环境变量 cloud native的应用程序一般可直接通过环境变量加载配置通过entrypoint脚本来预处理变量 存储卷 configMap 配置中心 pod从configMap