K8S 副本控制器ReplicaSet ReplicationController

零、介绍

ReplicationController和ReplicaSet介绍
  RC(ReplicationController)主要的作用就是用来确保容器应用的副本数始终保持在用户定义的副本数。即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收Kubernetes
  官方建议使用RS(Replicaset)替代RC(ReplicationController)进行部署,RS跟RC没有本质的不同,只是名字不一样,并且RS支持集合式的 selector 

一、ReplicationController 

1.1  准备rc-nginx.yml

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-rc
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.12.2
        ports:
        - containerPort: 80

 

1.2 查看资源 扩容

kubectl create -f rc-nginx.yml

kubectl get pods -o wide

kubectl get rc

kubectl scale rc nginx-rc --replicas=4

 

 

 

二、ReplicaSet 

2.1  准备rs-nginx.yml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-rs
  labels:
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      name: nginx
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

 

2.2 查看资源 扩容

kubectl create -f rs-nginx.yml

kubectl get pods -o wide

kubectl get rs

kubectl scale rs nginx-rs --replicas=4

 

 

三、Deployments

2.1  准备deployments-nginx.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.12.2
        ports:
        - containerPort: 80

  

3.2 查看资源 扩容

kubectl create -f deployment_nginx.yml

kubectl get pods -o wide

kubectl get deployment -o wide

# 修改镜像版本
kubectl set image deployment nginx-deployment nginx=nginx:1.13 

# 查看版本历史
kubectl rollout history deployment nginx-deployment

# 回滚到指定版本
kubectl rollout undo deployment nginx-deployment

# 暴露端口
kubectl expose deployment nginx-deployment --type=NodePort

#
kubectl get svc

 

posted @ 2021-10-04 20:56  year12  阅读(154)  评论(0编辑  收藏  举报