深入研究学习Pod

首先需要认识到Pod才是Kubernetes项目中最小的编排单位原子单位,凡是涉及到调度,网络,存储层面的,基本上都是Pod级别的!官方是用这样的语言来描述的: A Pod is the basic building block of Kubernetes–the smallest and simplest unit in the Kubernetes object model that you create or deploy.

1.Pod API对象

1.1.Pod中的一些重要字段解释
NodeSelector:将Pod与Node绑定的字段,可以用来指定对象的调度节点
NodeName:该字段被赋值,调度器会认为这个Pod已经经过了调度
HostAliases:定义Pod的hosts文件内容
shareProcessNamespace:Pod中的容器共享 PID Namespace
hostNetwork:共享宿主机Network
hostIPC:共享宿主机IPC
hostPID:共享宿主机PID Namespace
ImagePullPolicy:定义镜像拉取的策略
Lifecycle:Container Lifecycle Hooks的作用,钩子
1.2.Pod生命周期中的几种状态
Pending 悬而未决,Pod中的容器可能因为某种原因不能被顺利创建,比如调度不成功
Running Pod已经调度成功
Succeeded Pod里面的容器正常运行完毕并已经退出
Failed Pod中至少有一个容器以不正常的状态退出,你要想办法查日志了分析具体原因!
UnKnown 异常状态

2.认识特殊的Volume-Projected Volume

这块儿需要先理解Projected Volume的含义,在Kubernetes中有几种特殊的Volume它们为容器提供预先定义好的数据,从Container的角度来看,这些Volume就好像是被Kubernetes Project(投射)进入Container中的。

2.1.Projected Volume分类
Secret 把Pod想要访问的加密数据放入Etcd中
ConfigMap 把Pod想要访问的非加密数据放入Etcd中
Downward API 让Pod容器能够直接获取这个Pod API对象的本身
ServiceAccountToken 一种特殊的secret保存授权信息和文件
2.2.关于这些卷的实践

1.Secret保存账号密码的例子

1.首先准备YAML文件
[root@kubernetes01 ~]# cat test-projected-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-projected-volume001
spec:
containers:
- name: test-secret-volume001
image: busybox
args:
- sleep
- "86400"
volumeMounts:
- name: mysql-cred
mountPath: "/projected-volume"
readOnly: true
volumes:
- name: mysql-cred
projected:
sources:
- secret:
name: user001
- secret:
name: pass001 2.存放账号密码
[root@kubernetes01 ~]# cat ./username.txt
Ym95YW5nMDAx
[root@kubernetes01 ~]# cat ./password.txt
Ym95YW5nMDAx
[root@kubernetes01 ~]# kubectl create secret generic user001 --from-file=./username.txt
[root@kubernetes01 ~]# kubectl create secret generic pass001 --from-file=./password.txt 3.查看Secret对象
[root@kubernetes01 ~]# kubectl get secrets
NAME TYPE DATA AGE
default-token-8j8dl kubernetes.io/service-account-token 3 11d
pass Opaque 1 7d
pass001 Opaque 1 2d1h
user Opaque 1 7d
user001 Opaque 1 2d1h 4.创建Pod之后进入Pod进行查看
[root@kubernetes01 ~]# kubectl exec -it test-projected-volume001 -- /bin/sh
/ # ls /projected-volume/
password.txt username.txt
/ # cat /projected-volume/username.txt
Ym95YW5nMDAx
/ # cat /projected-volume/password.txt
Ym95YW5nMDAx
需要注意一点,生产使用中密码的加密方式!

2.ConfigMap保存java配置文件的例子

1.首先准备一段java配置文件
[root@kubernetes01 ~]# cat ./application.properties
#actuator \u53EA\u5F00\u653Ehealth\u63A5\u53E3
endpoints.enabled=false
endpoints.health.enabled=true # Server
server.port=15051
spring.application.name=loancenter-app-web
server.sessionTimeout=30
spring.profiles.active=dev
... 2.创建ConfigMap
kubectl create configmap loancenter-app-web-config --from-file=./application.properties 3.查看
[root@kubernetes01 ~]# kubectl get configmaps loancenter-app-web-config -o yaml
apiVersion: v1
data:
application.properties: |
#actuator \u53EA\u5F00\u653Ehealth\u63A5\u53E3
endpoints.enabled=false
endpoints.health.enabled=true # Server
server.port=15051
spring.application.name=loancenter-app-web
server.sessionTimeout=30
spring.profiles.active=dev
...

3.Downward API容器获取信息的例子

1.准备YAML文件
[root@kubernetes01 ~]# cat test-downwardapi-volume.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-downwardapi-volume
labels:
zone: cn-beijing-coast
cluster: cluster001
rack: rack-007
spec:
containers:
- name: client-container
image: busybox
command: ["sh", "-c"]
args:
- while true; do
if [[ -e /etc/podinfo/labels ]]; then
echo -en '\n\n'; cat /etc/podinfo/labels; fi;
sleep 5;
done;
volumeMounts:
- name: podinfo
mountPath: /etc/podinfo
readOnly: false
volumes:
- name: podinfo
projected:
sources:
- downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels 2.创建Pod
[root@kubernetes01 ~]# kubectl apply -f test-downwardapi-volume.yaml 3.查看日志输出
[root@kubernetes01 ~]# kubectl logs test-downwardapi-volume | tail -n 10
cluster="cluster001"
rack="rack-007"
zone="cn-beijing-coast" cluster="cluster001"
rack="rack-007"
zone="cn-beijing-coast" cluster="cluster001"
rack="rack-007"
可以看到Volume声明暴露的metadata.labels信息被Container打印出来了!

4.ServiceAccountToken

Service Account对象的作用,就是Kubernetes系统内置的一种“服务账户”。
这块需要我们记住的是Kubernetes API编程最佳授权方式是default Service Account自动授权

3.Pod中的容器健康检查和恢复机制

我们可以在Pod中定义Probe(探针),这是用来保证应用是否健康的重要手段!Pod还具有恢复机制restartPolicy!注意恢复永远只发生当前节点,如果同一个Pod出现在多个Node节点上就需要用控制器来管理,还要学会Pod中web应用的健康检查方法。

3.1.Pod中定义Probe

通过命令来充当探针的例子

1.准备YAML文件
[root@kubernetes01 ~]# cat test-liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: test-liveness-exec
spec:
containers:
- name: liveness
image: busybox
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 60
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5 2.创建Pod之后,观察效果
[root@kubernetes01 ~]# kubectl get pods | grep "test-liveness"
test-liveness-exec 0/1 CrashLoopBackOff 317 20h [root@kubernetes01 ~]# kubectl describe pods test-liveness-exec | tail -n 10
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Pulling 60m (x303 over 20h) kubelet, kubernetes05 pulling image "busybox"
Normal Killing 20m (x313 over 20h) kubelet, kubernetes05 Killing container with id docker://liveness:Container failed liveness probe.. Container will be killed and recreated.
Warning Unhealthy 6m2s (x951 over 20h) kubelet, kubernetes05 Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
Warning BackOff 61s (x3881 over 19h) kubelet, kubernetes05 Back-off restarting failed container
这块儿因为探针检测到的Unhealthy状态,再加上默认的restartPolicy Pod具有的恢复机制,所以20小时内重启了317次。
3.2.Pod中web应用的健康检查

通过http请求tomcat服务充当探针的例子

1.编写YAML文件
[root@kubernetes01 ~]# cat java-web-health.yaml
apiVersion: v1
kind: Pod
metadata:
name: java-web-healthtest
spec:
initContainers:
- image: registry:5000/jenkins:v2
name: war
command: ["cp","/jenkins.war","/app"]
volumeMounts:
- name: java-web-health-volume
mountPath: "/app"
containers:
- image: tomcat:latest
name: tomcat
volumeMounts:
- name: java-web-health-volume
mountPath: "/usr/local/tomcat/webapps"
ports:
- containerPort: 8080
name: tomcatwebsite
hostPort: 8004
protocol: TCP
livenessProbe:
httpGet:
path: /jenkins/
port: 8080
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
periodSeconds: 5
volumes:
- name: java-web-health-volume
emptyDir: {}
解释下这三个字段的意思
initialDelaySeconds: 容器启动后第一次探测需要等久,单位秒,因为是tomcat所以可以设置的长一点
timeoutSeconds: 探测的超时时间,单位秒
periodSeconds: 执行探测的频率,单位秒 2.创建Pod后查看
[root@kubernetes01 ~]# curl -I 10.44.0.10:8080/jenkins/
HTTP/1.1 403
Cache-Control: private
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Content-Type-Options: nosniff
Set-Cookie: JSESSIONID=C4379308E14D00BEAFFDA51E2E38A2F3; Path=/jenkins; HttpOnly
X-Hudson: 1.395
X-Jenkins: 2.132
X-Jenkins-Session: d5a26f97
X-You-Are-Authenticated-As: anonymous
X-You-Are-In-Group-Disabled: JENKINS-39402: use -Dhudson.security.AccessDeniedException2.REPORT_GROUP_HEADERS=true or use /whoAmI to diagnose
X-Required-Permission: hudson.model.Hudson.Administer
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Mar 2019 08:29:41 GMT
这块可以看到返回的是403! [root@kubernetes01 ~]# kubectl describe pods java-web-health | tail -n 15
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m30s default-scheduler Successfully assigned default/java-web-healthtest to kubernetes02
Normal Pulled 2m30s kubelet, kubernetes02 Container image "registry:5000/jenkins:v2" already present on machine
Normal Created 2m30s kubelet, kubernetes02 Created container
Normal Started 2m29s kubelet, kubernetes02 Started container
Normal Pulling 59s (x3 over 2m29s) kubelet, kubernetes02 pulling image "tomcat:latest"
Normal Killing 59s (x2 over 104s) kubelet, kubernetes02 Killing container with id docker://tomcat:Container failed liveness probe.. Container will be killed and recreated.
Normal Pulled 55s (x3 over 2m25s) kubelet, kubernetes02 Successfully pulled image "tomcat:latest"
Normal Created 55s (x3 over 2m25s) kubelet, kubernetes02 Created container
Normal Started 55s (x3 over 2m25s) kubelet, kubernetes02 Started container
Warning Unhealthy 20s (x8 over 115s) kubelet, kubernetes02 Liveness probe failed: HTTP probe failed with statuscode: 403
通过查看Events输出我们可以看到探针工作和恢复机制工作的过程
4.总结

自我感觉Pod对象相关的知识比较多但是很重要,在运行大规模集群包含各种各样任务的时候,会存在各种各样的关系,关系的妥善处理维护才是编排和系统管理最困难的地方。使用传统虚拟机的粒度控制的相对较粗,没有通过Kubernetes编排Pod这种方式粒度控制的细,最后Kubernetes声明式的API具有超强的编排能力。

PS:文中服务器使用的是国内某☁️的机器

欢迎大家留言讨论哦,欢迎大家和我一起研究学习Kubernetes~~~

Kubernetes探索学习004--深入Kubernetes的Pod的更多相关文章

  1. Kubernetes探索学习003--关于Kubernetes的Pod

    关于Pod 关于Pod我们要慢慢去体会去接受它去使用它,尤其是运维人员这块需要从逻辑上形成认识,首先理解Pod是Kubernetes项目的原子调度单位.为什么是Pod而不是单个DockerContai ...

  2. Kubernetes探索学习001--Centos7.6使用kubeadm快速部署Kubernetes集群

    Centos7.6使用kubeadm快速部署kubernetes集群 为什么要使用kubeadm来部署kubernetes?因为kubeadm是kubernetes原生的部署工具,简单快捷方便,便于新 ...

  3. Kubernetes探索学习005--Kubernetes的Controller模型和ReplicaSet伸缩

    1.Kubernetes的controller pattern 需要认识到Kubernetes操作Pod的逻辑,都是由控制器来完成的. 查看之前写过的nginx-deployment的YAML文件 [ ...

  4. Kubernetes探索学习002--Kubernetes的基本使用

    Kubernetes 的基本使用方法 原则:使用YAML文件描述你要部署的API对象! 以部署nginx静态站点为例,具体操作及内容如下 1.编写YAML文件 [root@kubernetes01 ~ ...

  5. kubernetes 安装学习

    什么是Kubernetes Kubernetes是一个开源平台,用于跨主机群集自动部署,扩展和操作应用程序容器,提供以容器为中心的基础架构. 使用Kubernetes,您可以快速高效地响应客户需求: ...

  6. kubernetes 入门学习

    kubernetes 学习 kubernetes 简介 Kubernetes这个名字源自希腊语,意思是"舵手",也是"管理者","治理者"等 ...

  7. Kubernetes入门学习--在Ubuntu16.0.4安装配置Minikube

    目 录 一. 安装minikube环境 1.1. 安装前准备 1.2. 安装Lantern 1.2.1. Lantern下载网站 1.2.2. Lantern下载地址 1.2.3. Lantern安装 ...

  8. Kubernetes基本概念和术语之《Pod》

    Pod是Kubernetes的最重要也最基本的概念.我们看到每个Pod都有一个特殊的被称为“根容器”的Pause容器对应的镜像属于Kubernetes平台的一部分.除了Pause容器,每个Pod还包含 ...

  9. 揭秘 Kubernetes attach/detach controller 逻辑漏洞致使 pod 启动失败

    前言 本文主要通过深入学习k8s attach/detach controller源码,了解现网案例发现的attach/detach controller bug发生的原委,并给出解决方案. 看完本文 ...

随机推荐

  1. etherlime-3-Etherlime Library API-Deployed Contract Wrapper

    Deployed Contract Wrapper部署合约的封装 Wrappers封装 One of the advancements of the etherlime is the result o ...

  2. VC++程序运行时间测试函数

    0:介绍 我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记录函数或者算 ...

  3. Redis(五)主从复制

    本文转载自编程迷思,原文链接 深入学习Redis(3):主从复制 前言 在前面的两篇文章中,分别介绍了Redis的内存模型和Redis的持久化. 在Redis的持久化中曾提到,Redis高可用的方案包 ...

  4. java官网门户源码 SSM框架 自适应-响应式 freemarker 静态模版引擎

    来源:http://www.fhadmin.org/webnewsdetail3.html 前台:支持(5+1[时尚单页风格])六套模版,可以在后台切换 官网:www.fhadmin.org 系统介绍 ...

  5. 修改通达oa数据库root密码

    第一步: 打开通达oamysql远程网页地址:如http://127.0.0.1/mysql,点击修改密码功能按钮,根据提示修改,不要生成加密密码,执行即可! 第二步:修改service.php文件的 ...

  6. Scala的高级特性

    高阶函数 概念 Scala混合了面向对象和函数式的特性,我们通常将可以作为参数传递到方法中的表达式叫做函数.在函数式编程语言中,函数是“头等公民”,高阶函数包含:作为值的函数.匿名函数.闭包.柯里化等 ...

  7. 基于BM3803处理器平台的PCI软硬件调试问题汇总(持续更新中)

    一:相关基本配置: FPGA:  XILINX XC5VFX130T-1FFG1738 PCI接口部分使用XILINX提供的pci32_v4_8硬核:PCI控制器由FPGA逻辑实现,主要完成PCI设备 ...

  8. Java基础加强——动态代理

    代理模式: 为其他对象提供一种代理以控制对这个对象的访问. 代理模式主要分为两类: 静态代理:由程序员创建或特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了.  ...

  9. lxml etree xpath

    from lxml import etree #####################基本用法: ##################### html = ''' <h1 class=&quo ...

  10. Windows和Linux下通用的线程接口

    对于多线程开发,Linux下有pthread线程库,使用起来比较方便,而Windows没有,对于涉及到多线程的跨平台代码开发,会带来不便.这里参考网络上的一些文章,整理了在Windows和Linux下 ...