DNS服务

作者: Robin92 | 来源:发表于2022-03-29 23:59 被阅读0次

    服务自发现,是在服务启动后,有自发现服务自己发现启动服务的 IP 和 Port 并保存下来提供使用。
    在 K8S 中,服务自发现通过两种方式:环境变量或 DNS。在没有 DNS 的时候, K8S 就用环境变量的方式,但一旦有很多 Service,环境变量就会变得极其复杂(每个服务都会有对应的环境变量),我们就需要用 DNS 的方式。

    安装 DNS 服务

    下载资料包: https://www.qstack.com.cn/skydns.zip

    资料中有三个文件:

    • skydns-rc.yaml,是启动 dns 服务的(是个 Deployment)
    • skydns-svc.yaml,启动服务,其中配置了端口
    • test_dns_pod.yaml,用于测试
    apiVersion: extensions/v1beta1
    kind: Deployment # 资源类型
    metadata:
      name: kube-dns
      namespace: kube-system # 指定了 namespace
      labels:
        k8s-app: kube-dns
        kubernetes.io/cluster-service: "true"
    spec:
      replicas: 1
      # replicas: not specified here:
      # 1. In order to make Addon Manager do not reconcile this replicas parameter.
      # 2. Default is 1.
      # 3. Will be tuned in real time if DNS horizontal auto-scaling is turned on.
      strategy: 
        rollingUpdate: # 升级属性
          maxSurge: 10%
          maxUnavailable: 0
      selector:
        matchLabels:
          k8s-app: kube-dns
      template:
        metadata:
          labels:
            k8s-app: kube-dns
          annotations:
            scheduler.alpha.kubernetes.io/critical-pod: ''
            scheduler.alpha.kubernetes.io/tolerations: '[{"key":"CriticalAddonsOnly", "operator":"Exists"}]'
        spec:
          containers:
          - name: kubedns
            image: myhub.fdccloud.com/library/kubedns-amd64:1.9 # 第一个镜像
            resources: # 资源使用的请求和限制
              # TODO: Set memory limits when we've profiled the container for large
              # clusters, then set request = limit to keep this container in
              # guaranteed class. Currently, this container falls into the
              # "burstable" category so the kubelet doesn't backoff from restarting it.
              limits: 
                memory: 170Mi
              requests:
                cpu: 100m
                memory: 70Mi
            livenessProbe: # 容器的探针服务,检查有没有运行起来,4xx/5xx 为不正常
              httpGet:
                path: /healthz-kubedns
                port: 8080
                scheme: HTTP
              initialDelaySeconds: 60
              timeoutSeconds: 5
              successThreshold: 1
              failureThreshold: 5
            readinessProbe: # 检查服务能否可用
              httpGet:
                path: /readiness
                port: 8081
                scheme: HTTP
              # we poll on pod startup for the Kubernetes master service and
              # only setup the /readiness HTTP server once that's available.
              initialDelaySeconds: 3
              timeoutSeconds: 5
            args: # 参数,指定了域名、端口、配置
            - --domain=cluster.local.
            - --dns-port=10053
            - --config-map=kube-dns
            - --kube-master-url=http://172.16.156.128:8080 # 这里要改成自己 api-server 的地址
            # This should be set to v=2 only after the new image (cut from 1.5) has
            # been released, otherwise we will flood the logs.
            - --v=0
            #__PILLAR__FEDERATIONS__DOMAIN__MAP__
            env:
            - name: PROMETHEUS_PORT
              value: "10055"
            ports:
            - containerPort: 10053
              name: dns-local
              protocol: UDP
            - containerPort: 10053
              name: dns-tcp-local
              protocol: TCP
            - containerPort: 10055
              name: metrics
              protocol: TCP
          - name: dnsmasq
            image: myhub.fdccloud.com/library/kube-dnsmasq-amd64:1.4 # 第二个容器
            livenessProbe:
              httpGet:
                path: /healthz-dnsmasq
                port: 8080
                scheme: HTTP
              initialDelaySeconds: 60
              timeoutSeconds: 5
              successThreshold: 1
              failureThreshold: 5
            args:
            - --cache-size=1000
            - --no-resolv
            - --server=127.0.0.1#10053
            #- --log-facility=-
            ports:
            - containerPort: 53
              name: dns
              protocol: UDP
            - containerPort: 53
              name: dns-tcp
              protocol: TCP
            # see: https://github.com/kubernetes/kubernetes/issues/29055 for details
            resources:
              requests:
                cpu: 150m
                memory: 10Mi
          - name: dnsmasq-metrics # 指标,配合做监控使用
            image: myhub.fdccloud.com/library/dnsmasq-metrics-amd64:1.0
            livenessProbe:
              httpGet:
                path: /metrics
                port: 10054
                scheme: HTTP
              initialDelaySeconds: 60
              timeoutSeconds: 5
              successThreshold: 1
              failureThreshold: 5
            args:
            - --v=2
            - --logtostderr
            ports:
            - containerPort: 10054
              name: metrics
              protocol: TCP
            resources:
              requests:
                memory: 10Mi
          - name: healthz
            image: myhub.fdccloud.com/library/exechealthz-amd64:1.2 # 健康检查的镜像
            resources:
              limits:
                memory: 50Mi
              requests:
                cpu: 10m
                # Note that this container shouldn't really need 50Mi of memory. The
                # limits are set higher than expected pending investigation on #29688.
                # The extra memory was stolen from the kubedns container to keep the
                # net memory requested by the pod constant.
                memory: 50Mi
            args:
            - --cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1 >/dev/null
            - --url=/healthz-dnsmasq
            - --cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1:10053 >/dev/null
            - --url=/healthz-kubedns
            - --port=8080
            - --quiet
            ports:
            - containerPort: 8080
              protocol: TCP
          dnsPolicy: Default  # Don't use cluster DNS.
    
    # Warning: This is a file generated from the base underscore template file: skydns-svc.yaml.base
    apiVersion: v1
    kind: Service
    metadata:
      name: kube-dns
      namespace: kube-system # 指定了 namespace
      labels:
        k8s-app: kube-dns
        kubernetes.io/cluster-service: "true"
        kubernetes.io/name: "KubeDNS"
    spec:
      selector:
        k8s-app: kube-dns
      clusterIP: 10.254.230.254 # v IP 地址
      ports:
      - name: dns
        port: 53
        protocol: UDP
      - name: dns-tcp
        port: 53
        protocol: TCP
    

    注意,这两个文件都指定了 namespace。以下是查看所有跑起来服务的截图。

    image.png

    要想使用此 DNS,我们需要改一下所有 kubelet 的配置文件。

    # /etc/kubernetes/kubelet
    KUBELET_ARGS="--cluster_dns=10.254.230.254 --cluster_domain=cluster.local"
    # 更改完后重启 kubelet: systemctl restart kubelet
    

    测试 DNS 生效

    资料包中 test_dns_pod.yaml 文件是通过 busybox 来测试 DNS 的

    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        name: busybox
        role: master
      name: busybox2
    spec:
      containers:
      - name: busybox
        image: docker.io/busybox:latest
        imagePullPolicy: IfNotPresent
        command:
        - sleep
        - "3600"
    

    通过创建它,并进入容器

    kubectl create -f test_dns_pod.yaml
    kubectl exec -it busybox2 sh # 此容器通过 sh 进入,没有 bash
    

    在容器中查询其他服务的名称,结果可以查询到服务地址:

    image.png

    注意,只有新启动的服务才能使用 DNS 服务,如上面的 busybox,其他服务需要重启才能查询。

    上文中 tomcat 连 mysql 服务的 ip 地址可以改为服务名进行连接。

    image.png

    最后重启 tomcat 服务。(连接数据库的项目没做,所以部分内容忽略,先做笔记做参考)

    相关文章

      网友评论

          本文标题:DNS服务

          本文链接:https://www.haomeiwen.com/subject/vexcjrtx.html