Kubernetes 安装 MetaLB 基于 service LoadBalancer 功能 实现wordpress
综合案例:wordpress
[root@master1 metallb]# kubectl get node NAME STATUS ROLES AGE VERSION master1.org Ready control-plane 12d v1.35.4 node1.org Ready <none> 12d v1.35.4 node2.org Ready <none> 12d v1.35.4 node3.org Ready <none> 12d v1.35.4
#下载指定版本
METALLB_VERSION='v0.15.3'
wget https://raw.githubusercontent.com/metallb/metallb/${METALLB_VERSION}/config/manifests/metallb-native.yaml
kubectl apply -f metallb-native.yaml
#指定IP地址池
[root@master1 metallb]# cat service-metallb-IPAddressPool.yaml apiVersion: metallb.io/v1beta1 kind: IPAddressPool metadata: name: localip-pool namespace: metallb-system spec: addresses: - 192.168.3.10-192.168.3.50 autoAssign: true avoidBuggyIPs: true
[root@master1 metallb]# cat service-metallb-L2Advertisement.yaml apiVersion: metallb.io/v1beta1 kind: L2Advertisement metadata: name: localip-pool-l2a namespace: metallb-system spec: ipAddressPools: - localip-pool interfaces: - ens33
kubectl apply -f service-metallb-IPAddressPool.yaml
kubectl apply -f service-metallb-L2Advertisement.yaml
kubectl get all -n metallb-system
[root@master1 metallb]# kubectl get all -n metallb-system NAME READY STATUS RESTARTS AGE pod/controller-7f475568c5-r6b4j 1/1 Running 0 71s pod/speaker-5d76c 1/1 Running 0 71s pod/speaker-lm4r7 1/1 Running 0 71s pod/speaker-qgp89 1/1 Running 0 71s pod/speaker-rj2b7 1/1 Running 0 71s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/metallb-webhook-service ClusterIP 10.105.251.51 <none> 443/TCP 71s NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE daemonset.apps/speaker 4 4 4 4 4 kubernetes.io/os=linux 71s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/controller 1/1 1 1 71s NAME DESIRED CURRENT READY AGE replicaset.apps/controller-7f475568c5 1 1 1 71s
kubectl get IPAddressPool -n metallb-system
[root@master1 metallb]# kubectl get IPAddressPool -n metallb-system NAME AUTO ASSIGN AVOID BUGGY IPS ADDRESSES localip-pool true true ["192.168.3.10-192.168.3.50"]
[root@master1 workload]# cat wordpress-mysql-svc-deployment.yaml apiVersion: v1 kind: Namespace metadata: name: demo --- apiVersion: apps/v1 kind: Deployment metadata: name: mysql namespace: demo spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - image: registry.cn-beijing.aliyuncs.com/wangxiaochun/mysql:8.0.29-oracle name: mysql env: - name: MYSQL_ROOT_PASSWORD value: "123456" - name: MYSQL_DATABASE value: "wordpress" - name: MYSQL_USER value: wordpress - name: MYSQL_PASSWORD value: "123456" --- apiVersion: v1 kind: Service metadata: name: mysql namespace: demo spec: ports: - port: 3306 protocol: TCP targetPort: 3306 selector: app: mysql --- apiVersion: apps/v1 kind: Deployment metadata: name: wordpress namespace: demo spec: replicas: 1 selector: matchLabels: app: wordpress template: metadata: labels: app: wordpress spec: containers: - image: registry.cn-beijing.aliyuncs.com/wangxiaochun/wordpress:php8.2-apache name: wordpress env: - name: WORDPRESS_DB_HOST #value: mysql.demo.svc.cluster.local. value: mysql - name: WORDPRESS_DB_USER value: wordpress - name: WORDPRESS_DB_PASSWORD value: "123456" - name: WORDPRESS_DB_NAME value: wordpress --- apiVersion: v1 kind: Service metadata: name: wordpress namespace: demo spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: wordpress #type: NodePort type: LoadBalancer #有前提,需要部署LoadBalancer軟件
kubectl apply -f wordpress-mysql-svc-deployment.yaml
[root@master1 workload]# kubectl get pod -n demo NAME READY STATUS RESTARTS AGE mysql-6d544df847-td9pw 1/1 Running 0 86s wordpress-bdcb8c7fb-x6nzf 1/1 Running 0 86s [root@master1 workload]# [root@master1 workload]# kubectl get deploy -n demo NAME READY UP-TO-DATE AVAILABLE AGE mysql 1/1 1 1 98s wordpress 1/1 1 1 98s
[root@master1 workload]# kubectl get svc -n demo NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP 10.111.141.148 <none> 3306/TCP 2m4s wordpress LoadBalancer 10.102.238.141 192.168.3.11 80:30644/TCP 2m4s
[root@master1 workload]# kubectl get ep -n demo Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice NAME ENDPOINTS AGE mysql 10.244.2.160:3306 2m27s wordpress 10.244.3.55:80 2m26s
进入 MySQL Pod 内部直接访问
进入 MySQL 容器
容器内用 localhost 连接
kubectl exec -it -n demo mysql-6d544df847-td9pw -- sh
[root@master1 workload]# kubectl exec -it -n demo mysql-6d544df847-td9pw -- sh sh-4.4# sh-4.4# sh-4.4# mysql -u wordpress -p123456 wordpress mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 8.0.29 MySQL Community Server - GPL Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
测试mysql
集群内 Pod 通过 Service DNS 访问
MySQL 的 Service 名为 mysql,在 demo 命名空间
完整service名称
mysql.demo.svc.cluster.local
弄个临时容器有mysql客户端命令的
kubectl run mysql-client --rm -it --image=registry.cn-beijing.aliyuncs.com/wangxiaochun/mysql:8.0.29-oracle -- bash
在容器内连接 使用service名称
mysql -h mysql.demo.svc.cluster.local -u wordpress -p123456 wordpress
[root@master1 workload]# kubectl run mysql-client --rm -it --image=registry.cn-beijing.aliyuncs.com/wangxiaochun/mysql:8.0.29-oracle -- bash All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt. If you don't see a command prompt, try pressing enter. bash-4.4# bash-4.4# mysql -h mysql.demo.svc.cluster.local -u wordpress -p123456 wordpress mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 16 Server version: 8.0.29 MySQL Community Server - GPL Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> exit
kubectl run dns-test --rm -it --image nicolaka/netshoot -- bash
root@master1 workload]# kubectl run dns-test --rm -it --image nicolaka/netshoot -- bash All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt. If you don't see a command prompt, try pressing enter. dns-test:~# host mysql.demo.svc.cluster.local. mysql.demo.svc.cluster.local has address 10.111.141.148
dns-test:~# nslookup mysql.demo.svc.cluster.local ;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 ;; Got recursion not available from 10.96.0.10 Server: 10.96.0.10 Address: 10.96.0.10#53 Name: mysql.demo.svc.cluster.local Address: 10.111.141.148 ;; Got recursion not available from 10.96.0.10 dns-test:~#
dns-test:~# dig mysql.demo.svc.cluster.local ; <<>> DiG 9.20.17 <<>> mysql.demo.svc.cluster.local ;; global options: +cmd ;; Got answer: ;; WARNING: .local is reserved for Multicast DNS ;; You are currently testing what happens when an mDNS query is leaked to DNS ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32657 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 1232 ; COOKIE: e69ab84f8f7aebe8 (echoed) ;; QUESTION SECTION: ;mysql.demo.svc.cluster.local. IN A ;; ANSWER SECTION: mysql.demo.svc.cluster.local. 30 IN A 10.111.141.148 ;; Query time: 4 msec ;; SERVER: 10.96.0.10#53(10.96.0.10) (UDP) ;; WHEN: Fri May 22 09:25:38 UTC 2026 ;; MSG SIZE rcvd: 113
root@master1 workload]# kubectl get svc -n demo NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql ClusterIP 10.111.141.148 <none> 3306/TCP 43m wordpress LoadBalancer 10.102.238.141 192.168.3.11 80:30644/TCP 43m
从集群外访问
http://192.168.3.11/


浙公网安备 33010602011771号