目录
一、ReplicationController 和 ReplicaSet
一、ReplicationController 和 ReplicaSet
RC (ReplicationController )主要的作用就是用来确保容器 应用的副本数始终保持在用户定义的副本数 。即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收。
Kubernetes 官方建议使用 RS(ReplicaSet ) 替代 RC (ReplicationController ) 进行部署,RS 跟 RC 没有本质的不同,只是名字不一样,并且 RS 支持集合式的 selector。
1.RC控制器
apiVersion: v1 #在核心组v1版本进行定义的
kind: ReplicationController #资源类别用全称描绘,可通过kubectl explain rc命令获得信息
metadata: #元数据
name: frontend #名字,forntend前端
spec: #期望
replicas: 3 #当前的副本数量3个
selector: #选择器
app: nginx #只要符合app:nginx这个标签的pod都是被我管理的。必须是下面labels标签的子集
template: #模板,告诉前面的3个副本怎么创建。(可以将pod资源清单照搬过来)
metadata: #元数据
labels: #标签
app: nginx #标签的内容
spec: #期望
containers: #mainC
- name: php-redis #当前mainC的名字叫php-redis
image: wangyanglinux/myapp:v1 #用的镜像时myapp:v1
env: #环境变量,在当前容器中注入了两个环境变量
- name: GET_HOSTS_FROM #环境变量的名字是GET_HOSTS_FROM
value: dns #value:dns
name: zhangsan #还有个环境变量叫zhangsan
value: "123" #值是123
ports:
- containerPort: 80 #端口是80
livenessProbe: #存活探测
httpGet: #基于httpGet方案检测
port: 80 #检测的端口是80
path: /index.html #检测的页面是index.html页面
initialDelaySeconds: 1 #延时的时间是1秒
periodSeconds: 3 #间隔3秒
timeoutSeconds: 3 #检测的超时时间3秒
kubectl apply -f rc.yaml
kubectl get pod
2.RS控制器
kubectl explain rs.spec.selector
01.matchExpressions 匹配运算符
In:label 的值在某个列表中
NotIn:label 的值不在某个列表中
Exists:某个 label 存在
DoesNotExist:某个 label 不存在
apiVersion: apps/v1 #apps组的v1版
kind: ReplicaSet #apps类型ReplicaSet
metadata:
name: rs-demo
spec:
selector:
matchExpressions: #匹配运算符
- key: app #匹配key是app的标签
operator: Exists #运算符是存在
template:
metadata:
labels:
app: spring-k8s #匹配运算符将匹配