จะเริ่มบริการ kubernetes บน NodePort นอกช่วงเริ่มต้นของบริการ - โหนด - พอร์ต - ช่วงได้อย่างไร

ฉันพยายามเริ่ม kubernetes-dashboard (และในที่สุดบริการอื่น ๆ ) บน NodePort นอกช่วงพอร์ตเริ่มต้นโดยประสบความสำเร็จเพียงเล็กน้อย นี่คือการตั้งค่าของฉัน: ผู้ให้บริการคลาวด์: Azure (บริการคอนเทนเนอร์ไม่ใช่สีฟ้า) ระบบปฏิบัติการ: CentOS 7

นี่คือสิ่งที่ฉันได้ลอง:

อัพเดตเจ้าบ้าน

$ yum update

ติดตั้ง kubeadm

$ cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://yum.kubernetes.io/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
$ setenforce 0
$ yum install -y docker kubelet kubeadm kubectl kubernetes-cni
$ systemctl enable docker && systemctl start docker
$ systemctl enable kubelet && systemctl start kubelet

เริ่มต้นคลัสเตอร์ด้วย kubeadm

$ kubeadm init

อนุญาตให้เรียกใช้คอนเทนเนอร์บนโหนดหลัก เนื่องจากเรามีคลัสเตอร์โหนดเดียว

$ kubectl taint nodes --all dedicated-

ติดตั้งเครือข่ายพ็อด

$ kubectl apply -f https://git.io/weave-kube

การปรับใช้แดชบอร์ด kubernetes ของเรา (@ ~/kubernetes-dashboard.yaml

# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Configuration to deploy release version of the Dashboard UI.
#
# Example usage: kubectl create -f <this_file>

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  labels:
    app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kubernetes-dashboard
  template:
    metadata:
      labels:
        app: kubernetes-dashboard
      # Comment the following annotation if Dashboard must not be deployed on master
      annotations:
        scheduler.alpha.kubernetes.io/tolerations: |
          [
            {
              "key": "dedicated",
              "operator": "Equal",
              "value": "master",
              "effect": "NoSchedule"
            }
          ]
    spec:
      containers:
      - name: kubernetes-dashboard
        image: gcr.io/google_containers/kubernetes-dashboard-amd64:v1.5.1
        imagePullPolicy: Always
        ports:
        - containerPort: 9090
          protocol: TCP
        args:
          # Uncomment the following line to manually specify Kubernetes API server Host
          # If not specified, Dashboard will attempt to auto discover the API server and connect
          # to it. Uncomment only if the default does not work.
          # - --apiserver-host=http://my-address:port
        livenessProbe:
          httpGet:
            path: /
            port: 9090
          initialDelaySeconds: 30
          timeoutSeconds: 30
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  type: NodePort
  ports:
  - port: 8880
    targetPort: 9090
    nodePort: 8880
  selector:
    app: kubernetes-dashboard

สร้างการปรับใช้ของเรา

$ kubectl create -f ~/kubernetes-dashboard.yaml
deployment "kubernetes-dashboard" created
The Service "kubernetes-dashboard" is invalid: spec.ports[0].nodePort: Invalid value: 8880: provided port is not in the valid range. The range of valid ports is 30000-32767

ฉันพบว่าหากต้องการเปลี่ยนช่วงของพอร์ตที่ถูกต้อง ฉันสามารถตั้งค่าตัวเลือก service-node-port-range บน kube-apiserver เพื่ออนุญาตช่วงพอร์ตอื่นได้ ดังนั้นฉันจึงลองทำสิ่งนี้:

$ kubectl get po --namespace=kube-system
NAME                                    READY     STATUS    RESTARTS       AGE
dummy-2088944543-lr2zb                  1/1       Running   0              31m
etcd-test2-highr                        1/1       Running   0              31m
kube-apiserver-test2-highr              1/1       Running   0              31m
kube-controller-manager-test2-highr     1/1       Running   2              31m
kube-discovery-1769846148-wmbhb         1/1       Running   0              31m
kube-dns-2924299975-8vwjm               4/4       Running   0              31m
kube-proxy-0ls9c                        1/1       Running   0              31m
kube-scheduler-test2-highr              1/1       Running   2              31m
kubernetes-dashboard-3203831700-qrvdn   1/1       Running   0              22s
weave-net-m9rxh                         2/2       Running   0              31m

เพิ่ม "--service-node-port-range=8880-8880" ใน kube-apiserver-test2-highr

$ kubectl edit po kube-apiserver-test2-highr --namespace=kube-system
{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "kube-apiserver",
    "namespace": "kube-system",
    "creationTimestamp": null,
    "labels": {
      "component": "kube-apiserver",
      "tier": "control-plane"
    }
  },
  "spec": {
    "volumes": [
      {
        "name": "k8s",
        "hostPath": {
          "path": "/etc/kubernetes"
        }
      },
      {
        "name": "certs",
        "hostPath": {
          "path": "/etc/ssl/certs"
        }
      },
      {
        "name": "pki",
        "hostPath": {
          "path": "/etc/pki"
        }
      }
    ],
    "containers": [
      {
        "name": "kube-apiserver",
        "image": "gcr.io/google_containers/kube-apiserver-amd64:v1.5.3",
        "command": [
          "kube-apiserver",
          "--insecure-bind-address=127.0.0.1",
          "--admission-control=NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,ResourceQuota",
          "--service-cluster-ip-range=10.96.0.0/12",
          "--service-node-port-range=8880-8880",
          "--service-account-key-file=/etc/kubernetes/pki/apiserver-key.pem",
          "--client-ca-file=/etc/kubernetes/pki/ca.pem",
          "--tls-cert-file=/etc/kubernetes/pki/apiserver.pem",
          "--tls-private-key-file=/etc/kubernetes/pki/apiserver-key.pem",
          "--token-auth-file=/etc/kubernetes/pki/tokens.csv",
          "--secure-port=6443",
          "--allow-privileged",
          "--advertise-address=100.112.226.5",
          "--kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname",
          "--anonymous-auth=false",
          "--etcd-servers=http://127.0.0.1:2379"
        ],
        "resources": {
          "requests": {
            "cpu": "250m"
          }
        },
        "volumeMounts": [
          {
            "name": "k8s",
            "readOnly": true,
            "mountPath": "/etc/kubernetes/"
          },
          {
            "name": "certs",
            "mountPath": "/etc/ssl/certs"
          },
          {
            "name": "pki",
            "mountPath": "/etc/pki"
          }
        ],
        "livenessProbe": {
          "httpGet": {
            "path": "/healthz",
            "port": 8080,
            "host": "127.0.0.1"
          },
          "initialDelaySeconds": 15,
          "timeoutSeconds": 15,
          "failureThreshold": 8
        }
      }
    ],
    "hostNetwork": true
  },
  "status": {}

$ :wq

ต่อไปนี้คือการตอบสนองที่ถูกตัดทอน

# pods "kube-apiserver-test2-highr" was not valid:
# * spec: Forbidden: pod updates may not change fields other than `containers[*].image` or `spec.activeDeadlineSeconds`

ดังนั้นฉันจึงลองใช้วิธีอื่น ฉันแก้ไขไฟล์การปรับใช้สำหรับ kube-apiserver ด้วยการเปลี่ยนแปลงเดียวกันกับที่อธิบายไว้ข้างต้น และรันสิ่งต่อไปนี้:

$ kubectl apply -f /etc/kubernetes/manifests/kube-apiserver.json --namespace=kube-system

และได้รับคำตอบดังนี้

The connection to the server localhost:8080 was refused - did you specify the right host or port?

ตอนนี้ฉันติดอยู่ ฉันจะเปลี่ยนช่วงของพอร์ตที่ถูกต้องได้อย่างไร


person Avi Farada    schedule 24.02.2017    source แหล่งที่มา


คำตอบ (3)


คุณกำลังระบุ --service-node-port-range=8880-8880 ผิด คุณตั้งค่าเป็นพอร์ตเดียวเท่านั้น ตั้งค่าเป็นช่วง

ปัญหาที่สอง: คุณกำลังตั้งค่าบริการให้ใช้ 9090 และไม่อยู่ในช่วง

 ports:
  - port: 80
    targetPort: 9090
    nodePort: 9090

เซิร์ฟเวอร์ API ควรมีการปรับใช้ด้วยเช่นกัน ลองแก้ไขช่วงพอร์ตในการปรับใช้เองและลบพ็อดเซิร์ฟเวอร์ api เพื่อให้ถูกสร้างขึ้นใหม่ผ่านการกำหนดค่าใหม่

person Farhad Farahi    schedule 24.02.2017
comment
ขอบคุณสำหรับความคิดเห็น ฉันลองเปลี่ยนช่วงเป็น --service-node-port-range=8880-8881 แล้วผลลัพธ์ก็เหมือนเดิม สิ่งที่พอร์ตเป็นเพียงการคัดลอกและวางที่ไม่ดี ฉันแก้ไขโพสต์ของฉัน - person Avi Farada; 24.02.2017
comment
โปรด kubectl delete -f และสร้างใหม่โดยใช้ไฟล์กำหนดค่าใหม่ - person Farhad Farahi; 24.02.2017
comment
ฉันพยายามเรียกใช้ kubectl delete -f /etc/kubernetes/manifests/kube-apiserver.json ตามที่คุณแนะนำ แต่ได้รับข้อผิดพลาดนี้ The connection to the server localhost:8080 was refused - did you specify the right host or port? - person Avi Farada; 25.02.2017

ช่วงพอร์ตโหนดบริการถูกตั้งค่าเป็นพอร์ตที่ใช้ไม่บ่อยด้วยเหตุผล ทำไมคุณถึงต้องการเผยแพร่สิ่งนี้ในทุกโหนด? คุณต้องการสิ่งนั้นจริงๆเหรอ?

อีกทางเลือกหนึ่งคือการเปิดเผยบนโหนดพอร์ตแบบกึ่งสุ่ม จากนั้นใช้พ็อดพร็อกซีบนโหนดที่รู้จักหรือชุดของโหนดเพื่อเข้าถึงผ่านโฮสต์พอร์ต

person Tim Hockin    schedule 24.02.2017
comment
ฉันเข้าใจเหตุผลเบื้องหลังช่วงพอร์ตเริ่มต้นและเห็นด้วยกับพวกเขา ตัวเลือกพอร์ตของฉันเป็นไปตามข้อจำกัดบนโหนดที่ฉันกำลังทดลองอยู่ ฉันไม่เข้าใจแนวทางอื่นของคุณอย่างสมบูรณ์ในเรื่องนี้ ดูเหมือนว่าคุณกำลังแนะนำให้เปิดเผยบริการคลัสเตอร์ของฉันผ่านพร็อกซีที่จัดการบนโหนดภายนอก kubernetes หรือเป็นพ็อดภายใน kubernetes ที่แมปกับพอร์ตโฮสต์ ฉันคาดหวังบริการ จะเป็นวิธีที่ถูกต้องในการเปิดเผยพ็อดและตัวเลือก NodePort เพื่อรองรับการส่งต่อพอร์ตกับโฮสต์ หากไม่มี LoadBalancer - person Avi Farada; 25.02.2017
comment
หากคุณมีโหนดเดียวและกำลังทดลอง คุณสามารถใช้พ็อดโฮสต์พอร์ตได้โดยไม่ต้องใช้บริการ มันง่ายกว่ามาก ถ้าอยากทำจริงก็ควรพูดถึงสถานการณ์ให้แน่ชัดก่อนจะได้เข้าใจมากขึ้น ข้อเสนอทางเลือกของฉันเป็นข้อเสนอระดับสาธิตจริงๆ ไม่ใช่ผลิตภัณฑ์ ฉันรู้ว่าข้อจำกัดของ NodePort นั้นน่าอึดอัดใจในบางสถานการณ์ และฉันก็เปิดรับการปรับแต่งอย่างมาก แต่ฉันต้องเข้าใจกรณีการใช้งานด้วย - person Tim Hockin; 26.02.2017

ปัญหานี้:

The connection to the server localhost:8080 was refused - did you specify the right host or port?

เกิดจากช่วงพอร์ตของฉันยกเว้น 8080 ซึ่ง kube-apiserver ให้บริการอยู่ ดังนั้นฉันจึงไม่สามารถส่งการอัปเดตใดๆ ไปยัง kubectl ได้

ฉันแก้ไขโดยเปลี่ยนช่วงพอร์ตเป็น 8080-8881 และรีสตาร์ทบริการ kubelet ดังนี้:

$ service kubelet restart

ทุกอย่างทำงานได้ตามที่คาดไว้ตอนนี้

person Avi Farada    schedule 25.02.2017