k8s入门_RC、RS、Deployment
RC
什么是RC:
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的更多相关文章
- 3.k8s资源控制器rs Deployment Job
k8s资源控制器 #控制器类型 ReplicaSet #rs,确保pod副本数,rs已替代rc Deployment #管理rs,升级.回滚.扩容pod DaemonSet #在每个节点运行一个Pod ...
- K8s 入门
中文文档:https://www.kubernetes.org.cn/kubernetes%E8%AE%BE%E8%AE%A1%E6%9E%B6%E6%9E%84 小结大白话 Portainer 挺好 ...
- 反手来个K8S入门到跑路
layout: post title: 反手来个K8S入门到跑路 category: linux date: 2019-06-09 tags: linux k8s 反手来个K8S入门到跑路 前言 放假 ...
- k8s入门你至少需要会哪些
body { margin: 0; overflow: auto; font: normal 14px Verdana; background: rgba(255, 255, 255, 1); pad ...
- k8s工作负载资源之deployment
首先我们要理解:一个应用跑在k8s集群上了,那么这个应用就是一个工作负载(workloads). 在k8s中会用pod的来承载这个应用,那么负责管理这个pod的东西就叫工作负载资源(workload ...
- k8s入门系列之guestbook快速部署
k8s集群以及一些扩展插件已经安装完毕,本篇文章介绍一下如何在k8s集群上快速部署guestbook应用. •实验环境为集群:master(1)+node(4),详细内容参考<k8s入门系列之集 ...
- k8s入门系列之扩展组件(一)DNS安装篇
DNS (domain name system),提供域名解析服务,解决了难于记忆的IP地址问题,以更人性可读可记忆可标识的方式映射对应IP地址. Cluster DNS扩展插件用于支持k8s集群系统 ...
- k8s入门系列之集群安装篇
关于kubernetes组件的详解介绍,请阅读上一篇文章<k8s入门系列之介绍篇> Kubernetes集群安装部署 •Kubernetes集群组件: - etcd 一个高可用的K/V键值 ...
- k8s 入门系列之集群安装篇
关于kubernetes组件的详解介绍,请阅读上一篇文章<k8s入门系列之介绍篇> Kubernetes集群安装部署 •Kubernetes集群组件: - etcd 一个高可用的K/V键值 ...
- k8s入门之集群搭建(二)
一.准备三台节点 从上篇文章 k8s入门之基础环境准备(一)安装的Ubuntu虚拟机克隆出三台虚拟机,如图所示 启动这三台虚拟机节点,分别做如下配置 虚拟机名称 IP HostName k8sMast ...
随机推荐
- you-get下载
you-get下载教程:https://www.jianshu.com/p/254d3b59313f
- linux kali 换源细节
1.打开命令行输入sudo vim /etc/apt/sources.list,并输入密码(也许你进入终端是空白的,也是没有问题的.).这里我们用root身份进去.不然后期会报错. (E45: 're ...
- inspeckage
c:\ChangZhi\dnplayer2 d:\ChangZhi\dnplayer2然后在电脑的 terminal 中执行 " 端口转发 " 命令:adb forward t ...
- 【FPGA学习】MATLAB与FPGA实现FIR滤波器
本篇博客记录一下在matlab设计和在FPGA平台实现FIR滤波器的方法,平台是Xilinx的ZYNQ 参考: AMBA AXI-Stream Protocol Specification 使用mat ...
- el-dialog 遮罩层覆盖内容的问题
页面组件层级太多,就会出现遮罩层覆盖dialog里面内容的问题 解决: :append-to-body="true" 把遮罩层添加到body上面 用z-index 设置没效 ...
- 微信h5调分享功能
功能背景: 基于微信公众号的h5商城页面,实现特定商品的分享,包括朋友圈和好友分享功能. 代码实现(以vue项目为例): 首先贴上官方开发文档:https://developers.weixin.qq ...
- Neo4j安装及简单使用【转】
转载防丢失. 一.Neo4j和图数据库简介 neo4j是基于Java语言编写图形数据库.图是一组节点和连接这些节点的关系.图形数据库也被称为图形数据库管理系统或GDBMS. Neo4j的是一种流行的图 ...
- Junit单元测试简单使用
第一步:pom文件引入相关文件 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- JavaSE——subString()方法
package com.zhao.stringtest;public class Test4 { //手机号屏蔽中间四位 //subString(int beginIndex,int endIndex ...
- 【layui】下拉控件dropdown 简单的使用
官方网站地址: http://test.microanswer.cn/page/dropdown.html 1.代码 layui.use(['index', 'table', 'form', 'dro ...