• docker及k8s安装consul


    一、docker部署consul集群

    参考文献:https://www.cnblogs.com/lonelyxmas/p/10880717.html

                     https://blog.csdn.net/qq_36228442/article/details/89085373

    consul是提供服务发现、简单配置管理、分区部署的服务注册发现解决方案。
    主要特性:服务发现健康检查基于Key-Value的配置支持TLS安全通讯支持多数据中心部署

    consul的实例叫agent
    agent有两种运行模式:server和client
    每个数据中心至少要有一个server,一般推荐3-5个server(避免单点故障)
    client模式agent是一个轻量级进程,执行健康检查,转发查询请求到server。
    服务service是注册到consul的外部应用,比如spring web server

    以下为docker-compose.yaml文件

    version: "3.0"
    services:
        consulserver:
            image: progrium/consul:latest
            hostname: consulserver
            ports:
                - "8300:8300"
                - "8400:8400"
                - "8500:8500"
                - "53"
            command: -server -ui-dir /ui -data-dir /tmp/consul --bootstrap-expect=3
        consulserver1:
            image: progrium/consul:latest
            hostname: consulserver1
            depends_on:
                - "consulserver"
            ports:
                - "8301:8300"
                - "8401:8400"
                - "8501:8500"
                - "53"
            command: -server -data-dir /tmp/consul -join consulserver
        consulserver2:
            image: progrium/consul:latest
            hostname: consulserver2
            depends_on:
                - "consulserver"
            ports:
                - "8302:8300"
                - "8402:8400"
                - "8502:8500"
                - "53"
            command: -server -data-dir /tmp/consul -join consulserver

    宿主机浏览器访问:http://localhost:8500 或者 http://localhost:8501 或者 http://localhost:8502

    创建test.json文件,以脚本形式注册服务到consul:

    test.json文件内容如下:

    {
        "ID": "test-service1",
        "Name": "test-service1",
        "Tags": [
            "test",
            "v1"
        ],
        "Address": "127.0.0.1",
        "Port": 8000,
        "Meta": {
            "X-TAG": "testtag"
        },
        "EnableTagOverride": false,
        "Check": {
            "DeregisterCriticalServiceAfter": "90m",
            "HTTP": "http://zhihu.com",
            "Interval": "10s"
        }
    }

    通过 http 接口注册服务(端口可以是8500. 8501, 8502等能够正常访问consul的就行):

    curl -X PUT --data @test.json http://localhost:8500/v1/agent/service/register

     二、k8s部署consul集群

    参考文献:https://www.cnblogs.com/tylerzhou/p/11161634.html

                     https://www.jianshu.com/p/b00de040d92e

    由于node节点只有两个,所以配置集群也两个实例,测试时候,配置三个实例不能正常工作,所以配置俩个

    1、先创建service资源

    apiVersion: v1
    kind: Service
    metadata:
      name: consul
      labels:
        name: 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
    #    port: 8301
    #    targetPort: 8301
    #  - name: serflan-udp
    #    port: 8301
    #    targetPort: 8301
    #    protocol: "UDP"
    #  - name: serfwan-tcp
    #    port: 8302
    #    targetPort: 8302
    #  - name: serfwan-udp
    #    port: 8302
    #    targetPort: 8302
    #    protocol: "UDP"
    #  - name: server
    #    port: 8300
    #    targetPort: 8300
    #  - name: consuldns
    #    port: 8600
    #    targetPort: 8600
      selector:
        app: consul
    View Code

    由于只暴露8500的ui端口,所以将其他端口都没有创建service,service为clusterIP

    2、创建statefulset资源

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: consul
    spec:
      serviceName: consul
      replicas: 2
      selector:
        matchLabels:
          app: consul
      template:
        metadata:
          labels:
            app: consul
        spec:
          affinity:
            podAntiAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
                - labelSelector:
                    matchExpressions:
                      - key: app
                        operator: In
                        values:
                          - consul
                  topologyKey: kubernetes.io/hostname
          terminationGracePeriodSeconds: 10
          containers:
          - name: consul
            image: consul:latest
            args:
                 - "agent"
                 - "-server"
                 - "-bootstrap-expect=2"
                 - "-ui"
                 - "-data-dir=/consul/data"
                 - "-bind=0.0.0.0"
                 - "-client=0.0.0.0"
                 - "-advertise=$(PODIP)"
                 - "-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"
            env:
                - name: PODIP
                  valueFrom:
                    fieldRef:
                      fieldPath: status.podIP
                - name: NAMESPACE
                  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
    View Code

    3、创建ingress,以使用域名访问

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: ingress-consul
      namespace: default
      annotations:
        kubernets.io/ingress.class: "nginx"
    spec:
      rules:
      - host: consul.cabel.com
        http:
          paths:
          - path:
            backend:
              serviceName: consul
              servicePort: 8500
    View Code

    以上资源创建完成后就可以访问了:

  • 相关阅读:
    Linux中使用 FTP 命令时出现 “-bash: ftp: command not found”
    Jenkins Build step 'Execute shell' marked build as failure
    centos7ping www.baidu.com没有ping通
    linux maven 安装
    位运算初步
    C++ STL:next_permutation和prev_permutation
    逆波兰表达式
    STL与基本数据结构
    学习记录:指针(未整理)
    Codeforces Round #615 (Div. 3) 补题记录
  • 原文地址:https://www.cnblogs.com/caibao666/p/11582909.html
Copyright © 2020-2023  润新知