k8s namespace/volume

https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/

只挑个人感觉使用较多/比较重要的点来说

namespace

Create a namespace so that the resources you create in this exercise are isolated from the rest of your cluster.

和Linux的namespace是类似的,做一个隔离的作用;可以将pod的资源和集群其他资源做隔离

可以使用下面方式创建namespace

创建namespace

kubectl create namespace mem-example

在pod的yaml文件里,可以配置memory和cpu的资源闲置

limits代表上限 requests代表下限

pods/resource/memory-request-limit.yaml

apiVersion: v1
kind: Pod
metadata:
name: memory-demo
namespace: mem-example
spec:
containers:
- name: memory-demo-ctr
image: polinux/stress
resources:
limits:
memory: "200Mi"
requests:
memory: "100Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]

可以创建pod

kubectl create -f a.yaml --namespace=mem-example

启动后,可以使用下面方式查看是否启动

kubectl get pod memory-demo --namespace=mem-example
# 使用yaml格式查看详细信息
kubectl get pod memory-demo --output=yaml --namespace=mem-example

删除pod

kubectl delete pod memory-demo --namespace=mem-example

还存在设置的资源限制大于Node资源的情况,以及资源超出限制的情况,具体参照官网

另外,对CPU的限制设置方式和memory相似,不作赘述

volume

可以创建一个将 /data/redis 目录挂载到 emptyDir 的pod作为实例;emptyDir是一个伴随着pod创建而建立的一个目录,即使pod重启也不会影响其数据,但当pod被delete之后,其内容就消失了。

实例过程如下:

根据下列配置文件创建一个pod

pods/storage/redis.yaml

apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
volumeMounts:
- name: redis-storage
mountPath: /data/redis
volumes:
- name: redis-storage
emptyDir: {}

启动pod

kubectl create -f redis.yaml

使用下列命令监控pod的变化

kubectl get pod --watch

进入pod,在 /data/redis 目录下创建一个文件

kubect exec -it redis -- /bin/bash
cd /data/redis
echo Hi > hel.txt

重启容器,再此进入之前的目录,会发现文件还在,内容没变

重启的方式可以是在pod中kill掉对应的redis服务的进程

通过 --watch 命令可以检测到对应pod的状态的变化

当然,也可以挂载到对应主机目录上,配置文件如下

apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory

注意:

单机部署时可以使用hostpath,但是集群的话不应该再使用hostpath

posted @ 2019-04-19 16:29  wswang  阅读(1229)  评论(0)    收藏  举报