• (2022版)一套教程搞定k8s安装到实战 | Secret


    视频来源:B站《(2022版)最新、最全、最详细的KubernetesK8s)教程,从K8s安装到实战一套搞定》

    一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持!

    附上汇总贴:(2022版)一套教程搞定k8s安装到实战 | 汇总_COCOgsta的博客-CSDN博客


    Secret用来保存敏感信息的,比如密码、令牌或者key、Redis、MySQL密码

    Secret介绍地址:kubernetes.io/docs/concep…

    $ * \ 特殊字符单引号无需转义

    ImagePullSecret:Pod拉取私有镜像仓库时使用的账号密码,里面的帐号信息,会传递给kubelet,然后kubelet就可以拉去有密码的仓库里面的镜像。

    创建一个docker registry的secret

    1. [root@k8s-master-lb ~]# kubectl create secret docker-registry docker-secret2 --docker-server=hub.docker.com --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
    2. secret/docker-secret2 created
    3. 复制代码

    test-env-pod.yaml

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: dapi-test-pod
    5. spec:
    6. nodeName: k8s-node01
    7. imagePullSecrets:
    8. - name: docker-secret2
    9. containers:
    10. - name: test-container
    11. image: busybox:1.28
    12. imagePullPolicy: IfNotPresent
    13. command: [ "/bin/sh", "-c", "sleep 3600" ]
    14. volumeMounts:
    15. - name: config-volume
    16. mountPath: /mnt
    17. envFrom:
    18. - configMapRef:
    19. name: special-config
    20. env:
    21. # Define the environment variable
    22. # - name: SPECIAL_LEVEL_KEY
    23. # valueFrom:
    24. # configMapKeyRef:
    25. # # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
    26. # name: special-config
    27. # # Specify the key associated with the value
    28. # key: special.how
    29. - name: test
    30. value: test-value
    31. - name: mysqlHostAddress
    32. value: 10.10.10.10
    33. - name: mysqlPort
    34. value: "3306" # only string
    35. restartPolicy: Never
    36. volumes:
    37. - name: config-volume
    38. configMap:
    39. name: special-config
    40. 复制代码

    subPath解决目录覆盖的问题

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: dapi-test-pod
    5. spec:
    6. nodeName: k8s-node01
    7. imagePullSecrets:
    8. - name: docker-secret2
    9. containers:
    10. - name: test-container
    11. image: busybox:1.28
    12. imagePullPolicy: IfNotPresent
    13. command: [ "/bin/sh", "-c", "sleep 3600" ]
    14. volumeMounts:
    15. - mountPath: /etc/nginx/nginx.conf
    16. name: config-volume
    17. subPath: etc/nginx/nginx.conf
    18. envFrom:
    19. - configMapRef:
    20. name: special-config
    21. env:
    22. # Define the environment variable
    23. # - name: SPECIAL_LEVEL_KEY
    24. # valueFrom:
    25. # configMapKeyRef:
    26. # # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
    27. # name: special-config
    28. # # Specify the key associated with the value
    29. # key: special.how
    30. - name: test
    31. value: test-value
    32. - name: mysqlHostAddress
    33. value: 10.10.10.10
    34. - name: mysqlPort
    35. value: "3306" # only string
    36. restartPolicy: Never
    37. volumes:
    38. - configMap:
    39. defaultMode: 420
    40. items:
    41. - key: nginx.conf
    42. path: etc/nginx/nginx.conf
    43. name: nginx-conf
    44. name: config-volume
    45. 复制代码

    ConfigMap和Secret如果是以subPath的形式挂载的,那么Pod是不会感知到ConfigMap和Secret的更新的。

    如果Pod的变量来自于ConfigMap和Secret中定义的内容,那么ConfigMap和Secret更新后,也不会更新Pod中的变量。

    解决办法

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: dapi-test-pod
    5. spec:
    6. nodeName: k8s-node01
    7. imagePullSecrets:
    8. - name: docker-secret2
    9. containers:
    10. - name: test-container
    11. image: busybox:1.28
    12. imagePullPolicy: IfNotPresent
    13. command: [ "/bin/sh", "-c", "sleep 3600" ]
    14. volumeMounts:
    15. - mountPath: /etc/nginx/nginx.conf
    16. name: config-volume
    17. subPath: etc/nginx/nginx.conf
    18. - mountPath: /mnt/
    19. name: config-volume-non-subpath
    20. envFrom:
    21. - configMapRef:
    22. name: special-config
    23. env:
    24. # Define the environment variable
    25. # - name: SPECIAL_LEVEL_KEY
    26. # valueFrom:
    27. # configMapKeyRef:
    28. # # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
    29. # name: special-config
    30. # # Specify the key associated with the value
    31. # key: special.how
    32. - name: test
    33. value: test-value
    34. - name: mysqlHostAddress
    35. value: 10.10.10.10
    36. - name: mysqlPort
    37. value: "3306" # only string
    38. restartPolicy: Never
    39. volumes:
    40. - configMap:
    41. defaultMode: 420
    42. items:
    43. - key: nginx.conf
    44. path: etc/nginx/nginx.conf
    45. name: nginx-conf
    46. name: config-volume
    47. - configMap:
    48. defaultMode: 420
    49. name: nginx-conf
    50. name: config-volume-non-subpath
    51. 复制代码

    postStart:容器启动之前执行的命令

    preStop:容器停止之前执行的命令

    热更新ConfigMap或Secret:

    1. kubectl create cm nginx-conf --from-file=nginx.conf --dry-run -oyaml | kubectl replace -f-
    2. 复制代码

    immutable:在ConfigMap和Secret的最后加上如下内容,则不再可以edit该ConfigMap或Secret

  • 相关阅读:
    Jmeter入门
    [附源码]计算机毕业设计JAVAjsp网上蛋糕订购系统
    C++ 智能指针最佳实践&源码分析
    uboot 顶层Makefile-make xxx_deconfig过程说明三
    【C++11保姆级教程】列表初始化(Literal types)和委派构造函数(delegating))
    初识ServletConfig
    树莓派——5、Ubuntu18-04虚拟机搭建VMware版本
    Microsoft SQL Server manual
    Vue.js vs React:哪一个更适合你的项目?
    麒麟信安组织开展国产操作系统技术赋能专题培训
  • 原文地址:https://blog.csdn.net/guolianggsta/article/details/126459635