k8s学习(十四) DaemonSet&StatefulSet
1、DaemonSetDaemon,就是用来部署守护进程的, DaemonSet 用于在每个 Kubernetes 节点中将守护进程的副本作为后台进程运行,说白了就是在每个节点部署一个 Pod 副本,当节点加入到 Kubernetes 集群中, Pod 会被调度到该节点上运行,当节点从集群只能够被移除后,该节点上的这个 Pod 也会被移除,当然,如果我们删除 DaemonSet ,所有和这个对..
1、DaemonSet
Daemon,就是用来部署守护进程的, DaemonSet 用于在每个 Kubernetes 节点中将守护进程的副本作为后台进程运行,说白了就是在每个节点部署一个 Pod 副本,当节点加入到 Kubernetes 集群中, Pod 会被调度到该节点上运行,当节点从集群只能够被移除后,该节点上的这个 Pod 也会被移除,当然,如果我们删除 DaemonSet ,所有和这个对象相关的 Pods 都会被删除。
应用场景:
•集群存储守护程序,如 glusterd 、 ceph 要部署在每个节点上以提供持久性存储;
•节点监视守护进程,如 Prometheus 监控集群,可以在每个节点上运行一个 node-exporter 进程来收集监控节点的信息;
•日志收集守护程序,如 fluentd 或 logstash ,在每个节点上运行以收集容器的日志
创建daemon.yaml
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
name: nginx-ds
labels:
k8s-app: nginx
spec:
template:
metadata:
labels:
k8s-app: nginx
spec:
containers:
- image: 172.16.10.190:8008/nginx
name: nginx
ports:
- name: http
containerPort: 80
imagePullSecrets:
- name: myregistrykey6
kubectl apply -f daemon.yaml
[root@k8s-node1 k8s]# kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-hpa-demo-d789d89b-9xgdx 1/1 Running 0 22h 10.244.1.9 k8s-node2 <none> <none>
kube-hpa-demo-d789d89b-pw77j 1/1 Running 0 22h 10.244.1.8 k8s-node2 <none> <none>
kube-node-6545cf5596-khw4x 1/1 Running 0 23h 10.244.2.3 k8s-node3 <none> <none>
kube-node-6545cf5596-q7kd7 1/1 Running 0 23h 10.244.1.5 k8s-node2 <none> <none>
liveness-tcpsocket 1/1 Running 0 23h 10.244.1.6 k8s-node2 <none> <none>
nginx-ds-7spc2 1/1 Running 0 110s 10.244.1.12 k8s-node2 <none> <none>
nginx-ds-ffd9v 1/1 Running 0 111s 10.244.2.13 k8s-node3 <none> <none>
static-nginx-k8s-node1 1/1 Running 0 23h 10.244.0.2 k8s-node1 <none> <none>
可以看到nginx在每个节点上都部署了一个pod
2、StatefulSet
StatefulSet 类似于 ReplicaSet ,但是它可以处理 Pod 的启动顺序,为保留每个 Pod 的状态设置唯一标识,同时具有以下功能:
•稳定的、唯一的网络标识符
•稳定的、持久化的存储
•有序的、优雅的部署和缩放
•有序的、优雅的删除和终止
•有序的、自动滚动更新
2.1 创建两个pv
pv1.yaml:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
labels:
release: stable
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
hostPath:
path: /tmp/data
pv2.yaml:
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
labels:
release: stable
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
hostPath:
path: /tmp/data
[root@k8s-node1 k8s]# kubectl create -f pv1.yaml
persistentvolume/pv001 created
[root@k8s-node1 k8s]# gedit pv2.yaml
[root@k8s-node1 k8s]# kubectl create -f pv2.yaml
persistentvolume/pv002 created
[root@k8s-node1 k8s]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv001 1Gi RWO Recycle Available 28s
pv002 1Gi RWO Recycle Available 5s
2.2 使用 StatefulSet 来创建一个 Nginx 的 Pod
对于这种类型的资源,我们一般是通过创建一个 Headless Service 类型的服务来暴露服务,将 clusterIP 设置为 None 就是一个无头的服务。
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 2 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: 172.16.10.190:8008/nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
imagePullSecrets:
- name: myregistrykey6
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
# storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
[root@k8s-node1 k8s]# kubectl apply -f nginx-stateful-demo.yaml
service/nginx created
statefulset.apps/web created
[root@k8s-node1 k8s]# kubectl get pods
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 0 44m
helloworld011-5cf986495c-6zslr 1/1 Running 0 22m
helloworld011-5cf986495c-9xh5d 1/1 Running 0 22m
kube-hpa-demo-d789d89b-9xgdx 1/1 Running 0 41h
kube-hpa-demo-d789d89b-pw77j 1/1 Running 0 41h
kube-node-6545cf5596-khw4x 1/1 Running 0 41h
kube-node-6545cf5596-q7kd7 1/1 Running 0 41h
liveness-tcpsocket 1/1 Running 0 41h
nginx-ds-7spc2 1/1 Running 0 18h
nginx-ds-ffd9v 1/1 Running 0 18h
static-nginx-k8s-node1 1/1 Running 0 41h
web-0 1/1 Running 0 48m
web-1 1/1 Running 0 48m
[root@k8s-node1 k8s]# kubectl get statefulset
NAME READY AGE
web 2/2 48m
statefulset的应用即使重启了也可以使用 <pod-name>.<service-name>.<namepsace>.svc.cluster.local
进行访问,不管IP变动与否
更多推荐
所有评论(0)