# singlenode-rabbitmq

* 数据存储采用 hostpath 方式
* Pod 多节点反亲和性调度，单节点失效
* Node 调度， 只运行在指定的节点上



```yaml
[root@master test]# cat singlenode-rabbitmq-statefulset.yaml 
apiVersion: v1
kind: Service
metadata:
  name: singlenode-rabbitmq
spec:
  type: NodePort
  #clusterIP: None
  ports:
    - name: singlenode-amqp
      port: 5672
      targetPort: 5672
      nodePort: 31671
    - name: singlenode-http
      port: 15672
      targetPort: 15672
      nodePort: 31672
  selector:
    app: singlenode-rabbitmq

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: singlenode-rabbitmq
spec:
  serviceName: singlenode-rabbitmq
  replicas: 1
  selector:
    matchLabels:
      app: singlenode-rabbitmq
  template:
    metadata:
      labels:
        app: singlenode-rabbitmq
    spec:
      # 节点打上中间件标签并污染：kubectl taint nodes <node-name> middleware=true:NoSchedule
      #tolerations:
      #  - key: "middleware"
      #    operator: "Equal"
      #    value: "true"
      #    effect: "NoSchedule"
      
      # 当多节点生效：不允许同一个pod调度到同一个节点
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - middleware
              topologyKey: "kubernetes.io/hostname"
      #initContainers:
      #  - name: init-singlenode-rabbitmq
      #    image: busybox
      #    command: ['sh', '-c', 'mkdir -p /data/k8s/rabbitmq']
      #    volumeMounts:
      #      - name: rabbitmq-data
      #        mountPath: /data/k8s/rabbitmq/
      containers:
        - name: singlenoderabbitmq
          image: rabbitmq:3.10-management
          env:
            - name: RABBITMQ_DEFAULT_USER
              value: admin
            - name: RABBITMQ_DEFAULT_PASS
              value: admin
            - name: TZ
              value: America/Sao_Paulo # 设置时区为巴西时间
              #value: Asia/Shanghai # 设置时区为上海
          ports:
            - containerPort: 5672
              name: singlenode-amqp
              protocol: TCP
            - containerPort: 15672
              name: singlenode-http
              protocol: TCP
          volumeMounts:
            - name: rabbitmq-data
              mountPath: /var/lib/rabbitmq
      volumes:
        - name: rabbitmq-data
          hostPath:
            path: /data/k8s/rabbitmq/
```

