kubernetes DNS搭建

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Arno_e/article/details/77935897

为了能够通过服务的名字在集群内部进行服务的相互访问,需要创建一个虚拟的DNS服务器来完成服务名到clusterIP的解释!
k8s 提供的DNS服务由四个部分组成:1.etcd DNS存储 2. kube2sky 注册master 服务到etcd 3.skyDNS DNS解析服务 4 healthz 对3功能的健康检查

skydns 是有一个RC 和 service 定义

cat skydns-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: kube-dns-v11
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    version: v11
    kubernetes.io/cluster-service: "true"
spec:
  replicas: 1
  selector:
    k8s-app: kube-dns
    version: v11
  template:
    metadata:
      labels:
        k8s-app: kube-dns
        version: v11
        kubernetes.io/cluster-service: "true"
    spec:
      containers:
      - name: etcd
        image: index.tenxcloud.com/google_containers/etcd-amd64:2.2.1
        imagePullPolicy: Never
        resources:
          limits:
            cpu: 100m
            memory: 50Mi
          requests:
            cpu: 100m
            memory: 50Mi
        command:
        - /usr/local/bin/etcd
        - -data-dir
        - /tmp/data
        - -listen-client-urls
        - http://127.0.0.1:2379,http://127.0.0.1:4001
        - -advertise-client-urls
        - http://127.0.0.1:2379,http://127.0.0.1:4001
        - -initial-cluster-token
        - skydns-etcd
        volumeMounts:
        - name: etcd-storage
          mountPath: /tmp/data
      - name: kube2sky
        image: index.tenxcloud.com/google_containers/kube2sky-amd64:1.15
        imagePullPolicy: Never
        resources:
          limits:
            cpu: 100m
            # Kube2sky watches all pods.
            memory: 50Mi
          requests:
            cpu: 100m
            memory: 50Mi
        livenessProbe:
          httpGet:
            path: /healthz
            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: 30
          timeoutSeconds: 5
        args:
        # command = "/kube2sky"
        - --kube-master-url=http://**192.168.1.82**:8080
        - --domain=cluster.local
      - name: skydns
        image: index.tenxcloud.com/google_containers/skydns:2015-10-13-8c72f8c
        imagePullPolicy: Never
        resources:
          limits:
            cpu: 100m
            memory: 50Mi
          requests:
            cpu: 100m
            memory: 50Mi
        args:
        # command = "/skydns"
        - -machines=http://127.0.0.1:4001
        - -addr=**0.0.0.0:53**
        - -ns-rotate=false
        - -domain=**cluster.local**
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
      - name: healthz
        image: index.tenxcloud.com/google_containers/exechealthz:1.0
        imagePullPolicy: Never
        resources:
          # keep request = limit to keep this container in guaranteed class
          limits:
            cpu: 10m
            memory: 20Mi
          requests:
            cpu: 10m
            memory: 20Mi
        args:
        - -cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1 >/dev/null
        - -port=8080
        ports:
        - containerPort: 8080
          protocol: TCP
      volumes:
      - name: etcd-storage
        emptyDir: {}
      dnsPolicy: Default  # Don't use cluster DNS.

上面三个加粗位置要自己定义,1为 master的IP,2 –addr表示提供的端口服务,可使用默认,3–domain 设置域名

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: **169.169.0.100**
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
    protocol: TCP

其中 clusterIP 是需要修改的

在进行create之前需要修改kubelet配置文件

KUBELET_ADDRESS="--address=**192.168.1.82**"
KUBELET_HOSTNAME="--hostname-override=node-master"
KUBELET_API_SERVER="--api-servers=http://127.0.0.1:8080"
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_ARGS=**"--cluster_dns=**169.169.0.100 --cluster_domain=cluster.local****

记住KUBELET_ADDRESS 这里一定要修改成node的ip地址,在这踩坑半天,添加ARGS
这里的cluster_dns就是skydns-svc.yaml 中的 clusterIP

这里额外说明一下,创建RC的时候要拉取4个镜像,虽然很小,但是没有翻墙的小伙伴还是很懵逼的,之前我用的daocloud的加速,但是发现这几个镜像这个里面没有,在网上找到了另外一个镜像仓库
时速云:
举个例子 :index.tenxcloud.com/google_containers/kube2sky-amd64
你可以把rc文件中的image 全部换成这个,速度还是十分可以的

kubectl  create -f skydns-rc.yaml
>kubectl  get rc --namespace=kube-system
NAME           DESIRED   CURRENT   READY     AGE
kube-dns-v11   1         1         1         1h

这里说明已经创建好了,再查看一下pod

> kubectl  get pod --namespace=kube-system 
NAME                          READY     STATUS             RESTARTS   AGE
kube-dns-v11-8jf6j            4/4       Running            0          1h

创建 service

kubectl  create -f skydns-svc.yaml
kubectl  get svc --namespace=kube-system 
NAME       CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
kube-dns   169.169.0.100   <none>        53/UDP,53/TCP   1h

最后创建一个 redis-master service 已经一个busybusybox 做测试

 kubectl exec busybox -- nslookup redis-master
Server:    169.169.0.100
Address 1: 169.169.0.100

Name:      redis-master
Address 1: 169.169.229.95

出现以上说明成功
以上代码都可以在我之前文章的百度云连接下载

展开阅读全文

没有更多推荐了,返回首页