在这里插入图片描述

一、环境变量

创建pod资源时,kubelet会将其所属名称空间内的每个活动的Service对象以一系列环境变量的形式注入其中。
1、创建一个service匹配标签为之前创建的pod的标签app:nginx,映射80和443端口
vim service.yaml

[root@k8s-master-101 yaml_test]# cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  - name: https
    protocol: TCP
    port: 443
    targetPort: 443
  selector:
    app: nginx

#创建service
[root@k8s-master-101 yaml_test]# kubectl create -f service.yaml 
service/my-service created

2、查看service和endpoints

[root@k8s-master-101 yaml_test]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP   10.10.10.1     <none>        443/TCP          19d
my-service   ClusterIP   10.10.10.164   <none>        80/TCP,443/TCP   15m

[root@k8s-master-101 yaml_test]# kubectl get ep
NAME         ENDPOINTS                                                  AGE
kubernetes   10.0.0.101:6443                                            19d
my-service   172.17.50.2:80,172.17.50.3:80,172.17.71.5:80 + 5 more...   2m39s

3、创建一个busybox。
vim busybox.yaml

[root@k8s-master-101 yaml_test]# cat busybox.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
spec:  containers:
  - image: busybox:1.28.2
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
    name: busybox
  restartPolicy: Always

4、创建pod,进入容器然后查看系统变量
可以看到系统变量中有刚才创建的my-service的ip相关信息

[root@k8s-master-101 yaml_test]# kubectl create -f busybox.yaml
pod/busybox created

[root@k8s-master-101 yaml_test]# kubectl exec -it busybox sh
/ # env
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.10.10.1:443
MY_SERVICE_PORT_80_TCP=tcp://10.10.10.164:80
MY_SERVICE_PORT_443_TCP_ADDR=10.10.10.164
HOSTNAME=busybox
SHLVL=1
MY_SERVICE_PORT_443_TCP_PORT=443
HOME=/root
MY_SERVICE_PORT_443_TCP_PROTO=tcp     <——在这里
MY_SERVICE_SERVICE_PORT_HTTP=80
MY_SERVICE_SERVICE_PORT_HTTPS=443
MY_SERVICE_PORT_443_TCP=tcp://10.10.10.164:443
MY_SERVICE_SERVICE_HOST=10.10.10.164
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.10.10.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
MY_SERVICE_SERVICE_PORT=80
MY_SERVICE_PORT=tcp://10.10.10.164:80
MY_SERVICE_PORT_80_TCP_ADDR=10.10.10.164
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.10.10.1:443
KUBERNETES_SERVICE_HOST=10.10.10.1
MY_SERVICE_PORT_80_TCP_PORT=80
PWD=/
MY_SERVICE_PORT_80_TCP_PROTO=tcp

service需要在pod创建之前创建,这样才能被写到pod环境变量里
pod只能获取同namespace的service环境变量

二、kube-dns

1、到git上下载yaml文件后修改,更改ip和域名,然后还有镜像地址
https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/dns/kube-dns/kube-dns.yaml.sed

[root@k8s-master-101 yaml_test]# cat kube-dns.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.

# Should keep target in cluster/addons/dns-horizontal-autoscaler/dns-horizontal-autoscaler.yaml
# in sync with this file.

# Warning: This is a file generated from the base underscore template file: kube-dns.yaml.base

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.10.10.2
  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.10
        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.10
        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
        - --dns-loop-detect
        - --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.10
        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

2、创建kube-dns并查看

kubectl create -f kube-dns.yaml

[root@k8s-master-101 yaml_test]# kubectl get all -n kube-system
NAME                                        READY   STATUS    RESTARTS   AGE
pod/kube-dns-67777cd9cc-cx865               3/3     Running   63         15d
pod/kubernetes-dashboard-6f79dd6f9f-hk5xc   1/1     Running   27         19d

NAME                           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)         AGE
service/kube-dns               ClusterIP   10.10.10.2     <none>        53/UDP,53/TCP   15d
service/kubernetes-dashboard   NodePort    10.10.10.117   <none>        80:43210/TCP    19d

NAME                                   DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/kube-dns               1         1         1            1           15d
deployment.apps/kubernetes-dashboard   1         1         1            1           19d

NAME                                              DESIRED   CURRENT   READY   AGE
replicaset.apps/kube-dns-67777cd9cc               1         1         1       15d
replicaset.apps/kubernetes-dashboard-6f79dd6f9f   1         1         1       19d

3、进入刚才创建的busybox容器并解析service进行测试,可以解析到刚才创建的my-service

[root@k8s-master-101 ~]# kubectl exec -it busybox -- nslookup kubernetes.default
Server:    10.10.10.2
Address 1: 10.10.10.2 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes.default
Address 1: 10.10.10.1 kubernetes.default.svc.cluster.local

[root@k8s-master-101 ~]# kubectl exec -it busybox -- nslookup my-service.default
Server:    10.10.10.2
Address 1: 10.10.10.2 kube-dns.kube-system.svc.cluster.local

Name:      my-service.default
Address 1: 10.10.10.164 my-service.default.svc.cluster.local

4、系统初始化时会默认将cluster.local和主机所在域作为DNS的本地域使用,这些文件会在Pod创建时以DNS配置的相关信息注入它的/etc/resolv.conf文件中。

[root@k8s-master-101 ~]# kubectl exec -it busybox -- cat /etc/resolv.conf
nameserver 10.10.10.2
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

在前面的kubelet的配置文件中定义了,cluster-dns指定了集群DNS服务的工作地址,cluster-domain定义了集群使用的本地域名。
在这里插入图片描述
基于DNS的服务发现是不受Service资源所在的名称空间和创建时间的限制。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐