wyh-l6

导航

Statefulset+Deamonset控制器实战(简单版本)

Statefulset说明:
    为了解决有状态服务的部署,例如mysql的主从等
    pod拥有唯一且固定的名称
    按照顺序对pod进行启停、伸缩和回收
    Headless Services(无头服务)请求直接解析到pod的ip上
    
参考链接:https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/statefulset/
    
yaml文件示例:
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx-sts
  namespace: wyh-test1-ns
  labels:
    app: nginx-sts
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-sts-pod
  serviceName: nginx-svc
  template:
    metadata:
      labels:
        app: nginx-sts-pod
    spec:
      containers:
      - name: nginx-sts-pod
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: wyh-test1-ns
  labels:
    app: nginx-svc
spec:
  type: ClusterIP
  clusterIP: None
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    app: nginx-sts-pod
 
运行文件:
kubectl apply -f nginx-statefulset.yaml
增加到三个节点:
kubectl scale --replicas=3 sts nginx-sts -nwyh-test1-ns
无头service:
 
################################################################################################
 
DeamonSet简介:
    在当前每个节点运行同一个pod,当有新节点加入集群时,也会在新节点运行相同的pod,当节点从集群中移除时,其pod也会被回收且不会在其他机器上重建
    参考链接:https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/daemonset/
 
yaml文件示例(信任了master的污点,会在master节点运行pod):
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-ds
  namespace: wyh-test1-ns
  labels:
    app: nginx-ds
spec:
  selector:
    matchLabels:
      app: nginx-ds-pod
  template:
    metadata:
      labels:
        app: nginx-ds-pod
    spec:
      #污点信任
      tolerations:
      - effect: NoSchedule
        key: node.kubernetes.io/unschedulable
        operator: Exists
      hostNetwork: true  #使用宿主机网络(如果此nginx是访问入口,那么使用hostnetwork性能会更好)
      hostPID: true      #使用宿主机进程
      containers:
      - name: nginx-ds-pod
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: wyh-test1-ns
  labels:
    app: nginx-svc
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    app: nginx-ds-pod
 
运行文件:
kubectl apply -f nginx-deamonset.yaml
查看节点运行情况(192.168.213.11是master):
kubectl get pod -A -owide | grep nginx-ds
 
 
 
 
 
 
 
 

posted on 2022-08-15 09:57  IT老六  阅读(57)  评论(0编辑  收藏  举报