搭建及使用K8s集羣 <k8s Dns 部署>

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/etcd 2.0.9
docker.io/cdchen/kube2sky 1.11
docker.io/cdchen/skydns 2015-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 應用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章