Kubernetes(k8s)安装dns附件组件以及使用

MissFuTT 2020-06-21

1、K8s附加组件DNS服务。

答:Kubernetes中有一个很重要的特性,服务子发现。一旦一个service被创建,该service的service ip和service port等信息都可以被注入到pod中供它们使用。kubernetes主要支持两种service发现机制,第一种是环境变量,第二种是DNS。没有dns服务的时候,kubernetes会采用环境变量的形式,一个有很多service,环境变量会变得很复杂,为了解决这个问题,我们使用DNS服务。

2、Pod的环境变量如何查看呢?

[ ~]# kubectl get all -o wide
NAME       DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                             SELECTOR
rc/mysql   1         1         1         16h       mysql          192.168.110.133:5000/mysql:5.7.30    app=mysql
rc/myweb   1         1         1         1h        myweb          192.168.110.133:5000/tomcat:latest   app=myweb

NAME             CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
svc/kubernetes   10.254.0.1       <none>        443/TCP          13d       <none>
svc/mysql        10.254.245.194   <none>        3306/TCP         16h       app=mysql
svc/myweb        10.254.222.197   <nodes>       8080:30008/TCP   1h        app=myweb

NAME             READY     STATUS    RESTARTS   AGE       IP            NODE
po/mysql-537xf   1/1       Running   1          16h       172.16.16.3   k8s-master
po/myweb-8b7n3   1/1       Running   0          1h        172.16.32.3   k8s-node3
[ ~]# kubectl exec -it myweb-8b7n3 /bin/bas
base64      basename    bash        bashbug     bashbug-64  
[ ~]# kubectl exec -it myweb-8b7n3 /bin/bas
base64      basename    bash        bashbug     bashbug-64  
[ ~]# kubectl exec -it myweb-8b7n3 /bin/bash
:/usr/local/tomcat# env 
KUBERNETES_SERVICE_PORT_HTTPS=443
MYSQL_PORT=tcp://10.254.245.194:3306
MYSQL_PORT_3306_TCP_ADDR=10.254.245.194
KUBERNETES_SERVICE_PORT=443
MYSQL_PORT_3306_TCP_PORT=3306
HOSTNAME=myweb-8b7n3
JAVA_HOME=/usr/local/openjdk-11
GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23
JAVA_BASE_URL=https://github.com/AdoptOpenJDK/openjdk11-upstream-binaries/releases/download/jdk-11.0.7%2B10/OpenJDK11U-jdk_
MYSQL_PORT_3306_TCP=tcp://10.254.245.194:3306
PWD=/usr/local/tomcat
JAVA_URL_VERSION=11.0.7_10
TOMCAT_SHA512=75e16a00e02782961a7753dc9afaf6d209afa5f22d320319778fd0ee5e3b47009da522ac735599f1739bff6e809c2da9081dbbd4b8de54a82cf5b8cfbd8030ff
TOMCAT_MAJOR=9
HOME=/root
LANG=C.UTF-8
KUBERNETES_PORT_443_TCP=tcp://10.254.0.1:443
TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib
TERM=xterm
MYSQL_SERVICE_PORT=3306
CATALINA_HOME=/usr/local/tomcat
MYSQL_SERVICE_HOST=10.254.245.194
MYSQL_PORT_3306_TCP_PROTO=tcp
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.254.0.1
LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib
KUBERNETES_SERVICE_HOST=10.254.0.1
KUBERNETES_PORT=tcp://10.254.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TOMCAT_VERSION=9.0.36
JAVA_VERSION=11.0.7
_=/usr/bin/env
:/usr/local/tomcat# java -version
openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment 18.9 (build 11.0.7+10)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10, mixed mode)
:/usr/local/tomcat#

如果有几个Service就会有几个环境变量,这里进行简单的过滤。

:/usr/local/tomcat# env | grep -iE ‘kubernetes|mysql|myweb‘
KUBERNETES_SERVICE_PORT_HTTPS=443
MYSQL_PORT=tcp://10.254.207.238:3306
MYSQL_PORT_3306_TCP_ADDR=10.254.207.238
KUBERNETES_SERVICE_PORT=443
MYSQL_PORT_3306_TCP_PORT=3306
HOSTNAME=myweb-hsdwn
MYSQL_PORT_3306_TCP=tcp://10.254.207.238:3306
KUBERNETES_PORT_443_TCP=tcp://10.254.0.1:443
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_HOST=10.254.207.238
MYSQL_PORT_3306_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.254.0.1
KUBERNETES_SERVICE_HOST=10.254.0.1
KUBERNETES_PORT=tcp://10.254.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
:/usr/local/tomcat#
1 :/usr/local/tomcat# env | grep -iE ‘kubernetes|mysql|myweb‘ | wc -l
2 16
3 :/usr/local/tomcat#

如果环境变量过多的话,会变得很复杂,所以引入了DNS服务。

3、下载DNS的资料包,如下所示:

[ tomcat_demo]# wget https://www.qstack.com.cn/skydns.zip
--2020-06-18 15:31:25--  https://www.qstack.com.cn/skydns.zip
Resolving www.qstack.com.cn (www.qstack.com.cn)... 123.125.46.149, 111.202.85.37
Connecting to www.qstack.com.cn (www.qstack.com.cn)|123.125.46.149|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3411 (3.3K) [application/zip]
Saving to: ‘skydns.zip’

100%[======================================================================================>] 3,411       --.-K/s   in 0s      

2020-06-18 15:31:26 (40.5 MB/s) - ‘skydns.zip’ saved [3411/3411]

[master tomcat_demo]# ls
mysql-rc.yml  mysql-svc.yml  skydns.zip  tomcat-rc.yml  tomcat-svc.yml
[master tomcat_demo]# cd ..
[ k8s]# mv tomcat_demo/skydns.zip .
[master k8s]# ls
book-master.war  deploy  pod  rc  skydns.zip  svc  tomcat_demo  tomcat_demo.zip
[master k8s]# unzip skydns.zip 
Archive:  skydns.zip
   creating: skydns/
  inflating: skydns/skydns-rc.yaml   
  inflating: skydns/skydns-svc.yaml  
  inflating: skydns/test_dns_pod.yaml  
[master k8s]# ls
book-master.war  deploy  pod  rc  skydns  skydns.zip  svc  tomcat_demo  tomcat_demo.zip
[ k8s]#

修改skydns-rc.yaml配置文件。

# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# TODO - At some point, we need to rename all skydns-*.yaml.* files to kubedns-*.yaml.*
# Should keep target in cluster/addons/dns-horizontal-autoscaler/dns-horizontal-autoscaler.yaml
# in sync with this file.

# __MACHINE_GENERATED_WARNING__

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: 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:
          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://10.0.0.11: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
        #__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.

需要将此地址的ip地址修改为自己api-server的地址。

Kubernetes(k8s)安装dns附件组件以及使用

开始创建RC,创建需要很长时间的。

1 [ skydns]# kubectl create -f skydns-rc.yaml 
2 deployment "kube-dns" created
3 [ skydns]#

开始创建SVC,修改配置文件,使用kubectl get svc查看CLUSTER-IP的地址,只要未被使用,就可以配置到skydns-svc.yaml 配置文件中即可。

[ ~]# kubectl get svc 
NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   10.254.0.1       <none>        443/TCP          13d
mysql        10.254.207.238   <none>        3306/TCP         42m
myweb        10.254.29.22     <nodes>       8080:30008/TCP   28m
[ ~]#

创建svc的命令,如下所示:

[ skydns]# vim skydns-svc.yaml 
[ skydns]# kubectl create -f skydns-svc.yaml 
service "kube-dns" created
[ skydns]#

Kubernetes(k8s)安装dns附件组件以及使用

根据命名空间来查看创建好的pod,可以看到这个Pod里面的四个容器都已经运行起来了。

[ skydns]# kubectl get pod --namespace=kube-system
NAME                       READY     STATUS    RESTARTS   AGE
kube-dns-778415672-f9ssw   4/4       Running   0          6m
[ skydns]#
[ skydns]# kubectl get all --namespace=kube-system
NAME              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/kube-dns   1         1         1            1           7m

NAME           CLUSTER-IP       EXTERNAL-IP   PORT(S)         AGE
svc/kube-dns   10.254.230.254   <none>        53/UDP,53/TCP   3m

NAME                    DESIRED   CURRENT   READY     AGE
rs/kube-dns-778415672   1         1         1         7m

NAME                          READY     STATUS    RESTARTS   AGE
po/kube-dns-778415672-f9ssw   4/4       Running   0          7m
[ skydns]#

如果希望所有的Pod都使用DNS的话,还需要修改一下配置文件。修改kubelet配置,修改各个node节点上的/etc/kubernetes/kubelet配置文件,增加如下行。然后再重启各个node节点的kubelet。

4、修改kubelet配置,修改各个node节点上的/etc/kubernetes/kubelet配置文件,增加如下行。然后再重启各个node节点的kubelet。

1 KUBELET_ARGS="--cluster_dns=10.254.230.254 --cluster_domain=cluster.local"

这个地址就是刚才自己配置的svc的地址,可以使用命令进行查看。

[ skydns]# vim /etc/kubernetes/kubelet 
[ skydns]# kubectl get svc --namespace=kube-system
NAME       CLUSTER-IP       EXTERNAL-IP   PORT(S)         AGE
kube-dns   10.254.230.254   <none>        53/UDP,53/TCP   9m
[ skydns]#

Kubernetes(k8s)安装dns附件组件以及使用

然后重启三台Node节点的kubelet服务。

[master skydns]# systemctl restart kubelet.service 

[ ~]# systemctl restart kubelet.service

[ ~]# systemctl restart kubelet.service

如何测试DNS是否生效呢,可以使用下面的配置进行测试。

[master skydns]# ls
skydns-rc.yaml  skydns-svc.yaml  test_dns_pod.yaml
[master skydns]# cat test_dns_pod.yaml 
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"
[ skydns]# kubectl create -f test_dns_pod.yaml 
pod "busybox2" created
[ skydns]# kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
busybox2      1/1       Running   0          20s
mysql-lmx4s   1/1       Running   0          59m
myweb-hsdwn   1/1       Running   0          45m
[ skydns]# kubectl exec -it busybox2 sh
/ #

可以检测一下,svc的vip是否可以解析出来。

/ # nslookup mysql
Server:        10.254.230.254
Address:    10.254.230.254:53

Name:    mysql.default.svc.cluster.local
Address: 10.254.207.238

可以看出来和SVC的地址一样。

[ ~]# kubectl get svc 
NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   10.254.0.1       <none>        443/TCP          13d
mysql        10.254.207.238   <none>        3306/TCP         1h
myweb        10.254.29.22     <nodes>       8080:30008/TCP   46m
[ ~]#

注意,刚才创建的dns服务,只有新起的pod里面才可以使用,如果已经存在的pod里面还没有使用dns服务的。如果想要之前的pod使用dns服务,需要将他们删除掉,重新创建一下的。

备注:如果配置好了DNS之后,创建的RC里面就可以不使用环境变量的地址(即VIP的地址),可以使用service(svc)的名称既可以找到这个service。

相关推荐