k8s-资源控制器RC、RS 、deployment
rs介绍:ReplicationController和ReplicaSet介绍 RC(ReplicationController)主要的作用就是用来确保容器应用的副本数始终保持在用户定义的副本数。即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收Kubernetes 官方建议使用RS(Replicaset)替代RC(ReplicationControll...
rs介绍:
ReplicationController和ReplicaSet介绍
RC(ReplicationController)主要的作用就是用来确保容器应用的副本数始终保持在用户定义的副本数。即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收Kubernetes
官方建议使用RS(Replicaset)替代RC(ReplicationController)进行部署,RS跟RC没有本质的不同,只是名字不一样,并且RS支持集合式的 selector
演示:
1.建立rs.yaml文档
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: frontend
spec:
replicas: 3 #有三个模板
selector: #标签选择器
matchLabels:
tier: frontend
template: #模板
metadata:
labels:
tier: frontend
spec:
containers:
- name: myapp
image: nginx:v1
env:
- name: GET_HOSTS_FROM
value: dns
ports:
- containerPort: 80
创建pod:
kubectl create -f rs.yaml
查看rs:
[root@apiserver ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
frontend 3 3 3 21s
查看pod:
[root@apiserver ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
frontend-87wtq 1/1 Running 0 23s
frontend-r8kjt 1/1 Running 0 23s
frontend-w7n98 1/1 Running 0 23s
查看rs标签:
[root@apiserver ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
frontend-87wtq 1/1 Running 0 2m39s tier=frontend
frontend-r8kjt 1/1 Running 0 2m39s tier=frontend
frontend-w7n98 1/1 Running 0 2m39s tier=frontend
这时我们尝试修改标签:将tier改为frontend1
[root@apiserver ~]# kubectl label pod frontend-87wtq tier=frontend1
error: 'tier' already has a value (frontend), and --overwrite is false
[root@apiserver ~]# kubectl label pod frontend-87wtq tier=frontend1 --overwrite=true
pod/frontend-87wtq labeled
[root@apiserver ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
frontend-87wtq 1/1 Running 0 5m49s tier=frontend1
frontend-r8kjt 1/1 Running 0 5m49s tier=frontend
frontend-w7n98 1/1 Running 0 5m49s tier=frontend
frontend-wh4xx 1/1 Running 0 2s tier=frontend
[root@apiserver ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
frontend 3 3 3 24m
这时我们会发现rs的数量没有变,pod的数量多了一个,原因是rs replicas: 3 是以标签tier=frontend为准,当标签修改后,他会自动新建一个和原来标签一样的pod,当我们删掉rs之后,还会存留tier=frontend1的pod
[root@apiserver ~]# kubectl delete rs --all
replicaset.extensions "frontend" deleted
[root@apiserver ~]#
[root@apiserver ~]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
frontend-87wtq 1/1 Running 0 29m tier=frontend1
deployment介绍:
Deployment通过RS去创建和管理对应的pod及不同的RS交替去完成滚动更新。
Deployment为Pod 和Replicaset 提供了一个声明式定义(declarative)方法,用来替代以前的ReplicationController 来方便的管理应用。典型的应用场景包括:
·定义Deployment来创建Pod和ReplicaSet
·滚动升级和回滚应用
·扩容和缩容
·暂停和继续Deployment
演示:
创建deployment.yaml
vim deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:v1
ports:
- containerPort: 80
创建deployment
[root@apiserver ~]# kubectl apply -f deployment.yaml --record #record 版本记录
deployment.extensions/nginx-deployment created
查看deployment:
[root@apiserver ~]# kubectl get deployments.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 12s
查看rs
[root@apiserver ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-86bddccdd7 3 3 3 96s
查看pod:
[root@apiserver ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-86bddccdd7-5s8ls 1/1 Running 0 19s
nginx-deployment-86bddccdd7-9frh8 1/1 Running 0 19s
nginx-deployment-86bddccdd7-wvcdx 1/1 Running 0 19s
[root@apiserver ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-86bddccdd7-5s8ls 1/1 Running 0 3m2s 192.168.102.179 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-9frh8 1/1 Running 0 3m2s 192.168.102.178 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-wvcdx 1/1 Running 0 3m2s 192.168.102.177 localhost.localdomain <none> <none>
查看程序是否正常运行(能访问页面及为正常):
[root@apiserver ~]# curl 192.168.102.179
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
扩容:扩容到原来的10倍。
[root@apiserver ~]# kubectl scale deployment nginx-deployment --replicas=10
deployment.extensions/nginx-deployment scaled
查看pod:
[root@apiserver ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-86bddccdd7-5s8ls 1/1 Running 0 135m 192.168.102.179 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-9frh8 1/1 Running 0 135m 192.168.102.178 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-db44r 0/1 ContainerCreating 0 3s <none> localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-gh2s4 0/1 ContainerCreating 0 3s <none> localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-mmvc8 0/1 ContainerCreating 0 3s <none> localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-sqzn7 0/1 ContainerCreating 0 3s <none> localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-tb7k5 1/1 Running 0 3s 192.168.102.180 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-vtk8g 0/1 ContainerCreating 0 3s <none> localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-wvcdx 1/1 Running 0 135m 192.168.102.177 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-z62js 0/1 ContainerCreating 0 3s <none> localhost.localdomain <none> <none>
对镜像进行修改更新:
[root@apiserver ~]# kubectl set image deployment/nginx-deployment nginx=linuxserver/nginx
deployment.extensions/nginx-deployment image updated
查看pod
[root@apiserver ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-d46468c78-6855p 1/1 Running 0 32s 192.168.102.135 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-lmnw7 1/1 Running 0 24s 192.168.102.138 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-lxhvq 1/1 Running 0 34s 192.168.102.132 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-mg7j7 1/1 Running 0 31s 192.168.102.136 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-mwcdt 1/1 Running 0 23s 192.168.102.143 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-nw4gv 1/1 Running 0 28s 192.168.102.137 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-qdzt7 1/1 Running 0 16s 192.168.102.145 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-qqmhs 1/1 Running 0 34s 192.168.102.131 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-r59lc 1/1 Running 0 27s 192.168.102.134 localhost.localdomain <none> <none>
nginx-deployment-d46468c78-r7572 1/1 Running 0 17s 192.168.102.144 localhost.localdomain <none> <none>
查看是否能访问:
[root@apiserver ~]# curl 192.168.102.135
<html>
<head>
<title>Welcome to our server</title>
<style>
body{
font-family: Helvetica, Arial, sans-serif;
}
.message{
width:330px;
padding:20px 40px;
margin:0 auto;
background-color:#f9f9f9;
border:1px solid #ddd;
}
center{
margin:40px 0;
}
h1{
font-size: 18px;
line-height: 26px;
}
p{
font-size: 12px;
}
</style>
</head>
<body>
<div class="message">
<h1>Welcome to our server</h1>
<p>The website is currently being setup under this address.</p>
<p>For help and support, please contact: <a href="me@example.com">me@example.com</a></p>
</div>
</body>
</html>
查看rs(因为试了几次,出现了4个rs)
[root@apiserver ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-8665475fd6 0 0 0 27m
nginx-deployment-867d9bcb4c 0 0 0 18m
nginx-deployment-86bddccdd7 0 0 0 171m
nginx-deployment-d46468c78 10 10 10 3m15s
查看deployment:
[root@apiserver ~]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 10/10 10 10 171m
进行回滚:
[root@apiserver ~]# kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment rolled back
所以就回到了以前的版本:
查看pod:
[root@apiserver ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-86bddccdd7-2mmfx 1/1 Running 0 81s 192.168.102.147 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-5hr2z 1/1 Running 0 81s 192.168.102.146 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-5xsrz 1/1 Running 0 79s 192.168.102.148 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-7rf94 1/1 Running 0 68s 192.168.102.155 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-bjmfv 1/1 Running 0 78s 192.168.102.149 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-pw79g 1/1 Running 0 67s 192.168.102.156 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-r9hs5 1/1 Running 0 75s 192.168.102.151 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-snjgw 1/1 Running 0 73s 192.168.102.152 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-sw8tk 1/1 Running 0 77s 192.168.102.150 localhost.localdomain <none> <none>
nginx-deployment-86bddccdd7-tcrsq 1/1 Running 0 73s 192.168.102.153 localhost.localdomain <none> <none>
查看是否和之前的版本一样:
[root@apiserver ~]# curl 192.168.102.147
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
回滚的其他命令:
查看回滚状态:
[root@apiserver ~]# kubectl rollout status deployment nginx-deployment
deployment "nginx-deployment" successfully rolled out
查看回滚历史:
[root@apiserver ~]# kubectl rollout history deployment nginx-deployment
deployment.extensions/nginx-deployment
REVISION CHANGE-CAUSE
2 kubectl apply --filename=deployment.yaml --record=true
4 kubectl apply --filename=deployment.yaml --record=true
6 kubectl apply --filename=deployment.yaml --record=true
7 kubectl apply --filename=deployment.yaml --record=true
回滚到某一确定的历史版本:
[root@apiserver ~]# kubectl rollout undo deployment nginx-deployment --to-revision=2
deployment.extensions/nginx-deployment rolled back
更多推荐
所有评论(0)