一篇特别好的文章:http://ccnuo.com/
服务发现
1.部署kubernetesubernetes的内网资源配置清单
在k8s-60.host.com上,配置一个NGINX,用以提供kubernetes统一的访问资源清单入口
[root@k8s-60 html]# vi /etc/nginx/conf.d/k8s-yaml.od.com.conf
server {
listen 80;
server_name k8s-yaml.od.com;
location / {
autoindex on;
default_type text/plain;
root /data/k8s-yaml;
}
}
[root@k8s-60 html]# nginx -t
[root@k8s-60 html]# nginx -s reload
[root@k8s-60 html]# cd /data
[root@k8s-60 data]# mkdir k8s-yaml
[root@k8s-60 data]# cd k8s-yaml/
[root@k8s-60 k8s-yaml]# mkdir coredns
2.配置dns解析
[root@k8s-20 ~]# vi /var/named/od.com.zone
$ORIGIN od.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.od.com. dnsadmin.od.com. (
2019111003 ; serial
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.od.com.
$TTL 60 ; 1 minute
dns A 192.168.10.20
harbor A 192.168.10.60
k8s-yaml A 192.168.10.60
[root@k8s-20 ~]# systemctl restart named
[root@k8s-20 ~]# dig -t A k8s-yaml.od.com @192.168.10.20 +short
192.168.10.60
3.浏览器访问k8s-yaml.od.com
可以看到有coredns目录
4.下载docker镜像并打包上传到harbor仓库
[root@hdss7-60 ~]# docker pull coredns/coredns:1.6.1
[root@hdss7-60 coredns]# docker tag c0f6e815079e harbor.od.com/public/coredns:v1.6.1
[root@hdss7-60 coredns]# docker push harbor.od.com/public/coredns:v1.6.1
5.准备资源配置清单
官方DNS插件—coredns 模板
https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns/coredns
rbac.yaml
权限相关
coredns需要用到集群底层的资源,需要拿到集群权限
[root@k8s-60 coredns]# cd /data/k8s-yaml/coredns/
[root@k8s-60 coredns]# vi rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: coredns
namespace: kube-system
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
kubernetes.io/bootstrapping: rbac-defaults
addonmanager.kubernetes.io/mode: Reconcile
name: system:coredns
rules:
- apiGroups:
- ""
resources:
- endpoints
- services
- pods
- namespaces
verbs:
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
labels:
kubernetes.io/bootstrapping: rbac-defaults
addonmanager.kubernetes.io/mode: EnsureExists
name: system:coredns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:coredns
subjects:
- kind: ServiceAccount
name: coredns
namespace: kube-system
cm.yaml
对coredns进行的配置 corefile
[root@k8s-60 coredns]# vi cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
log
health
ready
kubernetes cluster.local 10.96.0.0/22
forward . 192.168.10.20
cache 30
loop
reload
loadbalance
}
dp.yaml
coredns 的pod的控制器
[root@k8s-60 coredns]# vi dp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
namespace: kube-system
labels:
k8s-app: coredns
kubernetes.io/name: "CoreDNS"
spec:
replicas: 1
selector:
matchLabels:
k8s-app: coredns
template:
metadata:
labels:
k8s-app: coredns
spec:
priorityClassName: system-cluster-critical
serviceAccountName: coredns
containers:
- name: coredns
image: harbor.od.com/public/coredns:v1.6.1
args:
- -conf
- /etc/coredns/Corefile
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
ports:
- containerPort: 53
name: dns
protocol: UDP
- containerPort: 53
name: dns-tcp
protocol: TCP
- containerPort: 9153
name: metrics
protocol: TCP
livenessProbe:
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
dnsPolicy: Default
volumes:
- name: config-volume
configMap:
name: coredns
items:
- key: Corefile
path: Corefile
svc.yaml
service
[root@k8s-60 coredns]# vi svc.yaml
apiVersion: v1
kind: Service
metadata:
name: coredns
namespace: kube-system
labels:
k8s-app: coredns
kubernetes.io/cluster-service: "true"
kubernetes.io/name: "CoreDNS"
spec:
selector:
k8s-app: coredns
clusterIP: 10.96.0.2
ports:
- name: dns
port: 53
protocol: UDP
- name: dns-tcp
port: 53
- name: metrics
port: 9153
protocol: TCP
6.应用资源配置
陈述式资源管理方法
[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/rbac.yaml
[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/cm.yaml
[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/dp.yaml
[root@k8s-40 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/svc.yaml
7.检查创建的资源
[root@k8s-40 ~]# kubectl get all -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-6b6c4f9648-8fzsf 1/1 Running 0 17s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/coredns ClusterIP 10.96.0.2 <none> 53/UDP,53/TCP,9153/TCP 13s
## 可以查看到ClusterIP,这个ClusterIP在之前配置的kubelet.sh文件中就已经定好的了
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/coredns 1/1 1 1 18s
NAME DESIRED CURRENT READY AGE
replicaset.apps/coredns-6b6c4f9648 1 1 1 17s
10.96.0.2在安装集群的时候已经定好了
/opt/kubernetes/server/bin/kubelet.sh
—cluster-dns 10.96.0.2
CoreNDS是使server_name和cluster_ip关联起来
8.验证coreDNS
[root@k8s-40 ~]# dig -t A www.baidu.com @10.96.0.2 +short
www.a.shifen.com.
183.232.231.172
183.232.231.174
[root@k8s-40 ~]# dig -t A k8s-40.host.com @10.96.0.2 +short
192.168.10.40
coredns原理解析