前言
1.环境变量:
Pod创建的时候,服务的IP和Port会以环境变量的形式注入到pod里
2.dns:
K8s集群部署一个Dns服务器,Service创建成功后,会在Dns服务器里写入一些记录,想要访问某个服务,通过Dns服务器解析出对应的Ip和Port,从而实现k8s集群内部的服务访问
CoreDns可以解决Service的发现问题,k8s将Service的名称当做域名注册到CoreDns中,通过Service的名称就可以访问其提供的服务
CoreDNS部署
使用yaml方式部署CoreDNS
1、yaml资源配置清单
• rbac
$ vi /data/k8s-yaml/coredns/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
• ConfigMap
$ vi /data/k8s-yaml/coredns/cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
log
health
ready
kubernetes cluster.local 192.168.0.0/16
forward . 10.4.7.11
cache 30
loop
reload
loadbalance
}
关键参数解释:
kubernetes cluster.local 192.168.0.0/16:Cluster网段(该网段就是service ip地址范围)
forward . 10.4.7.11:制定上级dns服务器地址
• Deployment
$ vi /data/k8s-yaml/coredns/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
• Server
$ vi /data/k8s-yaml/coredns/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: 192.168.0.2
ports:
- name: dns
port: 53
protocol: UDP
- name: dns-tcp
port: 53
- name: metrics
port: 9153
protocol: TCP
关键参数解释:
clusterIP: 192.168.0.2 指定了coredns的server cluster ip地址(必须要和kubelet的--cluster-dns ip一致)
2、创建资源
$ kubectl apply -f rbac.yaml $ kubectl apply -f cm.yaml $ kubectl apply -f dp.yaml $ kubectl apply -f svc.yaml
3、
$ kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-6dd977c48b-rvb7h 1/1 Running 0 31s $ kubectl get svc -n kube-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE coredns ClusterIP 192.168.0.253/UDP,53/TCP,9153/TCP 3m20s
4、通过core dns解析svc名称获取到cluster-ip
# svc名称.命名空间.svc.cluster.local $ dig -t A nginx-ds.default.svc.cluster.local @192.168.0.2 +short 192.168.155.175
5、进入pod查看域名解析文件
$ kubectl exec -it nginx-ds-gbt9g bash root@nginx-ds-gbt9g:/# cat /etc/resolv.conf nameserver 192.168.0.2 # CoreDns地址 search default.svc.cluster.local svc.cluster.local cluster.local 4 options ndots:5
在容器内部是可以访问到本k8s集群里的所有svc