RC

什么是RC:

Replication Controller(副本控制器),RC能够保证pod在任意时间运行的副本数量,能够保证pod总是可用的。
RC控制的pod的多个副本,每个副本都有独立的ip,并且支持pod副本数量的扩、缩容。
 

RC定义文件格式:

这里还是以nginx为例,认识最简单的rc配置文件。 每一行配置都有详细的解释

#api版本
apiVersion: v1
#对象资源类型 RC
kind: ReplicationController
#RC元数据
metadata:
#对象资源名称
name: nginx
#RC的详细描述
spec:
#维持pod的共数量
replicas: 3
#RC选择器,指定对哪个Pod使用rc
selector:
#label 标签,选择有此 label 的 Pod
app: nginx
# 定义创建 Pod 实例的模板
template:
metadata:
name: nginx
# Pod 的 label,对应上面 rc 的 selector
labels:
app: nginx
spec:
containers:
# 定义 Pod 中的容器
- name: nginx
image: nginx
ports:
- containerPort: 80

RC常用基本操作

创建rc,其中rc_demo.yml是上面rc的定义文件

[root@k8s-01 pod_demo]# kubectl create -f rc_demo.yml
replicationcontroller/nginx created

查询创建的rc以及rc对应的pod、container

[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 3 3 3 9m56s nginx nginx app=nginx

[root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx
nginx-bwpbx 1/1 Running 0 7m13s 10.244.2.33 k8s-03 <none> <none>
nginx-qgj22 1/1 Running 0 7m13s 10.244.1.56 k8s-02 <none> <none>
nginx-vpz8h 1/1 Running 0 7m13s 10.244.2.34 k8s-03 <none> <none>
[root@k8s-01 pod_demo]# kubectl describe pod nginx-bwpbx
Name: nginx-bwpbx

删除rc,下面两种删除rc的方式都可以

[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 3 3 3 9m56s nginx nginx app=nginx
[root@k8s-01 pod_demo]# kubectl delete rc nginx
replicationcontroller "nginx" deleted
[root@k8s-01 pod_demo]# kubectl get rc -o wide
No resources found.
[root@k8s-01 pod_demo]# kubectl delete -f rc_demo.yml

rc副本扩、缩容。将原始副本状态为3的rc扩容到4再缩容到2

[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 3 3 0 5s nginx nginx app=nginx
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 3 3 3 77s nginx nginx app=nginx
[root@k8s-01 pod_demo]# kubectl scale rc nginx --replicas=4
replicationcontroller/nginx scaled
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 4 4 3 2m9s nginx nginx app=nginx
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 4 4 4 2m43s nginx nginx app=nginx
[root@k8s-01 pod_demo]# kubectl scale rc nginx --replicas=2
replicationcontroller/nginx scaled
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 2 2 2 2m50s nginx nginx app=nginx

修改文件配置重新应用也能够修改副本数量

[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 2 2 2 4m56s nginx nginx app=nginx
[root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx
nginx-bp8rd 1/1 Running 0 5m7s 10.244.1.57 k8s-02 <none> <none>
nginx-k2r5j 1/1 Running 0 5m7s 10.244.2.35 k8s-03 <none> <none>
[root@k8s-01 pod_demo]# cat rc_demo.yml|grep replicas
replicas: 3
[root@k8s-01 pod_demo]# kubectl apply -f rc_demo.yml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
replicationcontroller/nginx configured
[root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx
nginx-bp8rd 1/1 Running 0 6m1s 10.244.1.57 k8s-02 <none> <none>
nginx-k2r5j 1/1 Running 0 6m1s 10.244.2.35 k8s-03 <none> <none>
nginx-lrlw2 1/1 Running 0 42s 10.244.2.37 k8s-03 <none> <none>
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 3 3 3 6m9s nginx nginx app=nginx

RS

RS(Replication Set)和RC的功能基本一致,目前唯一的一个区别就是RC只支持基于等式的selector(env=dev或environment!=qa),但RS还支持基于集合的selector(version in (v1.0, v2.0)),这对复杂的运维管理就非常方便了。Label后面的随笔会介绍,简单的说就是为资源打标签,然后通过标签查询,比如:一个 【长得帅】、【有钱】、【单身】的人。

使用上面RC的定义文件改造的RS定义文件

#api版本,注意这里和RC不一样
apiVersion: apps/v1
#对象资源类型 RC
kind: ReplicaSet
#RC元数据
metadata:
#对象资源名称
name: nginx-rs
#RC的详细描述
spec:
#维持pod的共数量
replicas: 3
#RS选择器,指定对哪个Pod
selector:
#相比RC,选择器这里多了一层逻辑,可以满足更复杂的选择器场景
matchLabels:
app: nginx
# 定义创建 Pod 实例的模板
template:
metadata:
name: nginx
# Pod 的 label,对应上面 rs 的 selector
labels:
app: nginx
spec:
containers:
# 定义 Pod 中的容器
- name: nginx
image: nginx
ports:
- containerPort: 80

RS常用操作命令

#创建、查询RS
[root@k8s-01 ~]# kubectl create -f RS_demo.yml
replicaset.apps/nginx-rs created
[root@k8s-01 ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-rs 3 3 3 55s nginx nginx app=nginx
#查询RS详细内容,包含其关联的pod副本详细信息
[root@k8s-01 ~]# kubectl describe rs nginx-rs
Name: nginx-rs
Namespace: default
Selector: app=nginx
Labels: <none>
Annotations: <none>
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 2m57s replicaset-controller Created pod: nginx-rs-l6cnq
Normal SuccessfulCreate 2m57s replicaset-controller Created pod: nginx-rs-vvqh7
Normal SuccessfulCreate 2m57s replicaset-controller Created pod: nginx-rs-9jmcj
#删除一个pod模拟pod故障RS自愈。
[root@k8s-01 ~]# kubectl delete pod nginx-rs-9jmcj
pod "nginx-rs-9jmcj" deleted
[root@k8s-01 ~]# kubectl describe rs nginx-rs|grep nginx-rs
Name: nginx-rs
Normal SuccessfulCreate 5m49s replicaset-controller Created pod: nginx-rs-l6cnq
Normal SuccessfulCreate 5m49s replicaset-controller Created pod: nginx-rs-vvqh7
Normal SuccessfulCreate 5m49s replicaset-controller Created pod: nginx-rs-9jmcj
Normal SuccessfulCreate 33s replicaset-controller Created pod: nginx-rs-9spdp
[root@k8s-01 ~]# kubectl get pod|grep nginx-rs
nginx-rs-9spdp 1/1 Running 0 59s
nginx-rs-l6cnq 1/1 Running 0 6m15s
nginx-rs-vvqh7 1/1 Running 0 6m15s
#RS的Pod副本扩缩容,两种方式:1、kubectl命令如下 2、修改文件kubectl apply -f yml
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
nginx-rs 3 3 3 8m32s nginx nginx app=nginx
[root@k8s-01 ~]# kubectl scale rs nginx-rs --replicas=4
replicaset.extensions/nginx-rs scaled
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
nginx-rs 4 4 3 9m10s nginx nginx app=nginx
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
nginx-rs 4 4 4 12m nginx nginx app=nginx
#删除rs
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
nginx-rs 4 4 4 12m nginx nginx app=nginx
[root@k8s-01 ~]# kubectl delete rs nginx-rs
replicaset.extensions "nginx-rs" deleted
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs

Deployment

相比于RS,Deployment增加了升级方式的定义,并且实际生产环境也多使用Deployment。

根据上面RS定义文件修改的Deployment定义文件

#api版本
apiVersion: apps/v1
#对象资源类型 Deployment
kind: Deployment
#元数据
metadata:
#对象资源名称
name: nginx-d
#deployment的labels
labels:
app: nginx
#RC的详细描述
spec:
#维持pod的共数量
replicas: 3
#RC选择器,指定对哪个Pod使用rc
selector:
#label 标签,选择有此 label的为app: nginx-p的Pod
matchLabels:
app: nginx
#定义deployment的升级策略
strategy:
#表示升级时是将源所有pod删除后使用新的template信息创建pod
type: Recreate
#滚动升级配置,升级时使用template新建一个pod然后将旧Pod挑选一个停服删除,持续滚动,直到所有pod更新完毕
#type: rollintUpdate
#maxSurge: 1
#maxUnavailable: 1
# 定义创建 Pod 实例的模板
template:
metadata:
name: nginx
# Pod 的 label,对应上面 rc 的 selector
labels:
app: nginx
spec:
containers:
# 定义 Pod 中的容器
- name: nginx
image: nginx:1.18
ports:
- containerPort: 80

deployment常用基本操作

#创建、查询deployment
[root@k8s-01 ~]# kubectl create -f deployment_demo.yml
deployment.apps/nginx-d created
[root@k8s-01 ~]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-d 3/3 3 3 4m23s nginx nginx:1.18 app=nginx
#kubectl replace -f filename,如果存在同名deployment则进行替换 #deployment详情查看其关联的RS
[root@k8s-01 ~]# kubectl describe deployment nginx-d
Name: nginx-d
Namespace: default
CreationTimestamp: Fri, 01 Oct 2021 22:04:26 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: Recreate
MinReadySeconds: 0
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.18
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-d-659bf7c684 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 8m16s deployment-controller Scaled up replica set nginx-d-659bf7c684 to 3 #查看deployment对应的pod信息
[root@k8s-01 ~]# kubectl get rs nginx-d-659bf7c684 -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-d-659bf7c684 3 3 3 10m nginx nginx:1.18 app=nginx,pod-template-hash=659bf7c684
[root@k8s-01 ~]# kubectl describe rs nginx-d-659bf7c684
Name: nginx-d-659bf7c684
Namespace: default
Selector: app=nginx,pod-template-hash=659bf7c684
Labels: app=nginx
pod-template-hash=659bf7c684
Annotations: deployment.kubernetes.io/desired-replicas: 3
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/nginx-d
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
pod-template-hash=659bf7c684
Containers:
nginx:
Image: nginx:1.18
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 10m replicaset-controller Created pod: nginx-d-659bf7c684-qj5gh
Normal SuccessfulCreate 10m replicaset-controller Created pod: nginx-d-659bf7c684-sqkjr
Normal SuccessfulCreate 10m replicaset-controller Created pod: nginx-d-659bf7c684-cxwsc
[root@k8s-01 ~]# #删除pod构造deployment自愈
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d
nginx-d-659bf7c684-cxwsc 1/1 Running 0 11m 10.244.2.46 k8s-03 <none> <none>
nginx-d-659bf7c684-qj5gh 1/1 Running 0 11m 10.244.2.45 k8s-03 <none> <none>
nginx-d-659bf7c684-sqkjr 1/1 Running 0 11m 10.244.1.66 k8s-02 <none> <none>
[root@k8s-01 ~]# kubectl delete pod nginx-d-659bf7c684-cxwsc
pod "nginx-d-659bf7c684-cxwsc" deleted
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d
nginx-d-659bf7c684-qj5gh 1/1 Running 0 14m 10.244.2.45 k8s-03 <none> <none>
nginx-d-659bf7c684-sqkjr 1/1 Running 0 14m 10.244.1.66 k8s-02 <none> <none>
nginx-d-659bf7c684-z4jjz 1/1 Running 0 119s 10.244.2.47 k8s-03 <none> <none> #pod副本数量扩、缩容。还是有两种方法,这里只演示命令行方式
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d
nginx-d-659bf7c684-qj5gh 1/1 Running 0 14m 10.244.2.45 k8s-03 <none> <none>
nginx-d-659bf7c684-sqkjr 1/1 Running 0 14m 10.244.1.66 k8s-02 <none> <none>
nginx-d-659bf7c684-z4jjz 1/1 Running 0 119s 10.244.2.47 k8s-03 <none> <none>
[root@k8s-01 ~]#
[root@k8s-01 ~]# kubectl scale deployment nginx-d --replicas=4
deployment.extensions/nginx-d scaled
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d
nginx-d-659bf7c684-d6qhr 1/1 Running 0 18s 10.244.1.67 k8s-02 <none> <none>
nginx-d-659bf7c684-qj5gh 1/1 Running 0 16m 10.244.2.45 k8s-03 <none> <none>
nginx-d-659bf7c684-sqkjr 1/1 Running 0 16m 10.244.1.66 k8s-02 <none> <none>
nginx-d-659bf7c684-z4jjz 1/1 Running 0 4m16s 10.244.2.47 k8s-03 <none> <none> #image版本升降级。升降级也有两种方式,最好通过方式2更新。将镜像从1.1.8升级到latest版本
#方式1:kubectl set image deployment [dName] [imageNmae]=[新的镜像]
#方式2:kubectl apply -f [deployment 新文件] --record
[root@k8s-01 ~]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-d 4/4 4 4 21m nginx nginx:1.18 app=nginx
[root@k8s-01 ~]# kubectl apply -f deployment_demo.yml --record
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/nginx-d configured
[root@k8s-01 ~]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-d 3/3 3 3 22m nginx nginx app=nginx #查看deployment的升级记录
[root@k8s-01 ~]# kubectl rollout history deployment nginx-d
deployment.extensions/nginx-d
REVISION CHANGE-CAUSE
1 <none>
2 kubectl apply --filename=deployment_demo.yml --record=true #升级回滚,回滚到上一个版本,nginx有变回1.18版本
[root@k8s-01 ~]# kubectl rollout undo deployment nginx-d
deployment.extensions/nginx-d rolled back
[root@k8s-01 ~]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-d 3/3 3 3 25m nginx nginx:1.18 app=nginx

k8s入门_RC、RS、Deployment的更多相关文章

  1. 3.k8s资源控制器rs Deployment Job

    k8s资源控制器 #控制器类型 ReplicaSet #rs,确保pod副本数,rs已替代rc Deployment #管理rs,升级.回滚.扩容pod DaemonSet #在每个节点运行一个Pod ...

  2. K8s 入门

    中文文档:https://www.kubernetes.org.cn/kubernetes%E8%AE%BE%E8%AE%A1%E6%9E%B6%E6%9E%84 小结大白话 Portainer 挺好 ...

  3. 反手来个K8S入门到跑路

    layout: post title: 反手来个K8S入门到跑路 category: linux date: 2019-06-09 tags: linux k8s 反手来个K8S入门到跑路 前言 放假 ...

  4. k8s入门你至少需要会哪些

    body { margin: 0; overflow: auto; font: normal 14px Verdana; background: rgba(255, 255, 255, 1); pad ...

  5. k8s工作负载资源之deployment

    首先我们要理解:一个应用跑在k8s集群上了,那么这个应用就是一个工作负载(workloads). 在k8s中会用pod的来承载这个应用,那么负责管理这个pod的东西就叫工作负载资源(workload ...

  6. k8s入门系列之guestbook快速部署

    k8s集群以及一些扩展插件已经安装完毕,本篇文章介绍一下如何在k8s集群上快速部署guestbook应用. •实验环境为集群:master(1)+node(4),详细内容参考<k8s入门系列之集 ...

  7. k8s入门系列之扩展组件(一)DNS安装篇

    DNS (domain name system),提供域名解析服务,解决了难于记忆的IP地址问题,以更人性可读可记忆可标识的方式映射对应IP地址. Cluster DNS扩展插件用于支持k8s集群系统 ...

  8. k8s入门系列之集群安装篇

    关于kubernetes组件的详解介绍,请阅读上一篇文章<k8s入门系列之介绍篇> Kubernetes集群安装部署 •Kubernetes集群组件: - etcd 一个高可用的K/V键值 ...

  9. k8s 入门系列之集群安装篇

    关于kubernetes组件的详解介绍,请阅读上一篇文章<k8s入门系列之介绍篇> Kubernetes集群安装部署 •Kubernetes集群组件: - etcd 一个高可用的K/V键值 ...

  10. k8s入门之集群搭建(二)

    一.准备三台节点 从上篇文章 k8s入门之基础环境准备(一)安装的Ubuntu虚拟机克隆出三台虚拟机,如图所示 启动这三台虚拟机节点,分别做如下配置 虚拟机名称 IP HostName k8sMast ...

随机推荐

  1. LeetCode-1606 找到处理请求最多的服务器

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/find-servers-that-handled-most-number-of-requests ...

  2. 【5】java之日期处理类

    一.Date 类 ​ 在 Java 里提供有一个 java.util.Date 类,可以直接获取当前的日期时间. 范例:获取当前的日期时间 public class TestDemo { public ...

  3. windows-sam文件

    sam文件是账号密码的数据库文件 存放位置C:\Windows\System32\Config\sam

  4. CMT: Convolutional Neural Networks Meet Vision Transformers概述

    0.前言 相关资料: arxiv github 论文解读(CSDN,CSDN) 论文基本信息: 作者单位:华为诺亚, 悉尼大学 发表时间:CVPR2022(2021.7.13) 1.针对的问题 当前将 ...

  5. Eclipse git提交代码 覆盖问题

    创建本地分支 拉取远程分支到本地 进行代码更新 然后 commit  然后将本地代码推送到远程分支 再发起合入请求 使用elipse 提交合入代码时, 注意push 提交远程分支时,  选择中  要选 ...

  6. Java-获取真实用户ip

    1 import common.util.StringUtil; 2 import org.springframework.web.bind.annotation.ModelAttribute; 3 ...

  7. Linux学习 --- 系统网络配置

    前言 : 如果一台计算机想接入互联网,必须配置好IP地址,子网掩码,网关,DNS服务器.在Linux系统中,这些信息都可以修改对应的配置文件来进行配置.临时配置一下网络可以使用一些简单的命令来进行配置 ...

  8. 南大ics-pa/PA1.1过程及感想

    1.1 一.在红白模拟器上运行超级马里奥游戏 1.将游戏rom文件mario.nes移至~/ics2022/fceux-am/nes/rom文件下,并回到~/ics2022/fceux-am下执行ma ...

  9. 关于nginx隐藏index.php入口文件注意事项

    最近项目用的是nginx服务,apache怎么隐藏入口不赘述,官方文档有介绍,Nginx隐藏官方文档这样说的: 实践中,这样是不对的,应该在网站对应的vhost.conf中配置: server { l ...

  10. 使用Android studio配置软件签名,并在车机安装

    系统级APP,可在Androidmanifest.xml中配置属性,并使用系统级签名. 1. 签名方式 1.1 bulid.gradle(:app)中添加签名信息 1.2 使用Android stud ...