kubenetes入门学习-七-Pod控制器--控制器类型以及功能-ReplicaSet
Pod是标准的k8s资源,遵守资源清单回顾主要字段apiVersion、kind、metadata、spec、status(只读)spec:containersnodeSelectornodeNamerestartPolicy:Always、Never、OnFailurecon...
Pod是标准的k8s资源,遵守资源清单
回顾
主要字段
apiVersion、kind、metadata、spec、status(只读)
spec:
containers
nodeSelector
nodeName
restartPolicy:
Always、Never、OnFailure
containers:
name
image
iamgePullPolicy:Always、Never、IfNotPersent
ports:
name
containerPort
livenessProbe
readinessProbe
liftcycle
====================================================
Pod控制器:
Pod控制器是用于实现管理pod的中间层,确保pod资源符合预期的状态,pod的资源出现故障时,会尝试 进行重启,当根据重启策略无效,则会重新新建pod的资源。
pod控制器常用类型如下:
ReplicaSet: 代用户创建指定数量的pod副本数量,确保pod副本数量符合预期状态,并且支持滚动式自动扩容和缩容功能。
ReplicaSet主要三个组件组成:
(1)用户期望的pod副本数量
(2)标签选择器,判断哪个pod归自己管理
(3)当现存的pod数量不足,会根据pod资源模板进行新建
帮助用户管理无状态的pod资源,精确反应用户定义的目标数量,但是RelicaSet不是直接使用的控制器,而是使用Deployment。
Deployment:工作在ReplicaSet之上,用于管理无状态应用,目前来说最好的控制器。支持滚动更新和回滚功能,还提供声明式配置。
可以随时声明,更改配置。
可以创建多余node节点的pod副本数量,这样一个node可以运行多个副本也可以一个都不运行。
DaemonSet:用于确保集群中的每一个节点只运行特定的pod副本,通常用于实现系统级后台任务。比如ELK服务,log收集agent,每个节点上收集容器和node级别的log,一个node上部署一个agent。
特性:服务是无状态的
服务必须是守护进程,必须随时运行。
可以托管在k8s上,宕了可以重建,系统级别功能常用此控制器。
Job:只要完成就立即退出,不需要重启或重建。
Cronjob:周期性任务控制,不需要持续后台运行。
StatefulSet:管理有状态应用,每个pod副本被单独管理。配置管理mysql、redis不一样的,部署方式也可能不一样。
封装控制器,把人为操作封装为脚本,故障的时候他通过脚本自动恢复,感觉这个比较复杂。
TPR:第三方资源,及自定义资源。 1.2+ 1.7之后被费了
CDR:第三方资源,及自定义资源。1.8+
Operator:目前应用很少。
有状态应用托管在k8s之上还是一个挑战。
=============================
k8s自己写资源清单导致难以入门,后面就提供了helm,这样就简单了。
helm:类是于yum之类的功能,当然也不是全依赖他。
==============================
以下几个命令就能说明资源清单yaml文件的层级
命令行查看ReplicaSet清单定义规则
[root@k8s-master ~]# kubectl explain rs
[root@k8s-master ~]# kubectl explain rs.spec
[root@k8s-master ~]# kubectl explain rs.spec.template
==============================
(1)命令行查看ReplicaSet清单定义规则
[root@k8s-master ~]# kubectl explain rs
[root@k8s-master ~]# kubectl explain rs.spec
[root@k8s-master ~]# kubectl explain rs.spec.template
(2)新建ReplicaSet示例
[root@k8s-master ~]# vim rs-demo.yaml
apiVersion: apps/v1 #api版本定义
kind: ReplicaSet #定义资源类型为ReplicaSet
metadata: #元数据定义
name: myapp
namespace: default
spec: #ReplicaSet的规格定义
replicas: 2 #定义副本数量为2个
selector: #标签选择器,定义匹配pod的标签
matchLabels:
app: myapp
release: canary 这里很神奇,标签是几个,pod就是几个下面有实验
template: #pod的模板定义
metadata: #pod的元数据定义
name: myapp-pod #自定义pod的名称
labels: #定义pod的标签,需要和上面定义的标签一致,也可以多出其他标签
app: myapp
release: canary
environment: qa
spec: #pod的规格定义
containers: #容器定义
- name: myapp-container #容器名称
image: ikubernetes/myapp:v1 #容器镜像
ports: #暴露端口
- name: http
containerPort: 80
(3)创建ReplicaSet定义的pod
[root@k8s-master ~]# kubectl create -f rs-demo.yaml
[root@k8s-master ~]# kubectl get pods #获取pod信息
[root@k8s-master ~]# kubectl describe pods myapp-*** #查看pod详细信息
(4)标签( 这里很神奇,标签是几个,pod就是几个下面有实验)
[root@master ~]# kubectl get pods --show-labels 标签为app=myapp,release=canary的两个myapp
[root@master ~]# kubectl label pods readiness-httpget-pod release=canary
[root@master ~]# kubectl label pods readiness-httpget-pod app=myapp
[root@master ~]# kubectl get pods --show-labels 这里发现readiness-httpget-pod 他已经满足上面标签选择器的条件,上面myapp删除一个
生产中标签最好使用复杂条件,不然容易冲突了
这里实验完成,还是删除上面这个pod,会自动把myapp恢复
这里一个控制器创建两个pod,分别访问两个pod,但是当pod被删除的时候,会自动创建新的pod,这时候访问就不方便了,这就应该出现service,当然service使用和pod相同的标签选择器,访问的时候访问service就不会变了。
(5)修改pod的副本数量
[root@k8s-master ~]# kubectl edit rs myapp
replicas: 5
[root@k8s-master ~]# kubectl get rs -o wide
(6)修改pod的镜像版本
[root@k8s-master ~]# kubectl edit rs myapp
image: ikubernetes/myapp:v2
[root@k8s-master ~]# kubectl delete pods myapp-*** #修改了pod镜像版本,pod需要重建才能达到最新版本
[root@k8s-master ~]# kubectl create -f rs-demo.yaml
删一个删一个 这种是金丝雀发布
只删除一个 灰度
一次全部删除,这种小心哦
最妥当的方式创建两组rs,蓝绿发布
一个deployment可以管理多个rs
=========================================================
实验 https://www.cnblogs.com/linuxk/p/9578211.html 这个师兄整理的比较好
[root@master ~]# kubectl explain rs
KIND: ReplicaSet
VERSION: extensions/v1beta1
DESCRIPTION:
DEPRECATED - This group version of ReplicaSet is deprecated by
apps/v1beta2/ReplicaSet. See the release notes for more information.
ReplicaSet ensures that a specified number of pod replicas are running at
any given time.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
metadata <Object>
If the Labels of a ReplicaSet are empty, they are defaulted to be the same
as the Pod(s) that the ReplicaSet manages. Standard object's metadata. More
info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
spec <Object>
Spec defines the specification of the desired behavior of the ReplicaSet.
More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
status <Object>
Status is the most recently observed status of the ReplicaSet. This data
may be out of date by some window of time. Populated by the system.
Read-only. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
[root@master ~]# kubectl explain rs.spec
KIND: ReplicaSet
VERSION: extensions/v1beta1
RESOURCE: spec <Object>
DESCRIPTION:
Spec defines the specification of the desired behavior of the ReplicaSet.
More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
ReplicaSetSpec is the specification of a ReplicaSet.
FIELDS:
minReadySeconds <integer>
Minimum number of seconds for which a newly created pod should be ready
without any of its container crashing, for it to be considered available.
Defaults to 0 (pod will be considered available as soon as it is ready)
replicas <integer>
Replicas is the number of desired replicas. This is a pointer to
distinguish between explicit zero and unspecified. Defaults to 1. More
info:
https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
selector <Object>
Selector is a label query over pods that should match the replica count. If
the selector is empty, it is defaulted to the labels present on the pod
template. Label keys and values that must match in order to be controlled
by this replica set. More info:
https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
template <Object>
Template is the object that describes the pod that will be created if
insufficient replicas are detected. More info:
https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template
[root@master ~]# kubectl explain rs.spec.template
KIND: ReplicaSet
VERSION: extensions/v1beta1
RESOURCE: template <Object>
DESCRIPTION:
Template is the object that describes the pod that will be created if
insufficient replicas are detected. More info:
https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template
PodTemplateSpec describes the data a pod should have when created from a
template
FIELDS:
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
spec <Object>
Specification of the desired behavior of the pod. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
================================================
新建ReplicaSet示例
[root@master manifests]# vim rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
name: myapp-pod
labels:
app: myapp
release: canary
environment: qa
spec:
containers:
name: myapp-container
image: nginx
ports:
- name: http
containerPort: 80
[root@master manifests]# kubectl create -f rs-demo.yaml
replicaset.apps/myapp created
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3d1h
myapp-5xx8l 0/1 ContainerCreating 0 11s
myapp-dglps 1/1 Running 0 11s
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 6d23h
readiness-httpget-pod 1/1 Running 0 3d1h
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3d1h
myapp-5xx8l 1/1 Running 0 14s
myapp-dglps 1/1 Running 0 14s
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 6d23h
readiness-httpget-pod 1/1 Running 0 3d1h
[root@master manifests]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/liveness-httpget-pod 1/1 Running 1 3d1h
pod/myapp-5xx8l 1/1 Running 0 65s
pod/myapp-dglps 1/1 Running 0 65s
pod/nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h
pod/nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h
pod/nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h
pod/nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 6d23h
pod/readiness-httpget-pod 1/1 Running 0 3d1h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d
service/nginx-deploy ClusterIP 10.100.251.191 <none> 80/TCP 7d23h
[root@master manifests]# kubectl describe pod/myapp-5xx8l
Name: myapp-5xx8l
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: node01/10.249.6.101
Start Time: Fri, 08 Mar 2019 10:25:32 -0500
Labels: app=myapp
environment=qa
release=canary
Annotations: <none>
Status: Running
IP: 10.244.1.19
Controlled By: ReplicaSet/myapp
Containers:
myapp-container:
Container ID: docker://f14ee330ebb3c033cbf943489138af8be07c67acd4538c01d43ac8cce00225dc
Image: nginx
Image ID: docker-pullable://nginx@sha256:98efe605f61725fd817ea69521b0eeb32bef007af0e3d0aeb6258c6e6fe7fc1a
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 08 Mar 2019 10:25:44 -0500
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-6q28w (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-6q28w:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-6q28w
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 Scheduled 2m35s default-scheduler Successfully assigned default/myapp-5xx8l to node01
Normal Pulling 2m34s kubelet, node01 pulling image "nginx"
Normal Pulled 2m24s kubelet, node01 Successfully pulled image "nginx"
Normal Created 2m24s kubelet, node01 Created container
Normal Started 2m23s kubelet, node01 Started container
删除测试(这边删除的时候那边已经新的已经建起来了)
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3d1h
myapp-5xx8l 1/1 Running 0 5m13s
myapp-dglps 1/1 Running 0 5m13s
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 6d23h
readiness-httpget-pod 1/1 Running 0 3d1h
[root@master manifests]# kubectl delete pods myapp-5xx8l
pod "myapp-5xx8l" deleted
一边查看
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3d1h
myapp-5xx8l 0/1 Terminating 0 5m34s 这里还没完全删除
myapp-dglps 1/1 Running 0 5m34s
myapp-zn46b 1/1 Running 0 7s 这里果然新建了一个
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 6d23h
readiness-httpget-pod 1/1 Running 0 3d1h
神奇的标签选择器
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
liveness-httpget-pod 1/1 Running 1 3d1h <none>
myapp-dglps 1/1 Running 0 12m app=myapp,environment=qa,release=canary
myapp-zn46b 1/1 Running 0 6m53s app=myapp,environment=qa,release=canary
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 7d pod-template-hash=84cbfc56b6,release=canary,run=nginx-deploy
readiness-httpget-pod 1/1 Running 0 3d1h <none>
[root@master ~]# kubectl label pods readiness-httpget-pod release=canary
pod/readiness-httpget-pod labeled
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
liveness-httpget-pod 1/1 Running 1 3d1h <none>
myapp-dglps 1/1 Running 0 14m app=myapp,environment=qa,release=canary
myapp-zn46b 1/1 Running 0 9m3s app=myapp,environment=qa,release=canary
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 7d pod-template-hash=84cbfc56b6,release=canary,run=nginx-deploy
readiness-httpget-pod 1/1 Running 0 3d1h release=canary 有一个了,不满足,继续
[root@master ~]# kubectl label pods readiness-httpget-pod app=myapp
pod/readiness-httpget-pod labeled
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
liveness-httpget-pod 1/1 Running 1 3d1h <none>
myapp-dglps 1/1 Running 0 15m app=myapp,environment=qa,release=canary
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d22h pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 7d pod-template-hash=84cbfc56b6,release=canary,run=nginx-deploy
readiness-httpget-pod 1/1 Running 0 3d1h app=myapp,release=canary 满足了,发现上面的myapp pod少了一个,上面yaml定义的2,说明标签选择器生效
[root@master ~]# kubectl delete pods readiness-httpget-pod
pod "readiness-httpget-pod" deleted
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3d1h
myapp-4j4rp 1/1 Running 0 6s
myapp-dglps 1/1 Running 0 22m
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d23h
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d23h
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d23h
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 7d
================================
增加副本
[root@master manifests]# kubectl edit rs myapp
replicas: 2 修改为5
replicaset.extensions/myapp edited
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3d2h
myapp-4j4rp 1/1 Running 0 14m
myapp-7k2s5 0/1 ContainerCreating 0 3s
myapp-87tgz 0/1 ContainerCreating 0 3s
myapp-9sf9b 0/1 ContainerCreating 0 3s
myapp-dglps 1/1 Running 0 36m
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d23h
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d23h
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d23h
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 7d
================================
升级演示,我没有环境,我就把nginx换成apache试试 这种方法可用于升级版本,和之前命令一样的效果
[root@master manifests]# kubectl edit rs myapp
spec:
containers:
name: myapp-container
image: nginx 这里修改为apache
replicaset.extensions/myapp edited
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
liveness-httpget-pod 1/1 Running 1 3d2h
myapp-4j4rp 1/1 Running 0 16m
myapp-7k2s5 1/1 Running 0 2m36s
myapp-87tgz 1/1 Running 0 2m36s
myapp-9sf9b 1/1 Running 0 2m36s
myapp-dglps 1/1 Running 0 39m
nginx-7849c4bbcd-dscjr 1/1 Running 0 6d23h
nginx-7849c4bbcd-vdd45 1/1 Running 0 6d23h
nginx-7849c4bbcd-wrvks 1/1 Running 0 6d23h
nginx-deploy-84cbfc56b6-mjcw5 1/1 Running 0 7d
[root@master manifests]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp 5 5 5 39m myapp-container apache 这里已经是apache app=myapp,release=canary
nginx-775ff75bc8 0 0 0 6d23h nginx nginx pod-template-hash=775ff75bc8,run=nginx
nginx-7849c4bbcd 3 3 3 6d23h nginx nginx:1.14-alpine pod-template-hash=7849c4bbcd,run=nginx
nginx-deploy-84cbfc56b6 1 1 1 8d nginx-deploy nginx:1.14-alpine pod-template-hash=84cbfc56b6,run=nginx-deploy
这里只是rs更新了,但是实际pod并没有部署,需要删除老的,自动创建新的
[root@master manifests]# curl 10.244.1.21
<!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>
群名称:k8s学习群 群 号:153144292
更多推荐
所有评论(0)