Skip to content

antony@notes:~/kubernetes$ cat "Kubernetes-公共建設.md"

Kubernetes 公共建設

2022-09-15· kubernetes ·系統工程

Kubernetes 公共建設

[TOC]

學習目標

  • MetalLB
  • Nginx ingress
  • Metrics server
  • Rook ceph

Why MetalLB

Kubernetes does not offer an implementation of network load balancers (Services of type LoadBalancer) for bare-metal clusters. The implementations of network load balancers that Kubernetes does ship with are all glue code that calls out to various IaaS platforms (GCP, AWS, Azure…).

MetalLB aims to redress this imbalance by offering a network load balancer implementation that integrates with standard network equipment, so that external services on bare-metal clusters also “just work” as much as possible.

公有雲的環境

MetalLB 的環境

MetalLB 安裝

$ get -qO - https://raw.githubusercontent.com/metallb/metallb/v0.13.4/config/manifests/metallb-native.yaml | kubectl apply -f -

$ echo '
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: mlb1
  namespace: metallb-system
spec:
  addresses:
    - 192.168.61.220-192.168.61.230
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
  name: mlb1
  namespace: metallb-system' | kubectl apply -f -

$ kubectl get pod -n metallb-system -o wide
NAME                          READY   STATUS    RESTARTS   AGE   IP             NODE   NOMINATED NODE   READINESS GATES
controller-7476b58756-r5tn4   1/1     Running   0          13m   10.244.1.31    w1     <none>           <none>
speaker-bnzj7                 1/1     Running   0          41s   192.168.61.4   m1     <none>           <none>
speaker-hrcsp                 1/1     Running   0          13m   192.168.61.6   w1     <none>           <none>
speaker-mfwm5                 1/1     Running   0          13m   192.168.61.7   w2     <none>           <none>

MetalLB 測試

$ echo '
apiVersion: apps/v1
kind: Deployment
metadata:
  name: s1.dep
spec:
  replicas: 2
  selector:
    matchLabels:
      app: s1.dep
  template:
    metadata:
      labels:
        app: s1.dep
    spec:
      containers:
      - name: app
        image: quay.io/flysangel/image:app.golang' | kubectl apply -f -

$ echo '
apiVersion: v1
kind: Service
metadata:
  name: s1
  annotations:
    metallb.universe.tf/address-pool: mlb1
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: s1.dep
  type: LoadBalancer' | kubectl apply -f -

$ kubectl get pod
NAME                      READY   STATUS    RESTARTS   AGE
s1.dep-55f7bf48df-9znvv   1/1     Running   0          4m3s
s1.dep-55f7bf48df-ghstg   1/1     Running   0          4m3s

bigred@m1:~/0608$ kubectl get service
NAME         TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)        AGE
kubernetes   ClusterIP      10.98.0.1     <none>           443/TCP        8d
s1           LoadBalancer   10.98.0.253   192.168.61.220   80:32544/TCP   13s

$ curl -w "\n" http://192.168.61.220
{"message":"Hello Golang"}

$ curl -w "\n" http://192.168.61.220/hostname
{"message":"s1.dep-55f7bf48df-zcbcp"}

$ curl -w "\n" http://192.168.61.220/hostname
{"message":"s1.dep-55f7bf48df-b588s"}

MetalLB 驗證

$ sudo arping -c 4 192.168.61.220
ARPING 192.168.61.220 from 192.168.61.4 eth0
Unicast reply from 192.168.61.220 [00:50:56:AB:00:06]  1.083ms
Unicast reply from 192.168.61.220 [00:50:56:AB:00:06]  1.869ms
Unicast reply from 192.168.61.220 [00:50:56:AB:00:06]  0.912ms
Unicast reply from 192.168.61.220 [00:50:56:AB:00:06]  0.997ms
Sent 4 probes (1 broadcast(s))
Received 4 response(s)

請檢查 00:50:56:AB:00:07 此 MAC 為哪台主機? 將該台主機關機,五分鐘後重新測試命令,請問 MAC 換至哪台主機? 為甚麼是五分鐘後測試?


Ingress controller

Ingress Nginx 安裝

在windows 更改 /etc/hosts
需用管理員權限執行 CMD
$ notepad C:\Windows\System32\drivers\etc\hosts
192.168.61.220 test.k8s.org quay.k8s.org gf.k8s.org pgf.k8s.org jenkins.k8s.org


$ wget -qO - https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml | sed 's|name: nginx|name: ig1|g' | kubectl apply -f -

$ kubectl get service -n ingress-nginx
NAME                                 TYPE           CLUSTER-IP   EXTERNAL-IP      PORT(S)                      AGE
ingress-nginx-controller             LoadBalancer   10.98.0.47   192.168.61.220   80:31727/TCP,443:30789/TCP   23s
ingress-nginx-controller-admission   ClusterIP      10.98.0.17   <none>           443/TCP                      23s

$ kubectl get ingressclass
NAME   CONTROLLER             PARAMETERS   AGE
ig1    k8s.io/ingress-nginx   <none>       28s
tags: 系統工程