K8S 之 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.conf
server {
    listen       80;
    server_name  k8s-yaml.od.com;

    location / {
        autoindex on;
        default_type text/plain;
        root /data/k8s-yaml;
    }
}
conf.d]# mkdir /data/k8s-yaml
conf.d]# nginx -t
conf.d]# nginx -s reload
conf.d]# cd /data/k8s-yaml/
k8s-yaml]# mkdir coredns

2、创建四个yaml文件,用于coredns容器创建


[[email protected] coredns]# cat 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
-----------------------------------------------------------------------------------------------

[[email protected] coredns]# cat 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.3.151.13
        cache 30
        loop
        reload
        loadbalance
       }
-----------------------------------------------------------------------------------------------

[[email protected] coredns]# cat 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: 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
-----------------------------------------------------------------------------------------------

[[email protected] coredns]# cat 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

3、在其中一个节点服务器运行coredns(安装成功)

[[email protected] ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/rbac.yaml
serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
[[email protected] ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/cm.yaml
configmap/coredns created
[[email protected] ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/dp.yaml
deployment.apps/coredns created
[[email protected] ~]# kubectl apply -f http://k8s-yaml.cedarhd.com/coredns/svc.yaml
service/coredns created
[[email protected] ~]# kubectl get all -n kube-system
NAME                           READY   STATUS    RESTARTS   AGE
pod/coredns-6c69fbcc6c-6vqgr   1/1     Running   0          35s
NAME              TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                  AGE
service/coredns   ClusterIP   192.168.0.2   <none>        53/UDP,53/TCP,9153/TCP   14s
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/coredns   1/1     1            1           36s
NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/coredns-6c69fbcc6c   1         1         1       36s

三、使用场景描述

1、创建一个新的svc资源
[[email protected] ~]# kubectl create deployment nginx-test --image=test-harbor.cedarhd.com/public/nginx:v1.7.9
deployment.apps/nginx-test created
[[email protected] ~]# kubectl get all
NAME                              READY   STATUS    RESTARTS   AGE
pod/nginx-test-5674474869-c4mzx   1/1     Running   0          5s
NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   192.168.0.1   <none>        443/TCP   8h
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-test   1/1     1            1           5s
NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-test-5674474869   1         1         1       5s

[[email protected] ~]# kubectl expose deployment nginx-test --port=80
service/nginx-test exposed
[[email protected]s1 ~]# kubectl get all
NAME                              READY   STATUS    RESTARTS   AGE
pod/nginx-test-5674474869-c4mzx   1/1     Running   0          42s
NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   192.168.0.1      <none>        443/TCP   8h
service/nginx-test   ClusterIP   192.168.109.13   <none>        80/TCP    8s
#创建svc为nginx-test 对应的cluterip为192.168.109.13
NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-test   1/1     1            1           42s
NAME                                    DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-test-5674474869   1         1         1       42s

2、进入其中一个容器查看解释效果
[[email protected] ~]# kubectl get pods -n kube-public
NAME             READY   STATUS    RESTARTS   AGE
nginx-ds-dk9hf   1/1     Running   0          3h43m
nginx-ds-m6v9q   1/1     Running   0          3h43m
[[email protected] ~]# kubectl exec -ti nginx-ds-dk9hf /bin/bash -n kube-public
PING 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 ms
64 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(容器所在的空间,必须加)

原文地址:https://blog.51cto.com/12965094/2469193

时间: 2024-10-05 09:13:34

K8S 之 Coredns安装与理解的相关文章

kubernetes(九)二进制安装-CoreDns安装

部署 coredns 插件(在master节点上执行) 下载和配置 coredns cd /opt/k8s/work git clone https://github.com/coredns/deployment.git mv deployment coredns 启动 coredns cd /opt/k8s/work/coredns/kubernetes export CLUSTER_DNS_SVC_IP="10.254.0.2" export CLUSTER_DNS_DOMAIN=

Kubernetes1.12版本Dashboard和coredns安装

一.部署Web UI(Dashboard)1.解压包,进入目录包就在之前的master部署组件里这里里面kubernetes-server-linux-amd64.tar.gz 2.执行yaml文件 查看启动的pod,没在默认命名空间,在kube-system下 注:其中dashboard-controller.yaml这个里面的dashboard镜像是国外的,如果慢,可以换成国内的镜像地址 image: registry.cn-hangzhou.aliyuncs.com/google_cont

K8S 之 kubeadm 安装

kubeadm 概述 Kubeadm 是一个工具,它提供了 kubeadm init 以及 kubeadm join 这两个命令作为快速创建 kubernetes 集群的最佳实践. 环境 阿里云两台CentOS 7.7 64位 ECS 安装 K8S v1.16.3 版本 pod-network-cidr 地址段划分为 10.96.0.0/12 service-cluster-ip-range 地址段划分为 10.244.0.0/16 名称 内部IP 系统配制 k8s-master1 172.17

docker&amp;k8s&amp;openstack基本概念和理解

Iaas   Paas   Saas Iaas:基础设施服务,操作系统之上,常见如虚拟机,存储,网络(说通俗点 cpu,硬盘,网卡),当然这些是建立在虚拟技术之上的,不需要你买主机就可以使用阿里云服务器 Paas:平台服务, 常见如数据库,容器技术,web应用 Saas:软件服务,常见如浏览器,qq等 如果你是一个网站站长,想要建立一个网站.不采用云服务,你所需要的投入大概是:买服务器,安装服务器软件,编写网站程序. 现在你追随潮流,采用流行的云计算, 如果你采用IaaS服务,那么意味着你就不用

K8S 之 Dashboard安装

一.Dashboard安装 #在运维主机上运行 k8s-yaml]# docker pull k8scn/kubernetes-dashboard-amd64:v1.8.3 k8s-yaml]# docker images|grep dashboard k8s-yaml]# docker tag fcac9aa03fd6 harbor.od.com/public/dashboard:v1.8.3 k8s-yaml]# docker push test-harbor.cedarhd.com/pub

k8s中helm安装部署,升级和回滚(chart,helm,tiller,StorageClass)

一.Helm介绍 helm是基于kubernetes 的包管理器.它之于 kubernetes 就如 yum 之于 centos,pip 之于 python,npm 之于 javascript 那 helm 的引入对于管理集群有哪些帮助呢? 更方便地部署基础设施,如 gitlab,postgres,prometheus,grafana 等 更方便地部署自己的应用,为公司内部的项目配置 Chart,使用 helm 结合 CI,在 k8s 中部署应用一行命令般简单 1.Helm用途 Helm把Kub

k8s dns 服务安装配置说明

1. 提前条件 安装k8s 集群 2.  dns  安装配置 安装方式: 使用controller  service controller  脚本: 基于官方改动 apiVersion: v1 kind: ReplicationController metadata: name: kube-dns-v8 namespace: kube-system labels: k8s-app: kube-dns version: v8 kubernetes.io/cluster-service: "true

K8S 集群安装

一.环境 1.操作系统 Centos 7.4 2.主机信息 K8S Master主机: kb-master-001 192.168.0.11 kb-master-002 192.168.0.12 kb-master-003 192.168.0.13 K8S Node主机: kb-node-001 192.168.0.11 kb-node-002 192.168.0.12 kb-node-003 192.168.0.13 kb-node-004 192.168.0.14 K8S etcd主机: k

k8s之二进制安装etcd集群

前言 kubeadm安装的集群,默认etcd是一个单机的容器化的etcd,并且k8s和etcd通信没有经过ssl加密和认证,这点是需要改造的.所以首先我们需要先部署一个三节点的etcd集群,二进制部署,systemd守护进程,并且需要生成ca证书 ETCD集群详情 主机 IP 节点名称 etcd的名称 主机01 192.168.56.200 MM etcd1 主机02 192.168.56.201 SS01 etcd2 主机03 192.168.56.202 SS02 etcd3 master上