k8s常用资源之deployment资源(六)
k8s常用资源之deployment资源
1.k8s资源常见操作
创建一个资源
kubectl create -f xxx.yaml
查看一个资源
kubectl get pod | rc
查看一个资源的详细过程
kubectl describe pos pod_name
删除一个资源
kubectl delete pod pod_name
kubectl delete -f xxx.yaml
编辑一个资源的配置文件
kubectl edit pod pod_name
2.deployment资源
在rc滚动升级之后,会造成服务访问中断,于是k8s引入了deployment资源。
rc升级需要配置文件,然后rc的标签在两个配置文件中肯定不能相同,因此当rc升级成功后,标签就会更换成新的,标签一换,与svc关联的标签就无效了,就导致业务资源访问失败。
deployment资源先启动一个rs资源,rs控制pod的数量
rs 90%的功能和rc 一样
deployment回滚的时候很难区别回滚的版本,因此我们会使用kubectl run命令来创建资源
2.1.创建一个deployment资源
使用ymal文件创建deployment资源时可以使加上–record参数,记录每次操作的版本
[root@k8s-master k8s]# mkdir deployment
[root@k8s-master k8s]# cd deployment/
1)编辑deploy
[root@k8s-master deployment]# vim k8s_deploy.yaml
apiVersion: extensions/v1beta1 //api版本
kind: Deployment //资源类型
metadata:
name: nginx-deployment //资源名称
spec:
replicas: 3 //pod数量
template:
metadata:
labels:
app: nginx //标签名称
spec:
containers:
- name: nginx //容器名
image: 192.168.81.240/k8s/nginx:1.13 //镜像地址
ports:
- containerPort: 80 //端口
resources:
limits:
cpu: 100m //最小CPU使用
requests:
cpu: 100m //最大CPU使用
2)创建deploy资源
[root@k8s-master deployment]# kubectl create -f k8s_deploy.yaml
deployment "nginx-deployment" created
2.2.使外界能够访问到deployment资源
1)创建一个svc资源
apiVersion: v1
kind: Service
metadata:
name: mynginx //标签与deployment的对应上
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: nginx
2)查看svc资源
[root@k8s-master deployment]# kubectl describe svc mynginx
Name: mynginx
Namespace: default
Labels: <none>
Selector: app=nginx
Type: NodePort
IP: 10.254.36.92
Port: <unset> 80/TCP
NodePort: <unset> 9907/TCP
Endpoints: 172.16.16.2:80,172.16.56.2:80,172.16.56.3:80
Session Affinity: None
No events.
3)查看deployment和svc资源之间的关系
会发现deployment是由rs资源创建的标签,svc的标签和rs的标签一致
[root@k8s-master deployment]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/nginx-deployment 3 3 3 3 7m
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 11d <none>
svc/mynginx 10.254.36.92 <nodes> 80:9907/TCP 6m app=nginx
svc/myweb2 10.254.250.66 <nodes> 80:3000/TCP 1d app=myweb
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/nginx-deployment-416036800 3 3 3 7m nginx 192.168.81.240/k8s/nginx:1.13 app=nginx,pod-template-hash=416036800
NAME READY STATUS RESTARTS AGE IP NODE
po/nginx-deployment-416036800-6m3k3 1/1 Running 0 7m 172.16.16.2 192.168.81.230
po/nginx-deployment-416036800-p04lp 1/1 Running 0 7m 172.16.56.3 192.168.81.220
po/nginx-deployment-416036800-q25q8 1/1 Running 0 7m 172.16.56.2 192.168.81.220
4)访问资源
[root@k8s-master deployment]# curl -I 192.168.81.220:9907
HTTP/1.1 200 OK
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A17dKdV3-1603247126554)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20200722230715022.png)]
2.3.deployment升级
deployment升级非常简单,只需要edit配置文件即可
[root@k8s-master deployment]# kubectl edit deployment nginx-deployment
- image: 192.168.81.240/k8s/nginx:1.15 36行,修改镜像即可
deployment "nginx-deployment" edited
升级前后对比
前
[root@k8s-master deployment]# curl -I 192.168.81.220:9907
HTTP/1.1 200 OK
Server: nginx/1.13.12
后
[root@k8s-master deployment]# curl -I 192.168.81.220:9907
HTTP/1.1 200 OK
Server: nginx/1.15.12
2.4.deployment回滚和命令升级
升级回滚命令语法:
命令格式:kubectl rollout 参数 资源类型 资源名称
2.4.1.查看deployment有哪些历史升级版本
[root@k8s-master ~]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 <none>
2 <none>
REVISION:版本号
CHANGE-CAUSE:升级内容
2.4.2.回滚到上个版本
undo参数表示回滚到上一个版本
1)查看当前所有的deployment版本
[root@k8s-master deployment]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 <none>
2 <none>
2)查看当前nginx的版本
[root@k8s-master deployment]# curl -I 192.168.81.230:9907
HTTP/1.1 200 OK
Server: nginx/1.15.12
3)回滚到上一个版本
[root@k8s-master deployment]# kubectl rollout undo deployment nginx-deployment
deployment "nginx-deployment" rolled back
[root@k8s-master deployment]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
2 <none>
3 <none>
回滚完成后会发现当时的1变成了3
4)查看nginx版本
[root@k8s-master deployment]# curl -I 192.168.81.230:9907
HTTP/1.1 200 OK
Server: nginx/1.13.12
2.4.3.回滚到指定版本
常用于版本非常多的情况下
1)指定回滚到版本2
[root@k8s-master deployment]# kubectl rollout undo deployment nginx-deployment --to-revision=2
2)会发现当时的2变成了4
deployment "nginx-deployment" rolled back
[root@k8s-master deployment]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
3 <none>
4 <none>
2.4.4.kubectl 创建一个deployment
命令格式:
kubectl run 资源名称 --image=镜像地址 --replicas=pod数量 --record
–record表示记录回滚、升级内容
1)kubectl run创建一个deployment资源
[root@k8s-master deployment]# kubectl run nginx-deployment --image=192.168.81.230/k8s/nginx:1.13 --replicas=3 --record
deployment "nginx-deployment" created
2)查看资源的版本,会看到change-cause一列有了升级内容
[root@k8s-master deployment]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 kubectl run nginx-deployment --image=192.168.81.230/k8s/nginx:1.13 --replicas=3 --record
3)edit升级看结果
[root@k8s-master deployment]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 kubectl run nginx-deployment --image=192.168.81.230/k8s/nginx:1.13 --replicas=3 --record
2 kubectl edit deployment nginx-deployment
会发现只记录了edit命令并不记录里面改了什么
2.4.5.set命令升级
语法格式:
kubectl set image 资源类型 资源名 容器名=镜像路径
资源名后面的容器名是由于可能创建deployment资源时会创建多个容器,因此需要区分是对哪个容器进行升级
1)创建一个deployment资源
[root@k8s-master delpoyment]# kubectl run nginxdeploy --image=192.168.81.240/k8s/nginx:1.13 --replicas=3 --record
deployment "nginxdeploy" created
2)查看deployment资源创建的rs资源关联的容器名
[root@k8s-master delpoyment]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/ngin[root@k8s-master delpoyment]# kubectl get all -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deploy/nginxdeploy 3 3 3 3 3s
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 12d <none>
svc/mynginx 10.254.36.92 <nodes> 80:9907/TCP 1d app=nginx
svc/myweb2 10.254.250.66 <nodes> 80:3000/TCP 2d app=myweb
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rs/nginxdeploy-4280591583 3 3 3 3s nginxdeploy 192.168.81.240/k8s/nginx:1.13 pod-template-hash=4280591583,run=nginxdeploy
NAME READY STATUS RESTARTS AGE IP NODE
po/nginxdeploy-4280591583-6q6kg 1/1 Running 0 3s 172.16.19.3 192.168.81.220
po/nginxdeploy-4280591583-gcv47 1/1 Running 0 3s 172.16.48.2 192.168.81.230
po/nginxdeploy-4280591583-h80m5 1/1 Running 0 3s 172.16.19.2 192.168.81.220
3)查看deployment资源的版本
[root@k8s-master delpoyment]# kubectl rollout history deployment nginxdeploy
deployments "nginxdeploy"
REVISION CHANGE-CAUSE
1 kubectl run nginxdeploy --image=192.168.81.240/k8s/nginx:1.13 --replicas=3 --record
4)升级deployment资源
[root@k8s-master delpoyment]# kubectl set image deploy nginxdeploy nginxdeploy=192.168.81.240/k8s/nginx:1.15
deployment "nginxdeploy" image updated
5)查看升级后的版本
[root@k8s-master delpoyment]# kubectl rollout history deployment nginxdeploy
deployments "nginxdeploy"
REVISION CHANGE-CAUSE
1 kubectl run nginxdeploy --image=192.168.81.240/k8s/nginx:1.13 --replicas=3 --record
2 kubectl set image deploy nginxdeploy nginxdeploy=192.168.81.240/k8s/nginx:1.15
2.4.6.回滚set升级的版本
1)查看可以回滚的版本,最往后的表示当前版本
[root@k8s-master delpoyment]# kubectl rollout history deployment nginxdeploy
deployments "nginxdeploy"
REVISION CHANGE-CAUSE
1 kubectl run nginxdeploy --image=192.168.81.240/k8s/nginx:1.13 --replicas=3 --record
2 kubectl set image deploy nginxdeploy nginxdeploy=192.168.81.240/k8s/nginx:1.15
2)指定回滚到版本1
[root@k8s-master delpoyment]# kubectl rollout undo deployment nginxdeploy --to-revision=1
deployment "nginxdeploy" rolled back
3)查看回滚
[root@k8s-master delpoyment]# kubectl rollout history deployment nginxdeploy
deployments "nginxdeploy"
REVISION CHANGE-CAUSE
2 kubectl set image deploy nginxdeploy nginxdeploy=192.168.81.240/k8s/nginx:1.15
3 kubectl run nginxdeploy --image=192.168.81.240/k8s/nginx:1.13 --replicas=3 --record
2.4.7.利用yaml创建资源时记录升级内容
1)只需要增加--record即可
[root@k8s-master delpoyment]# kubectl create -f k8s_deploy.yaml --record
deployment "nginx-deployment" created
2)查看版本
[root@k8s-master delpoyment]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 kubectl create -f k8s_deploy.yaml --record
3)升级
[root@k8s-master delpoyment]# kubectl set image deploy nginx-deployment nginx=192.168.81.240/k8s/nginx:1.15
deployment "nginx-deployment" image updated
4)查看记录的版本
[root@k8s-master delpoyment]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 kubectl create -f k8s_deploy.yaml --record
2 kubectl set image deploy nginx-deployment nginx=192.168.81.240/k8s/nginx:1.15
目录 返回
首页