以consul 为例

首先搭建有状态集群服务,先要了解服务集群本身的工作原理,conusl 在启动的时候,是只要bootstrap-expect 达到数量,集群内部就可以选举出leader

另外,headless 服务,和一般service 相比,差别是,没有clusterip,所以在发现服务的时候,是直接绑定了dns,所以在consul 节点加入集群的时候,可以写-retry-join=$(NAMESPACE), 这个里的命名空间变量是headless 的服务名字,nslookup 解析的时候 就可以解析出集群的所有绑定上的ip

以下为yaml 配置


这里有必要提一句,一般无头服务都要配置动态存储来使用,这样方便 当集群中有成员挂掉,当再次加入进群的时候,会根据自身的id 与绑定的存储空间,可以快速加载之前的数据

consul_statefulset.yml

consul_statefulset.yml
consul_statefulset.yml
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: qa-consul
spec:
  serviceName: qa-consul
  replicas: 5
  template:
    metadata:
      labels:
        app: qa-consul
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - qa-consul
              topologyKey: kubernetes.io/hostname
      terminationGracePeriodSeconds: 10
      containers:
      - name: consul
        image: consul:latest
        args:
             - "agent"
             - "-server"
             - "-bootstrap-expect=3"
             - "-ui"
             - "-data-dir=/consul/data"
             - "-bind=0.0.0.0"
             - "-client=0.0.0.0"
             - "-advertise=$(PODIP)"
             - "-retry-join=qa-consul"
            # - "-retry-join=consul-0.consul.$(NAMESPACE).svc.cluster.local"
            # - "-retry-join=consul-1.consul.$(NAMESPACE).svc.cluster.local"
            # - "-retry-join=consul-2.consul.$(NAMESPACE).svc.cluster.local"
             - "-domain=cluster.local"
             - "-disable-host-node-id"
        volumeMounts:
            - name: data
              mountPath: /consul/data
        env:
            - name: PODIP
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
        ports:
            - containerPort: 8500
              name: ui-port
            - containerPort: 8400
              name: alt-port
            - containerPort: 53
              name: udp-port
            - containerPort: 8443
              name: https-port
            - containerPort: 8080
              name: http-port
            - containerPort: 8301
              name: serflan
            - containerPort: 8302
              name: serfwan
            - containerPort: 8600
              name: consuldns
            - containerPort: 8300
              name: server
      volumes:
        - name: data
          hostPath:
            path: /home/data

consul_service.yml

apiVersion: v1
kind: Service
metadata:  
    name: qa-consul  
    labels:    
      name: qa-consul
spec:  
    type: ClusterIP  
    ports:    
     - name: http      
       port: 8500      
       targetPort: 8500    
     - name: https      
       port: 8443      
       targetPort: 8443    
     - name: rpc      
       port: 8400      
       targetPort: 8400    
     - name: serflan-tcp      
       protocol: "TCP"      
       port: 8301      
       targetPort: 8301    
     - name: serflan-udp      
       protocol: "UDP"      
       port: 8301      
       targetPort: 8301    
     - name: serfwan-tcp      
       protocol: "TCP"      
       port: 8302      
       targetPort: 8302    
     - name: serfwan-udp      
       protocol: "UDP"      
       port: 8302      
       targetPort: 8302    
     - name: server      
       port: 8300      
       targetPort: 8300    
     - name: consuldns      
       port: 8600      
       targetPort: 8600  
    selector:    
      app: qa-consul
    clusterIP: None


bootstrap-expect
bootstrap-expect
bootstrap-expect

未完 待续。。。。。