Kubernetes中Pod详解——环境变量、端口和资源配额
1、环境变量:env还是老样子,用一个老的yaml文件先看看这个环境变量env的使用方法和格式apiVersion: v1kind: Podmetadata:name: pod-envnamespace: devlabels:user: Hud98spec:containers:- name: busyboximage: busybox:1.30command
1、环境变量:env
还是老样子,用一个老的yaml文件先看看这个环境变量env的使用方法和格式
apiVersion: v1
kind: Pod
metadata:
name: pod-env
namespace: dev
labels:
user: Hud98
spec:
containers:
- name: busybox
image: busybox:1.30
command: ["bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt; sleep 60; done;"]
env: #设置环境变量列表
- name: "username"
value: "Hud98"
- name: "password"
value: "199866"
验证一下,进入容器可以看看
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-env 1/1 Running 0 4h28m
[root@master ~]# kubectl exec pod-env -n dev -c busybox -it /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # echo $username
Hud98
/ # echo $password
199866
/ # exec attach failed: error on attach stdin: read escape sequence
command terminated with exit code 126
2、端口:ports
先来看一下ports支持的子选项:
[root@master ~]# kubectl explain pod.spec.containers.ports
KIND: Pod
VERSION: v1
RESOURCE: ports <[]Object>
FIELDS:
containerPort <integer> #容器要监听的端口(0<x<65536)
hostIP <string> #要将外部的端口绑定到主机的IP(一般省略)
hostPort <integer> #容器要在主机上公开的端口,如果设置,主机上只能运行容器的一个副本(一般省略)
name <string> #端口名称,若要指定,必须保证name在pod中是唯一的
protocol <string> #端口协议,必须是UDP、TCP或SCTP,默认为TCP
创建一个pod-ports.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-ports
namespace: dev
labels:
user: Hud98
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: Never
ports: # 设置容器暴露端口列表
- name: nginx-port
containerPort: 80
protocol: TCP
# 创建容器
[root@master ~]# vi pod-port.yaml
[root@master ~]# kubectl create -f pod-port.yaml
pod/pod-ports created
# 查看pod 在下面可以看到明显的配置信息
[root@master ~]# kubectl get pod pod-ports -n dev -o yaml
apiVersion: v1
kind: Pod
metadata:
user: Hud98
name: pod-ports
namespace: dev
spec:
containers:
- image: nginx
imagePullPolicy: Never
name: nginx
ports:
- containerPort: 80
name: nginx-port
protocol: TCP
state:
running:
startedAt: "2022-04-12T02:47:47Z"
hostIP: 192.168.88.102
phase: Running
podIP: 10.244.169.150
podIPs:
- ip: 10.244.169.150
qosClass: BestEffort
startTime: "2022-04-12T02:47:46Z"
访问容器中的程序,需要使用的是Pod的IP:containerPort
3、资源配额
容器中的程序要运行,可定是要占用一定的资源的,比如cpu和内存等等,如果我们不对某个容器的资源做限制,那么他就很可能吃掉大量的资源,从而导致其他的容器无法运行,针对这种情况,Kubernetes提供了对内存和cpu资源进行配额的机制,这种机制主要通过resources选项进行实现,他有两个子选项:
● limits:用于限制运行时容器的最大占用资源,当容器占用资源超过limits时会被终止,并进行重启
● requsets:用于设置容器运行需要的最小资源,如果环境资源不够用,容器将无法启动
可以通过以上的两个选项设置资源的上下限
接下来再编写一个测试案例,创建一个pod-resources.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-resources
namespace: dev
labels:
user: Hud98
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
resources: # 资源配额
limits: # 限制资源(上限)
cpu: "2" # CPU限制,单位是Core数
memory: "10Gi" #内存限制
requests: #请求资源(下限)
cpu: "1" # CPU限制,单位是Core数
memory: "20Mi" #内存限制
[root@master ~]# vi pod-resources.yaml
[root@master ~]# kubectl create -f pod-resources.yaml
pod/pod-resourcess created
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-resourcess 1/1 Running 0 7s
这里看到pod很顺利的启动了,下面我们再看看如果我把request内存下限写成10G,他还能不能启动成功:
apiVersion: v1
kind: Pod
metadata:
name: pod-resources
namespace: dev
labels:
user: Hud98
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
resources: # 资源配额
limits: # 限制资源(上限)
cpu: "2" # CPU限制,单位是Core数
memory: "10Gi" #内存限制
requests: #请求资源(下限)
cpu: "1" # CPU限制,单位是Core数
memory: "10Gi" #内存限制
[root@master ~]# vi pod-resources.yaml
[root@master ~]# kubectl create -f pod-resources.yaml
pod/pod-resources created
[root@master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-resources 0/1 Pending 0 20s
可以看到这里显示Pod没有成功的启动,我们再用describe来查看一下是什么原因
[root@master ~]# kubectl describe pod pod-resources -n dev
Warning FailedScheduling 76s (x31 over 33m) default-scheduler 0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 2 Insufficient memory.
没有
更多推荐
所有评论(0)