Kubernetes编译部署DNS

概述

在前一篇文章Kubernetes部署DNS和Dashboard中,我们使用官方提供的yaml启动相关容器实现DNS。本文通过go编译成可执行文件,然后配置启动skydns。

编译skydns和kube2sky

yum install -y go git
#go version   go1.7 linux/amd64
mkdir /tmp/skydns
export GOPATH=/tmp/skydns
cd /tmp/skydns 
git clone https://github.com/kubernetes/dns.git
cd $GOPATH/src/github.com/skynetservices/skydns
go build -v
cp skydns /usr/bin
go get github.com/tools/godep
go get -d github.com/GoogleCloudPlatform/Kubernetes/cluster/addons/dns/kube2sky
cd  github.com/GoogleCloudPlatform/kubernetes/cluster/addons/dns/kube2sky
make kube2sky
cp kube2sky /usr/bin

现在的github版本已经完全容器化,直接go get 获取不到kube2sky源码

配置启动DNS

etcd和k8s-master地址:

etcd:192.168.99.100
kube-master:192.168.99.100

下面的操作也是在192.168.99.100。

在etcd中添加dns配置

etcdctl mk /skydns/config '{"dns-addr": "0.0.0.0:53", "tt": 3600, "domain": "cluster.local", "nameservers": ["172.31.0.2:53"]}'
  • dns-addr: 指定skydns启动时的监听地址
  • ttl: 指域名的缓存的超时时间,单位秒
  • domain: 集群中的私有域名,这里如果不指定,默认为skydns.local. ,注意最后的一个点
  • nameservers:域名转发的dns列表,用来解析集群外部的域名(可以配置多个,可以用公共dns)

配置skydns

skydns配置文件:/etc/kubernetes/skydns

MACHINES=-machines=http://192.168.99.100:2379
ADDR=-addr=0.0.0.0:53
DOMAIN=-domain=cluster.local

systemd配置: /usr/lib/systemd/system/skydns.service

[Unit]
Description=skydns
After=network.target
After=etcd.service 
[Service]
WorkingDirectory=/etc     
EnvironmentFile=-/etc/kubernetes/skydns
ExecStart=/usr/bin/skydns \
  $MACHINES \
  $ADDR \
  $DOMAIN
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动skydns:

systemctl start skydns
systemctl enable skydns
systemctl status skydns

配置kube2sky

kube2sky配置文件: /etc/kubernetes/kube2sky

DOMAIN=-domain=cluster.local
ETCD_SERVER=-etcd-server=http://192.168.99.100:2379
KUBE_MASTER=-kube_master_url=http://192.168.99.100:8080

systemd配置: /usr/lib/systemd/system/kube2sky.service

[Unit]
Description=kube2sky
After=network.target
After=etcd.service

[Service]
WorkingDirectory=/etc
EnvironmentFile=-/etc/kubernetes/kube2sky
ExecStart=/usr/bin/kube2sky $DOMAIN $ETCD_SERVER $KUBE_MASTER
Restart=on-failure
[Install]
WantedBy=multi-user.target

修改kubelet

修改k8s集群minion的kubelet配置,添加:

KUBELET_ARGS="--cluster_dns=192.168.99.100 --cluster_domain=cluster.local"

重启

systemctl restart kubelet

验证

kubectl run curl --image=radial/busyboxplus:curl -i --tty

和容器启动的验证方式一样。

CONTENTS