1. 程式人生 > >k8s中的dns服務發現

k8s中的dns服務發現

一.dns服務

1.解決的問題

為了通過服務的名字在叢集內進行服務相互訪問,需要建立一個dns服務

2.k8s中使用的虛擬dns服務是skydns

二.搭建

1.建立並應用skydns-rc.yaml

[[email protected] dns]# cat skydns-rc.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: kube-dns
  namespace: kube-system
  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: docker.io/ist0ne/kubedns-amd64:latest
        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:
          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=rorshach.com.
        - --dns-port=10053
        - --config-map=kube-dns
        #增加kube-master-url,指向k8s_master地址
        - --kube-master-url=http://192.168.2.17:8080
        # 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
        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: docker.io/ist0ne/k8s-dns-dnsmasq-amd64:latest
        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: docker.io/ist0ne/dnsmasq-metrics-amd64:latest
        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: docker.io/ist0ne/exechealthz-amd64:latest
        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.huangzai.com 127.0.0.1 >/dev/null
        - --url=/healthz-dnsmasq
        - --cmd=nslookup kubernetes.default.svc.huangzai.com 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.

  

2.建立並應用skydns-svc

[[email protected] dns]# cat skydns-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    kubernetes.io/cluster-service: "true"
    kubernetes.io/name: "KubeDNS"
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: 10.254.0.100
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
    protocol: TCP

  

三.修改節點kubelet的引數

1.在/etc/kubernetes/kubelet檔案中增加一下內容

--cluster_dns=10.254.0.100 --cluster_domain=rorshach.com
增加完後如下所示

KUBELET_ADDRESS="--address=0.0.0.0 --cluster_dns=10.254.0.100 --cluster_domain=rorshach.com"

  

2.重啟kubelet服務

systemctl restart kubelet

  

四.測試

[[email protected]
ingress]# kubectl exec -it nginx-deployment-3606461909-s69n6 -- nslookup mysql-service nslookup: can't resolve '(null)': Name does not resolve Name: mysql-service Address 1: 18.211.9.206 ec2-18-211-9-206.compute-1.amazonaws.com

  

因為上文中的rorshach.com是實際存在的域名,所以我們在master上通過hosts指定該域名

[[email protected] k8s]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.18 rorshach.com

再次測試:

[[email protected] k8s]# kubectl exec -it nginx-deployment-3606461909-s69n6 -- nslookup mysql-service
nslookup: can't resolve '(null)': Name does not resolve

Name:      mysql-service
Address 1: 10.254.12.207 mysql-service.default.svc.rorshach.com

  

已按預期呈現,現在我們就可以在應用程式中使用mysql-service這個服務名稱來做host了