--- tags: Kubernetes, metallb description: metallb - LoadBalancer安裝與web樣本. robots: index, follow --- <style> html, body, .ui-content { background-color: #333; color: #ddd; } .markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6 { color: #ddd; } .markdown-body h1, .markdown-body h2 { border-bottom-color: #ffffff69; } .markdown-body h1 .octicon-link, .markdown-body h2 .octicon-link, .markdown-body h3 .octicon-link, .markdown-body h4 .octicon-link, .markdown-body h5 .octicon-link, .markdown-body h6 .octicon-link { color: #fff; } .markdown-body img { background-color: transparent; } .ui-toc-dropdown .nav>.active:focus>a, .ui-toc-dropdown .nav>.active:hover>a, .ui-toc-dropdown .nav>.active>a { color: white; border-left: 2px solid white; } .expand-toggle:hover, .expand-toggle:focus, .back-to-top:hover, .back-to-top:focus, .go-to-bottom:hover, .go-to-bottom:focus { color: white; } .ui-toc-dropdown { background-color: #333; } .ui-toc-label.btn { background-color: #191919; color: white; } .ui-toc-dropdown .nav>li>a:focus, .ui-toc-dropdown .nav>li>a:hover { color: white; border-left: 1px solid white; } .markdown-body blockquote { color: #bcbcbc; } .markdown-body table tr { background-color: #5f5f5f; } .markdown-body table tr:nth-child(2n) { background-color: #4f4f4f; } .markdown-body code, .markdown-body tt { color: #eee; background-color: rgba(230, 230, 230, 0.36); } a, .open-files-container li.selected a { color: #5EB7E0; } </style> # metallb - LoadBalancer ## install 建立namespace。 ```shell= inwin@master:~/metallb$ kubectl create -f ns.yaml namespace/metallb-system created ``` 建立metallb相關policy, RBAC, pod...等。 [metallb定義檔下載](https://raw.githubusercontent.com/google/metallb/v0.9.3/manifests/metallb.yaml "metallb") ```shell= inwin@master:~/metallb$ kubectl create -f metallb.yaml podsecuritypolicy.policy/controller created podsecuritypolicy.policy/speaker created serviceaccount/controller created serviceaccount/speaker created clusterrole.rbac.authorization.k8s.io/metallb-system:controller created clusterrole.rbac.authorization.k8s.io/metallb-system:speaker created role.rbac.authorization.k8s.io/config-watcher created role.rbac.authorization.k8s.io/pod-lister created clusterrolebinding.rbac.authorization.k8s.io/metallb-system:controller created clusterrolebinding.rbac.authorization.k8s.io/metallb-system:speaker created rolebinding.rbac.authorization.k8s.io/config-watcher created rolebinding.rbac.authorization.k8s.io/pod-lister created daemonset.apps/speaker created deployment.apps/controller created ``` ## Web應用 建立pool ```yaml= apiVersion: v1 kind: ConfigMap metadata: namespace: metallb-system name: config data: config: | address-pools: - name: my-ip-space avoid-buggy-ips: true protocol: layer2 addresses: - 10.12.1.190/30 ``` ```shell= inwin@master:~/metallb$ kubectl create -f pool.yaml configmap/config created ``` 建立web deployment,設定副本三份與service port為80,並且指定LB IP。 ```yaml= apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: web name: web spec: replicas: 3 selector: matchLabels: app: web strategy: {} template: metadata: creationTimestamp: null labels: app: web spec: containers: - image: yansheng133/hpa-php5:0.5 name: hpa-php5 ports: - containerPort: 80 resources: {} status: {} --- apiVersion: v1 kind: Service metadata: labels: app: web name: web spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: web type: LoadBalancer loadBalancerIP: 10.12.1.190 ``` 建立web相關資源。 ```shell= inwin@master:~/metallb$ kubectl create -f web.yaml deployment.apps/web created service/web created inwin@master:~/metallb$ kubectl get po NAME READY STATUS RESTARTS AGE web-659bcfd49c-55d4k 1/1 Running 0 2m14s web-659bcfd49c-c885b 1/1 Running 0 2m14s web-659bcfd49c-snb75 1/1 Running 0 2m14s inwin@master:~/metallb$ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE efkweb NodePort 10.43.232.132 <none> 80:32100/TCP 25h kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 7d1h web LoadBalancer 10.43.72.197 10.12.1.190 80:30209/TCP 94s inwin@master:~/metallb$ curl 10.12.1.190 Source IP:10.42.77.64, Response by: web-659bcfd49c-snb75 -- 2021/08/18 17:32:06 ```