【发布时间】:2020-06-28 22:29:13
【问题描述】:
如果我在 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
...
【问题讨论】:
标签: mysql docker kubernetes docker-compose mariadb