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. 干货!超实用的 Linux 初始化脚本

    咸鱼今天给大家分享一个无论是学习还是工作中都很实用的 Linux 系统初始化脚本,其实就是各种命令的集合 完整代码在文章最后哦 定义相关变量   配置 yum 镜像源 获取阿里云 yum 镜像源 判断 ...

  2. oracle 设置用户永不过期

    一.查看用户的proifle是哪个,一般是default SELECT username,PROFILE FROM dba_users; 二.查看指定概要文件(如default)的密码有效期 SELE ...

  3. ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)

    ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061) 报错原因:电脑之前有个5.0.2版本的mys ...

  4. MySQL查询 根据时间字段的值的时分秒进行查询

    例如,查询小于等于10:30的时间 SELECT * FROM  表名WHERE DATE_FORMAT(TranslateOverTime,'%H') <= 10 AND DATE_FORMA ...

  5. vue 一次显示多张图片的轮播图

    1. npm i vue-slick-carousel<template> <div> <div class="activities "> &l ...

  6. RabbitMQ的安装(linux版)

    原文地址:https://blog.csdn.net/jiguquan3839/article/details/91346261 注意:在web管理页面登录提示"User can only ...

  7. 二进制安装docker-20.10.9

    二进制包下载地址:https://download.docker.com/linux/static/stable/x86_64/  #解压tar xf docker-20.10.9.tgz #拷贝二进 ...

  8. csv文件导入数据库中文乱码

    在向数据库的表中导入csv数据时,出现了中文乱码的问题,解决办法是在选择编码格式时选择10008 (MAC - Simplified Chinese GB 2312)即可

  9. mysql正则替换 正宗!

    先看个官方例子 mysql> SELECT REGEXP_REPLACE('a b c', 'b', 'X'); +-----------------------------------+ | ...

  10. Spring中最常用的11个扩展点

    转载自: 微信公众号 [Java后端技术] 前言 我们一说到spring,可能第一个想到的是 IOC(控制反转) 和 AOP(面向切面编程). 没错,它们是spring的基石,得益于它们的优秀设计,使 ...