使用ReplicaSets
1.创建replicaset
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: rs-one
spec:
replicas: 2
template:
metadata:
labels:
system: ReplicaOne
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
student@ubuntu:~/job$kubectl create -f rs.yaml
replicaset.extensions/rs-one created
2.查看replicaset的状态
student@ubuntu:~/job$kubectl get rs
NAME DESIRED CURRENT READY AGE
rs-one 2 2 2 47s
student@ubuntu:~/job$kubectl describe replicasets. rs-one
Name: rs-one
Namespace: default
Selector: system=ReplicaOne
Labels: system=ReplicaOne
Annotations: <none>
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: system=ReplicaOne
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 88s replicaset-controller Created pod: rs-one-gccmz
Normal SuccessfulCreate 88s replicaset-controller Created pod: rs-one-j7p55
3.查看rs创建的pod
student@ubuntu:~/job$kubectl get po
NAME READY STATUS RESTARTS AGE
rs-one-gccmz 1/1 Running 0 3m15s
rs-one-j7p55 1/1 Running 0 3m15s
4.删除rs,而不删除创建的pod
student@ubuntu:~/job$kubectl delete rs rs-one --cascade=false
replicaset.extensions "rs-one" deleted
student@ubuntu:~/job$kubectl get pod,rs
NAME READY STATUS RESTARTS AGE
pod/rs-one-gccmz 1/1 Running 0 4m29s
pod/rs-one-j7p55 1/1 Running 0 4m29s
- 不更新label的情况下创建rs
student@ubuntu:~/job$kubectl get rs
NAME DESIRED CURRENT READY AGE
rs-one 2 2 2 16s
student@ubuntu:~/job$kubectl get pod
NAME READY STATUS RESTARTS AGE
rs-one-gccmz 1/1 Running 0 5m35s
rs-one-j7p55 1/1 Running 0 5m35s
- 修改pod label使其脱离rs的管控
student@ubuntu:~/job$kubectl edit po rs-one-gccmz
...
kind: Pod
metadata:
annotations:
cni.projectcalico.org/podIP: 192.168.0.169/32
kubernetes.io/limit-ranger: 'LimitRanger plugin set: cpu, memory request for container
nginx; cpu, memory limit for container nginx'
creationTimestamp: 2018-12-04T02:10:16Z
generateName: rs-one-
labels:
system: ReplicaOne-new
name: rs-one-gccmz
student@ubuntu:~/job$kubectl get pod
NAME READY STATUS RESTARTS AGE
rs-one-88cjg 1/1 Running 0 56s
rs-one-gccmz 1/1 Running 0 7m59s
rs-one-j7p55 1/1 Running 0 7m59s
7.查看pod的label
student@ubuntu:~/job$kubectl get pod -L system
NAME READY STATUS RESTARTS AGE SYSTEM
rs-one-88cjg 1/1 Running 0 111s ReplicaOne
rs-one-gccmz 1/1 Running 0 8m54s ReplicaOne-new
rs-one-j7p55 1/1 Running 0 8m54s ReplicaOne
8.删除rs
student@ubuntu:~/job$kubectl delete rs rs-one
replicaset.extensions "rs-one" deleted
student@ubuntu:~/job$kubectl get po
NAME READY STATUS RESTARTS AGE
rs-one-gccmz 1/1 Running 0 9m29s
rs-one-j7p55 0/1 Terminating 0 9m29s
9.使用label删除pod
student@ubuntu:~/job$kubectl delete pod -l system=ReplicaOne-new
pod "rs-one-gccmz" deleted
使用daemonset
1.创建daemonset
student@ubuntu:~/job$cat ds.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: rs-one
spec:
template:
metadata:
labels:
system: ReplicaOne
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
student@ubuntu:~/job$kubectl create -f ds.yaml
daemonset.extensions/rs-one created
2.查看daemonset状态
student@ubuntu:~/job$kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
rs-one 2 2 2 2 2 <none> 28s
student@ubuntu:~/job$kubectl get po
NAME READY STATUS RESTARTS AGE
rs-one-dmdxv 1/1 Running 0 33s
rs-one-jr62m 1/1 Running 0 33s
滚动升级和回滚
1.查看升级的policy
student@ubuntu:~/job$kubectl get ds rs-one -o yaml|grep -A 1 -i stra
updateStrategy:
type: OnDelete
2.升级ds
student@ubuntu:~/job$kubectl set image ds rs-one nginx=nginx:1.9.10
daemonset.extensions/rs-one image updated
3.查看升级状态,ds已更新,pod未更新
student@ubuntu:~/job$kubectl describe ds rs-one |grep -i image
Image: nginx:1.9.10
student@ubuntu:~/job$kubectl describe pod |grep -i image
Image: nginx
Image ID: docker-pullable://nginx@sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba
Normal Pulling 5m39s kubelet, node-193 pulling image "nginx"
Normal Pulled 5m35s kubelet, node-193 Successfully pulled image "nginx"
Image: nginx
Image ID: docker-pullable://nginx@sha256:5d32f60db294b5deb55d
4.删除pod,查看更新情况
student@ubuntu:~/job$kubectl get pod
NAME READY STATUS RESTARTS AGE
rs-one-dmdxv 1/1 Running 0 7m12s
rs-one-jr62m 1/1 Running 0 7m12s
student@ubuntu:~/job$kubectl delete pod rs-one-dmdxv
pod "rs-one-dmdxv" deleted
student@ubuntu:~/job$kubectl describe po |grep -i image
Image: nginx:1.9.10
Image ID:
Normal Pulling 9s kubelet, node-193 pulling image "nginx:1.9.10"
Image: nginx
5.查看升级历史
student@ubuntu:~/job$kubectl rollout history ds
daemonset.extensions/rs-one
REVISION CHANGE-CAUSE
1 <none>
2 <none>
student@ubuntu:~/job$kubectl rollout history ds rs-one --revision=2
daemonset.extensions/rs-one with revision #2
Pod Template:
Labels: system=ReplicaOne
Containers:
nginx:
Image: nginx:1.9.10
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
student@ubuntu:~/job$kubectl rollout history ds rs-one --revision=1
daemonset.extensions/rs-one with revision #1
Pod Template:
Labels: system=ReplicaOne
Containers:
nginx:
Image: nginx
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
6.回滚版本
student@ubuntu:~/job$kubectl rollout undo ds rs-one --to-revision=1
daemonset.extensions/rs-one rolled back
student@ubuntu:~/job$kubectl describe ds rs-one |grep -i image
Image: nginx
7.pod的升级policy是Ondelete,所以没有改变
student@ubuntu:~/job$kubectl describe pod |grep -i image:
Image: nginx:1.9.10
Image: nginx
student@ubuntu:~/job$kubectl delete pod --all
pod "rs-one-jphxj" deleted
pod "rs-one-jr62m" deleted
student@ubuntu:~/job$kubectl describe pod |grep -i image:
Image: nginx
Image: nginx
8.修改ds的升级为RollingUpdate
student@ubuntu:~/job$kubectl edit ds rs-one
updateStrategy:
type: RollingUpdate
9.查看升级状态
student@ubuntu:~/job$kubectl describe pod |grep -i image:
Image: nginx
Image: nginx
student@ubuntu:~/job$kubectl set image ds rs-one nginx=nginx:1.9.10
daemonset.extensions/rs-one image updated
student@ubuntu:~/job$kubectl get pod
NAME READY STATUS RESTARTS AGE
rs-one-797cw 1/1 Running 0 2m47s
rs-one-mb6ns 0/1 Terminating 0 2m47s
student@ubuntu:~/job$kubectl get pod
NAME READY STATUS RESTARTS AGE
rs-one-797cw 1/1 Running 0 2m55s
rs-one-dm6rl 0/1 ContainerCreating 0 5s
student@ubuntu:~/job$kubectl get pod
NAME READY STATUS RESTARTS AGE
rs-one-dm6rl 1/1 Running 0 14s
rs-one-v74l7 1/1 Running 0 7s
student@ubuntu:~/job$kubectl describe pod |grep -i image
Image: nginx:1.9.10
Image ID: docker-pullable://nginx@sha256:ca132615822fdf485c49ce2ca8d7d04494136af6c90e55c0f0b3a7f274817e0b
Normal Pulling 24s kubelet, ubuntu pulling image "nginx:1.9.10"
Normal Pulled 21s kubelet, ubuntu Successfully pulled image "nginx:1.9.10"
Image: nginx:1.9.10