Bjo*_*son 6 mysql mariadb docker kubernetes docker-compose
如果我在 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.
Run Code Online (Sandbox Code Playgroud)
但是,如果 MariaDB 从 Kubernetes 集群中的官方映像部署(尝试了 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.
Run Code Online (Sandbox Code Playgroud)
我试图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 = *
Run Code Online (Sandbox Code Playgroud)
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: {}
Run Code Online (Sandbox Code Playgroud)
和相应的持久卷声明:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
io.kompose.service: dbdata
name: dbdata
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
Run Code Online (Sandbox Code Playgroud)
令我感到困惑的是,相同的配置适用于 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: {}
Run Code Online (Sandbox Code Playgroud)
......我既包括app与name对spec.selector-我习惯了只具有name但是@瓦利德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
Run Code Online (Sandbox Code Playgroud)
$ 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>
Run Code Online (Sandbox Code Playgroud)
解决方案
比较 KoopaKiller 发布的服务声明,它被证明是有效的,我终于注意到protocol在端口声明中将属性设置为“TCP”是缺失的——这部分:
spec:
ports:
- protocol: TCP
...
Run Code Online (Sandbox Code Playgroud)
由于您使用的是 Kubernetes 部署,因此您的 pod 名称将根据您在规范文件中提供的名称动态生成,在您的示例中,pod 将以名称创建db-xxxxxxxxxx-xxxxx。
为了创建“固定”主机名,您需要创建一个服务来访问您的 Pod,例如:
apiVersion: v1
kind: Service
metadata:
name: db
spec:
selector:
name: db
ports:
- protocol: TCP
port: 3306
targetPort: 3306
type: ClusterIP
Run Code Online (Sandbox Code Playgroud)
并检查是否已成功部署:
$ kubectl get svc db
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.96.218.18 <none> 3306/TCP 89s
Run Code Online (Sandbox Code Playgroud)
您的服务的全名将是:<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
Run Code Online (Sandbox Code Playgroud)
之后您将能够通过 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>
Run Code Online (Sandbox Code Playgroud)
您知道,您还可以使用 HELM 模板来设置具有复制功能的 mariadb。看这篇文章
参考:
https://kubernetes.io/docs/concepts/services-networking/service/
https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
| 归档时间: |
|
| 查看次数: |
3524 次 |
| 最近记录: |