基于k8s搭建DNS服务器,并配置客户端(DNS端口非53)

一、 环境准备阶段

基本环境:
OS:Centos7.6
Kubernetes:v1.18.20
Docker:19.03.15
查看集群状态:
kubectl get node -o wide
在这里插入图片描述
kubectl get pods -n kube-system -owide
在这里插入图片描述

二 、配置dns服务端,依次创建namespace、configmap、deployment、svc

  1. 编写相关资源yaml文件
### 创建namespace
apiVersion: v1
kind: Namespace
metadata: 
  name: web-coredns
---
### 创建configMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: web-coredns
data:
  Corefile: |
    .:53 {
        errors
        health {
            lameduck 5s
        }
        ready
        kubernetes cluster.local. in-addr.arpa ip6.arpa {
            pods insecure
            fallthrough in-addr.arpa ip6.arpa
            ttl 30
        }
        hosts {
        ### 配置相关解析信息
        192.168.10.142 gitlab.zbq.com
        10.211.55.19 harbor.zbq.com
        10.211.55.20 ntp1.cloud.org
        fallthrough
        }
        forward . "/etc/resolv.conf"
        cache 30
        loop
        reload
        loadbalance
    }

---
### 创建service
apiVersion: v1
kind: Service
metadata:
  name: coredns
  namespace: web-coredns
spec:
  ports: 
    - name: dns-udp
      nodePort: 20053 #根据实际情况定义宿主机端口
      port: 53
      protocol: UDP
      targetPort: 53
    - name: dns-tcp
      nodePort: 20053  #根据实际情况定义宿主机端口
      port: 53
      protocol: TCP
      targetPort: 53
  selector:
    app: coredns
  type: NodePort
---
### 创建deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coredns
  namespace: web-coredns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: coredns
  template:
    metadata:
      labels:
        app: coredns
    spec:
      containers:
      - name: coredns
        image: coredns/coredns:1.9.0
        args:
        - -conf
        - /etc/coredns/Corefile
        volumeMounts:
        - name: config-volume
          mountPath: /etc/coredns
        ports:
        - containerPort: 53
          name: dns-udp
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          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

  1. 创建资源,并验证
    kubectl apply -f web.yaml
    在这里插入图片描述

三、配置dns客户端

方法一:通过配置dnsmasq服务
安装服务
yum -y install dnsmasq && rpm -qa | grep dnsmasq
在这里插入图片描述
配置服务

添加dns服务端ip及端口

在这里插入图片描述
配置解析文件/etc/resolv.conf
nameserver 127.0.0.1
在这里插入图片描述
方法二:通过配置iptables服务

添加目的端口nat

iptables -t nat -A OUTPUT -p udp --dport 53 -d 244.254.255.244 -j DNAT --to-destination 192.168.10.141:20053

iptables -t nat -A OUTPUT -p tcp --dport 53 -d 244.254.255.244 -j DNAT --to-destination 192.168.10.141:20053

备注:其中192.168.10.141:20053为服务端地址、端口。244.254.255.244为自定义地址,其实不真实存在。

配置解析文件/etc/resolv.conf
nameserver 244.254.255.244
在这里插入图片描述

验证:

在这里插入图片描述

ping gitlab.zbq.com
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Prometheus是一种流行的开源监控解决方案,它可以监控各种服务和服务器,包括Kubernetes集群。以下是在Kubernetes集群中搭建Prometheus的步骤: 1. 创建一个新的命名空间: ``` kubectl create namespace prometheus ``` 2. 下载Prometheus的配置文件: ``` wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.44.0/deploy/prometheus/manifests/prometheus-prometheus.yaml ``` 3. 修改Prometheus配置文件中的服务端口: ``` apiVersion: v1 kind: Service metadata: name: prometheus-operated namespace: prometheus annotations: prometheus.io/scrape: 'true' prometheus.io/port: '9090' spec: type: ClusterIP ports: - name: web port: 9090 targetPort: web protocol: TCP selector: app: prometheus release: prometheus ``` 4. 应用Prometheus配置文件: ``` kubectl apply -f prometheus-prometheus.yaml -n prometheus ``` 5. 创建一个Ingress资源: ``` apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: prometheus-ingress namespace: prometheus annotations: nginx.ingress.kubernetes.io/ssl-redirect: "false" spec: rules: - host: prometheus.example.com http: paths: - path: / pathType: Prefix backend: service: name: prometheus-operated port: name: web ``` 6. 配置DNS解析将prometheus.example.com指向集群的Ingress IP地址。 7. 访问prometheus.example.com即可访问Prometheus的UI界面。 注意事项: - 在Prometheus配置文件中,需要将prometheus.io/scrape和prometheus.io/port注释添加到Service资源的元数据中,以便Prometheus可以在该端口上抓取指标。 - 在Ingress资源中,需要将host字段设置为您的域名,并将backend.service.name设置为Prometheus Service资源的名称。 - 在访问Prometheus UI之前,需要先通过kubectl port-forward命令将Prometheus Service暴露到本地端口上。例如,kubectl port-forward -n prometheus svc/prometheus-operated 9090:9090。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值