k8s运行mysql服务
k8s mysql
·
k8s部署mysql服务
mysql-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath: #得挂载一个空目录
path: "/Users/joy/mysqlSVC"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
mysql-deploy.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306 #如果这里的port和pod的containerPort不一致,则需要通过targetPort显式指定,否则无法建立映射
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
imagePullPolicy: IfNotPresent
name: mysql
env:
# Use secret in real usage
- name: MYSQL_ROOT_PASSWORD
value: t1234
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
应用yaml
$ kubectl apply -f mysql-pv.yaml
$ kubectl apply -f mysql-deploy.yaml
查看确认是否生效:
$ kubectl get pod,svc,deploy,pv,pvc,rs,ingress
NAME READY STATUS RESTARTS AGE
pod/mysql-54dd7bb48-dxstj 1/1 Running 0 20m
pod/test 1/1 Running 0 16m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 97d
service/mysql ClusterIP 10.96.111.53 <none> 3306/TCP 20m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mysql 1/1 1 1 20m
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/mysql-pv-volume 20Gi RWO Retain Bound default/mysql-pv-claim manual 20m
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/mysql-pv-claim Bound mysql-pv-volume 20Gi RWO manual 20m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mysql-54dd7bb48 1 1 1 20m
验证:
$ kubectl exec -it pod/mysql-54dd7bb48-dxstj -- mysql -u root -p --default-character-set=utf8
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.51 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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>
- 在pod内可以通过环境变量看到相应的service,其环境变量的key和service name保持一致。
- 如果此时service删除了或者变动了service name,pod内不会及时感应到,不是热生效的。需要重启pod才能生效。
通过pod查看service: test.yaml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: test
image: library/busybox
imagePullPolicy: IfNotPresent
command: [ "/bin/sh", "-ce", "tail -f /dev/null" ]
$ kubectl apply -f test.yaml
pod/test created
$ kubectl exec -it pod/test -- /bin/sh
/ # env
......
MYSQL_PORT_3306_TCP_ADDR=10.96.111.53
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_SERVICE_HOST=10.96.111.53
MYSQL_SERVICE_PORT=3306
MYSQL_PORT=tcp://10.96.111.53:3306
MYSQL_PORT_3306_TCP=tcp://10.96.111.53:3306
......
/ #
mysql常用指令
- 查看数据库:
mysql> show databases;
- 查看表
mysql> use db; // 选中数据库
mysql> show tables; // 查看有哪些数据表
mysql> describe 表名; // 查看表结构
- 删除记录
DELETE FROM table_name [WHERE 条件]
更多推荐
已为社区贡献9条内容
所有评论(0)