kind:Service apiVersion:v1 metadata: namespace:test-rabbitmq name:rabbitmq labels: app:rabbitmq type:LoadBalancer spec: type:NodePort ports: -name:http protocol:TCP port:15672 targetPort:15672 nodePort:31672 -name:amqp protocol:TCP port:5672 targetPort:5672 nodePort:30672 selector: app:rabbitmq --- apiVersion:v1 kind:ConfigMap metadata: name:rabbitmq-config namespace:test-rabbitmq data: enabled_plugins:| [rabbitmq_management,rabbitmq_peer_discovery_k8s]. rabbitmq.conf:| ## Cluster formation. See http://www.rabbitmq.com/cluster-formation.html to learn more. cluster_formation.peer_discovery_backend=rabbit_peer_discovery_k8s cluster_formation.k8s.host=kubernetes.default.svc.cluster.local ## Should RabbitMQ node name be computed from the pod's hostname or IP address? ## IP addresses are not stable, so using [stable] hostnames is recommended when possible. ## Set to "hostname" to use pod hostnames. ## When this value is changed, so should the variable used to set the RABBITMQ_NODENAME ## environment variable. cluster_formation.k8s.address_type=ip ## How often should node cleanup checks run? cluster_formation.node_cleanup.interval=30 ## Set to false if automatic removal of unknown/absent nodes ## is desired. This can be dangerous, see ## * http://www.rabbitmq.com/cluster-formation.html#node-health-checks-and-cleanup ## * https://groups.google.com/forum/#!msg/rabbitmq-users/wuOfzEywHXo/k8z_HWIkBgAJ cluster_formation.node_cleanup.only_log_warning=true cluster_partition_handling=autoheal ## See http://www.rabbitmq.com/ha.html#master-migration-data-locality queue_master_locator=min-masters ## See http://www.rabbitmq.com/access-control.html#loopback-users loopback_users.guest=false --- apiVersion:apps/v1beta1 kind:StatefulSet metadata: name:rabbitmq namespace:test-rabbitmq spec: serviceName:rabbitmq replicas:3 template: metadata: labels: app:rabbitmq spec: serviceAccountName:rabbitmq terminationGracePeriodSeconds:10 containers: -name:rabbitmq-k8s image:rabbitmq:3.7 volumeMounts: -name:config-volume mountPath:/etc/rabbitmq ports: -name:http protocol:TCP containerPort:15672 -name:amqp protocol:TCP containerPort:5672 livenessProbe: exec: command: ["rabbitmqctl", "status"] initialDelaySeconds:60 periodSeconds:60 timeoutSeconds:10 readinessProbe: exec: command: ["rabbitmqctl", "status"] initialDelaySeconds:20 periodSeconds:60 timeoutSeconds:10 imagePullPolicy:Always env: -name:MY_POD_IP valueFrom: fieldRef: fieldPath:status.podIP -name:RABBITMQ_USE_LONGNAME value:"true" # See a note on cluster_formation.k8s.address_type in the config file section -name:RABBITMQ_NODENAME value:"rabbit@$(MY_POD_IP)" -name:K8S_SERVICE_NAME value:"rabbitmq" -name:RABBITMQ_ERLANG_COOKIE value:"mycookie" volumes: -name:config-volume configMap: name:rabbitmq-config items: -key:rabbitmq.conf path:rabbitmq.conf -key:enabled_plugins path:enabled_plugins
但直接修改address_type 并不能满足要求,注释部分也描述了“Set to hostname to use pod hostnames. When this value is changed, so should the variable used to set the RABBITMQ_NODENAME”。那么RABBITMQ_NODENAME该如何设置,就必须先要了解如何用hostname访问pod
apiVersion:v1 kind:Namespace metadata: name:rabbitmq --- apiVersion:v1 kind:ServiceAccount metadata: name:rabbitmq namespace:rabbitmq --- kind:Role apiVersion:rbac.authorization.k8s.io/v1beta1 metadata: name:endpoint-reader namespace:rabbitmq rules: -apiGroups: [""] resources: ["endpoints"] verbs: ["get"] --- kind:RoleBinding apiVersion:rbac.authorization.k8s.io/v1beta1 metadata: name:endpoint-reader namespace:rabbitmq subjects: -kind:ServiceAccount name:rabbitmq roleRef: apiGroup:rbac.authorization.k8s.io kind:Role name:endpoint-reader --- apiVersion:v1 kind:PersistentVolume metadata: name:rabbitmq-data labels: release:rabbitmq-data namespace:rabbitmq spec: capacity: storage:10Gi accessModes: -ReadWriteMany persistentVolumeReclaimPolicy:Retain nfs: path:/rabbit server:xxxxx# nas地址 --- apiVersion:v1 kind:PersistentVolumeClaim metadata: name:rabbitmq-data-claim namespace:rabbitmq spec: accessModes: -ReadWriteMany resources: requests: storage:10Gi selector: matchLabels: release:rabbitmq-data --- # headless service 用于使用hostname访问pod kind:Service apiVersion:v1 metadata: name:rabbitmq-headless namespace:rabbitmq spec: clusterIP:None # publishNotReadyAddresses, when set to true, indicates that DNS implementations must publish the notReadyAddresses of subsets for the Endpoints associated with the Service. The default value is false. The primary use case for setting this field is to use a StatefulSet's Headless Service to propagate SRV records for its Pods without respect to their readiness for purpose of peer discovery. This field will replace the service.alpha.kubernetes.io/tolerate-unready-endpoints when that annotation is deprecated and all clients have been converted to use this field. # 由于使用DNS访问Pod需Pod和Headless service启动之后才能访问,publishNotReadyAddresses设置成true,防止readinessProbe在服务没启动时找不到DNS publishNotReadyAddresses:true ports: -name:amqp port:5672 -name:http port:15672 selector: app:rabbitmq --- # 用于暴露dashboard到外网 kind:Service apiVersion:v1 metadata: namespace:rabbitmq name:rabbitmq-service spec: type:NodePort ports: -name:http protocol:TCP port:15672 targetPort:15672 nodePort:15672 -name:amqp protocol:TCP port:5672 targetPort:5672 selector: app:rabbitmq --- apiVersion:v1 kind:ConfigMap metadata: name:rabbitmq-config namespace:rabbitmq data: enabled_plugins:| [rabbitmq_management,rabbitmq_peer_discovery_k8s]. rabbitmq.conf:| cluster_formation.peer_discovery_backend=rabbit_peer_discovery_k8s cluster_formation.k8s.host=kubernetes.default.svc.cluster.local cluster_formation.k8s.address_type=hostname cluster_formation.node_cleanup.interval=10 cluster_formation.node_cleanup.only_log_warning=true cluster_partition_handling=autoheal queue_master_locator=min-masters loopback_users.guest=false