coredns部署
coredns部署
一:coredns-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:name: corednsnamespace: kube-systemlabels:kubernetes.io/cluster-service: "true"addonmanager.kubernetes.io/mode: Reconcile
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:labels:kubernetes.io/bootstrapping: rbac-defaultsaddonmanager.kubernetes.io/mode: Reconcilename: system:coredns
rules:
- apiGroups:- ""resources:- endpoints- services- pods- namespacesverbs:- list- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:annotations:rbac.authorization.kubernetes.io/autoupdate: "true"labels:kubernetes.io/bootstrapping: rbac-defaultsaddonmanager.kubernetes.io/mode: EnsureExistsname: system:coredns
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: system:coredns
subjects:
- kind: ServiceAccountname: corednsnamespace: kube-system
kubectl create -f coredns-rbac.yaml
二:coredns-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: corednsnamespace: kube-system
data:Corefile: |.:53 {errorsloghealthreadykubernetes cluster.local 10.8.0.0/16 #service资源cluster地址forward . 192.168.10.2 #上级DNS地址cache 30loopreloadloadbalance}
kubectl create -f coredns-configmap.yaml
三:coredns-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: corednsnamespace: kube-systemlabels:k8s-app: corednskubernetes.io/name: "CoreDNS"
spec:replicas: 1selector:matchLabels:k8s-app: corednstemplate:metadata:labels:k8s-app: corednsspec:priorityClassName: system-cluster-criticalserviceAccountName: corednscontainers:- name: corednsimage: docker.io/coredns/coredns args:- -conf- /etc/coredns/CorefilevolumeMounts:- name: config-volumemountPath: /etc/corednsports:- containerPort: 53name: dnsprotocol: UDP- containerPort: 53name: dns-tcpprotocol: TCP- containerPort: 9153name: metricsprotocol: TCPlivenessProbe:httpGet:path: /healthport: 8080scheme: HTTPinitialDelaySeconds: 60timeoutSeconds: 5successThreshold: 1failureThreshold: 5dnsPolicy: Defaultvolumes:- name: config-volumeconfigMap:name: corednsitems:- key: Corefilepath: Corefile
kubectl create -f coredns-deployment.yaml
四:coredns-svc.yaml
apiVersion: v1
kind: Service
metadata:name: corednsnamespace: kube-systemlabels:k8s-app: corednskubernetes.io/cluster-service: "true"kubernetes.io/name: "CoreDNS"
spec:selector:k8s-app: corednsclusterIP: 10.8.0.2ports:- name: dnsport: 53protocol: UDP- name: dns-tcpport: 53- name: metricsport: 9153protocol: TCP
kubectl create -f coredns-svc.yaml
五:验证
[root@k3 coredns]# kubectl get all -n kube-system -l k8s-app=coredns
NAME READY STATUS RESTARTS AGE
pod/coredns-5c956578dd-5lmwl 1/1 Running 0 2m9s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/coredns ClusterIP 10.8.0.2 53/UDP,53/TCP,9153/TCP 96s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/coredns 1/1 1 1 2m9s
NAME DESIRED CURRENT READY AGE
replicaset.apps/coredns-5c956578dd 1 1 1 2m9s
[root@k3 coredns]# dig -t A www.baidu.com @192.168.10.2 +short
www.a.shifen.com.
183.232.231.172
183.232.231.174
[root@k3 coredns]# dig -t A www.baidu.com @10.8.0.2 +short
www.a.shifen.com.
183.232.231.172
183.232.231.174
[root@k3 coredns]# kubectl create deployment nginx-dp --image=nginx
deployment.apps/nginx-dp created
[root@k3 coredns]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-dp 1/1 1 1 9s
[root@k3 coredns]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-dp-6cd9b76579-5dz7t 1/1 Running 0 19s
[root@k3 coredns]# kubectl expose deployment nginx-dp --port=80
service/nginx-dp exposed
[root@k3 coredns]# dig -t A nginx-dp @10.8.0.2 +short
[root@k3 coredns]# dig -t A nginx-dp.default.svc.cluster.local. @10.8.0.2 +short
10.8.223.101
[root@k3 coredns]# kubectl exec -it nginx-dp-6cd9b76579-5dz7t -- cat /etc/resolv.conf
nameserver 10.0.0.2
search default.svc.cluster.local. svc.cluster.local. cluster.local.
options ndots:5
##configmap 解析
volumes:- name: confconfigMap:name: cm-testitems:- key: index.jsp #key不能写错,cm文件里定义的就是这个path: index.jsp #挂载在容器后叫什么文件名