一、Pod介绍
1.1、Pod架构图:
Pause容器,每个Pod都会有的一个根容器,作用如下:
1)可以调整整个Pod的健康状态;
2)可以在根容器上设置IP地址,其他容器都以此IP实现网络内部的通信;
1.2、Pod资源介绍:
kubectl explain Pod.xxx
1.3、创建基础Pod
[root@master01 5.2]# cat pod-base.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-base
namespace: dev
labels:
user: heima
spec:
containers:
- name: nginx
image: nginx:1.17.1
- name: busybox
image: busybox:1.30
[root@master01 5.2]#
[root@master01 5.2]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
pod-base 1/2 Running 3 2m37s
# ready 表示pod中容器就绪数量
# restarts 表示不就绪的容器重启的次数
二、Pod常用参数
2.1、imagepullpolicy 镜像拉取策略
1)Always:总是从远程镜像仓库拉取
2)IfNotPresent:本地有就从本地拉取,本地仓库没有就从远程仓库拉取
3)Never:只从本地仓库拉取,不从远程拉取
apiVersion: v1
kind: Pod
metadata:
name: pod-imagepullpolicy
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
imagePullPolicy: Never # 用于设置镜像拉取策略
- name: busybox
image: busybox:1.30
[root@master01 5.2]# kubectl describe pod pod-imagepullpolicy -n dev
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned dev/pod-imagepullpolicy to node01
Normal Pulled 54s kubelet, node01 Container image "nginx:1.17.1" already present on machine
Normal Created 54s kubelet, node01 Created container nginx
Normal Started 54s kubelet, node01 Started container nginx
Normal Pulled 9s (x4 over 54s) kubelet, node01 Container image "busybox:1.30" already present on machine
Normal Created 9s (x4 over 54s) kubelet, node01 Created container busybox
Normal Started 9s (x4 over 54s) kubelet, node01 Started container busybox
Warning BackOff 8s (x5 over 52s) kubelet, node01 Back-off restarting failed container
2.2、command 启动命令
指定容器启动后执行的命令
apiVersion: v1
kind: Pod
metadata:
name: pod-command
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
- 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 3; done;"]
# 补充一个命令: kubectl exec pod名称 -n 命名空间 -it -c 容器名称 /bin/sh 在容器内部执行命令
[root@master01 5.2]# kubectl exec pod-command -n dev -it -c busybox /bin/sh
/ #
2.3、env 环境变量
用于在pod的容器中设置环境变量
如下方式不推荐,建议设置在配置文件中,方便后期维护
apiVersion: v1
kind: Pod
metadata:
name: pod-env
namespace: dev
spec:
containers:
- name: busybox
image: busybox:1.30
command: ["/bin/sh","-c","while true;do /bin/echo $(date +%T);sleep 60; done;"]
env: # 设置环境变量列表
- name: "username"
value: "admin"
- name: "password"
value: "123456"
[root@master01 5.2]# kubectl exec pod-env -n dev -it -c busybox -it /bin/sh
/ # echo $username
admin
/ #
2.4、ports 端口设置
1)name:端口名称
2)containerPort:容器要监听的端口
3)hostPort:容器要在主机上公开的端口
4)hostIP:要将外部端口绑定到主机IP
5)protocol:端口协议
apiVersion: v1
kind: Pod
metadata:
name: pod-ports
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports: # 设置容器暴露的端口列表
- name: nginx-port
containerPort: 80
protocol: TCP
2.5、resources 资源配置
1)limits:需要的最大资源
2)requests:需要的最小资源
资源单位:
1)cpu:core数,可以为整数或者小数
2)memory:可以使用Gi\Mi\G\M
apiVersion: v1
kind: Pod
metadata:
name: pod-resources
namespace: dev
spec:
containers:
- name: nginx
image: nginx:1.17.1
resources: # 资源配额
limits: # 限制资源(上限)
cpu: "2" # CPU限制,单位是core数
memory: "10Gi" # 内存限制
requests: # 请求资源(下限)
cpu: "1" # CPU限制,单位是core数
memory: "10Mi" # 内存限制
3、Pod生命周期