课堂记录:k8s滚动更新及数据管理(四)–技术流ken

滚动更新实践
实现效果当前生产环境httpd版本为2.2.31,在不停机以及中断服务的情况下,滚动更新到httpd:2.2.32版本
第一步:编写部署httpd的yaml文件
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd31
spec:
replicas: 3
template:
metadata:
labels:
run: httpd31
spec:
containers:
- name: httpd31
image: httpd:2.2.31
imagePullPolicy: IfNotPresent
第二步:查看deploy
[root@ken1 ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd31 3/3 3 3 29s httpd31 httpd:2.2.31 run=httpd31
第三步:查看rs
[root@ken1 ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpd31-78c4cf6f 3 3 3 61s httpd31 httpd:2.2.31 pod-template-hash=78c4cf6f,run=httpd31
第四步:查看po
[root@ken1 ~]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpd31-78c4cf6f-2f2wp 1/1 Running 0 84s 10.244.2.110 ken3 <none> <none>
httpd31-78c4cf6f-cmhz7 1/1 Running 0 84s 10.244.2.111 ken3 <none> <none>
httpd31-78c4cf6f-qvpct 1/1 Running 0 84s 10.244.1.32 ken2 <none> <none>
滚动更新
第五步;复制一份上述yml文件并把版本改为2.2.32
[root@ken1 ~]# cp httpd31.yml httpd32.yml
[root@ken1 ~]# cat httpd32.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd31
spec:
replicas: 3
template:
metadata:
labels:
run: httpd31
spec:
containers:
- name: httpd31
image: httpd:2.2.32
imagePullPolicy: IfNotPresent
第六步:执行该yml文件
[root@ken1 ~]# kubectl apply -f httpd32.yml
第七步:查看deploy
[root@ken1 ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd31 3/3 3 3 4m1s httpd31 httpd:2.2.32 run=httpd31
第八步:查看rs
[root@ken1 ~]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpd31-78c4cf6f 0 0 0 4m22s httpd31 httpd:2.2.31 pod-template-hash=78c4cf6f,run=httpd31
httpd31-7f8489457b 3 3 3 42s httpd31 httpd:2.2.32 pod-template-hash=7f8489457b,run=httpd31
第九步:查看po
[root@ken1 ~]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpd31-7f8489457b-8rccl 1/1 Running 0 77s 10.244.2.112 ken3 <none> <none>
httpd31-7f8489457b-j28z9 1/1 Running 0 77s 10.244.1.33 ken2 <none> <none>
httpd31-7f8489457b-p688p 1/1 Running 0 74s 10.244.1.34 ken2 <none> <none>
第十步:查看deploy详细信息
[root@ken1 ~]# kubectl describe deploy httpd31
Name: httpd31
Namespace: default
CreationTimestamp: Thu, 22 Aug 2019 09:38:12 +0800
Labels: run=httpd31
Annotations: deployment.kubernetes.io/revision: 2
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"extensions/v1beta1","kind":"Deployment","metadata":{"annotations":{},"name":"httpd31","namespace":"default"},"spec":{"repli...
Selector: run=httpd31
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=httpd31
Containers:
httpd31:
Image: httpd:2.2.32
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: httpd31-7f8489457b (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 5m45s deployment-controller Scaled up replica set httpd31-78c4cf6f to 3
Normal ScalingReplicaSet 2m5s deployment-controller Scaled up replica set httpd31-7f8489457b to 1
Normal ScalingReplicaSet 2m5s deployment-controller Scaled down replica set httpd31-78c4cf6f to 2
Normal ScalingReplicaSet 2m5s deployment-controller Scaled up replica set httpd31-7f8489457b to 2
Normal ScalingReplicaSet 2m2s deployment-controller Scaled down replica set httpd31-78c4cf6f to 1
Normal ScalingReplicaSet 2m2s deployment-controller Scaled up replica set httpd31-7f8489457b to 3
Normal ScalingReplicaSet 2m2s deployment-controller Scaled down replica set httpd31-78c4cf6f to 0
k8s回滚任意版本
实践:需要准备三份yml文件,版本分别为2.2.31 2.2.32 2.2.34, 三分yaml文件名称必须一致,只需要镜像版本不同就行
第一步:复制yaml文件
[root@ken1 ~]# cat httpd34.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd31
spec:
replicas: 3
template:
metadata:
labels:
run: httpd31
spec:
containers:
- name: httpd31
image: httpd:2.2.34
imagePullPolicy: IfNotPresent
第二步:部署31版本yml文件
[root@ken1 ~]# kubectl apply -f httpd31.yml --record
deployment.extensions/httpd31 configured
[root@ken1 ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd31 3/3 3 3 12m httpd31 httpd:2.2.31 run=httpd31
第三步:部署32版本的yml文件
[root@ken1 ~]# kubectl apply -f httpd32.yml --record
deployment.extensions/httpd31 configured
[root@ken1 ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd31 3/3 3 3 13m httpd31 httpd:2.2.32 run=httpd31
第四步:部署34版本的yml文件
[root@ken1 ~]# kubectl apply -f httpd34.yml --record
deployment.extensions/httpd31 configured
[root@ken1 ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd31 2/3 2 2 14m httpd31 httpd:2.2.34 run=httpd31
实践回滚到31版本
第五步:查看命令执行的历史信息
[root@ken1 ~]# kubectl rollout history deploy httpd31
deployment.extensions/httpd31
REVISION CHANGE-CAUSE
3 kubectl apply --filename=httpd31.yml --record=true
4 kubectl apply --filename=httpd32.yml --record=true
5 kubectl apply --filename=httpd34.yml --record=true
第六步:实现回滚
[root@ken1 ~]# kubectl rollout undo deploy httpd31 --to-revision=3
deployment.extensions/httpd31 rolled back
[root@ken1 ~]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd31 3/3 3 3 18m httpd31 httpd:2.2.31 run=httpd31
指定版本记录的条数
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd31
spec:
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
run: httpd31
spec:
containers:
- name: httpd31
image: httpd:2.2.31
imagePullPolicy: IfNotPresent
~
k8ss数据管理
1.emptyDir
2.hostPath
3.NFS
4.pv/pvc
一、实践emptyDir
第一步:编写yml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: ken
mountPath: /usr/share/nginx/html
volumes:
- name: ken
emptyDir: {}
第二步:执行yml文件
[root@ken1 ~]# kubectl apply -f nginx.yml
第三步:查看pod
[root@ken1 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 68s
第四步:在节点查看容器详细信息
[root@ken3 ken]# docker inspect 937208cf64ee
...
"Mounts": [
{
"Type": "bind",
"Source": "/var/lib/kubelet/pods/704e6443-9b31-45b5-b41d-39e8e84bacb2/volumes/kubernetes.io~empty-dir/ken",
"Destination": "/usr/share/nginx/html",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
},
...
第五步:编写测试文件
[root@ken3 ~]# cd /var/lib/kubelet/pods/704e6443-9b31-45b5-b41d-39e8e84bacb2/volumes/kubernetes.io~empty-dir/ken
[root@ken3 ken]# echo "test emptyDir" > index.html
第六步:主节点测试
[root@ken1 ~]# curl 10.244.2.119
test emptyDir
二、实践hostPath
效果相当于执行: docker run -v /tmp:/usr/share/nginx/html
第一步:编写yml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: ken
mountPath: /usr/share/nginx/html
volumes:
- name: ken
hostPath:
path: /tmp
第二步:执行yml文件
[root@ken1 ~]# kubectl apply -f nginx-hostp.yml
第三步:进行验证
[root@ken1 ~]# ls /tmp
ks-script-Kgah4x yum.log
systemd-private-1efe767de1b4430fb77c8d90bafce1de-vmtoolsd.service-nfl5a9 yum_save_tx.2019-08-21.17-03.Hc7vcy.yumtx
systemd-private-21c03f170cff40688b56b7b97b6d60d9-vmtoolsd.service-oR1NRh
[root@ken1 ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 31m
nginx-hostp 1/1 Running 0 31s
[root@ken1 ~]# kubectl exec nginx-hostp ls /usr/share/nginx/html
ks-script-Kgah4x
systemd-private-290df9f339b647a099c58e3b2d499278-vmtoolsd.service-2MNZBs
systemd-private-73ed2c6655a748c1b8190aa51cf682bb-vmtoolsd.service-nysa3U
yum.log
三、实践NFS
每个节点必须要安装nfs
主节点部署NFS服务器端
每个节点安装nfs-utils
第一步:部署nfs服务器端
[root@ken1 ~]# yum install nfs-utils rpcbind -y
[root@ken1 ~]# cat /etc/exports
/ken *(rw)
[root@ken1 ~]# chown -R nfsnobody: /ken
[root@ken1 ~]# systemctl restart nfs rpcbind
第二步:客户端下载nfs
[root@ken2 ~]# yum install nfs-utils -y
第三步:编写yml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: ken
mountPath: /usr/share/nginx/html
volumes:
- name: ken
nfs:
path: /ken
server: 192.168.64.11
第四步:执行yml文件
[root@ken1 ~]# kubectl apply -f nginx-nfs.yml
第五步:验证
[root@ken1 ~]# ls /ken
index.html test
[root@ken1 ~]# cat /ken/index.html
test
[root@ken1 ~]# echo "nfs" >> /ken/index.html
[root@ken1 ~]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 38m 10.244.2.119 ken3 <none> <none>
nginx-hostp 1/1 Running 0 7m32s 10.244.1.41 ken2 <none> <none>
nginx-nfs 1/1 Running 0 32s 10.244.2.120 ken3 <none> <none>
[root@ken1 ~]# curl 10.244.2.120
test
nfs
四、实践pv/pvc
第一步:部署NFS
上同,略
第二步:编写pv的yml文件
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
nfs:
path: /ken
server: 192.168.64.11
第三步:执行yml文件
[root@ken1 ~]# kubectl apply -f pv.yml
第四步:查看pv
[root@ken1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv 1Gi RWX Retain Available 17h
注:
accessModes有三类
- ReadWriteOnce – 可以被单个节点进行读写挂载
- ReadOnlyMany – 可以被多个节点进行只读挂载
- ReadWriteMany – 可以被多个节点进行读写挂载
reclaim policy有三类
- retain – pvc被删除之后,数据保留
- recyle- 删除pvc之后会删除数据(被废弃)
- delete – 删除pvc之后会删除数据
创建pvc
第一步:编写yml文件
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc1
spec:
accessModes:
- ReadWriteMany
volumeName: mypv
resources:
requests:
storage: 1Gi
第二步:执行pvc
[root@ken1 ~]# kubectl apply -f pvc.yml
第三步:查看pvc
[root@ken1 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc1 Bound mypv 1Gi RWX 19s
[root@ken1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv 1Gi RWX Retain Bound default/mypvc1 17h
[root@ken1 ~]#
使用pvc
第一步:编写部署nginx的yml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx-pvc
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
volumeMounts:
- name: ken
mountPath: /usr/share/nginx/html
volumes:
- name: ken
persistentVolumeClaim:
claimName: mypvc1
第二步:执行yml文件
[root@ken1 ~]# kubectl apply -f nginx-pvc.yml
第三步:测试
[root@ken1 ~]# kubectl exec -it nginx-pvc /bin/bash
root@nginx-pvc:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@nginx-pvc:/# cd /usr/share/nginx/i
bash: cd: /usr/share/nginx/i: No such file or directory
root@nginx-pvc:/# cd /usr/share/nginx/html/
root@nginx-pvc:/usr/share/nginx/html# ls
index.html test
root@nginx-pvc:/usr/share/nginx/html# touch ken
root@nginx-pvc:/usr/share/nginx/html# exit
exit
[root@ken1 ~]# ls /ken
index.html ken test
释放pv
第一步:先停掉pod
[root@ken1 ~]# kubectl delete -f nginx-pvc.yml
第二步:删除pvc
[root@ken1 ~]# kubectl delete pvc mypvc1
第三步:查看pv状态
[root@ken1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv 1Gi RWX Retain Released default/mypvc1 17h
第四步:删除pv
[root@ken1 ~]# kubectl delete -f pv.yml
第五步:查看数据
[root@ken1 ~]# ls /ken
index.html ken test
第六步:再次创建pv
[root@ken1 ~]# kubectl apply -f pv.yml
persistentvolume/mypv created
[root@ken1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv 1Gi RWX Retain Available 4s
This is a topic that is near to my heart… Best wishes!
Where are your contact details though?
you’ve got an incredible weblog here! would you prefer to make some invite posts on my weblog?
Thanks so much for giving everyone remarkably brilliant opportunity to read articles and blog posts from this blog. It is often so lovely and also full of amusement for me personally and my office co-workers to visit your website the equivalent of thrice in 7 days to study the newest stuff you will have. Of course, we’re at all times motivated with the superb points served by you. Certain 4 areas on this page are essentially the best I’ve ever had.
I enjoy you because of your own effort on this website. Ellie takes pleasure in conducting research and it is easy to see why. A lot of people learn all of the dynamic method you produce powerful tips by means of your web blog and in addition strongly encourage response from the others about this concept and our favorite daughter is certainly being taught a whole lot. Take advantage of the remaining portion of the year. You’re the one conducting a first class job.
I just wanted to send a brief remark in order to thank you for all of the splendid concepts you are writing at this site. My extensive internet search has now been paid with good quality ideas to talk about with my family and friends. I would believe that we site visitors actually are very lucky to dwell in a magnificent network with many lovely people with valuable secrets. I feel very much privileged to have discovered your entire web page and look forward to many more pleasurable moments reading here. Thanks a lot again for a lot of things.
I simply desired to appreciate you yet again. I’m not certain what I would have created without those pointers shared by you on that theme. It actually was a real frightening problem in my view, but witnessing the very specialised style you resolved the issue forced me to jump with contentment. I will be grateful for your work and in addition expect you comprehend what a powerful job you are always putting in instructing the others using a blog. Probably you’ve never encountered all of us.
My spouse and i were thankful that Chris could round up his web research while using the ideas he discovered using your site. It is now and again perplexing just to always be freely giving facts which some other people may have been selling. And we also see we have got the blog owner to appreciate for this. Those illustrations you have made, the easy blog navigation, the relationships you will aid to instill – it is all sensational, and it’s leading our son in addition to us recognize that this idea is pleasurable, which is certainly seriously important. Many thanks for all the pieces!
Thank you for all of the labor on this website. Ellie really loves getting into investigations and it’s really simple to grasp why. Most people learn all relating to the compelling way you deliver priceless tricks through your blog and in addition recommend participation from the others on this idea plus my princess is really starting to learn a lot. Have fun with the remaining portion of the new year. You have been performing a really great job.
I simply desired to thank you very much yet again. I do not know what I would’ve done in the absence of the tactics shown by you about that question. Previously it was the fearsome problem in my position, nevertheless witnessing the well-written avenue you solved the issue forced me to leap over contentment. I am happier for your service and in addition have high hopes you comprehend what a great job you were doing teaching many others using a site. Most probably you haven’t got to know all of us.
I’m also writing to let you know of the fine discovery my cousin’s child encountered studying your webblog. She noticed such a lot of things, which include what it is like to have an incredible giving spirit to let many more clearly fully grasp specified impossible issues. You truly surpassed our desires. I appreciate you for coming up with those helpful, dependable, revealing and also easy tips about the topic to Sandra.
I enjoy you because of all of the hard work on this website. My daughter really likes working on investigation and it’s obvious why. My spouse and i learn all of the compelling way you render practical guidance via this blog and recommend participation from other people on this idea so my daughter is always becoming educated a lot of things. Take advantage of the rest of the year. Your performing a brilliant job.