9gbqkip_永久3e38 cos

小科普 128

背景: 前端Pod 需要 访问 后端Pod ,可以采用service 的DNS 解析 ,为Kubernetes集群里的容器提供DNS服务,用于解析service名称

一、部署CoreDNS PodCoreDNS 是用于service做dns解析的,部署完成之后就可以通过service的名称访问service实现访问pod,CoreDNS是当前k8s的默认dns

[root@master-1yaml]catcoredns.yamlWarning: This is a file generated from the base underscore template file: coredns.yaml.baseapiVersion:v1kind:ServiceAccountmetadata:name:corednsnamespace:kube-systemlabels:kubernetes.io/cluster-service:"true"addonmanager.kubernetes.io/mode:Reconcile---apiVersion:rbac.authorization.k8s.io/v1kind:ClusterRolemetadata:labels:kubernetes.io/bootstrapping:rbac-defaultsaddonmanager.kubernetes.io/mode:Reconcilename:system:corednsrules:-apiGroups:-""resources:-endpoints-services-pods-namespacesverbs:-list-watch---apiVersion:rbac.authorization.k8s.io/v1kind:ClusterRoleBindingmetadata:annotations:rbac.authorization.kubernetes.io/autoupdate:"true"labels:kubernetes.io/bootstrapping:rbac-defaultsaddonmanager.kubernetes.io/mode:EnsureExistsname:system:corednsroleRef:apiGroup:rbac.authorization.k8s.iokind:ClusterRolename:system:corednssubjects:-kind:ServiceAccountname:corednsnamespace:kube-system---apiVersion:v1kind:ConfigMapmetadata:name:corednsnamespace:kube-systemlabels:addonmanager.kubernetes.io/mode:EnsureExistsdata:Corefile:|
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
upstream
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
proxy . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}---apiVersion:apps/v1kind:Deploymentmetadata:name:corednsnamespace:kube-systemlabels:k8s-app:kube-dnskubernetes.io/cluster-service:"true"addonmanager.kubernetes.io/mode:Reconcilekubernetes.io/name:"CoreDNS"spec:replicas: not specified here:1. In order to make Addon Manager do not reconcile this replicas parameter.2. Default is 1.3. Will be tuned in real time if DNS horizontal auto-scaling is turned on.strategy:type:RollingUpdaterollingUpdate:maxUnavailable:1selector:matchLabels:k8s-app:kube-dnstemplate:metadata:labels:k8s-app:kube-dnsannotations:seccomp.security.alpha.kubernetes.io/pod:docker/defaultspec:serviceAccountName:corednstolerations:-key:node-role.kubernetes.io/mastereffect:NoSchedule-key:"CriticalAddonsOnly"operator:"Exists"containers:-name:corednsimage:lizhenliang/coredns:1.2.2imagePullPolicy:IfNotPresentresources:limits:memory:170Mirequests:cpu:100mmemory:70Miargs:["-conf","/etc/coredns/Corefile"]volumeMounts:-name:config-volumemountPath:/etc/corednsreadOnly:trueports:-containerPort:53name:dnsprotocol:UDP-containerPort:53name:dns-tcpprotocol:TCP-containerPort:9153name:metricsprotocol:TCPlivenessProbe:httpGet:path:/healthport:8080scheme:HTTPinitialDelaySeconds:60timeoutSeconds:5successThreshold:1failureThreshold:5securityContext:allowPrivilegeEscalation:falsecapabilities:add:-NET_BIND_SERVICEdrop:-allreadOnlyRootFilesystem:truednsPolicy:Defaultvolumes:-name:config-volumeconfigMap:name:corednsitems:-key:Corefilepath:Corefile---apiVersion:v1kind:Servicemetadata:name:kube-dnsnamespace:kube-systemannotations:prometheus.io/port:"9153"prometheus.io/scrape:"true"labels:k8s-app:kube-dnskubernetes.io/cluster-service:"true"addonmanager.kubernetes.io/mode:Reconcilekubernetes.io/name:"CoreDNS"spec:selector:k8s-app:kube-dnsclusterIP:10.0.0.2ports:-name:dnsport:53protocol:UDP-name:dns-tcpport:53protocol:TCP

[root@k8s-master1 yaml] kubectl apply -f coredns.yaml

serviceaccount/coredns created

clusterrole.rbac.authorization.k8s.io/system:coredns created

clusterrolebinding.rbac.authorization.k8s.io/system:coredns created

configmap/coredns created

deployment.apps/coredns created

service/kube-dns created

查看Pod状态

[root@k8s-master1 yaml]kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGE

coredns-6d8cfdd59d-87b7p0/1ContainerCreating040s

下载完成

[root@k8s-master1 yaml]kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGE

coredns-6d8cfdd59d-7dfjz1/1Running03m44s

二、创建Pod 测试

[root@master-1yaml]cattest.yamlapiVersion:v1kind:Podmetadata:name:busyboxnamespace:defaultspec:containers:-image:busybox:1.28.4command:-sleep-"3600"imagePullPolicy:IfNotPresentname:busyboxrestartPolicy:Always[root@k8s-master1 yaml]kubectl apply -f test.yamlpod/busybox created

查看Pod状态

[root@k8s-master1yaml]kubectlgetpods-owideNAMEREADYSTATUSRESTARTSAGEIPNODENOMINATEDNODEREADINESSGATESbusybox1/1Running15m59s10.244.2.6k8s-node3nginx-demo-574b6ddfd8-j487f1/1Running140h10.244.2.64node-3

为了测试dns 解析,需要先查看下同命名空间内的 另一个pod 的service

[root@master-1yaml]kubectlgetserviceNAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGEkubernetesClusterIP10.0.0.1443/TCP17dnginx-demoNodePort10.0.0.23880:32729/TCP40h

可以看到另一个POD的 cluster-ip 为 10.0.0.238,service名称 为 nginx-demo

进入到容器里面(访问同命名空间内的POD资源)

[root@k8s-master1 yaml] kubectl exec -it busybox sh

/ping nginx-demoPINGweb(10.0.0.238): 56 data bytes

64 bytesfrom10.0.0.238: seq=0ttl=64time=0.135ms64bytesfrom10.0.0.238: seq=1ttl=64time=0.158ms64bytesfrom10.0.0.238: seq=2ttl=64time=0.236ms64bytesfrom10.0.0.238: seq=3ttl=64time=0.127ms

可以看到通过service name ,DNS 可以直接ping通另一个 POD

再通过dns 来访问下另一个POD的业务(通过查看上面另一个pod的service 可以看到暴露的 cluster-ip 的端口为 80)

[root@k8s-master1 yaml] kubectl exec -it busybox sh
/ curl nginx-demohtml><html><head><title>Hello World

上一篇:

下一篇:

  推荐阅读

分享