hostpath存储类型及headlines类型的service版mysql5.7部署并构建webstack
一、headlines类型的service版mysql构建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| --- apiVersion: v1 kind: ConfigMap metadata: name: mysql-cm namespace: dev data: my.cnf: | !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mysql.conf.d/ [mysqld] server-id=001 log-bin=mysql-bin gtid_mode=on enforce_gtid_consistency=on relay-log=relay-log.log binlog_format=ROW master_info_repository=TABLE relay_log_info_repository=TABLE sync_master_info=1 slave_parallel_workers=2 binlog_checksum=CRC32 master_verify_checksum=1 slave_sql_verify_checksum=1 binlog_rows_query_log_events=1 slave_parallel_type=logical_clock general_log=1 log_output=FILE,TABLE --- apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql namespace: dev spec: selector: matchLabels: app: mysql serviceName: "mysql" replicas: 1 template: metadata: labels: app: mysql spec: terminationGracePeriodSeconds: 10 containers: - name: mysql image: registry.cn-hangzhou.aliyuncs.com/zznn/mysql:5.7 env: - name: MYSQL_ROOT_PASSWORD value: "root_password" args: - --character-set-server=utf8mb4 - --collation-server=utf8mb4_unicode_ci volumeMounts: - mountPath: /var/lib/mysql name: mysql-storage - name: config-map mountPath: "/etc/mysql/my.cnf" subPath: my.cnf ports: - containerPort: 3306 name: mysql volumeMounts: - name: www mountPath: /var/lib/mysql volumes: - name: config-map configMap: name: mysql-cm
- name: www hostPath: path: /opt/webstack_db type: DirectoryOrCreate
--- apiVersion: v1 kind: Service metadata: name: service-mysql namespace: dev spec: selector: app: mysql clusterIP: None type: ClusterIP ports: - port: 3306 targetPort: 3306
|
查看域名内部dns解析
1 2 3 4 5 6 7
| kubectl exec -it mysql-0 -n dev -- /bin/sh ls
cat /etc/resolv.conf
|
备注:域名规则 svc名称+ns名称+写死部分svc.cluster.local 即此headliness类型的pod域名方式访问应该是
service-mysql.dev.svc.cluster.local
验证(宿主机执行):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| dig @10.96.0.10 service-mysql.dev.svc.cluster.local // 返回 root@k8s:/opt/webstack
; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> @10.96.0.10 service-mysql.dev.svc.cluster.local ; (1 server found) ;; global options: +cmd ;; Got answer: ;; WARNING: .local is reserved for Multicast DNS ;; You are currently testing what happens when an mDNS query is leaked to DNS ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52747 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ; COOKIE: dd22da56f562b801 (echoed) ;; QUESTION SECTION: ;service-mysql.dev.svc.cluster.local. IN A
;; ANSWER SECTION: service-mysql.dev.svc.cluster.local. 30 IN A 172.16.0.9
;; Query time: 0 msec ;; SERVER: 10.96.0.10#53(10.96.0.10) ;; WHEN: Tue Dec 24 15:19:34 CST 2024 ;; MSG SIZE rcvd: 127
root@k8s:/opt/webstack# kubectl get pod -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mysql-0 1/1 Running 0 18m 172.16.0.9 k8s <none> <none> qexo-0 1/1 Running 1 (64m ago) 77m 172.16.0.6 k8s <none> <none>
|

headlines类型的service集群之间通信直接使用svc名称

二、webstack构建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| --- apiVersion: apps/v1 kind: Deployment metadata: name: webstack namespace: dev spec: replicas: 1 selector: matchLabels: app: webstack template: metadata: labels: app: webstack spec: terminationGracePeriodSeconds: 10 containers: - name: webstack image: registry.cn-hangzhou.aliyuncs.com/zznn/mycentos:webstackv-1.2.2 env: - name: LOGIN_COPTCHA value: "false" - name: DB_HOST value: "service-mysql" - name: DB_DATABASE value: webstack - name: DB_USERNAME value: root - name: DB_PASSWORD value: root_password - name: APP_URL value: "" ports: - containerPort: 8000 name: webstack command: ['/entrypoint.sh','serve']
--- apiVersion: v1 kind: Service metadata: namespace: dev name: webstack-service spec: selector: app: webstack ports: - protocol: TCP port: 8000 targetPort: 8000 sessionAffinity: ClientIP type: NodePort
|
成功!