# K8s 配置管理 Configmap
# 1. Configmap
# 1. 1 基于 from-env-file 创建 Configmap
| |
| podname=nf-flms-system |
| podip=192.168.1.100 |
| env=prod |
| nacosaddr=nacos.svc.cluster.local |
| |
| |
# 1.2 基于 from-literal 创建 Configmap
# 1.3 基于 from-file 创建 Configmap
| |
| server { |
| listen 80; |
| server_name s.hmallleasing.com; |
| client_max_body_size 1G; |
| location / { |
| proxy_pass http://192.168.1.134; |
| proxy_set_header Host $http_host; |
| proxy_set_header X-Real-IP $remote_addr; |
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| |
| proxy_connect_timeout 30; |
| proxy_send_timeout 60; |
| proxy_read_timeout 60; |
| |
| proxy_buffering on; |
| proxy_buffer_size 32k; |
| proxy_buffers 4 128k; |
| proxy_temp_file_write_size 10240k; |
| proxy_max_temp_file_size 10240k; |
| } |
| } |
| |
| server { |
| listen 80; |
| server_name s.hmallleasing.com; |
| return 302 https://$server_name$request_uri; |
| } |
| |
| |
# 1.4 Deployment 挂载 configmap 示例
| [root@k8s-master01 cm] |
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| labels: |
| app: nginx-deploy |
| name: nginx-deploy |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: nginx-deploy |
| template: |
| metadata: |
| labels: |
| app: nginx-deploy |
| spec: |
| imagePullSecrets: |
| - name: harboradmin |
| containers: |
| - image: nginx |
| name: nginx |
| ports: |
| - name: http |
| containerPort: 80 |
| protocol: TCP |
| envFrom: |
| - configMapRef: |
| name: cmenv |
| env: |
| - name: MYSQL_ADDR |
| value: "192.168.40.150" |
| - name: MYSQL_PASSWD |
| value: Superman*2022 |
| - name: LOG_LEVEL |
| valueFrom: |
| configMapKeyRef: |
| name: cmliteral |
| key: level |
| volumeMounts: |
| - name: nginx-config |
| mountPath: "/etc/nginx/conf.d" |
| readOnly: true |
| volumes: |
| - name: nginx-config |
| configMap: |
| name: nginxconfig |
# 1.5 重命名挂载的 configmaq key 的名称
| [root@k8s-master01 cm] |
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| labels: |
| app: nginx-deploy |
| name: nginx-deploy |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: nginx-deploy |
| template: |
| metadata: |
| labels: |
| app: nginx-deploy |
| spec: |
| imagePullSecrets: |
| - name: harboradmin |
| containers: |
| - image: nginx |
| name: nginx |
| ports: |
| - name: http |
| containerPort: 80 |
| protocol: TCP |
| envFrom: |
| - configMapRef: |
| name: cmenv |
| env: |
| - name: MYSQL_ADDR |
| value: "192.168.40.150" |
| - name: MYSQL_PASSWD |
| value: Superman*2022 |
| - name: LOG_LEVEL |
| valueFrom: |
| configMapKeyRef: |
| name: cmliteral |
| key: level |
| volumeMounts: |
| - name: nginx-config |
| mountPath: "/etc/nginx/conf.d" |
| readOnly: true |
| volumes: |
| - name: nginx-config |
| configMap: |
| name: nginxconfig |
| items: |
| - key: s.hmallleasing.com.conf |
| path: nginx.conf |
| |
| |
| [root@k8s-master01 cm] |
| NAME READY STATUS RESTARTS AGE |
| nginx-deploy-bc476bc56-flln4 1/1 Running 0 10h |
| nginx-deploy-bc476bc56-jhsh6 1/1 Running 0 10h |
| nginx-deploy-bc476bc56-splv9 1/1 Running 0 10h |
| [root@k8s-master01 cm] |
| root@nginx-deploy-bc476bc56-flln4:/ |
| nginx.conf |
# 1.6 修改挂载的 configmaq 权限
| [root@k8s-master01 cm] |
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| labels: |
| app: nginx-deploy |
| name: nginx-deploy |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: nginx-deploy |
| template: |
| metadata: |
| labels: |
| app: nginx-deploy |
| spec: |
| imagePullSecrets: |
| - name: harboradmin |
| containers: |
| - image: nginx |
| name: nginx |
| ports: |
| - name: http |
| containerPort: 80 |
| protocol: TCP |
| envFrom: |
| - configMapRef: |
| name: cmenv |
| env: |
| - name: MYSQL_ADDR |
| value: "192.168.40.150" |
| - name: MYSQL_PASSWD |
| value: Superman*2022 |
| - name: LOG_LEVEL |
| valueFrom: |
| configMapKeyRef: |
| name: cmliteral |
| key: level |
| volumeMounts: |
| - name: nginx-config |
| mountPath: "/etc/nginx/conf.d" |
| readOnly: true |
| volumes: |
| - name: nginx-config |
| configMap: |
| name: nginxconfig |
| items: |
| - key: s.hmallleasing.com.conf |
| path: nginx.conf |
| mode: 0644 |
| defaultMode: 0666 |
| |
| |
| root@nginx-deploy-7657fbffc7-k75l5:/ |
| lrwxrwxrwx 1 root root 17 Apr 16 13:37 /etc/nginx/conf.d/nginx.conf -> ..data/nginx.conf |
| root@nginx-deploy-7657fbffc7-k75l5:/ |
| -rw-rw-rw- 1 root root 722 Apr 16 13:37 /etc/nginx/conf.d/..data/nginx.conf |
# 1.7 subpath 解决挂载覆盖问题
| |
| [root@k8s-master01 cm] |
| |
| user nginx; |
| worker_processes 1; |
| |
| error_log /var/log/nginx/error.log warn; |
| pid /var/run/nginx.pid; |
| |
| |
| events { |
| worker_connections 512; |
| } |
| |
| |
| 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; |
| |
| |
| keepalive_timeout 65; |
| |
| |
| |
| include /etc/nginx/conf.d/*.conf; |
| } |
| |
| [root@k8s-master01 cm] |
| |
| |
| [root@k8s-master01 study] |
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| labels: |
| app: nginx-deploy |
| name: nginx-deploy |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: nginx-deploy |
| template: |
| metadata: |
| labels: |
| app: nginx-deploy |
| spec: |
| imagePullSecrets: |
| - name: harboradmin |
| containers: |
| - image: nginx |
| name: nginx |
| ports: |
| - name: http |
| containerPort: 80 |
| protocol: TCP |
| envFrom: |
| - configMapRef: |
| name: cmenv |
| env: |
| - name: MYSQL_ADDR |
| value: "192.168.40.150" |
| - name: MYSQL_PASSWD |
| value: Superman*2022 |
| - name: LOG_LEVEL |
| valueFrom: |
| configMapKeyRef: |
| name: cmliteral |
| key: level |
| volumeMounts: |
| - name: config |
| mountPath: "/etc/nginx/nginx.conf" |
| subPath: nginx.conf |
| volumes: |
| - name: config |
| configMap: |
| name: nginx-config |
# 2. Secret
# 2.1 Secret 拉取私有仓库镜像
| |
| --docker-server=s.hmallleasing.com \ |
| --docker-username=admin \ |
| --docker-password=Superman*2023 |
# 2.2 创建 ssl Secret
# 2.3 基于命令创建 generic Secret
| |
| |
| username=xuyong |
| passwd=Superman*2023 |
| |
| |
| |
| |
| kubectl create secret generic db-user-pass \ |
| --from-literal=username=admin \ |
| --from-literal=password='S!B\*d$zDsb=' |
# 2.4 Secret 加密、解密
| 1.加密 |
| |
| U3VwZXJtYW4qMjAyMw== |
| |
| 2.解密 |
| |
# 2.5 基于文件创建非加密 generic Secret
| |
| apiVersion: v1 |
| data: |
| passwd: U3VwZXJtYW4qMjAyMw== |
| username: eHV5b25n |
| kind: Secret |
| metadata: |
| name: dbconf |
| namespace: default |
| type: Opaque |
# 2. 6 基于 yaml 创建加密 generic Secret
| |
| apiVersion: v1 |
| kind: Secret |
| metadata: |
| name: mysql-secret |
| namespace: dev |
| stringData: |
| MYSQL_ROOT_PASSWORD: Superman*2023 |
| type: Opaque |
# 2.7 Deployment 挂载 Secret 示例
| [root@k8s-master01 study] |
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| labels: |
| app: nginx-deploy |
| name: nginx-deploy |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: nginx-deploy |
| template: |
| metadata: |
| labels: |
| app: nginx-deploy |
| spec: |
| imagePullSecrets: |
| - name: harboradmin |
| containers: |
| - image: nginx |
| name: nginx |
| ports: |
| - name: http |
| containerPort: 80 |
| protocol: TCP |
| - name: MYSQL_ROOT_PASSWORD |
| valueFrom: |
| secretKeyRef: |
| name: mysql-secret |
| key: MYSQL_ROOT_PASSWORD |
# 3. ConfigMap&Secret 热更新
本文出自于:https://edu.51cto.com/course/23845.html