本文介绍 Zadig 服务相关的操作。Zadig 的服务可以是一组 Kubernetes 资源集合(下面简称 K8s Yaml)、一个完整的 Helm Chart 或者是传统的云主机服务。下面针对分别介绍这三种类型的服务操作。

K8s Yaml 服务

新增服务

项目中选择点击服务部分,进入服务管理页面。

创建服务

K8s Yaml 服务的创建支持平台管理和代码仓库托管两种方式:

  • 平台管理:服务的 Kubenertes Yaml 配置存储到 Zadig 系统中
  • 仓库托管:将服务用到的 Yaml(包括服务配置使用的 Configmap)托管到代码仓库中,通过 Zadig 平台配置好文件的路径目录后,Zadig 从代码仓库同步,支持两种同步方式:
    • 手动同步:点击加载按钮,获取仓库中最新的配置
    • 自动同步:通过配置 Webhook(参阅 配置 GitLab Webhook),Zadig 监听分支上有代码改动,对应的服务都会自动同步。

平台管理

  • 点击新建按钮新建服务。

创建服务

  • 输入新的服务名称。

创建服务

  • 将服务 YAML 填入编辑器并保存。

创建服务

  • 更新环境,该服务会自动加入到选择的环境中。

更新环境

仓库托管

  • 选择仓库托管,选择代码仓库
  • 选择服务配置所在文件目录,加载服务

更新服务

平台管理

  • 修改服务 Yaml并保存。

修改服务

  • 选择相应环境进行更新。

更新环境中的服务

仓库托管

  • 提交服务配置变更到代码仓库

配置变更

  • 变更合并到主干分支后,通过 Webhook 的能力自动同步最新配置到 Zadig 系统。也可以在界面上手动同步服务配置,如下图所示。

服务手动配置同步

  • 在 Zadig 集成环境中,查看服务配置的变更,点击服务更新按钮执行更新操作

服务版本diff 服务更新

删除服务

  • 服务 模块中将服务配置删除。
  • 更新环境,将删除的服务从相应环境中移除。

删除环境

服务 Yaml 样例

无状态服务

概念:服务运行的实例不会在本地存储需要持久化的数据,并且多个实例对于同一个请求响应的结果是完全一致的。可以参考这篇文章服务 - 图13 (opens new window)了解无状态服务的更多细节。

点击查看

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 2 # 2 个 Pod 实例
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.14.2
  18. ports:
  19. - containerPort: 80

有状态服务

概念:服务的实例可以将一部分数据随时进行备份,并且在创建一个新的有状态服务时,可以通过备份恢复这些数据,以达到数据持久化的目的。可以参考这篇文章服务 - 图14 (opens new window)了解有状态服务的更多细节。

点击查看

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: mysql
  5. labels:
  6. app: mysql
  7. data:
  8. master.cnf: |
  9. # Apply this config only on the master.
  10. [mysqld]
  11. log-bin
  12. slave.cnf: |
  13. # Apply this config only on slaves.
  14. [mysqld]
  15. super-read-only
  16. ---
  17. # Headless service for stable DNS entries of StatefulSet members.
  18. apiVersion: v1
  19. kind: Service
  20. metadata:
  21. name: mysql
  22. labels:
  23. app: mysql
  24. spec:
  25. ports:
  26. - name: mysql
  27. port: 3306
  28. clusterIP: None
  29. selector:
  30. app: mysql
  31. ---
  32. # Client service for connecting to any MySQL instance for reads.
  33. # For writes, you must instead connect to the master: mysql-0.mysql.
  34. apiVersion: v1
  35. kind: Service
  36. metadata:
  37. name: mysql-read
  38. labels:
  39. app: mysql
  40. spec:
  41. ports:
  42. - name: mysql
  43. port: 3306
  44. selector:
  45. app: mysql
  46. ---
  47. apiVersion: apps/v1beta1
  48. kind: StatefulSet
  49. metadata:
  50. name: mysql
  51. spec:
  52. selector:
  53. matchLabels:
  54. app: mysql
  55. serviceName: mysql
  56. # 1 master and 2 slave
  57. replicas: 3
  58. template:
  59. metadata:
  60. labels:
  61. app: mysql
  62. spec:
  63. initContainers:
  64. - name: init-mysql
  65. image: mysql:5.7
  66. command:
  67. - bash
  68. - "-c"
  69. - |
  70. set -ex
  71. # Generate mysql server-id from pod ordinal index.
  72. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  73. ordinal=${BASH_REMATCH[1]}
  74. echo [mysqld] > /mnt/conf.d/server-id.cnf
  75. # Add an offset to avoid reserved server-id=0 value.
  76. echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
  77. # Copy appropriate conf.d files from config-map to emptyDir.
  78. if [[ $ordinal -eq 0 ]]; then
  79. cp /mnt/config-map/master.cnf /mnt/conf.d/
  80. else
  81. cp /mnt/config-map/slave.cnf /mnt/conf.d/
  82. fi
  83. volumeMounts:
  84. - name: conf
  85. mountPath: /mnt/conf.d
  86. - name: config-map
  87. mountPath: /mnt/config-map
  88. - name: clone-mysql
  89. image: gcr.azk8s.cn/google-samples/xtrabackup:1.0
  90. command:
  91. - bash
  92. - "-c"
  93. - |
  94. set -ex
  95. # Skip the clone if data already exists.
  96. [[ -d /var/lib/mysql/mysql ]] && exit 0
  97. # Skip the clone on master (ordinal index 0).
  98. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  99. ordinal=${BASH_REMATCH[1]}
  100. [[ $ordinal -eq 0 ]] && exit 0
  101. # Clone data from previous peer.
  102. ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
  103. # Prepare the backup.
  104. xtrabackup --prepare --target-dir=/var/lib/mysql
  105. volumeMounts:
  106. - name: data
  107. mountPath: /var/lib/mysql
  108. subPath: mysql
  109. - name: conf
  110. mountPath: /etc/mysql/conf.d
  111. containers:
  112. - name: mysql
  113. image: mysql:5.7
  114. env:
  115. - name: MYSQL_ALLOW_EMPTY_PASSWORD
  116. value: "1"
  117. ports:
  118. - name: mysql
  119. containerPort: 3306
  120. volumeMounts:
  121. - name: data
  122. mountPath: /var/lib/mysql
  123. subPath: mysql
  124. - name: conf
  125. mountPath: /etc/mysql/conf.d
  126. resources:
  127. requests:
  128. cpu: 500m
  129. memory: 1Gi
  130. limits:
  131. cpu: 500m
  132. memory: 1Gi
  133. livenessProbe:
  134. exec:
  135. command: ["mysqladmin", "ping"]
  136. initialDelaySeconds: 30
  137. periodSeconds: 10
  138. timeoutSeconds: 5
  139. readinessProbe:
  140. exec:
  141. # Check we can execute queries over TCP (skip-networking is off).
  142. command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
  143. initialDelaySeconds: 5
  144. periodSeconds: 2
  145. timeoutSeconds: 1
  146. - name: xtrabackup
  147. image: gcr.azk8s.cn/google-samples/xtrabackup:1.0
  148. ports:
  149. - name: xtrabackup
  150. containerPort: 3307
  151. command:
  152. - bash
  153. - "-c"
  154. - |
  155. set -ex
  156. cd /var/lib/mysql
  157. # Determine binlog position of cloned data, if any.
  158. if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
  159. # XtraBackup already generated a partial "CHANGE MASTER TO" query
  160. # because we're cloning from an existing slave. (Need to remove the tailing semicolon!)
  161. cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
  162. # Ignore xtrabackup_binlog_info in this case (it's useless).
  163. rm -f xtrabackup_slave_info xtrabackup_binlog_info
  164. elif [[ -f xtrabackup_binlog_info ]]; then
  165. # We're cloning directly from master. Parse binlog position.
  166. [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
  167. rm -f xtrabackup_binlog_info xtrabackup_slave_info
  168. echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
  169. MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
  170. fi
  171. # Check if we need to complete a clone by starting replication.
  172. if [[ -f change_master_to.sql.in ]]; then
  173. echo "Waiting for mysqld to be ready (accepting connections)"
  174. until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
  175. echo "Initializing replication from clone position"
  176. mysql -h 127.0.0.1 \
  177. -e "$(<change_master_to.sql.in), \
  178. MASTER_HOST='mysql-0.mysql', \
  179. MASTER_USER='root', \
  180. MASTER_PASSWORD='', \
  181. MASTER_CONNECT_RETRY=10; \
  182. START SLAVE;" || exit 1
  183. # In case of container restart, attempt this at-most-once.
  184. mv change_master_to.sql.in change_master_to.sql.orig
  185. fi
  186. # Start a server to send backups when requested by peers.
  187. exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
  188. "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
  189. volumeMounts:
  190. - name: data
  191. mountPath: /var/lib/mysql
  192. subPath: mysql
  193. - name: conf
  194. mountPath: /etc/mysql/conf.d
  195. resources:
  196. requests:
  197. cpu: 100m
  198. memory: 100Mi
  199. volumes:
  200. - name: conf
  201. emptyDir: {}
  202. - name: config-map
  203. configMap:
  204. name: mysql
  205. volumeClaimTemplates:
  206. - metadata:
  207. name: data
  208. spec:
  209. accessModes: ["ReadWriteOnce"]
  210. resources:
  211. requests:
  212. storage: 10Gi

Helm Chart 服务

文档建设中,敬请期待……

传统云主机服务

文档建设中,敬请期待……