configmap&secrets基本操作

一,configmap简介:

ConfigMap是一种API对象,用来将非加密数据保存到键值对中,如etcd中。

可以用作环境变量、命令行参数或者存储卷中的配置文件。

ConfigMap可以将 环境变量 配置信息和容器镜像解耦,便于应用配置的修改。

如果需要存储加密信息时可以使用Secret对象。

1,configmap的创建:

[root@k8s-master configmap]# kubectl create configmap --help
查看帮助如何创建 configmap常用两种创建方式:

(1.1)第一种:基于文件的方式进行创建,–from-file:指明文件名称,名称为键,文件内容为值。也可以指明多个文件。

语法: kubectl create configmap my-config --from-file=path/to/bar

示例:基于向nginx注入配置文件。

[root@k8s-master configmap]# cat www.conf    创建一个nginx配置文件。
server {
       server_name myapp.shuo.com;
       listen 80;
       root /data/web/html/;
}
[root@k8s-master configmap]# kubectl create configmap nginx-www --from-file=www.conf 创建一个configmap名称为nginx-www,键为www.conf,值为文件内容。
configmap/nginx-www created

[root@k8s-master configmap]# kubectl get cm    查看configmap
NAME               DATA   AGE
kube-root-ca.crt   1      43d
nginx-www          1      10s
[root@k8s-master configmap]# 
[root@k8s-master configmap]# kubectl describe cm nginx-www       查看创建的configmap详细信息。
Name:         nginx-www
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
www.conf:----
server {
       server_name myapp.shuo.com;
       listen 80;                       
       root /data/web/html/;
}


Events:  <none>

创建一个pod,使用configmap类型挂载卷
[root@k8s-master configmap]# vim pod-configmap-3.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-3
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    mingyuanyun/create: "yanss"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxconf
      mountPath: /etc/nginx/conf.d/ 挂载到这个nginx资源路径下,为nginx提供资源。
      readOnly: true      不允许pod容器对配置文件修改
  volumes: 
  - name: nginxconf
    configMap:
      name: nginx-www

[root@k8s-master configmap]# kubectl apply -f pod-configmap-3.yaml
pod/pod-cm-3 created
[root@k8s-master configmap]# kubectl get pods -o wide
NAME          READY   STATUS    RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
pod-cm-3      1/1     Running   0          9m47s   10.244.1.11   k8s-node1   <none>           <none>

[root@k8s-master configmap]# kubectl exec -it pod-cm-3 -- /bin/sh
/ # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # ls           可以看到我们创建的confimap,nginx-www中的键。
www.conf
/etc/nginx/conf.d # cat www.conf 
server {
       server_name myapp.shuo.com;
       listen 80;
       root /data/web/html/;
}

/etc/nginx/conf.d # 
/etc/nginx/conf.d # nginx -T              查看nginx加载的配置文件。
# configuration file /etc/nginx/conf.d/www.conf:
server {
       server_name myapp.shuo.com;
       listen 80;
       root /data/web/html/;
}


/etc/nginx/conf.d # mkdir -p /data/web/html                   构建nginx资源访问路径。
/etc/nginx/conf.d # vi /data/web/html/index.html
/etc/nginx/conf.d # 
/etc/nginx/conf.d # cat /data/web/html/index.html 
weclome nginx

验证测试: node1上访问,如果想要通过域名访问,那么需要修改node1上hosts文件
[root@k8s-node1 ~]# curl 10.244.1.11
weclome nginx

修改configmap  nginx-www中监听的端口号,看看nginx配置文件会不会生效
因为我们通过存储卷挂载的方式进行同步,时间有点慢。
[root@k8s-master configmap]# kubectl edit cm nginx-www      修改configmap文件中监听端口为8080
configmap/nginx-www edited
[root@k8s-master configmap]# kubectl exec -it pod-cm-3 -- /bin/sh
/data/web/html # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # ls
www.conf
/etc/nginx/conf.d # cat www.conf 
server {
       server_name myapp.shuo.com;
       listen 8080;
       root /data/web/html/;
}

(1.2)基于键值对进行创建configmap

语法:kubectl create configmap my-config --from-literal=key1=config1
–from-literal=key2=config2

示例:向pod中注入环境变量

[root@k8s-master ~]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=Yan.com.cn
configmap/nginx-config created
[root@k8s-master ~]# kubectl get cm
NAME               DATA   AGE
kube-root-ca.crt   1      43d
nginx-config       2      6s

[root@k8s-master ~]# kubectl describe cm nginx-config
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx_port:              定义的键
----
80                             键的值
server_name:
----
Yan.com.cn
Events:  <none>

[root@k8s-master configmap]# cat pod-configmap.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations: 
    mingyuanyun/create: "yanss"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    - name: https
      containerPort: 443
    env:
    - name: NGINX_SERVER_PORT              定义变量名称
      valueFrom:                值的来源
        configMapKeyRef:             
          name: nginx-config      configmap的名称
          key: nginx_port             configmap中的键
    - name: NGINX_SERVER_NAME
      valueFrom:
        configMapKeyRef:
          name: nginx-config
          key: server_name

[root@k8s-master configmap]# kubectl apply -f pod-configmap.yaml 
pod/pod-cm-1 created
[root@k8s-master configmap]# kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
pod-cm-1      1/1     Running   0          9s
[root@k8s-master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh     进入pod查看变量。
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=Yan.com.cn

2,通过存储卷的方式加载,这是键为文件名,内容为值。
[root@k8s-master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh     进入pod查看变量。
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=Yan.com.cn

[root@k8s-master configmap]# vim pod-configmap-2.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-2
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    mingyuanyun/create: "yanss"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxconf
      mountPath: /etc/nginx/config.d/
      readOnly: true              不允许pod容器对配置文件修改
  volumes:
  - name: nginxconf
    configMap:
      name: nginx-config
  
[root@k8s-master configmap]# kubectl apply -f pod-configmap-2.yaml 
pod/pod-cm-2 created
[root@k8s-master configmap]# kubectl get pods 
NAME          READY   STATUS    RESTARTS   AGE
pod-cm-2      1/1     Running   0          7s
[root@k8s-master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh
/ # ls
bin    etc    lib    mnt    root   sbin   sys    usr
dev    home   media  proc   run    srv    tmp    var
/ # cd /etc/nginx/conf
conf.d/    config.d/
/ # cd /etc/nginx/config.d/
/etc/nginx/config.d # ls
nginx_port   server_name
/etc/nginx/config.d # cat nginx_port 
/etc/nginx/config.d # cat nginx_port 
/etc/nginx/config.d # cat server_name 
Yan.com.cn/etc/nginx/config.d # 

secrets:不同于confimap在于加密存储数据,不是明文存储。

三中类型:
docker-registry 创建一个给 Docker registry 使用的 secret       这个类型是存储登录私有仓库需要认证的账号密码
generic         从本地 file, directory 或者 literal value 创建一个 secret,例如存储Mysql账号和密码。
tls             创建一个 TLS secret          这个类型是存储相关认证秘钥的secret。保存的是私钥和证书

[root@k8s-master configmap]# kubectl create secret generic mysql-root-password --from-literal=password=Myp@ss123   创建一个通用类型的secret,键为password,值为密码。
secret/mysql-root-password created
[root@k8s-master configmap]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-2rtnx   kubernetes.io/service-account-token   3      48d
mysql-root-password   Opaque                                1      14s

[root@k8s-master configmap]# kubectl describe secret mysql-root-password    查看值是加密的。
Name:         mysql-root-password
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password:  9 bytes
[root@k8s-master configmap]# 

[root@k8s-master configmap]# kubectl get secret mysql-root-password -o yaml
apiVersion: v1
data:
  password: TXlwQHNzMTIz
kind: Secret
metadata:
  creationTimestamp: "2022-07-26T08:57:25Z"
  name: mysql-root-password
  namespace: default
  resourceVersion: "5523531"
  uid: 24b481cc-55e5-423e-9026-7fd30fefd8fc
type: Opaque
[root@k8s-master configmap]# echo TXlwQHNzMTIz | base64 -d               通过base64解码可以查看密码
Myp@ss123[root@k8s-master configmap]# 

总结:configmap和secret基本操作完成,区别在于一个明文存储一个加密存储数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值