004.kubernets对于pod的简单管理
一 pod简介
1.1 介绍
- Pod是K8s集群中所有业务类型的基础
- Pod是在K8s集群中运行部署应用或服务的最小单元,它是可以支持多容器的。
- Pod的设计理念是支持多个容器在一个Pod中共享网络地址和文件系统
pod和容器的区别就是,一个pod可以有多个容器,当一个pod只有一个容器的时候,访问pod就是访问容器,对于一个kubernets来说,一个pods至少有两个容器,一个是不可见的,称为pause容器,另一个就是业务容器
pod是一个逻辑概念,pod中的一个容器异常,整个pod重新创建
- Kubernetes为每个Pod都分配了唯一的IP地址,称之为PodIP,一个Pod里的多个容器共享PodIP地址。要求底层网络支持集群内任意两个Pod之间的直接通信,通常采用虚拟二层网络技术来实现(Flannel)。
- POD可以与其它主机上的POD直接通讯。
- 如果有POD意外停止,K8S会根据资源设定重启或创建POD,直到符合预期设定值
- pause容器劫持业务容器的所有流量,IP是配置在pause容器的,在创建pod的时候,自动创建,用来接管容器网络
1.2 pod的一个应用场景
- pod含有两个容器,File Puller先于web server容器启动
- 拉取代码放到volume中,然后自毁
- web server容器启动,读物volume的代码,用于用户访问
二 POD简单操作
2.1 创建一个关于nginx的pods
[root@docker-server1 namespace]# cd ../
[root@docker-server1 yamls]# mkdir pods
[root@docker-server1 yamls]# cd pods
[root@docker-server1 pods]# vi nginx-pods.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
annotations:
test: this is a test app
spec: #资源描述信息
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
这是一个最简单的pods,只是运行一个nginx的业务,没有任何其他的东西
[root@docker-server1 pods]# kubectl apply -f nginx-pods.yaml
由于没有指定ns,所以pods运行在defaults中,查看
[root@docker-server1 pods]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 85s
1/1:后面的1表示这个pods运行了几个容器,前面的1表示几个容器处于redy状态
查看容器
[root@docker-server1 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 6m41s 10.244.2.6 192.168.132.133 <none> <none>
访问
[root@docker-server1 pods]# curl http://10.244.2.6
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
2.2 配置映射端口
[root@docker-server1 pods]# vim nginx-pods.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
hostNetwork: true
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
[root@docker-server1 pods]# kubectl delete -f nginx-pods.yaml
[root@docker-server1 pods]# kubectl create -f nginx-pods.yaml
[root@docker-server1 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 0/1 ContainerCreating 0 4s 192.168.132.133 192.168.132.133 <none> <none>
[root@docker-server1 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 95s 192.168.132.133 192.168.132.133 <none> <none>
2.3 pod常用配置
name: string
image: string
imagePullPolicy: [Always |Never | IfNotPresent] #拉取镜像策略,默认是第三种,先看本地,本地没有,才拉取
restartPolicy: [Always | Never | OnFailure]
command: [string]
args: [string]
ports:
containerPort: int
hostPort: int
protocol: string
env:
name: string
value: string hostNetwork: bool
resources
volumes
livenessProbe
ReadnessProbe
2.4 配置其他策略
运行多个容器,并使用拉取镜像策略
[root@docker-server1 pods]# vim nginx-pods.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
annotations:
test: this is a test app
spec:
imagePullPolicy: Always
restartPolicy: Always
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
hostPost: 8080
env:
- name: test
value: aaa
- name: test1
value: bbb
- name: busybox
image: busybox
command:
- sh
- -c
- sleep 3600
2.4 删除再创建pod
[root@docker-server1 pods]# kubectl delete pod nginx
[root@docker-server1 pods]# kubectl create -f nginx-pods.yaml
error: error validating "nginx-pods.yaml": error validating data: ValidationError(Pod.spec): unknown field "imagePullPolicy" in io.k8s.api.core.v1.PodSpec; if you choose to ignore these errors, turn validation off with --validate=false
imagePullPolicy这个不能指定所有容器
[root@docker-server1 pods]# vim nginx-pods.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
annotations:
test: this is a test app
spec:
restartPolicy: Always
containers:
- name: nginx
image: nginx
imagePullPolicy: Always
ports:
- containerPort: 80
hostPort: 8080
env:
- name: test
value: aaa
- name: test1
value: bbb
- name: busybox
image: busybox
command:
- sh
- -c
- sleep 3600
[root@docker-server1 pods]# kubectl create -f nginx-pods.yaml
[root@docker-server1 pods]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 2/2 Running 0 28s
[root@docker-server1 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 2/2 Running 0 36s 10.244.2.7 192.168.132.133 <none> <none>
做了端口映射,两种方式访问
[root@docker-server1 pods]# curl http://192.168.132.133:8080
[root@docker-server1 pods]# curl http://10.244.2.7
2.5 查看pods的详细信息
[root@docker-server1 pods]# kubectl describe po nginx
Name: nginx
Namespace: default
Priority: 0
Node: 192.168.132.133/192.168.132.133
Start Time: Thu, 09 Jan 2020 18:17:48 -0500
Labels: app=nginx
Annotations: test: this is a test app
Status: Running
IP: 10.244.2.7
IPs:
IP: 10.244.2.7
Containers:
nginx:
Container ID: docker://676a2d9bebda40d86138190093d1a6d6cf6f16e5ff0e89fc22df53a74bdf8048
Image: nginx
Image ID: docker-pullable://nginx@sha256:8aa7f6a9585d908a63e5e418dc5d14ae7467d2e36e1ab4f0d8f9d059a3d071ce
Port: 80/TCP
Host Port: 8080/TCP
State: Running
Started: Thu, 09 Jan 2020 18:17:55 -0500
Ready: True
Restart Count: 0
Environment:
test: aaa
test1: bbb
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-bwbrn (ro)
busybox:
Container ID: docker://e8cc006f3ab292701d9876d84881af90f4c97ea22f32bf0cabf2b93d82b8c82b
Image: busybox
Image ID: docker-pullable://busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
Port: <none>
Host Port: <none>
Command:
sh
-c
sleep 3600
State: Running
Started: Thu, 09 Jan 2020 18:18:00 -0500
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-bwbrn (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-bwbrn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-bwbrn
Optional: false
QoS Class: BestEffort
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 4m43s kubelet, 192.168.132.133 Pulling image "nginx"
Normal Scheduled 4m41s default-scheduler Successfully assigned default/nginx to 192.168.132.133
Normal Pulled 4m38s kubelet, 192.168.132.133 Successfully pulled image "nginx"
Normal Created 4m37s kubelet, 192.168.132.133 Created container nginx
Normal Started 4m37s kubelet, 192.168.132.133 Started container nginx
Normal Pulling 4m37s kubelet, 192.168.132.133 Pulling image "busybox"
Normal Pulled 4m32s kubelet, 192.168.132.133 Successfully pulled image "busybox"
Normal Created 4m32s kubelet, 192.168.132.133 Created container busybox
Normal Started 4m32s kubelet, 192.168.132.133 Started container busybox
三 yaml文件找回
如果不小心删除了yaml文件,可以通过描述信息找回
3.1 删除yaml文件
[root@docker-server1 pods]# rm -rf nginx-pods.yaml
[root@docker-server1 pods]# kubectl get pods nginx -o yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
test: this is a test app
creationTimestamp: "2020-01-09T23:17:51Z"
labels:
app: nginx
name: nginx
namespace: default
resourceVersion: "43864"
selfLink: /api/v1/namespaces/default/pods/nginx
uid: 41510342-de97-4b37-ab95-0a01dd73aac7
spec:
containers:
- env:
- name: test
value: aaa
- name: test1
value: bbb
image: nginx
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
hostPort: 8080
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-bwbrn
readOnly: true
- command:
- sh
- -c
- sleep 3600
image: busybox
imagePullPolicy: Always
name: busybox
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: default-token-bwbrn
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: 192.168.132.133
priority: 0
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300
- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: default-token-bwbrn
secret:
defaultMode: 420
secretName: default-token-bwbrn
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2020-01-09T23:17:48Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2020-01-09T23:18:01Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2020-01-09T23:18:01Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2020-01-09T23:17:51Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://e8cc006f3ab292701d9876d84881af90f4c97ea22f32bf0cabf2b93d82b8c82b
image: busybox:latest
imageID: docker-pullable://busybox@sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
lastState: {}
name: busybox
ready: true
restartCount: 0
started: true
state:
running:
startedAt: "2020-01-09T23:18:00Z"
- containerID: docker://676a2d9bebda40d86138190093d1a6d6cf6f16e5ff0e89fc22df53a74bdf8048
image: nginx:latest
imageID: docker-pullable://nginx@sha256:8aa7f6a9585d908a63e5e418dc5d14ae7467d2e36e1ab4f0d8f9d059a3d071ce
lastState: {}
name: nginx
ready: true
restartCount: 0
started: true
state:
running:
startedAt: "2020-01-09T23:17:55Z"
hostIP: 192.168.132.133
phase: Running
podIP: 10.244.2.7
podIPs:
- ip: 10.244.2.7
qosClass: BestEffort
startTime: "2020-01-09T23:17:48Z"
3.2 使用命令恢复
[root@docker-server1 pods]# kubectl get pods nginx -o yaml > nginx-pods.yaml
删除不必要的信息
[root@docker-server1 pods]# vim nginx-pods.yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
test: this is a test app
labels:
app: nginx
name: nginx
namespace: default
spec:
containers:
- env:
- name: test
value: aaa
- name: test1
value: bbb
image: nginx
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
hostPort: 8080
protocol: TCP
- command:
- sh
- -c
- sleep 3600
image: busybox
imagePullPolicy: Always
name: busybox
restartPolicy: Always
[root@docker-server1 pods]# kubectl delete pod nginx
[root@docker-server1 pods]# kubectl create -f nginx-pods.yaml
3.3 根据恢复的yaml文件验证
[root@docker-server1 pods]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 2/2 Running 0 14s
[root@docker-server1 pods]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 2/2 Running 0 27s 10.244.2.8 192.168.132.133 <none> <none>
[root@docker-server1 pods]# curl http://192.168.132.133:8080
[root@docker-server1 pods]# curl http://10.244.2.8
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>
pod的简单操作学习到这里
博主声明:本文的内容来源主要来自誉天教育晏威老师,由本人实验完成操作验证,需要的博友请联系誉天教育(http://www.yutianedu.com/),获得官方同意或者晏老师(https://www.cnblogs.com/breezey/)本人同意即可转载,谢谢!
004.kubernets对于pod的简单管理的更多相关文章
- ssdb主从及双主模型配置和简单管理
ssdb主从及双主模型配置和简单管理 levelDB是一个key->value 的数据存储库,其只能在本地保存数据,支持持久化,并且支持保存非常大的数据,单机redis在保存较大数据的时候数十G ...
- Kubernetes1.3:POD生命周期管理
转:http://blog.csdn.net/horsefoot/article/details/52324830 (一) 核心概念 Pod是kubernetes中的核心概念,kubernetes对 ...
- Service系统服务(一):安装一个KVM服务器、KVM平台构建及简单管理、virsh基本管理操作、xml配置文件的应用、为虚拟机制作快照备份、快建新虚拟机
一.安装一个KVM服务器 目标: 本例要求准备一台 RHEL7.2 服务器,将其搭建为KVM平台,主要完成下列操作: 1> 关闭本机的SELinux保护.防火墙服务 2> 挂载RHEL ...
- MacOS下对postgresql的简单管理操作
如何安装在另一篇blog中有述,这里不再赘述.本篇简单说一下安装完postgresql之后的一些管理和查询操作. 首先安装完postgresql之后需要初始化数据库: initdb /usr/loca ...
- django admin后台(数据库简单管理后台)
只需要简单的几行胆码就可以生成一个完整的管理后台 这个就是django魅力之一 创建超级用户 python manage.py createsuperuser ---- 之后会提示输入用慕名 ...
- [JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改
上一次的文章,我们讲解了如何用JAVA访问MySql数据库,对数据进行增加删除修改查询.那么这次我们把具体的页面的数据库操作结合在一起,进行一次简单的学生信息操作案例. 首先我们创建一个专门用于学生管 ...
- kubernets之pod的标签的使用
一 对于kubernets里面的资源标记完成之后的使用 1 node节点标签的应用(将资源调度到特定的节点上) #kubia-gpu.ymlapiVersion: v1 kind: Pod metad ...
- kubernets之pod简介
一 k8s集群里面的最小单位是pod 1.1 一个较为简单的pod的配置文件 apiverson: api的版本号 kind: 资源的种类 metadata: pod的种类等相关信息 spec: p ...
- 简单管理用户SESSION小记(个人观点,欢迎斧正)
做了几年码农,记录下一般涉及到用户session管理的方法. 问题说明: a.用户如果点击退出时,可以获取用户动作,这样可以销毁session. b.用户直接关闭浏览器或者直接意外关机情况,无法获取用 ...
随机推荐
- hello world!goodbye world~
我有个朋友,做ios开发做了5年,年前回家转行赚大钱去了,这个标题,其实就是因他而生. 我本人做的.net开发,也差不多快5年时间了,在这个时候暂借博客园这个平台说几句心里话,骚了勿喷:) 其实我是个 ...
- 使用gradle插件发布项目到nexus中央仓库
目录 简介 Gradle Nexus Publish Plugin历史 插件的使用 Groovy DSL Kotlin DSL 插件背后的故事 总结 简介 Sonatype 提供了一个叫做开源软件资源 ...
- 什么?女神发了朋友圈,快来围观之Java设计模式:观察者模式
目录 观察者模式 示例 定义 设计原则 意图 主要解决问题 何时使用 优缺点 女神和追求者的故事 Java中的实现 观察者模式 示例 微信公众号,关注就可以收到推送的消息,取消关注就不会收到 定义 定 ...
- 面试题:让你捉摸不透的 Go reslice
面试题: package main func a() []int { a1 := []int{3} a2 := a1[1:] return a2 } func main() { a() } 看到这个题 ...
- Day15_87_通过反射机制获取某个特定的方法
通过反射机制获取某个特定的方法 反射是通过 方法名+形参列表来区分各个方法的(形参列表要用class类型.加.class) 示例代码 import java.lang.reflect.Method; ...
- Day11_58_增强for循环
增强for循环 * 语法 : for(数据类型 变量名:数组名/集合名) * 集合如果要使用增强for循环需要先使用泛型来确定元素类型,如果没有使用泛型就使用foreach,那么变量类型设置为Obje ...
- H5小技巧之——巧用<a>标签锚链接(#锚点链接 #页面特定位置 #DOM定位 #hash路由中使用锚链接)
#作者:矩阵鱼--代码中游泳的咸鱼 前端开发中,常遇到定位到页面某特定位置的需求,JavaScript提供的el.scrollIntoView() 和 el.scrollIntoViewIfNeede ...
- 1. Robot Framework入门
RF定义: 通用型的 自动测试框架, 绝大部分的软件的的自动化系统都可以采用它. 特点: 测试数据文件(Test Data)对应一个个的测试用例.测试数据文件里面使用的功能小模块叫关键字,由测试库(T ...
- POJ2983 查分约束系统
题意: 给你n个点,然后给你两种情况,P a b c,表明a在b的北边c那么远,V a b 表明a在b的北边(距离最少是1),问你这些条件是否冲突. 思路: 一开始想用带权并 ...
- hdu4291 暴力循环节+矩阵快速幂
题意: 给你一个关系式,x[n] = 3*x[n-1] + x[n-2],求x(x(x[n]))%1000000007. 思路: 做这个题目要明确一点,就是对于取余操作大多数时 ...