kubernetes部署-Kubernetes DNS(九)

kubernetes部署-Kubernetes DNS
 

部署-Kubernetes DNS

创建DNS 用来自动对应主机和集群IP

直接创建yaml文件来创建dns

apiVersion: v1
kind: Service
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    kubernetes.io/name: "KubeDNS"
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: 10.0.0.2    #我们指定一个dns地址空的没用的ip,这个地址其实在node1,2的kubelet.config里有指定
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
    protocol: TCP
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: kube-dns
  namespace: kube-system
  labels:
    k8s-app: kube-dns
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
spec:
  # 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: ''
    spec:
      tolerations:
      - key: "CriticalAddonsOnly"
        operator: "Exists"
      volumes:
      - name: kube-dns-config
        configMap:
          name: kube-dns
          optional: true
      containers:
      - name: kubedns
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-kube-dns-amd64:1.14.7
        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: /healthcheck/kubedns
            port: 10054
            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-dir=/kube-dns-config
        - --v=2
        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
        volumeMounts:
        - name: kube-dns-config
          mountPath: /kube-dns-config
      - name: dnsmasq
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.7
        livenessProbe:
          httpGet:
            path: /healthcheck/dnsmasq
            port: 10054
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        args:
        - -v=2
        - -logtostderr
        - -configDir=/etc/k8s/dns/dnsmasq-nanny
        - -restartDnsmasq=true
        - --
        - -k
        - --cache-size=1000
        - --no-negcache
        - --log-facility=-
        - --server=/cluster.local/127.0.0.1#10053
        - --server=/in-addr.arpa/127.0.0.1#10053
        - --server=/ip6.arpa/127.0.0.1#10053
        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: 20Mi
        volumeMounts:
        - name: kube-dns-config
          mountPath: /etc/k8s/dns/dnsmasq-nanny
      - name: sidecar
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-sidecar-amd64:1.14.7
        livenessProbe:
          httpGet:
            path: /metrics
            port: 10054
            scheme: HTTP
          initialDelaySeconds: 60
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 5
        args:
        - --v=2
        - --logtostderr
        - --probe=kubedns,127.0.0.1:10053,kubernetes.default.svc.cluster.local,5,SRV
        - --probe=dnsmasq,127.0.0.1:53,kubernetes.default.svc.cluster.local,5,SRV
        ports:
        - containerPort: 10054
          name: metrics
          protocol: TCP
        resources:
          requests:
            memory: 20Mi
            cpu: 10m
      dnsPolicy: Default  # Don't use cluster DNS.
      serviceAccountName: kube-dns

kubectl apply -f kube-dns.yaml

查看一下pod是否已经正常运行

[root@node dns]# kubectl apply -f kube-dns.yaml ^C
[root@node dns]# kubectl get pod -o wide -n kube-system
NAME                                    READY   STATUS    RESTARTS   AGE     IP            NODE             NOMINATED NODE   READINESS GAT
ESheapster-c679b85f9-bldkw                1/1     Running   0          27m     172.17.71.6   10.167.130.206   <none>           <none>
kube-dns-5c74856c48-j44h4               3/3     Running   0          51m     172.17.71.8   10.167.130.206   <none>           <none>
kubernetes-dashboard-7b79f98f97-wxkkc   1/1     Running   0          5d19h   172.17.71.4   10.167.130.206   <none>           <none>
monitoring-grafana-76bd45d446-8v6bb     1/1     Running   0          27m     172.17.71.5   10.167.130.206   <none>           <none>
monitoring-influxdb-84f8f76d94-76ffj    1/1     Running   0          27m     172.17.71.7   10.167.130.206   <none>           <none>

可以看到kube-dns已经Running了

创建新的pod进行dns的验证看是否正常

cd /home/dns

cat > my-nginx.yaml<<EOF
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-nginx   #最终会显示到svc中,与集群IP对应关系
spec:
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
EOF


kubectl create -f my-nginx.yaml
deployment "my-nginx" created

Expose 该Deployment,生成my-nginx 服务

kubectl expose deploy my-nginx

[root@k8s-master01 pod]# kubectl get svc
NAME         TYPE           CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP      10.0.0.1     <none>        443/TCP          11d
my-nginx     ClusterIP      10.0.0.157   <none>        80/TCP           51m
nginx        NodePort       10.0.0.116   <none>        88:31961/TCP     10d
tomcat       LoadBalancer   10.0.0.16    <pending>     8080:32934/TCP   6d19h

然后创建另外一个Pod,查看/etc/resolv.conf是否包含kubelet配置的–cluster-dns 和–cluster-domain,是否能够将服务my-nginx 解析到上面显示的CLUSTER-IP 10.0.0.157 上

cat > pod-nginx.yaml<<EOF
apiVersion: v1
kind: Pod
metadata:
  name: podnginx
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
EOF

kubectl create -f pod-nginx.yaml
pod "podnginx" created

kubectl exec  podnginx -i -t /bin/bash   #终端交互进入podnginx容器内
查看一下/etc/resolv.conf  看是否是我们创建的dns地址10.0.0.2

[root@node dns]# kubectl exec podnginx -it /bin/bash
root@podnginx:/# cat /etc/resolv.conf 
nameserver 10.0.0.2
search default.svc.cluster.local. svc.cluster.local. cluster.local.
options ndots:5

尝试ping一下我们之前创建的my-nginx
root@podnginx:/# ping my-nginx
PING my-nginx.default.svc.cluster.local (10.0.0.157): 48 data bytes   #正确解析到了集群IP,ping是不通的,但是可以解析到就OK

猜你喜欢

转载自blog.csdn.net/u013726175/article/details/88173381