使用Minikube:部署了SQL Server 2022,但在主机上使用ssms无法访问它

问题描述 投票:0回答:1

我的主机上运行着一个 Minikube。

由于某种原因,在应用 yaml 脚本后,我无法从主机连接到容器化 SQL Server。

这就是我所拥有的:

apiVersion: v1
kind: Secret
metadata:
  name: mssql-secret
  namespace: new-kubernetes
type: Opaque
stringData:
  MSSQL_SA_PASSWORD: Qwerty123!
---
apiVersion: v1
kind: Service
metadata:
  name: mssql-service
  namespace: new-kubernetes
spec:
  selector:
    app: mssql
  ports:
    - protocol: TCP
      port: 30001
      targetPort: 1433
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mssql-pv
  namespace: new-kubernetes
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data/mssql"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mssql-pvc
  namespace: new-kubernetes
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mssql-deployment
  namespace: new-kubernetes
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mssql
  template:
    metadata:
      labels:
        app: mssql
    spec:
      containers:
      - name: mssql
        image: mcr.microsoft.com/mssql/server:2022-latest  # MSSQL Server 2022
        ports:
        - containerPort: 1433
        env:
        - name: ACCEPT_EULA
          value: "Y"
        - name: MSSQL_SA_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mssql-secret
              key: MSSQL_SA_PASSWORD
        volumeMounts:
        - name: mssql-data
          mountPath: /var/opt/mssql
      volumes:
      - name: mssql-data
        persistentVolumeClaim:
          claimName: mssql-pvc

kubectl get all -n new-kubernetes

NAME                                     READY   STATUS    RESTARTS   AGE
pod/mssql-deployment-5d8d4b4bc7-v7szb    1/1     Running   0          28s

NAME                        TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE
service/mssql-service       LoadBalancer   10.108.132.45   127.0.0.1     30001:31453/TCP   28s

NAME                                READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/mssql-deployment    1/1     1            1           28s

NAME                                           DESIRED   CURRENT   READY   AGE
replicaset.apps/mssql-deployment-5d8d4b4bc7    1         1         1       28s

Minikube 隧道:

W0903 20:18:09.098870   73348 main.go:291] Unable to resolve the current Docker CLI context "default": context "default": context not found: open C:\Users\XXXXXXX\.docker\contexts\meta\XXXXXXX\meta.json: The system cannot find the path specified.
* Tunnel successfully started

* NOTE: Please do not close this terminal as this process must stay alive for the tunnel to be accessible ...

* Starting tunnel for service mssql-service.

kubectl describe pod mssql-deployment-5d8d4b4bc7-v7szb -n new-kubernetes

....
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  4m8s  default-scheduler  Successfully assigned new-kubernetes/mssql-deployment-5d8d4b4bc7-v7szb to minikube
  Normal  Pulled     4m8s  kubelet            Container image "mcr.microsoft.com/mssql/server:2022-latest" already present on machine
  Normal  Created    4m8s  kubelet            Created container mssql
  Normal  Started    4m8s  kubelet            Started container mssql

SSMS

Server name: localhost,30001
Authentication: SQL Server authentication
Login: sa
Password: Qwerty123!
Encryption: Mandatory
[V] Trust server certificate

SSMS 错误消息:

无法连接到本地主机,30001。

附加信息:
建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。
找不到或无法访问该服务器。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:TCP 提供程序,错误:0 - 远程计算机拒绝网络连接。)(Microsoft SQL Server,错误:1225)

我似乎无法弄清楚我是否有配置错误或为什么这不起作用。也许有人可以帮助解决为什么我无法使用 SQL Server Management Studio 连接到我的容器化 SQL Server?

sql-server kubernetes ssms minikube
1个回答
0
投票

卷配置不匹配,这是我当前使用的解决方案:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mssql-deployment
  namespace: new-kubernetes
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mssql
  template:
    metadata:
      labels:
        app: mssql
    spec:
      terminationGracePeriodSeconds: 30
      hostname: mssqlinst
      securityContext:
        fsGroup: 10001
      containers:
      - name: mssql
        image: mcr.microsoft.com/mssql/server:2022-latest
        ports:
        - containerPort: 1433
        env:
        - name: MSSQL_PID
          value: "Developer"
        - name: ACCEPT_EULA
          value: "Y"
        - name: MSSQL_SA_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mssql-secret
              key: MSSQL_SA_PASSWORD
        volumeMounts:
        - name: mssqldb
          mountPath: /var/opt/mssql
      volumes:
      - name: mssqldb
        persistentVolumeClaim:
          claimName: mssql-data
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mssql-data
  namespace: new-kubernetes
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mssql-data
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: /data/mssql
---
apiVersion: v1
kind: Service
metadata:
  name: mssql-service
  namespace: new-kubernetes
spec:
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 1433
      targetPort: 1433
  selector:
    app: mssql
---
apiVersion: v1
kind: Secret
metadata:
    name: mssql-secret
    namespace: new-kubernetes
type: Opaque
stringData:
    MSSQL_SA_PASSWORD: Qwerty123!

为什么yaml文件不正确?

由于缺少

mssql-pv

 字段或名称不同,
持久卷未链接到持久卷声明(
mssql-pvc
volumeName
)。因此,将问题中的 yaml 中的
mssql-pvc
重命名为
mssql-pv
也足够了。

更详细的解释

在下面的解释中我只会列出重要字段作为答案,以便更清楚地说明发生了什么以及为什么持久卷声明找不到它的持久卷。

首先声明卷挂载:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      volumeMounts:
      - name: mssqldb

然后将卷安装链接到持久卷声明:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      volumes:
      - name: mssqldb
        persistentVolumeClaim:
          claimName: mssql-data

注意:

mssqldb
现在引用volumeMount。

注意:

mssqldb
现在请求名为
mssql-data
的持久卷声明。

然后声明一个名为“mssql-data”的持久卷声明:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mssql-data

最后声明一个名为“mssql-data”的持久卷:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mssql-data

注意:

mssqldb
现在引用持久卷声明(同名)。

如果您想为持久卷使用不同的名称怎么办?

在持久卷声明中进行以下更改:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mssql-data
spec:
  volumeName: mssql-data-new-name

最后为持久卷指定所需的名称:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mssql-data-new-name

注意:

mssql-data-new-name
现在引用持久卷声明(在持久卷声明中定义)。

备注:

请注意,Kubernetes/Docker 没有记录任何表明这一点的内容。 因此,我的建议是要么始终在持久卷声明中明确命名您的持久卷,要么始终保持名称与上面片段中所示的名称相同。

© www.soinside.com 2019 - 2024. All rights reserved.