Tag: kube-dns

  • วิธีการติดตั้ง Kubernetes Add-on DNS และ DashBoard

    Kubernetes มี DNS กับ GUI ให้ใช้งานบ้างไหม

                หลังจากติดตั้ง Kubernetes แล้วการสร้าง containner โดยในที่นี้เรียกว่า pod (ใช้เรียก 1 containner หรือกลุ่มของ containner ก็ได้) ซึ่งสามารถสร้างด้วยคำสั่ง Kubectl ได้เลย (สร้างจากที่ไหนก็ได้) สำหรับผู้ที่ยังไม่ได้ติดตั้ง ต้องติดตั้งก่อนตามวิธีติดตั้ง Kubernetes บน CoreOS ตอนที่ 1[1] และ ตอนที่ 2[2] 

    สำหรับ Add-on ที่แนะนำจะเป็น DNS และ Dashboard ซึ่งเรียกได้ว่าจำเป็นต้องมี ไม่งั้นชีวิตจะยุ่งยากขึ้นเยอะครับ คงไม่มีใครอยากดูรายการ pod เยอะ ๆ ด้วย command line กันนะครับ

     วิธีการติดตั้ง DNS-Addon[3]

    • เข้าไปยังเครื่องที่ติดตั้ง Kubectl ไว้แล้ว ทำการสร้าง yaml file ดังนี้ (ถ้า vim วางแล้วเพี้ยนให้ใช้ nano แทน)
      nano dns-addon.yaml

      เนื้อหาในไฟล์ดังนี้ (อย่าลืมเปลี่ยน > เป็น > เวลา Copy & Paste ด้วยครับ)

      apiVersion: v1
      kind: Service
      metadata:
        name: kube-dns
        namespace: kube-system
        labels:
          k8s-app: kube-dns
          kubernetes.io/cluster-service: "true"
          kubernetes.io/name: "KubeDNS"
      spec:
        selector:
          k8s-app: kube-dns
        clusterIP: <DNS Cluster-IP>
        ports:
        - name: dns
          port: 53
          protocol: UDP
        - name: dns-tcp
          port: 53
          protocol: TCP
      
      
      ---
      
      
      apiVersion: v1
      kind: ReplicationController
      metadata:
        name: kube-dns-v20
        namespace: kube-system
        labels:
          k8s-app: kube-dns
          version: v20
          kubernetes.io/cluster-service: "true"
      spec:
        replicas: 1
        selector:
          k8s-app: kube-dns
          version: v20
        template:
          metadata:
            labels:
              k8s-app: kube-dns
              version: v20
            annotations:
              scheduler.alpha.kubernetes.io/critical-pod: ''
              scheduler.alpha.kubernetes.io/tolerations: '[{"key":"CriticalAddonsOnly", "operator":"Exists"}]'
          spec:
            containers:
            - name: kubedns
              image: gcr.io/google_containers/kubedns-amd64:1.9
              resources:
                limits:
                  memory: 170Mi
                requests:
                  cpu: 100m
                  memory: 70Mi
              livenessProbe:
                httpGet:
                  path: /healthz-kubedns
                  port: 8080
                  scheme: HTTP
                initialDelaySeconds: 60
                timeoutSeconds: 5
                successThreshold: 1
                failureThreshold: 5
              readinessProbe:
                httpGet:
                  path: /readiness
                  port: 8081
                  scheme: HTTP
                initialDelaySeconds: 3
                timeoutSeconds: 5
              args:
              - --domain=cluster.local.
              - --dns-port=10053
              ports:
              - containerPort: 10053
                name: dns-local
                protocol: UDP
              - containerPort: 10053
                name: dns-tcp-local
                protocol: TCP
            - name: dnsmasq
              image: gcr.io/google_containers/kube-dnsmasq-amd64:1.4
              livenessProbe:
                httpGet:
                  path: /healthz-dnsmasq
                  port: 8080
                  scheme: HTTP
                initialDelaySeconds: 60
                timeoutSeconds: 5
                successThreshold: 1
                failureThreshold: 5
              args:
              - --cache-size=1000
              - --no-resolv
              - --server=127.0.0.1#10053
              - --log-facility=-
              ports:
              - containerPort: 53
                name: dns
                protocol: UDP
              - containerPort: 53
                name: dns-tcp
                protocol: TCP
            - name: healthz
              image: gcr.io/google_containers/exechealthz-amd64:1.2
              resources:
                limits:
                  memory: 50Mi
                requests:
                  cpu: 10m
                  memory: 50Mi
              args:
              - --cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1 > /dev/null
              - --url=/healthz-dnsmasq
              - --cmd=nslookup kubernetes.default.svc.cluster.local 127.0.0.1:10053 > /dev/null
              - --url=/healthz-kubedns
              - --port=8080
              - --quiet
              ports:
              - containerPort: 8080
                protocol: TCP
            dnsPolicy: Default
    • จากนั้นทำการสร้าง pod ดังนี้
      kubectl create -f dns-addon.yaml
    • สามารถตรวจสอบสถานะการสร้าง pod และ log ได้ดังนี้
      kubectl get pods --namespace=kube-system
      kubectl logs kube-dns-v20-xxxxx -n=kube-system dnsmasq
    • โดยอันที่จริงมีอีกหลายแบบครับ มีแบบ auto scale ก็มีครับ ลองศึกษาดูใน Internet ครับ

      วิธีการติดตั้ง Dashboard-Addon[3]

    • เข้าไปยังเครื่องที่ติดตั้ง Kubectl ไว้แล้ว ทำการสร้าง yaml file ดังนี้ (ถ้า vim วางแล้วเพี้ยนให้ใช้ nano แทน)
      nano kube-dashboard-rc.yaml

      เนื้อหาในไฟล์ดังนี้

      apiVersion: v1
      kind: ReplicationController
      metadata:
        name: kubernetes-dashboard-v1.6.1
        namespace: kube-system
        labels:
          k8s-app: kubernetes-dashboard
          version: v1.6.1
          kubernetes.io/cluster-service: "true"
      spec:
        replicas: 1
        selector:
          k8s-app: kubernetes-dashboard
        template:
          metadata:
            labels:
              k8s-app: kubernetes-dashboard
              version: v1.6.1
              kubernetes.io/cluster-service: "true"
            annotations:
              scheduler.alpha.kubernetes.io/critical-pod: ''
              scheduler.alpha.kubernetes.io/tolerations: '[{"key":"CriticalAddonsOnly", "operator":"Exists"}]'
          spec:
            containers:
            - name: kubernetes-dashboard
              image: gcr.io/google_containers/kubernetes-dashboard-amd64:v1.6.1
              resources:
                limits:
                  cpu: 100m
                  memory: 50Mi
                requests:
                  cpu: 100m
                  memory: 50Mi
              ports:
              - containerPort: 9090
              livenessProbe:
                httpGet:
                  path: /
                  port: 9090
                initialDelaySeconds: 30
                timeoutSeconds: 30
    • จากนั้นสร้างอีกไฟล์ดังนี้
      nano kube-dashboard-svc.yaml

      เนื้อหาในไฟล์ดังนี้

      apiVersion: v1
      kind: Service
      metadata:
        name: kubernetes-dashboard
        namespace: kube-system
        labels:
          k8s-app: kubernetes-dashboard
          kubernetes.io/cluster-service: "true"
      spec:
        selector:
          k8s-app: kubernetes-dashboard
        ports:
        - port: 80
          targetPort: 9090
    • จากนั้นทำการสร้าง pod ดังนี้
      kubectl create -f kube-dashboard-rc.yaml
      kubectl create -f kube-dashboard-svc.yaml
    • สามารถตรวจสอบสถานะการสร้าง pod และทำการ forward port ได้ดังนี้
      kubectl get pods --namespace=kube-system
      kubectl port-forward kubernetes-dashboard-v1.6.1-XXXX 9090 --namespace=kube-system

       

    • หลังจากนั้นสามารถทดสอบเปิด Browser บนเครื่องที่รัน kubectl เพื่อทดสอบได้เลย (สามารถเปิดใช้งานได้ 2 วิธีคือผ่านช่องทางปกติ หรือใช้ kubectl ดึงมาจาก pod โดยตรงแล้วทำการ forward port ก็ได้)

                      จะเห็นว่าถ้าเป็น web จะดูอะไรก็ง่ายขึ้นเยอะครับ แต่เนื่องจากไม่มีระบบ login ก็ต้องระวังดี ๆ ถ้าจะเปิดให้ใครก็ได้ใช้งาน อาจต้องสร้าง proxy ดักให้ใส่รหัสผ่านก่อนชั้นหนึ่ง หรือจะเน้นใช้ command line อย่างเดียวก็ได้ครับ….

    ==================================

    Reference :

    [1] รู้จักกับ Kubernetes และวิธีติดตั้ง Kubernetes ด้วย CoreOS (ตอนที่ 1 Master Node) : https://sysadmin.psu.ac.th/2017/05/17/setup-kubernetes-coreos-section-1/

    [2] วิธีติดตั้ง Kubernetes ด้วย CoreOS (ตอนที่ 2 Worker Node) : https://sysadmin.psu.ac.th/2017/05/27/setup-kubernetes-coreos-section-2/

    [3] CoreOS -> Deploy Add-ons : https://coreos.com/kubernetes/docs/latest/deploy-addons.html