在k8s中基于PersistenVolume部署MS SqlServer

在K8s中,基于PersistenVolume部署MS SqlServer ,以下yaml在 k8s版本 v1.17.3中 通过

# 创建专属命名空间
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==
---

#创建PersistenVolume
apiVersion: v1
kind: PersistentVolume
metadata:
 name: sqlserver1pv
 namespace: datacenter
spec:
 capacity:
  #存储大小
  storage: 5Gi
 accessModes:
  - ReadWriteOnce
 persistentVolumeReclaimPolicy: Retain
 #采用hostPath,也可以更改成nfs
 hostPath: 
    path: /tmp/mssqlserver

---
#创建PersistentVolumeCalim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: sqlserver1-pvc
 namespace: datacenter
spec:
 resources:
  requests: 
    storage: 1Gi
 accessModes:
  - ReadWriteOnce
 storageClassName: ""
---
#创建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
   volumes:
    - name: sqlserver-data
      persistentVolumeClaim: 
       claimName: sqlserver1-pvc
---
#创建sqlserver的Service
apiVersion: v1
kind: Service
metadata:
 name: sqlserver1
 namespace: datacenter
spec:
 type: NodePort
 selector:
  app: sqlserver1
 ports:
  - protocol: TCP
    port: 1433
    targetPort: 1433