背景:
因业务上线需要,研发中心要求在kubernetes测试集群部署一个sql server 2017的数据库,用于业务功能调试。
一、实施部署sql server数据库:
1、拉取sql server 2017的镜像:
[root@harbor-02 ~]# docker pull mcr.microsoft.com/mssql/server:2017-latest
2017-latest: Pulling from mssql/server
699451820735: Pull complete
861e6e6e8e5e: Pull complete
4403d783b046: Pull complete
Digest: sha256:3b913841850a4d57fcfcb798be06acc88ea0f2acc5418bc0c140a43e91c4a545
Status: Downloaded newer image for mcr.microsoft.com/mssql/server:2017-latest
2、上传sql server 2017镜像到私库:
先tag处理一下。
[root@harbor-02 ~]# docker tag mcr.microsoft.com/mssql/server:2017-latest dockerhub.jiang.com/jiang-public/sqlserver:2017-latest
再上传到私有harbor库,
[root@harbor-02 ~]# docker push dockerhub.jiang.com/jiang-public/sqlserver:2017-latest
The push refers to repository [dockerhub.jiang.com/jiang-public/sqlserver]
fdfec26b634f: Pushed
ef3ecb9d5e46: Pushed
548a79621a42: Pushed
2017-latest: digest: sha256:ff8a63a2c900cf3201b948526dff804db58fbeaed83287c94edd3b3359708854 size: 954
3、创建PersistentVolume、PersistentVolumeClaim存储:
因kubernetes集群环境是对接了hpe(华三)的iscsi存储,所以直接通过stoageclass创建pvc存储即可。不过这里会展示pv、pvc存储的yml配置内容。
pvc存储yml配置:
apiVersion: storage.k8s.io/v1
kind: PersistentVolumeClaim
metadata:
name: sqlserver-db-pvc
namespace: main-data-test
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: hpe-san
volumeMode: Filesystem
pv存储的yml配置:
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: csi.hpe.com
finalizers:
- kubernetes.io/pv-protection
- external-attacher/csi-hpe-com
name: pvc-3361b2d3-7c05-4e38-8533-6a14a9a891f1
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: sqlserver-db-pvc
namespace: main-data-test
csi:
controllerExpandSecretRef:
name: hpe-backend
namespace: hpe-storage
controllerPublishSecretRef:
name: hpe-backend
namespace: hpe-storage
driver: csi.hpe.com
fsType: xfs
nodePublishSecretRef:
name: hpe-backend
namespace: hpe-storage
nodeStageSecretRef:
name: hpe-backend
namespace: hpe-storage
volumeAttributes:
accessProtocol: iscsi
allowOverrides: cpg,provisioningType
compression: "false"
cpg: SSD_r6
description: Volume provisioned by the HPE CSI Driver
fsType: xfs
hostEncryption: "false"
hostSeesVLUN: "true"
provisioningType: tpvv
snapCpg: SSD_r6
storage.kubernetes.io/csiProvisionerIdentity: 1723080244247-8081-csi.hpe.com
volumeAccessMode: mount
volumeHandle: pvc-3361b2d3-7c05-4e38-8533-6a14a9a891f1
persistentVolumeReclaimPolicy: Delete
storageClassName: hpe-san
volumeMode: Filesystem
4、创建无状态的sql server数据库:
这里的无状态就是deployment控制器的配置。
kind: Deployment
metadata:
name: sqlserver-db-auuqd
namespace: main-data-test
generation: 1
labels:
app: sqlserver-db-auuqd
name: sqlserver-db
version: '2017'
spec:
replicas: 1
selector:
matchLabels:
app: sqlserver-db-auuqd
name: sqlserver-db
template:
metadata:
labels:
app: sqlserver-db-auuqd
name: sqlserver-db
version: '2017'
spec:
volumes:
- name: sqlserver-db-pvc
persistentVolumeClaim:
claimName: sqlserver-db-pvc
containers:
- name: sqlserver
image: 'dockerhub.jiang.com/jiang-public/sqlserver:2017-latest'
ports:
- containerPort: 1433
protocol: TCP
env:
- name: ACCEPT_EULA
value: 'Y'
- name: SA_PASSWORD
value: mdm@1234
resources:
limits:
cpu: '2'
memory: 4Gi
requests:
cpu: '1'
memory: 1Gi
volumeMounts:
- name: sqlserver-db-pvc
mountPath: /var/opt/mssql
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
securityContext:
privileged: false
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
serviceAccountName: default
serviceAccount: default
securityContext: {}
strategy:
type: Recreate
minReadySeconds: 10
revisionHistoryLimit: 10
progressDeadlineSeconds: 600
注:这里需要修改的内容主要如下:
1、volumes存储的声明:
2、volumeMounts:存储的挂载
3、env:环境变量的声明
- name: ACCEPT_EULA(表示接受最终用户协议)
value: 'Y'
- name: SA_PASSWORD(sa用户的密码)
value: mdm@12344、ports:端口的声明
这里默认的是1433
5、创建servcie控制器配置,对外提供访问:
sql server数据库需要对外提供服务,需要配置service控制器。
kind: Service
metadata:
name: sqlserver-db
namespace: main-data-test
labels:
name: sqlserver-db
system/appName: master-data-base
spec:
ports:
- name: tcp-port-0
protocol: TCP
port: 1433
targetPort: 1433
selector:
name: sqlserver-db
clusterIP: 172.32.60.172
type: ClusterIP
sessionAffinity: None
二、使用Navicat Premium连接:
可以直接通过 Navicat Premium
来连接sql server数据库。
登陆之后可以创建一个数据库,进行测试一下。
注:到此,在kubernetes集群上部署sql server数据库就完成了。希望可以帮助到大家。