Deployment 是一个高级 API 对象,底层由replicaset实现,虽然replicaset可以独立使用,但如今,主要由Deployments用作协调Pod创建,删除和更新的机制。因此,建议你在需要副本集时使用deployment。
# 查看 replicaset 的定义
kubectl explain replicaset
# 查看 spec 的定义
kubectl explain replicaset.spec
# 查看 template 的定义
kubectl explain replicaset.spec.template
# 查看 template.spec 的定义
kubectl explain replicaset.spec.template.spec
[root@master1 test]# kubectl explain replicaset
KIND: ReplicaSet
VERSION: apps/v1
DESCRIPTION:
ReplicaSet ensures that a specified number of pod replicas are running at
any given time.
FIELDS:
apiVersion
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/sig-architecture/api-conventions.md#resources
kind
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/sig-architecture/api-conventions.md#types-kinds
metadata
# 定义
kubectl explain replicaset.spec.template
# 定义
kubectl explain replicaset.spec.template.spec.restartPolicy
# 定义
kubectl explain replicaset.spec.selector
# 定义
kubectl explain replicaset.spec.replicas
replicaSet使用 selector(选择器)中的定义,查找对应的Pod模板,通过pod模板,创建或删除Pod,下面就是持有三个 Pod对象的 Replica 拓扑图:
被 ReplicaSet 持有的Pod有一个metadata.ownerReferences指针,该指针指向当前的 ReplicaSet,表示当前Pod的所有者,这个引用主要会被集群中的垃圾收集器使用以清理失去所有者的Pod对象。
vim /root/test/replicaset_nginx.yaml
内容
apiVersion: apps/v1
kind: ReplicaSet
metadata:
# ReplicaSet 的名称
name: replicaset-nginx
# ReplicaSet 的标签
labels:
k1: k1_la
k2: k2_la
spec:
# 副本数目
replicas: 3
# 选择器
selector:
# 选择器匹配的标签
matchLabels:
nginx_pod: nginx_pod_la
# Pod 模板
template:
metadata:
# Pod的标签
labels:
nginx_pod: nginx_pod_la
spec:
containers:
# 容器名称
- name: nginx
# 镜像
image: nginx
# 镜像策略
imagePullPolicy: IfNotPresent
# 容器端口
ports:
- containerPort: 80
kubectl apply -f /root/test/replicaset_nginx.yaml
kubectl get replicaset
# rs是replicaset的缩写
# kubectl get rs
kubectl get pods
kubectl get pods | grep replicaset-nginx
kubectl delete pods replicaset-nginx-rtrgg
kubectl get pods | grep replicaset-nginx
kubectl delete -f /root/test/replicaset_nginx.yaml
kubectl get pods | grep replicaset-nginx