k8s Dns 部署

  • 1. k8s dns 相关镜像准备
  • 2. yamls 编写
  • 3. 创建pod service
  • 4. 验证k8s dns

docker 容器的ip 是动态的,多个服务之间没法通讯,k8s dns 解决了该问题,访问不通过ip,通过app name 进行访问。


1. k8s dns 相关镜像准备

百度一把 文章都是google的镜像,国内pull不下来,可以去dockerhub上搜到的对应版本,tag & push到个人的dockerhub下

镜像版本
docker.io/cdchen/etcd2.0.9
docker.io/cdchen/kube2sky1.11
docker.io/cdchen/skydns2015-03-11-001

2. 修改配置文件

2.1 修改各个node上的kubelet

[root@node2 ~]# cat /etc/kubernetes/kubelet
###
# kubernetes kubelet (minion) config

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"

# The port for the info server to serve on
KUBELET_PORT="--port=10250"

# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=node2"

# location of the api-server
KUBELET_API_SERVER="--api-servers=http://master:8080"

# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"

# Add your own!
KUBELET_ARGS=" --cluster_dns=10.254.0.2 --cluster_domain=atomic.io   "

2.2 修改APIserver

[root@master yamls]# cat /etc/kubernetes/apiserver
###
# kubernetes system config
#
# The following values are used to configure the kube-apiserver
#

# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"

# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"

# Port minions listen on
# KUBELET_PORT="--kubelet-port=10250"

# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379"

# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

# default admission control policies
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

# Add your own!

比对下 KUBE_ADMISSION_CONTROL内容

重启 Master 和 各Node 服务

3. yamls 编写

3.1 skydns-rc.yml

apiVersion: v1
kind: ReplicationController
metadata:
  name: kube-dns-v6
  namespace: default
  labels:
    k8s-app: kube-dns
    version: v6
    kubernetes.io/cluster-service: "true"
spec:
  replicas: 1
  selector:
    k8s-app: kube-dns
    version: v6
  template:
    metadata:
      labels:
        k8s-app: kube-dns
        version: v6
        kubernetes.io/cluster-service: "true"
    spec:
      containers:
      - name: etcd
        image: docker.io/cdchen/etcd:2.0.9 
        command:
        - /usr/local/bin/etcd
        - -listen-client-urls
        - http://0.0.0.0:2379,http://0.0.0.0:4001
        - -advertise-client-urls
        - http://127.0.0.1:2379,http://127.0.0.1:4001
        - -initial-cluster-token
        - skydns-etcd
      - name: kube2sky
        image: docker.io/cdchen/kube2sky:1.11   
        resources:
          limits:
            cpu: 100m
            memory: 50Mi
        command:
        - /kube2sky
        - --kube_master_url=http://192.168.6.45:8080
        - -domain=atomic.io
      - name: skydns
        image: docker.io/cdchen/skydns:2015-03-11-001 
        resources:
        command:
        - /skydns
        - -machines=http://localhost:4001
        - -addr=0.0.0.0:53
        - -domain=atomic.io.
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
      dnsPolicy: Default

3.2 skydns-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: kube-dns
  namespace: default
  labels:
    k8s-app: kube-dns
    kubernetes.io/cluster-service: "true"
    kubernetes.io/name: "KubeDNS"
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: 10.254.0.2
  ports:
  - name: dns
    port: 53
    protocol: UDP
  - name: dns-tcp
    port: 53
    protocol: TCP

4 创建pods

[root@master sky_dns]# kubectl create -f skydns-rc.yml 
replicationcontroller "kube-dns-v6" created
[root@master sky_dns]# kubectl create -f skydns-svc.yaml 
service "kube-dns" created
[root@master sky_dns]# kubectl  get pods
NAME                                   READY     STATUS    RESTARTS   AGE
cloud-eureka-server-1593312766-cx7w8   1/1       Running   0          2h
kube-dns-v6-5tf2j                      3/3       Running   0          1m

5 验证k8s dns

5.1 部署busybox

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

[root@master yamls]# kubectl create -f busybox.yaml 
pod "busybox" created

3.2 nslookup 验证

[root@master yamls]# kubectl exec busybox -it sh
[root@master ~]# kubectl exec busybox -it sh
/ # nslookup cloud-eureka-server
Server:    10.254.0.2
Address 1: 10.254.0.2

Name:      cloud-eureka-server
Address 1: 10.254.247.31
/ # 

解析成功,其中 cloud-eureka-server 是我部署的一个springcloud 应用。

Logo

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

更多推荐