RS

Replication Controller(简称rc)用来管理Pod的副本,保证集群中存在指定数量的Pod副本。集群中副本的数量大于指定数量,则会停止指定数量之外的多余容器数量,反之,则会启动少于指定数量个数的容器,保证数量不变。Replication Controller是实现弹性伸缩、动态扩容和滚动升级的核心。

Kubernetes 官方建议使用 RS(ReplicaSet )替代 RC (ReplicationController )进行部署,RS 跟 RC 没有本质的不同,只是名字不一样,并且 RS 支持集合式的 selector。

# vim nginx.yaml
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: nginx
spec:
  replicas: 5   # 副本数
  selector:
    matchLabels:
      app: myweb  # 标签
  template:     # 副本模版
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: harbor.ui.com/library/nginx:1.13
        ports:
        - containerPort: 80

伸缩扩容

# kubectl scale rs nginx --replicas=4

更改镜像

方法一
# kubectl set image rs nginx nginx=harbor.ui.com/library/nginx:1.13
方法二
# vim nginx.yaml
        image: harbor.ui.com/library/nginx:1.13 => image: harbor.ui.com/library/nginx:1.15
# kubectl replace --force -f nginx.yaml  更新配置
# kubectl delete pod --all  删除pod之后重新创建新的pod

RS与Deployment的关联

Deployment

Deployment为Pod和ReplicaSet(下一代Replication Controller)提供声明式更新

# vim nginx-deploy.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: harbor.ui.com/library/nginx:1.13
        ports:
        - containerPort: 80

创建

–record 创建时备注信息 创建deploy时推荐,可以查看版本历史

# kubectl create -f nginx-deploy.yml --record

查看pod资源

# kubectl get pod

扩展式查看

# kubectl get pod -o wide

详细查看信息

#  kubectl describe deploy nginx-deployment -n kube-public

删除pod(重启)

强制删除参数:–force –grace-period=0

# kubectl delete pod nginx-deployment-5dfc689474-5b76h
pod "nginx-deployment-5dfc689474-5b76h" deleted
# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-deployment-5dfc689474-c7kmt 1/1 Running 0 13s

扩容

# kubectl scale deployment nginx-deployment --replicas 10

修改资源配置清单

在线修改

# kubectl edit deploy nginx-deployment
spec:
containers:
- image: harbor.ui.com/library/nginx:1.15

离线修改

# vim nginx-deploy.yml
spec:
containers:
- image: harbor.ui.com/library/nginx:1.15
# kubectl apply -f nginx-deploy.yml

暂停更新

# kubectl rollout pause deploy/nginx-deployment

回滚

# kubectl rollout undo deploy/nginx-deployment

查看历史版本

# kubectl rollout history deployment nginx-deployment
deployment.extensions/nginx-deployment
REVISION  CHANGE-CAUSE
2         kubectl apply --filename=nginx-deploy.yml --record=true
3         kubectl apply --filename=nginx-deploy.yml --record=true

回滚上一个版本

# kubectl rollout undo deployment nginx-deployment

回滚指定版本

# kubectl rollout undo deployment nginx-deployment --to-revision=1

查看pod状态

# kubectl rollout status deploy/nginx-deployment

删除deploy资源

陈述式
# kubectl delete -f nginx-deploy.yml
声明式
# kubectl delete delpoy nginx-deployment