511. 【kubernetes】部署-CoreDNS-服务

一、修改每个 Node 上 kubelet 的 DNS 启动参数

修改每个 Node 上的启动参数,在其中加上一下两个参数:

  • –cluster-dns=169.169.0.100:为DNS 服务的 ClusterIP 地址
  • –cluster-domain=cluster.local:为在 DNS 服务中设置的域名

然后后重启 kubelet 服务。

systemctl restart kubelet

二、配置 coredns.yaml

[root@k8s0 coredns]#  cat >coredns.yaml <<EOF
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
data:
  Corefile: |
    cluster.local {
      errors
      health {
        lameduck 5s
      }
      ready
      kubernetes cluster.local 169.169.0.0/16 {
        fallthrough in-addr.arpa ip6.arpa
      }
      prometheus :9153
      forward . /etc/resolv.conf
      cache 30
      loop
      reload
      loadbalance
    }
    . {
        cache 30
        loadbalance
        forward . /etc/resolv.conf
    }

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coredns
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    kubernetes.io/name: "CoreDNS"
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  selector:
    matchLabels:
      k8s-app: kube-dns
  template:
    metadata:
      labels:
        k8s-app: kube-dns
    spec:
      priorityClassName: system-cluster-critical
      tolerations:
        - key: "CriticalAddonsOnly"
          operator: "Exists"
      nodeSelector:
        kubernetes.io/os: linux
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                  - key: k8s-app
                    operator: In
                    values: ["kube-dns"]
              topologyKey: kubernetes.io/hostname
      containers:
      - name: coredns
        image: coredns/coredns:1.10.0
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            memory: 170Mi
          requests:
            cpu: 100m
            memory: 70Mi
        args: [ "-conf", "/etc/coredns/Corefile" ]
        volumeMounts:
        - name: config-volume
          mountPath: /etc/coredns
          readOnly: true
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
        - containerPort: 9153
          name: metrics
          protocol: TCP
        securityContext:
          allowPrivilegeEscalation: false
          capabilities:
            add:
            - NET_BIND_SERVICE
            drop:
            - all
          readOnlyRootFilesystem: true
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: 8181 
            scheme: HTTP
      dnsPolicy: Default
      volumes:
        - name: config-volume
          configMap:
            name: coredns
            items:
            - key: Corefile
              path: Corefile

---
apiVersion: v1
kind: Service
metadata: 
  name: kube-dns
  namespace: kube-system
  annotations:
    prometheus.io/port: "9153"
    prometheus.io/scrape: "true"
  labels:
    k8s-app: kube-dns
    kubernetes.io/cluster-service: "true"
    kubernetes.io/name: "CoreDNS"
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
  - name: metrics
    port: 9153
    protocol: TCP
EOF

三、创建 CoreDNS

[root@k8s0 coredns]#  kubectl create -f coredns.yaml 
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created

四、验证是否安装成功

  1. 命令查看:
[root@k8s0 coredns]#  kubectl get deploy -n=kube-system | grep dns
coredns                   3/3     3            3           47s
[root@k8s0 coredns]#  kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   169.169.0.100   <none>        53/UDP,53/TCP,9153/TCP   2m55s
[root@k8s0 coredns]#  kubectl get pod -n kube-system | grep dns
coredns-7777c5c849-4k7cx                   1/1     Running   0               3m13s
coredns-7777c5c849-75hqs                   1/1     Running   0               3m13s
coredns-7777c5c849-h2jbt                   1/1     Running   0               3m13s
  1. 管理界面查看:

  2. 服务验证:
    随便新建一个Pod,进入pod之后

[root@xxxxxxdb-statefulset-ak8s-0 bin]#  cat /etc/resolv.conf 
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver 169.169.0.100
options ndots:5
  • 可以看到 /etc/resolv.conf 文件中的 nameserver 已经变成了指定的 169.169.0.100

CoreDNS 部署完成之后,就可以稳定通过域名来ping通集群内部的各个pod了,解决了 kubernetes 集群中IP不够稳定,但有需要稳定通信的问题!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值