当前位置:网站首页>【云原生 | Kubernetes篇】深入RC、RS、DaemonSet、StatefulSet(七)
【云原生 | Kubernetes篇】深入RC、RS、DaemonSet、StatefulSet(七)
2022-06-10 14:13:00 【Lanson】
深入RC、RS、DaemonSet、StatefulSet
一、RC、RS
RC (ReplicationController )主要的作用就是用来确保容器应用的副本数始终保持在用户定义的副本数 。即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收
Kubernetes 官方建议使用 RS(ReplicaSet ) 替代 RC (ReplicationController ) 进行部署,RS 跟 RC 没有本质的不同,只是名字不一样,并且 RS 支持集合式的 selector
RC 控制器
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: php-redis
image: lansonlinux/myapp:v1
env:
- name: GET_HOSTS_FROM
value: dns
name: zhangsan
value: "123"
ports:
- containerPort: 80RS控制器
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: myapp
image: lansonlinux/myapp:v1
env:
- name: GET_HOSTS_FROM
value: dns
ports:
- containerPort: 80二、DaemonSet
DaemonSet 控制器确保所有(或一部分)的节点都运行了一个指定的 Pod 副本。
- 每当向集群中添加一个节点时,指定的 Pod 副本也将添加到该节点上
- 当节点从集群中移除时,Pod 也就被垃圾回收了
- 删除一个 DaemonSet 可以清理所有由其创建的 Pod
DaemonSet 的典型使用场景有:
- 在每个节点上运行集群的存储守护进程,例如 glusterd、ceph
- 在每个节点上运行日志收集守护进程,例如 fluentd、logstash
- 在每个节点上运行监控守护进程,例如 Prometheus Node Exporter、Sysdig Agent、collectd、Dynatrace OneAgent、APPDynamics Agent、Datadog agent、New Relic agent、Ganglia gmond、Instana Agent 等
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: logging
labels:
app: logging
spec:
selector:
matchLabels:
name: logging
template:
metadata:
labels:
name: logging
spec:
containers:
- name: logging
image: nginx
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
tolerations: #设置容忍master的污点
- key: node-role.kubernetes.io/master
effect: NoSchedule查看效果
kubectl get pod -l name=logging -o wide三、StatefulSet
有状态副本集;Deployment等属于无状态的应用部署(stateless)
- StatefulSet 使用场景;对于有如下要求的应用程序,StatefulSet 非常适用:
- 稳定、唯一的网络标识(dnsname)
- StatefulSet通过与其相关的无头服务为每个pod提供DNS解析条目。假如无头服务的DNS条目为: "$(service name).$(namespace).svc.cluster.local", 那么pod的解析条目就是"$(pod name).$(service name).$(namespace).svc.cluster.local",每个pod name也是唯一的。
- 稳定的、持久的存储;【每个Pod始终对应各自的存储路径(PersistantVolumeClaimTemplate)】
- 有序的、优雅的部署和缩放。【按顺序地增加副本、减少副本,并在减少副本时执行清理】
- 有序的、自动的滚动更新。【按顺序自动地执行滚动更新】
- 稳定、唯一的网络标识(dnsname)
- 限制
- 给定 Pod 的存储必须由PersistentVolume 驱动基于所请求的
storage class来提供,或者由管理员预先提供。 - 删除或者收缩 StatefulSet 并不会删除它关联的存储卷。 这样做是为了保证数据安全,它通常比自动清除 StatefulSet 所有相关的资源更有价值。
- StatefulSet 当前需要无头服务来负责 Pod 的网络标识。你需要负责创建此服务。
- 当删除 StatefulSets 时,StatefulSet 不提供任何终止 Pod 的保证。 为了实现 StatefulSet 中的 Pod 可以有序地且体面地终止,可以在删除之前将 StatefulSet 缩放为 0。
- 在默认Pod 管理策略(
OrderedReady) 时使用 滚动更新,可能进入需要人工干预才能修复的损坏状态。
- 给定 Pod 的存储必须由PersistentVolume 驱动基于所请求的
如果一个应用程序不需要稳定的网络标识,或者不需要按顺序部署、删除、增加副本,就应该考虑使用 Deployment 这类无状态(stateless)的控制器
apiVersion: v1 kind: Service #定义一个负载均衡网络 metadata: name: stateful-tomcat labels: app: stateful-tomcat spec: ports: - port: 8123 name: web targetPort: 8080 clusterIP: None #NodePort:任意机器+NodePort都能访问,ClusterIP:集群内能用这个ip、service域名能访问,clusterIP: None;不要分配集群ip。headless;无头服务。稳定的域名 selector: app: stateful-tomcat --- apiVersion: apps/v1 kind: StatefulSet #控制器。 metadata: name: stateful-tomcat spec: selector: matchLabels: app: stateful-tomcat # has to match .spec.template.metadata.labels serviceName: "stateful-tomcat" #这里一定注意,必须提前有个service名字叫这个的 replicas: 3 # by default is 1 template: metadata: labels: app: stateful-tomcat # has to match .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers: - name: tomcat image: tomcat:7 ports: - containerPort: 8080 name: web #观察效果。 删除一个,重启后名字,ip等都是一样的。保证了状态 #细节 kubectl explain StatefulSet.spec podManagementPolicy: OrderedReady(按序)、Parallel(并发) serviceName -required- 设置服务名,就可以用域名访问pod了。 pod-specific-string.serviceName.default.svc.cluster.local #测试 kubectl run -i --tty --image busybox dns-test --restart=Never --rm /bin/sh ping stateful-tomcat-0.stateful-tomcat #我们在这里没有加存储卷。如果有的话 kubectl get pvc -l app=stateful-tomcat 我们就能看到即使Pod删了再拉起,卷还是同样的。
边栏推荐
- 初学者自己搭建博客的设计工具和技巧
- net core天马行空系列-可用于依赖注入的,数据库表和c#实体类互相转换的接口实现
- [technical analysis] discuss the production process and technology of big world games - preliminary process
- C#多线程学习笔记二
- Flutter Icon Stack LIsttitle... Learning summary 3
- What can the graph of training and verification indicators tell us in machine learning?
- 2022山东省安全员C证复训题库及在线模拟考试
- 为doc2vec生成训练向量的数据集
- SIGIR 2022 | HKU and Wuhan University put forward kgcl: a recommendation system based on knowledge map comparative learning
- 【Vue/js】通过localStorage浏览器实现变量和对象的本地缓存(图文+完整源代码)
猜你喜欢
随机推荐
[solution] each time the trained model is loaded, the generated vector will be different
Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
C multithreading learning note 4
1
What is CAS and ABA in CAS
[note] the environment for setting up get injectedthread script supplemented by shellcode in Windows Security III and its use
Still saying that university rankings are a joke? The latest regulation: Top50 universities in the world can be directly settled in Shanghai!
Use of 5.8G microwave radar module, working principle and introduction of 5.8G microwave radar module
Ue5 Comment convertir les coordonnées de l'écran en coordonnées du monde et en direction du monde
C#多线程学习笔记二
【C语言】指针函数与函数指针、数组函数
How to build Haojing technology when the computing power network is brought into reality?
大厂必备的40个方法论
CentOS Linux 已死!Oracle Linux 可能是它的更好替代品
【离散数学期复习系列】一、命题逻辑
【Vue/js】通过localStorage浏览器实现变量和对象的本地缓存(图文+完整源代码)
Mutual transformation among lists, arrays and tensors
Resolve the error reported when installing gerapy: error: cannot uninstall 'certificate' It is a distutils installed project...
Celery 异步调用方法改动记录
Brief description of adaptive function






![[solution] each time the trained model is loaded, the generated vector will be different](/img/30/92ddcda69832df2bd5e335fc3b4fdd.png)


