Kubernetes DNS的配置
Kubernetes集群机制通过DNS进行服务名和ip的映射,如果没有配置dns,你可以通过下面命令查询到集群ip
kubectl get svc --namespace=kube-system |
得到结果
但Cluster-ip是变化的,如果通过一个create命令一次批量建立一堆具有相互依赖关系的Pod或者RC,就需要配置DNS
配置DNS的方式有几种,我这里试通了最简单的一种SkyDNS的配置.
skydns-rc.yml
apiVersion: v1 kind: ReplicationController metadata: name: kube-dns-v11 namespace: kube-system labels: k8s-app: kube-dns version: v11 kubernetes.io/cluster-service: "true" spec: replicas: 1 selector: k8s-app: kube-dns version: v11 template: metadata: labels: k8s-app: kube-dns version: v11 kubernetes.io/cluster-service: "true" spec: containers: - name: etcd image: gcr.io/google_containers/etcd-amd64:2.2.1 resources: limits: cpu: 100m memory: 500Mi requests: cpu: 100m memory: 50Mi command: - /usr/local/bin/etcd - -data-dir - /var/etcd/data - -listen-client-urls - http://127.0.0.1:2379,http://127.0.0.1:4001 - -advertise-client-urls - http://127.0.0.1:2379,http://127.0.0.1:4001 - -initial-cluster-token - skydns-etcd #volumeMounts: #- name: etcd-storage # mountPath: /var/etcd/data - name: kube2sky - --domain=cluster.local - --kube-master-url=http://192.168.0.105:8080 resources: limits: cpu: 100m memory: 200Mi requests: cpu: 100m memory: 50Mi livenessProbe: httpGet: path: /healthz port: 8080 scheme: HTTP initialDelaySeconds: 60 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 5 readinessProbe: httpGet: path: /readiness port: 8080 scheme: HTTP initialDelaySeconds: 30 timeoutSeconds: 5 args: - --domain=cluster.local - name: skydns image: gcr.io/google_containers/skydns:2015-10-13-8c72f8c resources: limits: cpu: 100m memory: 200Mi requests: cpu: 100m memory: 50Mi args: - -machines=http://127.0.0.1:4001 - -addr=0.0.0.0:53 - -ns-rotate=false - -domain=cluster.local. ports: - containerPort: 53 name: dns protocol: UDP - containerPort: 53 name: dns-tcp protocol: TCP - name: healthz image: gcr.io/google_containers/exechealthz:1.0 resources: limits: cpu: 10m memory: 20Mi requests: cpu: 10m memory: 20Mi args: - -cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1 >/dev/null - -port=8080 ports: - containerPort: 8080 protocol: TCP #volumes: #- name: etcd-storage # emptyDir: {} dnsPolicy: Default
注意这里有个配置是指向你的api server
kube-master-url=http://192.168.0.105:8080
skydns-svc.yml
apiVersion: v1 kind: Service metadata: name: kube-dns namespace: kube-system labels: k8s-app: kube-dns kubernetes.io/cluster-service: "true" kubernetes.io/name: "KubeDNS" spec: selector: k8s-app: kube-dns clusterIP: 10.254.254.254 ports: - name: dns port: 53 protocol: UDP - name: dns-tcp port: 53 protocol: TCP
先去minion节点上下载所需要的images
docker pull gcr.io/google_containers/etcd-amd64:2.2.1 docker pull gcr.io/google_containers/skydns:2015-10-13-8c72f8c docker pull gcr.io/google_containers/exechealthz:1.0
然后运行构建
kubectl create -f skydns-rc.yml
kubectl create -f skydns-svc.yml
最后看到Pods处于Running状态
查看日志需要通过-c指定container name
[root@k8s-master skydns]# kubectl get pods --namespace=kube-system NAME READY STATUS RESTARTS AGE kube-dns-v11-x0vr3 4/4 Running 20 1d [root@k8s-master skydns]# kubectl logs kube-dns-v11-x0vr3 -n kube-system Error from server (BadRequest): a container name must be specified for pod kube-dns-v11-x0vr3, choose one of: [etcd kube2sky skydns healthz] [root@k8s-master skydns]# kubectl logs kube-dns-v11-x0vr3 -n kube-system -c etcd 2017-05-24 00:06:03.899264 I | etcdmain: etcd Version: 2.2.1 2017-05-24 00:06:03.899305 I | etcdmain: Git SHA: 75f8282 2017-05-24 00:06:03.899310 I | etcdmain: Go Version: go1.5.1 2017-05-24 00:06:03.899314 I | etcdmain: Go OS/Arch: linux/amd64 2017-05-24 00:06:03.899319 I | etcdmain: setting maximum number of CPUs to 1, total number of available CPUs is 1 2017-05-24 00:06:03.900546 I | etcdmain: listening for peers on http://localhost:2380 2017-05-24 00:06:03.900607 I | etcdmain: listening for peers on http://localhost:7001 2017-05-24 00:06:03.900626 I | etcdmain: listening for client requests on http://127.0.0.1:2379 2017-05-24 00:06:03.900640 I | etcdmain: listening for client requests on http://127.0.0.1:4001 2017-05-24 00:06:03.950602 I | etcdserver: name = default 2017-05-24 00:06:03.950621 I | etcdserver: data dir = /var/etcd/data 2017-05-24 00:06:03.950627 I | etcdserver: member dir = /var/etcd/data/member 2017-05-24 00:06:03.950630 I | etcdserver: heartbeat = 100ms 2017-05-24 00:06:03.950633 I | etcdserver: election = 1000ms 2017-05-24 00:06:03.950637 I | etcdserver: snapshot count = 10000 2017-05-24 00:06:03.950649 I | etcdserver: advertise client URLs = http://127.0.0.1:2379,http://127.0.0.1:4001 2017-05-24 00:06:03.950655 I | etcdserver: initial advertise peer URLs = http://localhost:2380,http://localhost:7001 2017-05-24 00:06:03.950665 I | etcdserver: initial cluster = default=http://localhost:2380,default=http://localhost:7001 2017-05-24 00:06:03.984118 I | etcdserver: starting member 6a5871dbdd12c17c in cluster f68652439e3f8f2a 2017-05-24 00:06:03.984177 I | raft: 6a5871dbdd12c17c became follower at term 0 2017-05-24 00:06:03.984194 I | raft: newRaft 6a5871dbdd12c17c [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0] 2017-05-24 00:06:03.984198 I | raft: 6a5871dbdd12c17c became follower at term 1 2017-05-24 00:06:03.984315 I | etcdserver: starting server... [version: 2.2.1, cluster version: to_be_decided] 2017-05-24 00:06:03.997533 N | etcdserver: added local member 6a5871dbdd12c17c [http://localhost:2380 http://localhost:7001] to cluster f68652439e3f8f2a 2017-05-24 00:06:04.785143 I | raft: 6a5871dbdd12c17c is starting a new election at term 1 2017-05-24 00:06:04.785217 I | raft: 6a5871dbdd12c17c became candidate at term 2 2017-05-24 00:06:04.785224 I | raft: 6a5871dbdd12c17c received vote from 6a5871dbdd12c17c at term 2 2017-05-24 00:06:04.785245 I | raft: 6a5871dbdd12c17c became leader at term 2 2017-05-24 00:06:04.785253 I | raft: raft.node: 6a5871dbdd12c17c elected leader 6a5871dbdd12c17c at term 2 2017-05-24 00:06:04.785631 I | etcdserver: setting up the initial cluster version to 2.2 2017-05-24 00:06:04.785697 I | etcdserver: published {Name:default ClientURLs:[http://127.0.0.1:2379 http://127.0.0.1:4001]} to cluster f68652439e3f8f2a 2017-05-24 00:06:04.839450 N | etcdserver: set the initial cluster version to 2.2 [root@k8s-master skydns]#
好,dns配置完成!