Kubernetes精讲之存储

目录

一 configmap

1.1 configmap的功能

1.2 configmap的使用场景

1.3 configmap创建方式

1.3.1 字面值创建

1.3.2 通过文件创建

1.3.3 通过目录创建

1.3.4 通过yaml文件创建

1.3.5 configmap的使用方式

1.3.5.1 使用configmap填充环境变量

1.3.5.2 通过数据卷使用configmap

1.3.5.3 利用configMap填充pod的配置文件

1.3.5.4 通过热更新cm修改配置

二 secrets配置管理

2.1 secrets的功能介绍

2.2 secrets的创建

2.2.1从文件创建

编写yaml文件

2.3 Secret的使用方法

2.3.1 将Secret挂载到Volume中

2.3.2 向指定路径映射 secret 密钥

2.3.3 将Secret设置为环境变量

2.3.4 存储docker registry的认证信息

三 volumes配置管理

3.1 kubernets支持的卷的类型

3.2 emptyDir卷

3.3 hostpath卷

3.4 nfs卷

3.4.1 部署一台nfs共享主机并在所有k8s节点中安装nfs-utils

3.4.2 部署nfs卷

3.5 PersistentVolume持久卷

3.5.1 静态持久卷pv与静态持久卷声明pvc

PersistentVolume(持久卷,简称PV)

PersistentVolumeClaim(持久卷声明,简称PVC)

volumes访问模式

volumes回收策略

volumes状态说明

静态pv实例:

在pod中使用pvc

四 存储类storageclass

4.1 StorageClass说明

4.2 StorageClass的属性

4.3 存储分配器NFS Client Provisioner

4.4 部署NFS Client Provisioner

4.4.1 创建sa并授权

4.4.2 部署应用

4.4.3 创建存储类

4.4.4 创建pvc

4.4.5 创建测试pod

4.4.6 设置默认存储类

五 statefulset控制器

5.1 功能特性

5.2 StatefulSet的组成部分

5.3 构建方法

5.4 测试:

5.5 statefulset的弹缩


一 configmap

1.1 configmap的功能

  • configMap用于保存配置数据,以键值对形式存储。
  • configMap 资源提供了向 Pod 注入配置数据的方法。
  • 镜像和配置文件解耦,以便实现镜像的可移植性和可复用性。
  • etcd限制了文件大小不能超过1M

1.2 configmap的使用场景

  • 填充环境变量的值
  • 设置容器内的命令行参数
  • 填充卷的配置文件

1.3 configmap创建方式

1.3.1 字面值创建

[root@K8s-master ~]# kubectl create configmap userlist --from-literal  fname=haha --from-literal lname=redhat
configmap/userlist created

[root@K8s-master ~]# kubectl describe configmaps userlist 
Name:         userlist
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data                  #键值信息显示
====
fname:
----
haha
lname:
----
redhat

BinaryData
====

Events:  <none>

1.3.2 通过文件创建

[root@K8s-master ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 114.114.114.114

[root@K8s-master ~]#  kubectl create cm haha --from-file /etc/resolv.conf
configmap/haha created

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

Data
====
resolv.conf:
----
# Generated by NetworkManager
nameserver 114.114.114.114


BinaryData
====

Events:  <none>

1.3.3 通过目录创建

[root@K8s-master ~]# mkdir haha
[root@K8s-master ~]# cp /etc/fstab /etc/rc.d/rc.local  haha/

[root@K8s-master ~]# kubectl create cm haha3 --from-file haha/
configmap/haha3 created
[root@K8s-master ~]# kubectl describe cm haha3
Name:         haha3
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
fstab:
----

#
# /etc/fstab
# Created by anaconda on Tue Jul 30 14:59:55 2024
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root   /                       xfs     defaults        0 0
UUID=a7e1b327-b16e-4720-a287-3cd97c041077 /boot                   xfs     defaults        0 0
#/dev/mapper/rhel-swap   none                    swap    defaults        0 0

rc.local:
----
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
mount /dev/sr0 /rhel9


BinaryData
====

Events:  <none>

1.3.4 通过yaml文件创建

[root@K8s-master ~]# kubectl create cm haha4 --from-literal db_host=172.25.254.100 --from-literal db_port=3306 --dry-run=client -o yaml > haha.yaml

[root@K8s-master ~]# vim haha.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: haha4
data:
  db_host: 172.25.254.100
  db_port: "3306"

[root@K8s-master ~]# kubectl apply -f haha.yaml 
configmap/haha4 created
[root@K8s-master ~]# kubectl describe cm haha4
Name:         haha4
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
db_host:
----
172.25.254.100
db_port:
----
3306

BinaryData
====

Events:  <none>

1.3.5 configmap的使用方式

  • 通过环境变量的方式直接传递给pod
  • 通过pod的 命令行运行方式
  • 作为volume的方式挂载到pod内
1.3.5.1 使用configmap填充环境变量

#讲cm中的内容映射为指定变量

#讲cm中的内容映射为指定变量
[root@K8s-master ~]# vim testpod1.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: testpod1
  name: testpod1
spec:
  containers:
  - image: busyboxplus:latest
    name: testpod1
    command:
    - /bin/sh
    - -c
    - env
    env:
    - name: key1
      valueFrom:
        configMapKeyRef:
          name: haha4
          key: db_host
    - name: key2
      valueFrom:
        configMapKeyRef:
          name: haha4
          key: db_port
  restartPolicy: Never

[root@K8s-master ~]# kubectl apply -f testpod1.yml
pod/testpod created

#查看日志
[root@K8s-master ~]# kubectl logs pods/testpod1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
MYAPP_V1_SERVICE_HOST=10.102.142.86
HOSTNAME=testpod1
SHLVL=1
MYAPP_V2_SERVICE_HOST=10.99.120.99
HOME=/
MYAPP_V1_PORT=tcp://10.102.142.86:80
MYAPP_V1_SERVICE_PORT=80
MYAPP_V2_PORT=tcp://10.99.120.99:80
MYAPP_V2_SERVICE_PORT=80
MYAPP_V1_PORT_80_TCP_ADDR=10.102.142.86
MYAPP_V2_PORT_80_TCP_ADDR=10.99.120.99
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
MYAPP_V1_PORT_80_TCP_PORT=80
MYAPP_V1_PORT_80_TCP_PROTO=tcp
MYAPP_V2_PORT_80_TCP_PORT=80
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
MYAPP_V2_PORT_80_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PROTO=tcp
key1=172.25.254.100
key2=3306
MYAPP_V1_PORT_80_TCP=tcp://10.102.142.86:80
MYAPP_V2_PORT_80_TCP=tcp://10.99.120.99:80
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1

#把cm中的值直接映射为变量

[root@K8s-master ~]# vim testpod2.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: testpod2
  name: testpod2
spec:
  containers:
  - image: busyboxplus:latest
    name: testpod2
    command:
    - /bin/sh
    - -c
    - env
    envFrom:
    - configMapRef:
        name: haha4
  restartPolicy: Never

#查看日志
[root@K8s-master ~]# kubectl apply -f testpod2.yml 
pod/testpod2 created

[root@K8s-master ~]# kubectl logs pods/testpod2
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
MYAPP_V1_SERVICE_HOST=10.102.142.86
HOSTNAME=testpod2
SHLVL=1
MYAPP_V2_SERVICE_HOST=10.99.120.99
HOME=/
db_port=3306
MYAPP_V1_SERVICE_PORT=80
MYAPP_V1_PORT=tcp://10.102.142.86:80
MYAPP_V2_SERVICE_PORT=80
MYAPP_V2_PORT=tcp://10.99.120.99:80
MYAPP_V1_PORT_80_TCP_ADDR=10.102.142.86
MYAPP_V2_PORT_80_TCP_ADDR=10.99.120.99
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
MYAPP_V1_PORT_80_TCP_PORT=80
MYAPP_V2_PORT_80_TCP_PORT=80
MYAPP_V1_PORT_80_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
MYAPP_V2_PORT_80_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PROTO=tcp
MYAPP_V1_PORT_80_TCP=tcp://10.102.142.86:80
MYAPP_V2_PORT_80_TCP=tcp://10.99.120.99:80
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
db_host=172.25.254.100

#把cm中的值直接映射为变量

[root@K8s-master ~]# vim testpod3.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: testpod3
  name: testpod3
spec:
  containers:
  - image: busyboxplus:latest
    name: testpod3
    command:
    - /bin/sh
    - -c
    - echo ${db_host} ${db_port}        
    envFrom:
    - configMapRef:
        name: haha4
  restartPolicy: Never


[root@K8s-master ~]# kubectl apply -f  testpod3.yml 
pod/testpod3 created

#查看日志
[root@K8s-master ~]# kubectl logs pods/testpod3
172.25.254.100 3306

1.3.5.2 通过数据卷使用configmap
[root@k8s-master ~]# vim testpod4.yml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: testpod4
  name: testpod4
spec:
  containers:
  - image: busyboxplus:latest
    name: testpod4
    command:
    - /bin/sh
    - -c
    - cat /config/db_host
    volumeMounts:                   #调用卷策略
    - name: config-volume           #卷名称
      mountPath: /config
  volumes:                          #声明卷的配置
  - name: config-volume             #卷名称
    configMap:
      name: haha4
  restartPolicy: Never

#确保haha4在使用
[root@K8s-master ~]# kubectl apply -f haha.yaml
[root@K8s-master ~]# cat haha.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: haha4
data:
  db_host: 172.25.254.100
  db_port: "3306"


#启动
[root@K8s-master ~]# kubectl apply -f testpod4.yml 

#查看日志
[root@K8s-master ~]# kubectl logs pods/testpod4 
172.25.254.100

1.3.5.3 利用configMap填充pod的配置文件
#建立配置文件模板
[root@k8s-master ~]# vim nginx.conf
server {
  listen 8000;
  server_name _;
  root /usr/share/nginx/html;
  index index.html;
}

#利用模板生成cm
[root@K8s-master ~]# kubectl create cm nginx-conf --from-file nginx.conf
configmap/nginx-conf created

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

Data
====
nginx.conf:
----
server {
  listen 8000;
  server_name _;
  root /usr/share/nginx/html;
  index index.html;
}


BinaryData
====

Events:  <none>

#建立nginx控制器文件
[root@K8s-master ~]# kubectl create deployment nginx --image nginx:latest --replicas 1 --dry-run=client -o yaml > nginx.yml

#设定nginx.yml中的卷
[root@K8s-master ~]# vim nginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        name: nginx
        volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx/conf.d

      volumes:
        - name: config-volume
          configMap:
            name: nginx-conf

[root@K8s-master ~]# kubectl apply -f nginx.yml 
deployment.apps/nginx created
[root@K8s-master ~]# kubectl get pods -o wide
NAME                        READY   STATUS      RESTARTS   AGE     IP            NODE        NOMINATED NODE   READINESS GATES
haha-8545d569f4-f5c79       1/1     Running     0          2d      10.244.2.60   k8s-node2   <none>           <none>
myapp-v1-7479d6c54d-5f742   1/1     Running     0          5h24m   10.244.1.78   k8s-node1   <none>           <none>
myapp-v2-7cd6d597d-zb6bt    1/1     Running     0          5h24m   10.244.1.79   k8s-node1   <none>           <none>
nginx-8487c65cfc-x77wf      1/1     Running     0          3s      10.244.1.88   k8s-node1   <none>           <none>
testpod4                    0/1     Completed   0          6m14s   10.244.1.87   k8s-node1   <none>           <none>

#测试
[root@K8s-master ~]# curl 10.244.1.88:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

1.3.5.4 通过热更新cm修改配置
[root@K8s-master ~]# kubectl e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值