在k8s中基于hostPath部署MS SqlServer

在K8s中,基于hostPath部署MS SqlServer ,以下yaml在 k8s版本 v1.17.3中 通过,Pod被删除后,重新部署,SqlServer依然会自动挂载数据库.

PS: 注意pod所在node服务器上的docker是否有挂载目录的权限.

chown -R 10001:0 /var/opt/mssql
chown -R 1000:1000 /var/opt/mssql
# 创建专属命名空间
apiVersion: v1
kind: Namespace
metadata:
 name: datacenter

---
#创建需要的secret
apiVersion: v1
kind: Secret
metadata:
 name: mssqlpassword
 namespace: datacenter
type: Opaque
data:
 #值需要base64    123456@QAZ
 SA_PASSWORD: MTIzNDU2QFFBWg==
---
#创建sqlserver的Pod
apiVersion: apps/v1
kind: Deployment
metadata:
 name: sqlserver1
 namespace: datacenter
spec:
 replicas: 1
 selector:
    matchLabels:
      app: sqlserver1
 template:
  metadata:
   labels:
    app: sqlserver1
  spec:
   containers:
    - name: sqlserver1
      image: mcr.microsoft.com/mssql/server 
      imagePullPolicy: IfNotPresent
      ports:
       - containerPort: 1433
      env:
       - name: ACCEPT_EULA
         value: "Y"
       - name: MSSQL_PID
         value: "Developer"
       - name: MSSQL_SA_PASSWORD
         valueFrom: 
          secretKeyRef:
           name: mssqlpassword 
           key: SA_PASSWORD
      volumeMounts:
       - name: sqlserver-data
         mountPath: /var/opt/mssql
   volumes:
    - name: sqlserver-data
      hostPath:
       path: /var/opt/mssql
---
#创建sqlserver的Service
apiVersion: v1
kind: Service
metadata:
 name: sqlserver1
 namespace: datacenter
spec:
 type: NodePort
 selector:
  app: sqlserver1
 ports:
  - protocol: TCP
    port: 1433
    targetPort: 1433