赞
踩
[root@master ~]# cat rs.yaml apiVersion: "v1" kind: Namespace metadata: name: nginx-latest --- apiVersion: "apps/v1" kind: ReplicaSet metadata: name: frontend namespace: nginx-latest labels: app: guestbook tier: frontend spec: # 按照你的实际情况修改副本数 replicas: 3 # 定义标签选择:表示这个 ReplicaSet 将管理标签带有 tier=frontend 的Pod # 可以通过 kubectl get pod -A l tier=frontend 查看带有此标签的Pod selector: matchLabels: tier: frontend # 定义容器模板 template: metadata: labels: tier: frontend spec: # 只要容器退出就自动重启 restartPolicy: Always containers: - name: nginx-80 image: nginx:latest # 定义拉取策略 imagePullPolicy: IfNotPresent
rs.yaml
中,并将其提交到kubernetes集群,就能创建yaml文件所定义的ReplicaSet及其管理的Pod[root@master ~]# kubectl apply -f rs.yaml
[root@master ~]# kubectl get rs -n nginx-latest
NAME DESIRED CURRENT READY AGE
frontend 3 3 3 65s
ReplicaSet
的详细信息[root@master ~]# kubectl describe rs -n nginx-latest Name: frontend Namespace: nginx-latest Selector: tier=frontend Labels: app=guestbook tier=frontend Annotations: <none> Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: tier=frontend Containers: nginx-80: Image: nginx:latest Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 4m42s replicaset-controller Created pod: frontend-qb825 Normal SuccessfulCreate 4m42s replicaset-controller Created pod: frontend-vb77t Normal SuccessfulCreate 4m42s replicaset-controller Created pod: frontend-ww8g9
[root@master ~]# kubectl get pod -n nginx-latest
NAME READY STATUS RESTARTS AGE
frontend-qb825 1/1 Running 0 5m27s
frontend-vb77t 1/1 Running 0 5m27s
frontend-ww8g9 1/1 Running 0 5m27s
ownerReferences
字段中:[root@master ~]# kubectl get pod frontend-qb825 -n nginx-latest -o yaml apiVersion: v1 kind: Pod metadata: creationTimestamp: "2024-07-25T08:39:51Z" generateName: frontend- labels: tier: frontend name: frontend-qb825 namespace: nginx-latest ################################################################ ownerReferences: - apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: ReplicaSet name: frontend uid: c0f64a9f-9032-470b-840e-17c93c392d3d ################################################################ resourceVersion: "5931" uid: 2897d78c-1646-401f-bdc0-c5b146028645 spec: containers: - image: nginx:latest imagePullPolicy: IfNotPresent name: nginx-80 resources: {} ...
尽管你完全可以直接创建裸的Pod,强烈建议你确保这些裸的Pod并不包含可能与你的某个ReplicaSet的选择算符匹配的标签。原因在于ReplicaSet并不仅限于拥有在其模板中设置的Pod,只要创建的裸Pod的标签和ReplicaSet标签一样,那么这个裸Pod将由ReplicaSet管理。
在以下清单中指定这些裸Pod:
[root@master ~]# cat pod.yaml apiVersion: "v1" kind: Pod metadata: name: pod1 namespace: nginx-latest labels: tier: frontend spec: containers: - name: hello1 image: nginx # 容器十分钟后自动退出 command: ["/bin/sh","-c","sleep 600"] --- apiVersion: "v1" kind: Pod metadata: name: pod2 namespace: nginx-latest labels: tier: frontend spec: containers: - name: hello2 image: nginx # 容器十分钟后自动退出 command: ["/bin/sh","-c","sleep 600"]
[root@master ~]# kubectl apply -f pod.yaml
[root@master ~]# kubectl get pod -n nginx-latest
NAME READY STATUS RESTARTS AGE
frontend-qb825 1/1 Running 0 24m
frontend-vb77t 1/1 Running 0 24m
frontend-ww8g9 1/1 Running 0 24m
pod1 1/1 Terminating 0 35s
pod2 1/1 Terminating 0 35s
# 先把之前创建的 ReplicaSet删除
# 提示:因为pod的标签和ReplicaSet的标签一样,所以删除ReplicaSet就将会删除Pod
[root@master ~]# kubectl delete -f rs.yaml
# Pod需要在顶部额外添加名称空间
[root@master ~]# cat pod.yaml
apiVersion: "v1"
kind: Namespace
metadata:
name: nginx-latest
[root@master ~]# kubectl apply -f pod.yaml
[root@master ~]# kubectl apply -f rs.yaml
[root@master ~]# kubectl get pod -n nginx-latest
NAME READY STATUS RESTARTS AGE
frontend-zrj69 1/1 Running 0 67s
pod1 1/1 Running 0 94s
pod2 1/1 Running 0 94s
apiVersion
、kind
、和metadata
字段。对于ReplicaSet而言,其kind始终是Replicasetkind: ReplicaSet
.metadata.name
是命名这些Pod的部分基础。ReplicaSet的名称必须是一个合法的DNS 子域值
,但这可能对Pod的主机名产生以外的结果。为获得最佳兼容性,名称应遵循更严格的DNS 标签规则。.spec
部分metadata:
name: frontend
.spec.template
是一个Pod模板,要求设置标签。在rs.yaml
示例中,我们指定了标签tier: frontend
。注意不要将标签与其他控制器的选择标签重叠,否则那些控制器会尝试收养此Pod。template:
metadata:
labels:
tier: frontend
.spec.template.spec.restartPolicy
,唯一允许的取值是Always
,这也是默认值。spec:
template:
spec:
imagePullPolicy: IfNotPresent
.spec.selector
字段是一个标签选择算符。如前文中所讨论的,这些是用来标识要被获取的Pod的标签。在签名的rs.yaml
示例中,选择算符为:.spec.template.metadata.labels
的值必须与spec.selector
值相匹配,是否该配置会被API拒绝matchLabels:
tier: frontend
metadata:
labels:
tier: frontend
.spec.replicas
来指定要同时运行的Pod的个数。.spec.replicas
,那么默认值为1,也就是容器1个Podspec:
# 按照你的实际情况修改副本数
replicas: 3
[root@master ~]# kubectl delete -f rs.yaml
kubectl delete
命令并设置--cascade=orphan
选项[root@master ~]# kubectl delete rs frontend --cascade=orphan -n nginx-latest
[root@master ~]# kubectl get rs -n nginx-latest
[root@master ~]# kubectl get pod -n nginx-latest
NAME READY STATUS RESTARTS AGE
frontend-582rl 1/1 Running 0 3m22s
frontend-rqw86 1/1 Running 0 3m22s
frontend-xxcnh 1/1 Running 0 3m22s
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。