[root@k8s-master ~]# vim test01.yaml
[root@k8s-master ~]# cat test01.yaml
apiVersion: v1
kind: Service
metadata:name: redislabels: app: redis-db
spec:ports:- name: redisportport: 27017clusterIP: Noneselector:app: redis-db
---apiVersion: apps/v1
kind: StatefulSet
metadata: name: redis-dbnamespace: defaultlabels:app: redis
spec:updateStrategy:rollingUpdate:partition: 3type: RollingUpdateselector:matchLabels:app: redisreplicas: 10serviceName: "redis"minReadySeconds: 10template:metadata:labels:app: redisspec:terminationGracePeriodSeconds: 10containers:- name: redisimage: docker.io/library/redis:latestimagePullPolicy: Neverports:- name: redisportcontainerPort: 27017[root@k8s-master ~]# kubectl create -f test01.yaml
service/redis created
statefulset.apps/redis-db created
[root@k8s-master ~]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 7d20h
metrics-server ClusterIP 10.96.178.37 <none> 443/TCP 7d
您在 /var/spool/mail/root 中有新邮件
[root@k8s-master ~]# kubectl get svc -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d20h
redis ClusterIP None <none> 27017/TCP 3m53s
[root@k8s-master ~]# kubectl get sts
NAME READY AGE
redis-db 3/3 4m9s
[root@k8s-master ~]# kubectl get pods -n default -l app=redis
NAME READY STATUS RESTARTS AGE
redis-db-0 1/1 Running 0 4m38s
redis-db-1 1/1 Running 0 4m18s
redis-db-2 1/1 Running 0 3m58s
[root@k8s-master ~]# kubectl get pods -n default -l app=redis --show-labels
NAME READY STATUS RESTARTS AGE LABELS
redis-db-0 1/1 Running 0 4m56s app=redis,apps.kubernetes.io/pod-index=0,controller-revision-hash=redis-db-7d674fd94d,statefulset.kubernetes.io/pod-name=redis-db-0
redis-db-1 1/1 Running 0 4m36s app=redis,apps.kubernetes.io/pod-index=1,controller-revision-hash=redis-db-7d674fd94d,statefulset.kubernetes.io/pod-name=redis-db-1
redis-db-2 1/1 Running 0 4m16s app=redis,apps.kubernetes.io/pod-index=2,controller-revision-hash=redis-db-7d674fd94d,statefulset.kubernetes.io/pod-name=redis-db-2
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d23h
redis ClusterIP None <none> 27017/TCP 44s
[root@k8s-master ~]# kubectl get sts
NAME READY AGE
redis-db 3/3 63s[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test5-6597c95b6c-28tkj 1/1 Running 30 (134m ago) 6d22h
redis-db-0 1/1 Running 0 89s
redis-db-1 1/1 Running 0 69s
redis-db-2 1/1 Running 0 49s删除:只删除了服务,pod没有删除[root@k8s-master ~]# kubectl delete -f test01.yaml --cascade=false
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
service "redis" deleted
statefulset.apps "redis-db" deleted[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
cluster-test5-6597c95b6c-28tkj 1/1 Running 30 (135m ago) 6d22h
redis-db-0 1/1 Running 0 3m12s
redis-db-1 1/1 Running 0 2m52s
redis-db-2 1/1 Running 0 2m32s[root@k8s-master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d23h
[root@k8s-master ~]# kubectl get sts
No resources found in default namespace.
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d23h[root@k8s-master ~]# for i in 0 1 2;do kubectl delete pod redis-db-$i; done
pod "redis-db-0" deleted
pod "redis-db-1" deleted
pod "redis-db-2" deleted级联删除:服务和pod都删除了[root@k8s-master ~]# kubectl delete -f test01.yaml
修改和更新StatefulSet
修改镜像:
[root@k8s-master ~]# kubectl set image sts redis-db redis=docker.io/library/redis:7.0.14
statefulset.apps/redis-db image updated
[root@k8s-master ~]# kubectl describe pod redis-db-1|grep Image:
Image: docker.io/library/redis:7.0.14
[root@k8s-master ~]# for i in 0 1 2 ;do kubectl describe pod redis-db-$i|grep Image:;done
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:7.0.14
更新顺序:
[root@k8s-master ~]# kubectl set image sts redis-db redis=docker.io/library/redis:latest
statefulset.apps/redis-db image updated
[root@k8s-master ~]# for i in 0 1 2 ;do kubectl describe pod redis-db-$i|grep Image:;done
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:latest
[root@k8s-master ~]# for i in 0 1 2 ;do kubectl describe pod redis-db-$i|grep Image:;done
Image: docker.io/library/redis:7.0.14
Image: docker.io/library/redis:latest
Image: docker.io/library/redis:latest
on delete
rolling update:从序号最大的pod开始更新
扩容:
[root@k8s-master ~]# kubectl scale statefulset redis-db --replicas 4
statefulset.apps/redis-db scaled
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
cluster-test5-6597c95b6c-28tkj 1/1 Running 31 (14m ago) 6d23h
redis-db-0 1/1 Running 0 3m13s
redis-db-1 1/1 Running 0 3m33s
redis-db-2 1/1 Running 0 3m53s
redis-db-3 1/1 Running 0 6s
缩容:
[root@k8s-master ~]# kubectl scale statefulset redis-db --replicas 3
statefulset.apps/redis-db scaled
[root@k8s-master ~]# kubectl get po
NAME READY STATUS RESTARTS AGE
cluster-test5-6597c95b6c-28tkj 1/1 Running 31 (20m ago) 6d23h
redis-db-0 1/1 Running 0 9m36s
redis-db-1 1/1 Running 0 9m56s
redis-db-2 1/1 Running 0 10m
分段更新:
删除服务和控制器
[root@k8s-master ~]# kubectl delete -f test01.yaml
service "redis" deleted
statefulset.apps "redis-db" deleted
修改资源清单(yaml文件)
[root@k8s-master ~]# kubectl create -f test01.yaml
service/redis created
statefulset.apps/redis-db created[root@k8s-master ~]# for i in 0 1 2 3 4 5 6 7 8 9;do kubectl describe pod redis-db-$i|grep Image:;doneImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latest
您在 /var/spool/mail/root 中有新邮件
[root@k8s-master ~]# kubectl set image sts redis-db redis=docker.io/library/redis:7.0.14
statefulset.apps/redis-db image updated
[root@k8s-master ~]# for i in 0 1 2 3 4 5 6 7 8 9;do kubectl describe pod redis-db-$i|grep Image:;doneImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:7.0.14[root@k8s-master ~]# for i in 0 1 2 3 4 5 6 7 8 9;do kubectl describe pod redis-db-$i|grep Image:;doneImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:latestImage: docker.io/library/redis:7.0.14Image: docker.io/library/redis:7.0.14Image: docker.io/library/redis:7.0.14Image: docker.io/library/redis:7.0.14Image: docker.io/library/redis:7.0.14Image: docker.io/library/redis:7.0.14Image: docker.io/library/redis:7.0.14
打标签:
297 kubectl get nodes --show-labels298 kubectl label nodes k8s-node02 abc=1300 kubectl label nodes k8s-node01 abc=1301 kubectl get nodes --show-labels302 kubectl label nodes k8s-node02 abc=2 --overwrite[root@k8s-master pods]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready control-plane 8d v1.28.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01 Ready <none> 8d v1.28.2 abc=1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
k8s-node02 Ready <none> 8d v1.28.2 abc=1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux
[root@k8s-master pods]# kubectl label nodes k8s-node02 abc=2 --overwrite
[root@k8s-master pods]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready control-plane 8d v1.28.2 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01 Ready <none> 8d v1.28.2 abc=1,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux
k8s-node02 Ready <none> 8d v1.28.2 abc=2,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux
DaemonSet:监控
[root@k8s-master pods]# cat test0028.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:name: nginx
spec:selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: docker.io/library/nginx:latestimagePullPolicy: Neverports:- name: nginxportcontainerPort: 80[root@k8s-master pods]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
cluster-test5-6597c95b6c-28tkj 1/1 Running 33 (6m7s ago) 7d 172.16.58.230 k8s-node02 <none> <none> app=cluster-test5,pod-template-hash=6597c95b6c
nginx-8vkwj 1/1 Running 0 15m 172.16.85.235 k8s-node01 <none> <none> app=nginx,controller-revision-hash=7499fc66f4,pod-template-generation=1
nginx-kkmqh 1/1 Running 0 15m 172.16.58.252 k8s-node02 <none> <none> app=nginx,controller-revision-hash=7499fc66f4,pod-template-generation=1[root@k8s-master pods]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cluster-test5-6597c95b6c-28tkj 1/1 Running 33 (6m12s ago) 7d 172.16.58.230 k8s-node02 <none> <none>
nginx-8vkwj 1/1 Running 0 15m 172.16.85.235 k8s-node01 <none> <none>
nginx-kkmqh 1/1 Running 0 15m 172.16.58.252 k8s-node02 <none> <none>
只要有新的或者是符合条件的节点加入到cluster ,就会自动在这个节点上布署一个pod,一旦节点不符合,该节点上的pod就会回收(销毁)