生成secret来保存relang-cookie:
secret.yml文件:
apiVersion: v1
kind: Secret
metadata:
name: rabbit-cookie
type: Opaque
data:
cookie: UERVU0hWVU9KT1ZQVUVYT0JXWU8=
生成statefulset:
rabbit-cluster.yml
--- apiVersion: v1 kind: Service metadata: name: rabbitmq-management labels: app: rabbitmq spec: ports: - port: 15672 name: http nodePort: 32001 - port: 5672 name: amqp nodePort: 32002 selector: app: rabbitmq type: NodePort --- apiVersion: v1 kind: Service metadata: name: rabbitmq labels: app: rabbitmq spec: clusterIP: None ports: - port: 5672 name: amqp selector: app: rabbitmq --- apiVersion: apps/v1beta1 kind: StatefulSet metadata: name: rabbitmq spec: serviceName: rabbitmq replicas: 3 template: metadata: labels: app: rabbitmq spec: serviceAccountName: rabbitmq imagePullSecrets: - name: regsecret containers: - name: rabbitmq image: 192.168.78.4/rabbit/rabbitmq:3.6.11 imagePullPolicy: IfNotPresent resources: requests: memory: "256Mi" cpu: "150m" limits: memory: "512Mi" cpu: "250m" ports: - containerPort: 5672 name: amqp env: - name: RABBITMQ_DEFAULT_USER value: rabbituser - name: RABBITMQ_DEFAULT_PASS value: rabbitmq - name: RABBITMQ_ERLANG_COOKIE valueFrom: secretKeyRef: name: rabbit-cookie key: cookie - name: MY_POD_NAME valueFrom: fieldRef: fieldPath: metadata.name - name: K8S_SERVICE_NAME value: "rabbitmq" - name: RABBITMQ_USE_LONGNAME value: "true" - name: RABBITMQ_NODENAME value: "rabbit@$(MY_POD_NAME).$(K8S_SERVICE_NAME)" - name: RABBITMQ_NODE_TYPE value: disc - name: AUTOCLUSTER_TYPE value: "k8s" - name: AUTOCLUSTER_DELAY value: "10" - name: AUTOCLUSTER_CLEANUP value: "true" - name: CLEANUP_WARN_ONLY value: "false" - name: K8S_ADDRESS_TYPE value: "hostname" - name: K8S_HOSTNAME_SUFFIX value: ".$(K8S_SERVICE_NAME)" volumeMounts: - name: rabbitmq-volume mountPath: /var/lib/rabbitmq volumeClaimTemplates: - metadata: name: rabbitmq-volume spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 5Gi
这里有个很重要的知识:
每个pod的hostname之间可以相互解析,
If there exists a headless service in the same namespace as the pod and with the same name as the subdomain, the cluster’s KubeDNS Server also returns an A record for the Pod’s fully qualified hostname。
具体看:https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/