k8s配置管理

Secret

作用:加密数据存在etcd中,让Pod容器以挂载Volume方式进行访问。
场景:凭证
创建secret加密数据:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Qpaque
data:
  username: ssvdve
  password: wsfefgeg
kubectl create -f secret.yaml
kubectl get secret

以变量形式挂载到Pod容器中:

apiVersion: v1
kind: Pod
metadata:
  name: myPod
spec:
  containers:
  - name: nginx
    image: nginx
    env: 
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mySecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mySecret
            key: password
kubectl apply -f secret-val.yaml
kubectl exec -it myPod bash
echo $SECRET_USERNAME

以Volume形式挂载Pod容器中

apiVersion: v1
kind: Pod
metadata: 
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
      readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret
kubectl apply -f secret-val.yaml
kubectl exec -it mypod bash
ls /etc/foo
cat username

ConfigMap

作用:存储不加密数据到etcd中,让Pod以变量或者Volume挂载到容器中。
场景:配置文件

创建配置文件:

redis.host=127.0.0.1
redis.port=6379
redis.password=123456
kubectl create configmap redis-config --from-file=redis.properties
kubectl get cm
kubectl describe cm redis-config

以Volume形式挂载Pod容器中

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: busybox
      image: busybox
      command: ["/bin/sh","-c","cat /etc/config/redis.properties"]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: redis-config
  restartPolicy: Never
kubectl apply -f cm.yaml
kubectl logs mypod

以变量形式挂载到Pod容器中

apiVersion: v1
kind: ConfigMap
metadata: 
  name: myconfig
  namespace: default
data:
  special.level: info
  special.type: hello
kubectl apply -f myconfig.yaml
kubectl get cm
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec: 
  containers:
    - name: busybox
      image: busybox
      command: ["/bin/sh","-c","echo $(LEVEL) $(TYPE)"]
      env:
        - name: LEVEL
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.level
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              name: myconfig
              key: special.type
restartPolicy: Never
kubectl apply -f config-var.yaml
kubectl get pods
kubectl logs mypod
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值