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