k8s使用volume卷实现pod数据持久化和资源共享

容器中的文件在磁盘上是临时存放的,当容器崩溃的时候,文件会丢失
kubelet会重新启动容器,容器会以干净的状态重启

和docker中的挂载到本地磁盘的目录类似

192.168.10.106=n1

192.168.10.107=n2

192.168.10.108=m1

我们把存储的地方放到n1的节点

在n1节点创建目录和yml文件

mkdir /opt/prometheus
cd /opt/prometheus

cat > prometheus.yml <<-'EOF'
#测试挂载文件
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

EOF

在主节点上面创建pod 并把n1节点的目录和文件都挂载到容器里面

volumeMounts 是pod容器里面的路径

volumes 是n1本地节点的路径

volumeMounts 和volumes 的name要一致

hostPort 表示本地192.168.10.106在浏览器访问的端口

nodeName 是n1的节点名字  你要改成你自己的从节点的名字

cat > pod-prometheus.yaml <<-'EOF'
apiVersion: v1 
kind: Pod 
metadata: 
  name: pod-prometheus-1
spec: 
  nodeName: n1
  containers: 
  - name: p1
    image: prom/prometheus 
    ports: 
    - containerPort: 9090
      hostPort: 8000
    volumeMounts:
    - name: vol-data
      mountPath: /etc/prometheus
  volumes:
  - name: vol-data
    hostPath:
      path: /opt/prometheus
EOF

执行

kubectl apply -f pod-prometheus.yaml 

查看pod信息

 kubectl get pods -o wide

可以看到节点已经部署在了n1的节点 

进入容器查看文件是否挂载上 

kubectl exec --stdin --tty pod-prometheus-1 -- sh

可以看到n1节点的文件 已经挂载到了容器里面,这样数据就不会丢失了 

我们在浏览器使用n1的ip访问hostPort 的端口  

http://192.168.10.106:8000/

也可以在linux内使用 虚拟ip 访问containerPort 容器的端口

curl 172.16.10.247:9090

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值