Kubernetes中文社区 | 中文文档
vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
curl -LO https://dl.k8s.io/release/v1.20.0/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
yum install conntrack
前提: 安装docker(安装笔记)
启动minikube
minikube start --vm-driver='none' --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers'
停止minikube
minikube stop
查看节点——kubectl get node(s)
[root@*********************** ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
*********************** Ready control-plane,master 4d3h v1.22.3
node是⽤于承载运⾏中的容器的。
有⼀个node, 这个node的⻆⾊是master。
k8s也有集群的概念, 即:cluster, 包含master和node, 该节点即是master节点, ⼜是node节点。
创建deployment
[root@*********************** ~]# kubectl create deployment my-nginx --image nginx:latest
deployment.apps/my-nginx created
查看所有deployment——kubectl get deploy(ment)(s)
[root@*********************** ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 1/1 1 1 7m46s
查看所有pod——kubectl get pod(s)
[root@*********************** ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-b7d7bc74d-qq56l 1/1 Running 0 8m46s
查看所有pod信息以及ip和port——kubectl get pods -o wide
[root@*********************** ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-b7d7bc74d-qq56l 1/1 Running 0 12m ---.--.-.- ***********************
查看所有service——kubectl get service(s)
[root@*********************** ~]# kubectl get service(s)
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 4d3h
查询所有命名空间——kubectl get namespace(s)
[root@*********************** ~]# kubectl get namespaces
NAME STATUS AGE
default Active 4d3h
kube-node-lease Active 4d3h
kube-public Active 4d3h
kube-system Active 4d3h
1.默认的是default
2.kube-*为k8s系统自身的命名空间
修改副本数
即修改pod的个数, 如不指定副本个数, 则默认就是⼀个pod
# 将pods个数修改为3个
[root@*********************** ~]# kubectl scale deployments/my-nginx --replicas=3
deployment.apps/my-nginx scaled
[root@*********************** ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 1/3 3 1 33m
[root@*********************** ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-b7d7bc74d-ht5fx 1/1 Running 0 36s
my-nginx-b7d7bc74d-mc9p5 1/1 Running 0 36s
my-nginx-b7d7bc74d-qq56l 1/1 Running 0 33m
# 将pods个数修改为2个
[root@*********************** ~]# kubectl scale deployments/my-nginx --replicas=2
deployment.apps/my-nginx scaled
[root@*********************** ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 2/2 2 2 38m
[root@*********************** ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-b7d7bc74d-mc9p5 1/1 Running 0 5m49s
my-nginx-b7d7bc74d-qq56l 1/1 Running 0 38m
资源的集合, k8s利⽤这些资源运⾏各种基于容器的应⽤。
- Cluster的⼤脑, 司令部
- 主要的任务就是⽤来调度的, 决定应⽤应该放到哪⾥去执⾏, 为了⾼可⽤, 也可以运⾏多个master
- 负责运⾏容器应⽤
- Master管理, 负责监控和容器状态的汇报
k8s的最⼩的⼯作单元, 包含1~N个容器
使⽤⽅式:
⼀个pod运⾏⼀个容器(最常⽤)
⼀个pod运⾏多个容器(较少见)
⾮常紧密相关的⼀组容器, 并且需要资源的共享, ⼀起启动、⼀起停⽌
- 管理Pod
- 针对不同的业务场景, k8s提供了多种Controller, 其中包含: Deployment、ReplicaSet、DaemonSet、StatefulSet、Job
Deployment
1.最常⽤的Controller, 可以管理Pod的多个副本(例如: --replicas=3), 并且可以确保Pod按照期望的状态去运⾏
2.实质: 会⾃动创建ReplicaSet,
最终是由ReplicaSet创建pod
ReplicaSet
也是管理Pod的多个副本
DaemonSet
⽤于每个Node最多只运⾏⼀个Pod副本的创建
StatefulSet
保证副本按照固定的顺序启动、更新、删除
为Pod提供了负载均衡、固定的IP和Port——因为pod是不稳定的, IP会变化的, 所以需要⼀个固定的IP或Port
资源隔离
1.kubelet是唯一没有以容器形式运行的Kubernetes组件
2.k8s架构中, 主要是由Master和Node组成的
当执行部署应用并指定两个副本的时候
API-Server
属于前端交互接⼝, 提供基于Http/https RESTful API接收对应的指令
Scheduler
负责决定将pod放到哪个Node上运⾏
Controller Manager
⾮常关键
的组件, 管理Cluster中的各种资源
etcd
负责保存k8s的配置信息和各种资源的状态信息, 如果数据发⽣了变化, etcd会快速通知相关的组件
kubelet
创建和运⾏容器
kube-proxy
负责请求的转发, 如果有多个副本, 则会实现负载均衡
kubectl命令
kubectl run
kubectl run nginx-deployment --image nginx:1.7.9 --replicas=2
[root@*********************** nginx]# kubectl run nginx-deployment --image nginx:1.7.9 --replicas=2
Flag --replicas has been deprecated, has no effect and will be removed in the future.
pod/deployments created
在命令行中通过参数指定资源的属性(但是, 在K8S v1.18.0以后, -–replicas已弃用, 推荐用 kubectl apply 创建 pods)
kubectl create
kubectl create deployment nginx-deployment --image nginx:1.7.9 --replicas=2
配置文件 + kubectl apply
编写yml配置文件(文件内部, 自定义名称不能包含大写字母
)
例如:
# 版本号
apiVersion: apps/v1
# 类型: Pod/ReplicationController/Deployment/Service/Ingress
kind: Deployment
metadata:
# Kind的名称
name: nginx-deployment
spec:
selector:
matchLabels:
# 容器标签的名称, 发布Service时, selector需要和这里对应
app: nginx
# 部署的实例数量(默认一个)
replicas: 2
template:
# 至少定义一个label, key和value可以任意指定
metadata:
labels:
app: nginx
# 此部分描述Pod的规格, 定义Pod中每一个容器的属性, name和image是必需的
spec:
# 容器配置, 数组类型, 说明可以配置多个容器
containers:
# 容器名称(必须)
- name: nginx
# 容器镜像(必须)
image: nginx:1.7.9
# 策略: "Always", "IfNotPresent", "Never"
# 只有镜像不存在时, 才会进行镜像拉取
imagePullPolicy: IfNotPresent
ports:
# Pod端口
- containerPort: 80
执行命令
[root@*********************** nginx]# kubectl apply -f nginx.yaml
deployment.apps/nginx-deployment created
[root@*********************** ~]# kubectl delete deployment my-nginx
deployment.apps “my-nginx” deleted
注意点
如配置2个pod, 对其中一个pod进行删除, 依然会根据deployment配置, 再补充为2个pod
删除deployment时, pod会随之自动被删除
kubectl——创建Deployment——创建ReplicaSet——创建Pod
[root@*********************** nginx]# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
.
.
.
NewReplicaSet: nginx-deployment-5d59d67564 (2/2 replicas created)
.
.
.
[root@*********************** nginx]# kubectl describe rs nginx-deployment-5d59d67564
Name: nginx-deployment-5d59d67564
Namespace: default
.
.
.
.
.
.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 13m replicaset-controller Created pod: nginx-deployment-5d59d67564-mcrsm
Normal SuccessfulCreate 13m replicaset-controller Created pod: nginx-deployment-5d59d67564-qt7rc
[root@*********************** nginx]# kubectl describe pod nginx-deployment-5d59d67564-mcrsm
Name: nginx-deployment-5d59d67564-mcrsm
Namespace: default
.
.
.
Controlled By: ReplicaSet/nginx-deployment-5d59d67564
.
.
.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 17m default-scheduler Successfully assigned default/nginx-deployment-5d59d67564-mcrsm to ***********************
Normal Pulled 17m kubelet Container image "nginx:1.7.9" already present on machine
Normal Created 17m kubelet Created container nginx
Normal Started 17m kubelet Started container nginx
默认配置下, Scheduler会将Pod调度到所有可用的Node, 不过有些情况我们可以通过lable将Pod部署到指定的Node, 例如:
- 将有大量磁盘I/O的Pod部署到配置了SSD的Node上
- 将需要GPUPod部署到配置了GPU的Node上
查看节点的标签信息
[root@*********************** nginx]# kubectl get node
NAME STATUS ROLES AGE VERSION
*********************** Ready control-plane,master 4d22h v1.22.3
[root@*********************** nginx]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
*********************** Ready control-plane,master 4d22h v1.22.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=***********************,kubernetes.io/os=linux,minikube.k8s.io/commit=76b94fb3c4e8ac5062daf70d60cf03ddcc0a741b,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2021_11_19T17_01_50_0700,minikube.k8s.io/version=v1.24.0,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
添加/删除标签
# 添加
[root@*********************** nginx]# kubectl label node *********************** disktype=ssd
node/*********************** labeled
[root@*********************** nginx]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
*********************** Ready control-plane,master 4d22h v1.22.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=***********************,kubernetes.io/os=linux,minikube.k8s.io/commit=76b94fb3c4e8ac5062daf70d60cf03ddcc0a741b,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2021_11_19T17_01_50_0700,minikube.k8s.io/version=v1.24.0,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
# 删除
[root@*********************** nginx]# kubectl label node *********************** disktype-
node/*********************** labeled
[root@*********************** nginx]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
*********************** Ready control-plane,master 4d22h v1.22.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=***********************,kubernetes.io/os=linux,minikube.k8s.io/commit=76b94fb3c4e8ac5062daf70d60cf03ddcc0a741b,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2021_11_19T17_01_50_0700,minikube.k8s.io/version=v1.24.0,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
修改xx.yml配置文件
重新部署Deployment
容器按照持续运行时间可以分为
服务类容器
持续提供服务, 则需要一直运行, 比如HTTP Server、Daemon
Deployment、ReplicaSet和DaemonSet都用于管理服务类容器
工作类容器
一次性任务, 比如批处理程序, 完成后容器就退出
Job用于管理工作类容器
普通Job
apiVersion: batch/v1
kind: Job
metadata:
name: myjob
spec:
# 需要完成的数量
# completions: 6
# 并行数量
# parallelism: 2
template:
metadata:
name: myjob
spec:
containers:
- name: hello
image: busybox
command: ["echo", "hello k8s job!"]
# command: ["invalid_command", "hello k8s job!"]
restartPolicy: Never
# restartPolicy: OnFailure
command: [“echo”, “hello k8s job!”]
[root@*********************** deployConfigFile]# kubectl apply -f myJob.yaml
job.batch/myjob created
[root@*********************** ~]# kubectl get job
NAME COMPLETIONS DURATION AGE
myjob 1/1 22s 16h
[root@*********************** ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myjob--1-jvttw 0/1 Completed 0 16h
[root@*********************** ~]# kubectl logs myjob--1-jvttw
hello k8s job!
command: [“invalid_command”, “hello k8s job!”]
restartPolicy: Never
[root@*********************** deployConfigFile]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myjob--1-2z8dk 0/1 ContainerCreating 0 9s
myjob--1-rlzmk 0/1 ContainerCannotRun 0 28s
```
restartPolicy: OnFailure
[root@*********************** deployConfigFile]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myjob--1-xkbrj 0/1 RunContainerError 1 (1s ago) 35s
定时Job
# apiVersion: batch/v1
apiVersion: batch/v2alpha1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
command: ["echo", "hello k8s cronJob!"]
restartPolicy: OnFailure
可能出现的错误信息
[root@*********************** deployConfigFile]# kubectl apply -f myCronJob.yaml
error: unable to recognize "myCronJob.yaml": no matches for kind "CronJob" in version "batch/v2alpha1"
查看支持的版本
[root@*********************** deployConfigFile]# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
# 常用
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
# 常用
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
引入batch/v2alpha1
[root@*********************** deployConfigFile]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
spec:
containers:
- command:
- kube-apiserver
- --runtime-config=batch/v2alpha1=true
.
.
.
可能需要手动重启——systemctl restart kubelet
可能会出现Server无法重启的现象, 提升Kubernetes版本至1.5及其以上
batch/v1——参考文档
[root@*********************** deployConfigFile]# kubectl apply -f myCronJob.yaml
cronjob.batch/hello created
[root@*********************** deployConfigFile]# kubectl get cronJob
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 0 37s
[root@*********************** deployConfigFile]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-27296887--1-7nk7q 0/1 Completed 0 2m8s
hello-27296888--1-dg6nh 0/1 Completed 0 67s
hello-27296889--1-t5gqx 0/1 ContainerCreating 0 8s
Service提供了固定的ip和端口, 并且里面包含一组pod, 即使Pod的ip发生变化, 但是面对客户端的是Service的固定ip和端口
- Pod是不健壮的, Pod中的容器很可能因为各种原因发生故障而挂掉
- Deployment等Controller会通过动态创建和销毁Pod来保证应用整体的健壮性, 所以应用才是是健壮的
创建Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
spec:
selector:
matchLabels:
run: httpd
replicas: 3
template:
metadata:
labels:
name: httpd
spec:
containers:
- name: httpd
image: httpd
ports:
- containerPort: 80
[root@*********************** httpd]# kubectl apply -f httpd.yaml
deployment.apps/httpd created
[root@*********************** httpd]# kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-5cbd65d46c-f7hgm 1/1 Running 0 2m35s
httpd-5cbd65d46c-qvmqw 1/1 Running 0 2m35s
httpd-5cbd65d46c-xzbkv 1/1 Running 0 2m35s
[root@*********************** httpd]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpd-5cbd65d46c-f7hgm 1/1 Running 0 3m16s 172.18.0.6 ***********************
httpd-5cbd65d46c-qvmqw 1/1 Running 0 3m16s 172.18.0.4 ***********************
httpd-5cbd65d46c-xzbkv 1/1 Running 0 3m16s 172.18.0.5 ***********************
[root@*********************** httpd]# curl 172.18.0.6
It works!
[root@*********************** httpd]# curl 172.18.0.5
It works!
[root@*********************** httpd]# curl 172.18.0.4
It works!
创建Service
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
spec:
selector:
run: httpd
ports:
- protocol: TCP
port: 8080
targetPort: 80
[root@*********************** httpd]# kubectl apply -f httpdSvc.yaml
service/httpd-svc created
[root@*********************** httpd]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd-svc ClusterIP 10.106.23.93 8080/TCP 5m57s
[root@*********************** httpd]# curl 10.106.23.93:8080
It works!
关联性
kubectl describe service httpd-svc
[root@*********************** httpd]# kubectl describe service httpd-svc
Name: httpd-svc
Namespace: default
Labels:
Annotations:
Selector: run=httpd
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.106.23.93
IPs: 10.106.23.93
Port: 8080/TCP
TargetPort: 80/TCP
Endpoints: 172.18.0.4:80,172.18.0.5:80,172.18.0.6:80
.
.
.
iptables-save
[root@*********************** httpd]# iptables-save
.
.
.
-A KUBE-SERVICES -d 10.106.23.93/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-SVC-IYRDZZKXS5EOQ6Q6
.
.
.
-A KUBE-SVC-IYRDZZKXS5EOQ6Q6 ! -s 10.244.0.0/16 -d 10.106.23.93/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-MARK-MASQ
-A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.33333333349 -j KUBE-SEP-CERU7ZF6K2KWGLTG
-A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-FEWNLSQ65IOCPPEH
-A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -j KUBE-SEP-VEOTCWMCCOB2VENI
-A KUBE-SEP-CERU7ZF6K2KWGLTG -s 172.18.0.4/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
-A KUBE-SEP-CERU7ZF6K2KWGLTG -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 172.18.0.4:80
-A KUBE-SEP-FEWNLSQ65IOCPPEH -s 172.18.0.5/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
-A KUBE-SEP-FEWNLSQ65IOCPPEH -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 172.18.0.5:80
-A KUBE-SEP-VEOTCWMCCOB2VENI -s 172.18.0.6/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
-A KUBE-SEP-VEOTCWMCCOB2VENI -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 172.18.0.6:80
.
.
.
k8s自带功能
什么是滚动更新
一次只更新一小部分副本, 成功后再更新更多的副本, 最终完成所有副本的更新
优势
最大好处是零停机
, 整个更新过程始终有副本在运行, 从而保证了业务的连续性
示例
更新
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
spec:
selector:
matchLabels:
run: httpd
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.2.31
# image: httpd:2.2.32
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
[root@*********************** httpd]# kubectl apply -f httpdRollingUpdate.yaml
deployment.apps/httpd configured
[root@*********************** httpd]# vim httpdRollingUpdate.yaml
[root@*********************** httpd]# kubectl apply -f httpdRollingUpdate.yaml
deployment.apps/httpd configured
[root@*********************** httpd]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-55f56846f-875sg 1/1 Running 0 108s
httpd-55f56846f-ggqb5 1/1 Running 0 108s
httpd-55f56846f-tjn66 1/1 Running 0 108s
httpd-846d8d79dd-d7bzk 0/1 ContainerCreating 0 13s
[root@*********************** httpd]# kubectl describe deployment httpd
Name: httpd
Namespace: default
.
.
.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m12s deployment-controller Scaled up replica set httpd-55f56846f to 3
Normal ScalingReplicaSet 97s deployment-controller Scaled up replica set httpd-846d8d79dd to 1
Normal ScalingReplicaSet 58s deployment-controller Scaled down replica set httpd-55f56846f to 2
Normal ScalingReplicaSet 58s deployment-controller Scaled up replica set httpd-846d8d79dd to 2
Normal ScalingReplicaSet 53s deployment-controller Scaled down replica set httpd-55f56846f to 1
Normal ScalingReplicaSet 53s deployment-controller Scaled up replica set httpd-846d8d79dd to 3
Normal ScalingReplicaSet 48s deployment-controller Scaled down replica set httpd-55f56846f to 0
[root@*********************** httpd]# kubectl get rs
NAME DESIRED CURRENT READY AGE
httpd-55f56846f 0 0 0 3m49s
httpd-846d8d79dd 3 3 3 2m14s
回滚(–record)
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
spec:
selector:
matchLabels:
run: httpd
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.16
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
spec:
selector:
matchLabels:
run: httpd
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
run: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.18
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
[root@*********************** httpd]# kubectl apply -f httpd.v1.yaml --record
deployment.apps/httpd created
[root@*********************** httpd]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 47s httpd httpd:2.4.16 run=httpd
[root@*********************** httpd]# kubectl apply -f httpd.v2.yaml --record
deployment.apps/httpd configured
[root@*********************** httpd]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 100s httpd httpd:2.4.18 run=httpd
[root@*********************** httpd]# kubectl rollout history deployment httpd
deployment.apps/httpd
REVISION CHANGE-CAUSE
1 kubectl apply --filename=httpd.v1.yaml --record=true
2 kubectl apply --filename=httpd.v2.yaml --record=true
进行回滚
[root@*********************** httpd]# kubectl rollout undo deployment httpd --to-revision=1
deployment.apps/httpd rolled back
[root@*********************** httpd]# kubectl rollout history deployment httpd
deployment.apps/httpd
REVISION CHANGE-CAUSE
2 kubectl apply --filename=httpd.v2.yaml --record=true
3 kubectl apply --filename=httpd.v1.yaml --record=true