注意:若是沒有特殊指明,本文檔的全部操做均在 K8s-master1 節點上執行。linux
將下載的 kubernetes-server-linux-amd64.tar.gz 解壓後,再解壓其中的 kubernetes-src.tar.gz 文件。nginx
coredns 對應的目錄是:cluster/addons/dns
。git
cd /opt/k8s/work/kubernetes/cluster/addons/dns/coredns cp coredns.yaml.base coredns.yaml source /opt/k8s/bin/environment.sh sed -i -e "s/__PILLAR__DNS__DOMAIN__/${CLUSTER_DNS_DOMAIN}/" -e "s/__PILLAR__DNS__SERVER__/${CLUSTER_DNS_SVC_IP}/" coredns.yaml
kubectl create -f coredns.yaml
[root@k8s-master1 coredns]# kubectl get all -n kube-system NAME READY STATUS RESTARTS AGE pod/coredns-5b969f4c88-wfn6g 0/1 ContainerCreating 0 13s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kube-dns ClusterIP 10.254.0.2 <none> 53/UDP,53/TCP,9153/TCP 13s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/coredns 0/1 1 0 13s NAME DESIRED CURRENT READY AGE replicaset.apps/coredns-5b969f4c88 1 1 0 13s
若是出現coredns鏡像拉取失敗,在每一個工做節點上執行github
images=( coredns:1.3.1 ) for imageName in ${images[@]} ; do docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName done
新建一個 Deploymentdocker
cd /opt/k8s/work cat > my-nginx.yaml <<EOF apiVersion: extensions/v1beta1 kind: Deployment metadata: name: my-nginx spec: replicas: 2 template: metadata: labels: run: my-nginx spec: containers: - name: my-nginx image: nginx:1.7.9 ports: - containerPort: 80 EOF kubectl create -f my-nginx.yaml
Export 該 Deployment, 生成 my-nginx
服務:api
[root@k8s-master1 work]# kubectl expose deploy my-nginx service/my-nginx exposed [root@k8s-master1 work]# kubectl get services --all-namespaces |grep my-nginx default my-nginx ClusterIP 10.254.110.68 <none> 80/TCP 6s
建立另外一個 Pod,查看 /etc/resolv.conf
是否包含 kubelet
配置的 --cluster-dns
和 --cluster-domain
,是否可以將服務 my-nginx
解析到上面顯示的 Cluster IP 10.254.242.255
app
cd /opt/k8s/work cat > dnsutils-ds.yml <<EOF apiVersion: v1 kind: Service metadata: name: dnsutils-ds labels: app: dnsutils-ds spec: type: NodePort selector: app: dnsutils-ds ports: - name: http port: 80 targetPort: 80 --- apiVersion: extensions/v1beta1 kind: DaemonSet metadata: name: dnsutils-ds labels: addonmanager.kubernetes.io/mode: Reconcile spec: template: metadata: labels: app: dnsutils-ds spec: containers: - name: my-dnsutils image: tutum/dnsutils:latest command: - sleep - "3600" ports: - containerPort: 80 EOF kubectl create -f dnsutils-ds.yml
[root@k8s-master1 coredns]# kubectl exec dnsutils-ds-9t5r5 nslookup kubernetes Server: 10.254.0.2 Address: 10.254.0.2#53 Name: kubernetes.default.svc.cluster.local Address: 10.254.0.1