zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

k8s安装教程 实战 2022 搞定 一套
2023-09-14 09:09:08 时间

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

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

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


一般用ConfigMap去管理一些配置文件,或者一些大量的环境变量信息。

ConfigMap将配置和Pod分开,有一个nginx,nginx.conf -> configmap。更易于配置文件的更改和管理。

Secret:Secret更倾向于存储和共享敏感、加密的配置信息。

ConfigMap中文地址:kubernetes.io/zh/docs/tas…

生成special-config的configmap

kubectl create configmap special-config --from-literal=special.how=very
复制代码

test-env-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  nodeName: k8s-node01
  containers:
    - name: test-container
      image: busybox:1.28
      imagePullPolicy: IfNotPresent
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
      - configMapRef:
          name: special-config
      env:
        # Define the environment variable
        # - name: SPECIAL_LEVEL_KEY
        #   valueFrom:
        #     configMapKeyRef:
        #       # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
        #       name: special-config
        #       # Specify the key associated with the value
        #       key: special.how
        - name: test
          value: test-value
        - name: mysqlHostAddress
          value: 10.10.10.10
        - name: mysqlPort
          value: "3306" # only string
  restartPolicy: Never
复制代码
[root@k8s-master-lb ~]# kubectl apply -f test-env-pod.yaml 
pod/dapi-test-pod created
[root@k8s-master-lb ~]# kubectl get pods
NAME                     READY   STATUS      RESTARTS   AGE
busybox                  1/1     Running     31         15d
dapi-test-pod            0/1     Completed   0          4s
nginx-66bbc9fdc5-mbqkf   1/1     Running     4          16d
nginx-66bbc9fdc5-x7vkl   1/1     Running     2          16d
web-0                    1/1     Running     2          15d
web-1                    1/1     Running     4          15d
web-2                    1/1     Running     4          15d
[root@k8s-master-lb ~]# kubectl logs -f dapi-test-pod
mysqlPort=3306
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
mysqlHostAddress=10.10.10.10
test=test-value
HOSTNAME=dapi-test-pod
SHLVL=1
HOME=/root
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
special.how=very
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
[root@k8s-master-lb ~]# 
复制代码

把ConfigMap挂载到容器中

[root@k8s-master-lb ~]# cat test-env-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  nodeName: k8s-node01
  containers:
    - name: test-container
      image: busybox:1.28
      imagePullPolicy: IfNotPresent
      command: [ "/bin/sh", "-c", "sleep 3600" ]
      volumeMounts:
      - name: config-volume
        mountPath: /mnt
      envFrom:
      - configMapRef:
          name: special-config
      env:
        # Define the environment variable
        # - name: SPECIAL_LEVEL_KEY
        #   valueFrom:
        #     configMapKeyRef:
        #       # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
        #       name: special-config
        #       # Specify the key associated with the value
        #       key: special.how
        - name: test
          value: test-value
        - name: mysqlHostAddress
          value: 10.10.10.10
        - name: mysqlPort
          value: "3306" # only string
  restartPolicy: Never
  volumes:
    - name: config-volume
      configMap:
        name: special-config
[root@k8s-master-lb ~]#