[英]Can't connect to MariaDB by hostname within a Kubernetes cluster
如果我在 Docker Compose 配置中從官方鏡像中設置 MariaDB,我可以通過它的主機名訪問它 - 例如,如果在 MariaDB 容器內的 bash shell 中:
# host db
db has address 172.21.0.2
# curl telnet://db:3306
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
但是,如果從 Kubernetes 集群中的官方鏡像部署了 MariaDB(嘗試了 MicroK8s 和 GKE),我可以通過localhost
連接到它,但不能通過主機名連接到它:
# host db
db.my-namspace.svc.cluster.local has address 10.152.183.124
# curl telnet://db:3306
curl: (7) Failed to connect to db port 3306: Connection refused
# curl telnet://localhost:3306
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
我試圖用簡化版本替換包含的my.cnf
,例如:
[mysqld]
skip-grant-tables
skip-networking=0
#### Unix socket settings (making localhost work)
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
#### TCP Socket settings (making all remote logins work)
port = 3306
bind-address = *
MariaDB Kubernetes 部署如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: db
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
name: db
template:
metadata:
labels:
name: db
spec:
containers:
- env:
- name: MYSQL_PASSWORD
value: template
- name: MYSQL_ROOT_PASSWORD
value: root
- name: MYSQL_USER
value: template
image: mariadb:10.4
name: db
ports:
- containerPort: 3306
resources: {}
volumeMounts:
- mountPath: /var/lib/mysql
name: dbdata
restartPolicy: Always
volumes:
- name: dbdata
persistentVolumeClaim:
claimName: dbdata
status: {}
和相應的持久卷聲明:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
io.kompose.service: dbdata
name: dbdata
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
令我感到困惑的是,相同的配置適用於 Docker Compose,但不適用於 Kubernetes 集群。
任何想法可能會發生什么?
2020 年 3 月 18 日更新我忘記包含數據庫的服務聲明並在此處添加:
apiVersion: v1
kind: Service
metadata:
labels:
app: db
name: db
spec:
ports:
- name: "3306"
port: 3306
targetPort: 3306
selector:
app: db
name: db
type: ClusterIP
status:
loadBalancer: {}
...我包括了spec.selector
app
和name
- 我習慣於只有name
但@Al-waleed Shihadeh 的示例包括app
所以我也會包括它,以防萬一 - 但沒有成功。
以下是幾個 kubectl 列表命令的輸出:
$ sudo microk8s.kubectl get svc db -n my-namespace
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.152.183.246 <none> 3306/TCP 35m
$ sudo microk8s.kubectl get pods -owide -n my-namespace
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
db-77cbcf87b6-l44lm 1/1 Running 0 34m 10.1.48.118 microk8s-vm <none> <none>
解決方案比較了 KoopaKiller 發布的服務聲明,它被證明是有效的,我終於注意到在端口聲明中將protocol
屬性設置為“TCP”是缺失的——這部分:
spec:
ports:
- protocol: TCP
...
由於您使用的是 Kubernetes 部署,因此您的 pod 的名稱將根據您在規范文件中提供的名稱動態生成,在您的示例中,將使用名稱db-xxxxxxxxxx-xxxxx
創建 pod。
為了創建“固定”主機名,您需要創建一個服務來訪問您的 pod,例如:
apiVersion: v1
kind: Service
metadata:
name: db
spec:
selector:
name: db
ports:
- protocol: TCP
port: 3306
targetPort: 3306
type: ClusterIP
並檢查是否已成功部署:
$ kubectl get svc db
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.96.218.18 <none> 3306/TCP 89s
您的服務的全名將是: <name>.<namespace>.cluster.local
在這種情況下,使用default
命名空間將是db.default.cluster.local
指向 ip 10.96.218.18
如上例所示。
要獲得您的服務,您需要使用他的信息配置您的 /etc/hosts:
echo -ne "10.96.218.18\tdb.default.cluster.local db db.default" >> /etc/hosts
之后,您將能夠通過 dns 訪問您的服務:
$ dig +short db
10.96.218.18
$ mysql -h db -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.5.5-10.4.12-MariaDB-1:10.4.12+maria~bionic mariadb.org binary distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
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>
眾所周知,您還可以使用 HELM 模板來設置帶有復制功能的 mariadb。 看這篇文章
參考:
https://kubernetes.io/docs/concepts/services-networking/service/
https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
為了能夠從主機節點訪問服務,您需要在 Kubernetes 中定義一個服務對象
所以完整的 k8s 對象應該看起來像下面的代碼片段 PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
creationTimestamp: null
labels:
io.kompose.service: db-data
name: db-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
服務
apiVersion: v1
kind: Service
metadata:
labels:
app: mysql
name: mysql
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql
type: ClusterIP
部署
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
app: mysql
name: mysql
spec:
replicas: 1
template:
metadata:
creationTimestamp: null
labels:
app: mysql
spec:
containers:
- name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: dummy
- name: MYSQL_DATABASE
value: community_db
resources: {}
volumeMounts:
- mountPath: /var/lib/mysql
name: db-data
image: mysql:5.7
ports:
- containerPort: 3306
volumes:
- name: db-data
persistentVolumeClaim:
claimName: db-data
restartPolicy: Always
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.