k8s部署consul集群

yaml内容如下,设置nodePort: 32685外部访问端口

apiVersion: v1
kind: Namespace
metadata:
  name: public-service

---

apiVersion: v1
kind: Service
metadata:
  name: consul-server
  namespace: public-service
  labels:
    name: consul-server
spec:
  selector:
    name: consul-server
  ports:
    - name: http
      port: 8500
      targetPort: 8500
    - name: https
      port: 8443
      targetPort: 8443
    - name: rpc
      port: 8400
      targetPort: 8400
    - name: serf-lan-tcp
      protocol: "TCP"
      port: 8301
      targetPort: 8301
    - name: serf-lan-udp
      protocol: "UDP"
      port: 8301
      targetPort: 8301
    - name: serf-wan-tcp
      protocol: "TCP"
      port: 8302
      targetPort: 8302
    - name: serf-wan-udp
      protocol: "UDP"
      port: 8302
      targetPort: 8302
    - name: server
      port: 8300
      targetPort: 8300
    - name: consul-dns
      port: 8600
      targetPort: 8600

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: consul-server
  namespace: public-service
  labels:
    name: consul-server
spec:
  serviceName: consul-server
  selector:
    matchLabels:
      name: consul-server
  replicas: 3
  template:
    metadata:
      labels:
        name: consul-server
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: consul
          image: consul:latest
          imagePullPolicy: IfNotPresent
          args:
            - "agent"
            - "-server"
            - "-bootstrap-expect=3"
            - "-ui"
            - "-data-dir=/consul/data"
            - "-bind=0.0.0.0"
            - "-client=0.0.0.0"
            - "-advertise=$(POD_IP)"
            - "-retry-join=consul-server-0.consul-server.$(NAMESPACE).svc.cluster.local"
            - "-retry-join=consul-server-1.consul-server.$(NAMESPACE).svc.cluster.local"
            - "-retry-join=consul-server-2.consul-server.$(NAMESPACE).svc.cluster.local"
            - "-domain=cluster.local"
            - "-disable-host-node-id"
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          ports:
            - containerPort: 8500
              name: http
            - containerPort: 8400
              name: rpc
            - containerPort: 8443
              name: https-port
            - containerPort: 8301
              name: serf-lan
            - containerPort: 8302
              name: serf-wan
            - containerPort: 8600
              name: consul-dns
            - containerPort: 8300
              name: server

---

apiVersion: v1
kind: Service
metadata:
  name: consul-server-http
  namespace: public-service
spec:
  selector:
    name: consul-server
  type: NodePort
  ports:
    - protocol: TCP
      port: 8500
      targetPort: 8500
      nodePort: 32685
      name: consul-server-tcp
      
---

apiVersion: apps/v1
kind: DaemonSet
metadata:
 name: consul-client
 namespace: public-service
 labels:
  name: consul-client
spec:
  selector:
    matchLabels:
      name: consul-client
  template:
    metadata:
      labels:
        name: consul-client
    spec:
      containers:
        - name: consul
          image: consul:latest
          imagePullPolicy: IfNotPresent
          args:
            - "agent"
            - "-data-dir=/consul/data"
            - "-bind=0.0.0.0"
            - "-client=0.0.0.0"
            - "-advertise=$(POD_IP)"
            - "-retry-join=consul-server-0.consul-server.$(NAMESPACE).svc.cluster.local"
            - "-retry-join=consul-server-1.consul-server.$(NAMESPACE).svc.cluster.local"
            - "-retry-join=consul-server-2.consul-server.$(NAMESPACE).svc.cluster.local"
            - "-domain=cluster.local"
            - "-disable-host-node-id"
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          lifecycle:
            postStart:
              exec:
                command:
                  - /bin/sh
                  - -c
                  - consul reload
            preStop:
              exec:
                command:
                  - /bin/sh
                  - -c
                  - consul leave
          volumeMounts:
            - name: consul-data-dir
              mountPath: /consul/data
          ports:
            - containerPort: 8500
              hostPort: 8500
              name: http
            - containerPort: 8400
              name: rpc
            - containerPort: 8443
              name: https
            - containerPort: 8301
              name: serf-lan
            - containerPort: 8302
              name: serf-wan
            - containerPort: 8600
              name: consul
            - containerPort: 8300
              name: server
      volumes:
        - name: consul-data-dir
          hostPath:
            path: /consul/data
            type: DirectoryOrCreate

kubectl get svc -n public-service #查看服务
kubectl get pod -n public-service #查看pod
kubectl exec -n public-service consul-server-0 -- consul members #查看集群

Kubernetes部署Consul可以通过以下步骤进行: 1. 创建一个命名空间(Namespace)来部署Consul。可以使用命令`kubectl create namespace <namespace-name>`来创建一个新的命名空间。 2. 创建一个Consul配置文件,例如consul.yaml,其中包含了需要的配置信息。可以使用以下命令创建一个基本的Consul配置文件: ``` apiVersion: v1 kind: Service metadata: name: consul labels: app: consul spec: ports: - port: 8500 targetPort: 8500 protocol: TCP selector: app: consul --- apiVersion: apps/v1 kind: StatefulSet metadata: name: consul spec: replicas: 3 serviceName: consul selector: matchLabels: app: consul template: metadata: labels: app: consul spec: containers: - name: consul image: consul:latest command: - consul - agent - -server - -bootstrap-expect=3 - -ui - -client=0.0.0.0 - -config-dir=/consul/config ports: - containerPort: 8500 protocol: TCP volumeMounts: - name: consul-config mountPath: /consul/config volumeClaimTemplates: - metadata: name: consul-config spec: accessModes: [ "ReadWriteOnce" ] storageClassName: <storage-classname> resources: requests: storage: 10Gi ``` 其中,`<storage-classname>`应替换为Kubernetes中已定义的存储类名称,用来提供持久化存储。 3. 使用kubectl命令部署Consul,执行以下命令: ``` kubectl apply -f consul.yaml -n <namespace-name> ``` 其中,`<namespace-name>`应替换为步骤1中创建的命名空间名称。 4. 部署完成后,可以使用以下命令检查Consul的状态: ``` kubectl get pod -n <namespace-name> ``` 其中,`<namespace-name>`应替换为步骤1中创建的命名空间名称。 这样,你就成功地在Kubernetes部署Consul集群。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [k8s搭建有状态应用consul](https://blog.csdn.net/soonyigkka/article/details/123370134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shi2jiao3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值