pod的基本概念
pod
基本概念
pod是集群中最小的部署单元,将这个设计落实到API对象上,容器(Container)就成了Pod属性里一个普通的字段。
包含多个容器,也是一组容器的集合
一个pod中容器共享网络的命名空间
pod是短暂的
每个pod都有被称为根容器的pause容器
每个pod还包含一个或多个用户业务容器
存在的意义
创建容器使用的是docker,一个docker对应一个容器,一个容器有进程,一个容器运行一个应用程序
pod是多进程的设计,运行多个应用程序
一个pod有多个容器,一个容器里面运行一个应用程序
pod的存在也是为了亲密性应用
两个应用之间进行交互
网络之间的调用
两个应用需要频繁调用
容器可选的设置属性
"name": 容器名称
"image": 容器镜像
"command": 容器启动指令
"args": 指令参数
"workingDir": 工作目录
"ports": 容器端口
"env": 环境变量
"resource": 资源限制
"volumeMounts": 卷挂载
"livenessProbe": 存活探针
"readinessProbe": 就绪探针
"startupProbe": 启动探针
"livecycle": 钩子函数
"terminationMessagePath": 容器的异常终止消息的路径,默认在 /dev/termination-log
"imagePullPolicy": 镜像拉去策略 Always IfNotPresent Nerver 大驼峰 小驼峰
"securityContext": 安全上下文
"stdin": 给容器分配标准输入,类似docker run -i
"tty": 分配终端
创建pod
单容器pod
vim mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
labels: #定义标签
name: mysql
spec:
restartPolicy: OnFailure #重启策略
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
env: #环境变量
- name: MYSQL_ROOT_PASSWORD
value: "123456"
resources: #资源限制
limits:
memory: "1024Mi"
cpu: "1000m"
ports:
- containerPort: 3306
nodeSelector:
kubernetes.io/hostname: kub-k8s-node2
# nodeName: kub-k8s-node2
kubectl apply -f mysql.yaml //执行文件
策略
restartPolicy:
重启策略,可选参数有:
Always:Pod中的容器无论如何停止都会自动重启
OnFailure: Pod中的容器非正常停止会自动重启
Never: Pod中的容器无论怎样都不会自动重启
imagePullPolicy:
镜像拉取策略,可选参数有:
Always:总是重新拉取
IfNotPresent:默认,如果本地有,则不拉取
Never:只是用本地镜像,从不拉取
pod.spec.restartPolicy:
恢复策略,参数有:
Always(默认):在任何情况下,只要容器不在运行状态,就会自动重启容器。
OnFailure:只有容器异常时,才自动重启容器。
Never:无论容器处于何种状态,Kubernetes 都不会自动重启容器,即永远不会重启容器
nodeSelector:
节点选择器:可以指定node的标签,查看标签指令:
nodeName:
节点名称: 可以指定node的名称进行调度
kubectl get node --show-labels
多容器pod
vim test.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
name: my-pod
spec:
containers:
- name: write
image: centos:7
command: ["bash", "-c", "for i in {1..100}; do echo $i >> /data/hello; sleep 1; done"]
volumeMounts:
- name: data
mountPath: /data
- name: read
image: centos:7
command: ["bash", "-c", "tail -f /data/hello"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
kubectl apply -f test.yaml //执行文件
其中两个容器,一个容器写内容,一个容器读取内容
pod交互
vim host-alias.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: centos
labels:
name: centos
spec:
containers:
- name: centos
image: centos:7
command:
- "tail"
- "-f"
- "/dev/null"
hostAliases: #本地域名解析
- ip: "192.168.100.128"
hostnames:
- "master"
- "k8s-master"
- "apiserver"
# 字段解析
command:
启动容器时执行的指令,类似于docker run -it 镜像 tail -f /dev/null
hostAliases:
在容器中的/etc/hosts文件中配置本地解析
pod内访问网站
curl svc的名字.命名空间
pod共享进程
vim pod.yml #修改如下。最好是提前将镜像pull下来。
---
apiVersion: v1
kind: Pod
metadata:
name: website
labels:
app: website
spec:
shareProcessNamespace: true #共享进程名称空间
containers:
- name: test-web
image: daocloud.io/library/nginx
ports:
- containerPort: 80
- name: busybox
image: daocloud.io/library/busybox
stdin: true
tty: true
kubectl apply -f pod.yml //创建pod
1. 定义了 shareProcessNamespace=true
表示这个 Pod 里的容器要共享进程(PID Namespace)如果是false则为不共享。
2. 定义了两个容器:
一个 nginx 容器
一个开启了 tty 和 stdin 的 busybos 容器
在Pod的YAML文件里声明开启它们俩,等同于设置了docker run里的-it(-i即stdin,-t即tty)参数。此 Pod被创建后,就可以使用shell容器的tty跟这个容器进行交互了。
配置节点标签
添加标签
kubectl label pod pod名称 标签名=标签值
kubectl label pod mysql password=0
删除标签
kubectl label pod pod名称 需要移除的标签名-
kubectl label pod mysql password-
查看标签
kubectl get pods --show-labels
钩子函数
kubernetes 在主容器启动之后和删除之前提供了两个钩子函数
post start:容器创建之后执行,如果失败会重启容器
pre stop:容器删除之前执行,执行完成之后容器将成功删除,在其完成之前会阻塞删除容器的操作
钩子函数有三种定义方式:
exec执行指令
lifecycle:
postStart:
exec:
command:
- cat
- /etc/hosts
容器中请求端口
lifecycle:
postStart:
tcpSocket:
port: 8080
向容器发送http请求
lifecycle:
postStart:
httpGet:
path: / # URI地址
port: 80 # 端口号
host: 192.168.96.10 # 主机地址
scheme: HTTP
示例
$ cat nginx-lifecycle.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-lifecycle
namespace: default
labels:
app: nginx
spec:
containers:
- name: nginx-lifecycle
image: nginx:1.16.1
ports:
- containerPort: 80
protocol: TCP
lifecycle: #探针健康检查
postStart:
exec:
command: ["/bin/sh", "-c", "echo '<h1>this is a nginx-lifecycle test page</h1>' > /usr/share/nginx/html/index.html"] #启动时的钩子
preStop:
exec:
command: ["/usr/sbin/nginx", "-s", "quit"] #关闭时的钩子
$ kubectl apply -f nginx-lifecycle.yaml
pod/pod-lifecycle created