kubernetes中怎么搭建和使用Coredns
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章给大家分享的是kubernetes中搭建和使用Coredns的方法,相信大部分人都还不知道怎么安装和使用,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。一、Coredns作用重
千家信息网最后更新 2026年11月03日kubernetes中怎么搭建和使用Coredns
这篇文章给大家分享的是kubernetes中搭建和使用Coredns的方法,相信大部分人都还不知道怎么安装和使用,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。
一、Coredns作用

重点:通过coredns 通过service名称,解释到相应的cluter集群IP
二、Coredns安装(以容器搭建服务)
1、在运维主机上搭建一个HTTP服务存放yaml文件
~]# cd /etc/nginx/conf.d/conf.d]# vi /etc/nginx/conf.d/k8s-yaml.od.com.confserver { listen 80; server_name k8s-yaml.od.com; location / { autoindex on; default_type text/plain; root /data/k8s-yaml; }}conf.d]# mkdir /data/k8s-yamlconf.d]# nginx -tconf.d]# nginx -s reloadconf.d]# cd /data/k8s-yaml/k8s-yaml]# mkdir coredns
2、创建四个yaml文件,用于coredns容器创建
[root@test-operator coredns]# cat rbac.yaml apiVersion: v1kind: ServiceAccountmetadata: name: coredns namespace: kube-system labels: kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: Reconcile---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: labels: kubernetes.io/bootstrapping: rbac-defaults addonmanager.kubernetes.io/mode: Reconcile name: system:corednsrules:- apiGroups: - "" resources: - endpoints - services - pods - namespaces verbs: - list - watch---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: annotations: rbac.authorization.kubernetes.io/autoupdate: "true" labels: kubernetes.io/bootstrapping: rbac-defaults addonmanager.kubernetes.io/mode: EnsureExists name: system:corednsroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: system:corednssubjects:- kind: ServiceAccount name: coredns namespace: kube-system-----------------------------------------------------------------------------------------------[root@test-operator coredns]# cat cm.yamlapiVersion: v1kind: ConfigMapmetadata: name: coredns namespace: kube-systemdata: Corefile: | .:53 { errors log health ready kubernetes cluster.local 192.168.0.0/16 forward . 10.3.151.13 cache 30 loop reload loadbalance }-----------------------------------------------------------------------------------------------[root@test-operator coredns]# cat dp.yamlapiVersion: apps/v1kind: Deploymentmetadata: 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: test-harbor.cedarhd.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-----------------------------------------------------------------------------------------------[root@test-operator coredns]# cat svc.yaml apiVersion: v1kind: Servicemetadata: 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: TCP3、在其中一个节点服务器运行coredns(安装成功)
[root@test-nodes1 ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/rbac.yamlserviceaccount/coredns createdclusterrole.rbac.authorization.k8s.io/system:coredns createdclusterrolebinding.rbac.authorization.k8s.io/system:coredns created[root@test-nodes1 ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/cm.yamlconfigmap/coredns created[root@test-nodes1 ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/dp.yamldeployment.apps/coredns created[root@test-nodes1 ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/svc.yamlservice/coredns created[root@test-nodes1 ~]# kubectl get all -n kube-systemNAME READY STATUS RESTARTS AGEpod/coredns-6c69fbcc6c-6vqgr 1/1 Running 0 35sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/coredns ClusterIP 192.168.0.2 53/UDP,53/TCP,9153/TCP 14sNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/coredns 1/1 1 1 36sNAME DESIRED CURRENT READY AGEreplicaset.apps/coredns-6c69fbcc6c 1 1 1 36s 三、使用场景描述
1、创建一个新的svc资源[root@test-nodes1 ~]# kubectl create deployment nginx-test --image=test-harbor.cedarhd.com/public/nginx:v1.7.9deployment.apps/nginx-test created[root@test-nodes1 ~]# kubectl get all NAME READY STATUS RESTARTS AGEpod/nginx-test-5674474869-c4mzx 1/1 Running 0 5sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 192.168.0.1443/TCP 8hNAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-test 1/1 1 1 5sNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-test-5674474869 1 1 1 5s[root@test-nodes1 ~]# kubectl expose deployment nginx-test --port=80service/nginx-test exposed[root@test-nodes1 ~]# kubectl get allNAME READY STATUS RESTARTS AGEpod/nginx-test-5674474869-c4mzx 1/1 Running 0 42sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/kubernetes ClusterIP 192.168.0.1 443/TCP 8hservice/nginx-test ClusterIP 192.168.109.13 80/TCP 8s #创建svc为nginx-test 对应的cluterip为192.168.109.13NAME READY UP-TO-DATE AVAILABLE AGEdeployment.apps/nginx-test 1/1 1 1 42sNAME DESIRED CURRENT READY AGEreplicaset.apps/nginx-test-5674474869 1 1 1 42s2、进入其中一个容器查看解释效果[root@test-nodes2 ~]# kubectl get pods -n kube-publicNAME READY STATUS RESTARTS AGEnginx-ds-dk9hf 1/1 Running 0 3h53mnginx-ds-m6v9q 1/1 Running 0 3h53m[root@test-nodes2 ~]# kubectl exec -ti nginx-ds-dk9hf /bin/bash -n kube-publicPING nginx-test.default.svc.cluster.local (192.168.109.13) 56(84) bytes of data.64 bytes from nginx-test.default.svc.cluster.local (192.168.109.13): icmp_seq=1 ttl=64 time=0.070 ms64 bytes from nginx-test.default.svc.cluster.local (192.168.109.13): icmp_seq=2 ttl=64 time=0.077 ms#nginx-test.default defalut(容器所在的空间,必须加)
以上就是kubernetes中搭建和使用Coredns的方法介绍了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注行业资讯!
容器
服务
内容
文件
方法
解释
成功
主机
作用
名称
场景
大部分
就是
所在
效果
更多
服务器
看吧
空间
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
疫情对互联网科技公司影响
科技与互联网发展
互联网科技排名
百单互联网科技有限公司
哈尔滨新华互联网科技
互联网科技产品
隆华科技工业互联网
互联网大会黑科技排名
知域互联网科技有限公司大骗子
互联网科技扶贫时间
中国互联网十大科技股排名
想开互联网科技公司怎么样
工业互联网等科技实现智能化赋能
区块链结合互联网科技
工业互联网科技创业
东营紫格互联网科技
长沙渺峰互联网科技有限公司
山东蛋芒互联网科技有限公司
现代互联网科技公司
青岛互联网科技园
互联网科技保险
为想互联网科技
科技互联网公司logo
国外互联网科技网站
互联网 科技服务平台
成都仁贤领冠互联网科技有限公司
注册互联网科技公司经营范围大全
济南有互联网科技公司吗
元气玛特互联网科技公司是外包吗
广州富喜互联网络科技