namespace
mysql-ns.yaml
apiVersion: v1
kind: Namespace
metadata:
labels:
kubernetes.io/metadata.name: wgs-mysql
name: wgs-mysql
创建ns
# kubectl apply -f mysql-ns.yaml
namespace/wgs-mysql created
查看ns
# kubectl get ns wgs-mysql -o yaml
apiVersion: v1
kind: Namespace
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"labels":{"kubernetes.io/metadata.name":"wgs-mysql"},"name":"wgs-mysql"}}
creationTimestamp: "2023-01-03T08:59:29Z"
labels:
kubernetes.io/metadata.name: wgs-mysql
name: wgs-mysql
resourceVersion: "1094074"
uid: 54afc0a0-9070-4946-b74f-a8741479dbd0
spec:
finalizers:
- kubernetes
status:
phase: Active
secret
mysql-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysql-auth
namespace: wgs-mysql
type: Opaque
data:
username: cm9vdA== # echo -n 'root' | base64
password: MTIzNDU2 # echo -n '123456' | base64
创建secret
# kubectl apply -f mysql-secret.yaml
secret/mysql-auth created
查看secret
# kubectl get secret -n wgs-mysql
NAME TYPE DATA AGE
mysql-auth Opaque 2 24s
pv
mysql-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
namespace: wgs-mysql
labels:
type: nfs
spec:
storageClassName: manual
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
nfs:
path: "/data/k8s-data/mysql"
server: 192.168.174.102
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
namespace: wgs-mysql
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
创建pv
# kubectl apply -f mysql-pv.yaml
persistentvolume/mysql-pv-volume created
persistentvolumeclaim/mysql-pv-claim created
查看pv
# kubectl get pv -n wgs-mysql
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv-volume 5Gi RWO Retain Bound wgs-mysql/mysql-pv-claim manual 108s
# kubectl get pvc -n wgs-mysql
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound mysql-pv-volume 5Gi RWO manual 112s
mysql
mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: wgs-mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: wgs-mysql
spec:
#type: NodePort
ports:
- port: 3306
# targetPort: 3306
# nodePort: 33306
selector:
app: mysql
clusterIP: None
创建mysql资源
# kubectl apply -f mysql-deployment.yaml
deployment.apps/mysql created
service/mysql created
查看mysql资源
# kubectl get pod -n wgs-mysql -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-7487d69b7f-9p898 1/1 Running 0 35s 172.20.154.231 192.168.174.106 <none> <none>
# kubectl get svc -n wgs-mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP None <none> 3306/TCP 51s
验证mysql
# kubectl exec -it mysql-7487d69b7f-tf2rp -n wgs-mysql -- mysql -uroot -p123456 -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
查看pv数据
# ls -l mysql/
total 188476
-rw-r----- 1 999 999 56 Jan 3 19:08 auto.cnf
-rw------- 1 999 999 1680 Jan 3 19:08 ca-key.pem
-rw-r--r-- 1 999 999 1112 Jan 3 19:08 ca.pem
-rw-r--r-- 1 999 999 1112 Jan 3 19:08 client-cert.pem
-rw------- 1 999 999 1676 Jan 3 19:08 client-key.pem
-rw-r----- 1 999 999 1318 Jan 3 19:08 ib_buffer_pool
-rw-r----- 1 999 999 50331648 Jan 3 19:09 ib_logfile0
-rw-r----- 1 999 999 50331648 Jan 3 19:08 ib_logfile1
-rw-r----- 1 999 999 79691776 Jan 3 19:09 ibdata1
-rw-r----- 1 999 999 12582912 Jan 3 19:09 ibtmp1
drwxr-x--- 2 999 999 4096 Jan 3 19:08 mysql
lrwxrwxrwx 1 999 999 27 Jan 3 19:08 mysql.sock -> /var/run/mysqld/mysqld.sock
drwxr-x--- 2 999 999 4096 Jan 3 19:08 performance_schema
-rw------- 1 999 999 1680 Jan 3 19:08 private_key.pem
-rw-r--r-- 1 999 999 452 Jan 3 19:08 public_key.pem
-rw-r--r-- 1 999 999 1112 Jan 3 19:08 server-cert.pem
-rw------- 1 999 999 1680 Jan 3 19:08 server-key.pem
drwxr-x--- 2 999 999 12288 Jan 3 19:08 sys
标签:name,19,999,Jan,实例,wgs,mysql,k8s
From: https://www.cnblogs.com/wangguishe/p/17022754.html