本文部署以概述中图1组网方式为例,3个MySQL Pod分别部署在3个计算节点上,外部client访问K8s集群内部的3个MySQL Pod有2种方式:
通过yaml文件中3个kind: Service中dbport的nodePort使的MySQL Pod被外部client访问,即外部client可以通过主节点间接访问mysql Pod,外部client访问3个mysql时用的IP地址为主节点IP地址(192.168.200.10),端口分别为30001、30002、30003(例如yaml文件中kind: Service,name: dbport,nodePort: 30001)。
将mysql Pod的Pod IP地址网段(10.99.0.0/16)路由到主节点的IP地址(192.168.200.10),主节点也有10.99.0.0/16网段的IP地址,所以外部client可以通过主节点直接访问mysql Pod的Pod IP地址,外部client访问3个mysql时用的IP地址为3个mysql Pod的Pod IP地址,端口都是3306(yaml文件中kind: Service,name: dbport,port: 3306)。具体操作就是在gateway上配置静态路由,例如"route add -net 10.99.0.0/16 gw 192.168.200.10",详细可见《Kube-OVN组件 用户指南》中“Pod IP地址直接对外暴露”的章节。
本文以部署3个MySQL Pod为例,在K8s的主节点物理机上编写部署yaml(例如文件名称为mysql_deployment.yaml)配置文件。
下面yaml文件编辑内容和上面链接中下载的一致,为了方便可以直接通过链接下载。
1 | vim mysql_deployment.yaml
|
编辑内容如下:
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | apiVersion: v1 kind: Namespace metadata: name: ns-mysql-test labels: name: ns-mysql-test --- apiVersion: kubeovn.io/v1 kind: Subnet metadata: name: ns-mysql-test spec: protocol: IPv4 cidrBlock: 10.99.0.0/16 excludeIps: - 10.99.0.1..10.99.0.10 gateway: 10.99.0.1 namespaces: - ns-mysql-test private: false gatewayType: distributed natOutgoing: false --- apiVersion: v1 kind: Pod metadata: labels: app: mysql-1 name: mysql-1 namespace: ns-mysql-test annotations: ovn.kubernetes.io/ip_address: 10.99.0.15 ovn.kubernetes.io/mac_address: 00:00:00:53:6B:B6 spec: nodeSelector: test: "mysql-test-1" hostAliases: - ip: "10.99.0.15" hostnames: - "mysql-1" - ip: "10.99.0.16" hostnames: - "mysql-2" - ip: "10.99.0.17" hostnames: - "mysql-3" containers: - name: mysql-1 image: mymysql/centos8-mysql-arm:8.0.19 resources: limits: cpu: 16 memory: 64Gi ports: - name: mysql-port containerPort: 3306 - name: copy-port containerPort: 33061 env: - name: MYSQL_ROOT_PASSWORD value: test123 - name: MYSQL_ROOT_HOST value: "%" volumeMounts: - name: mysql-data mountPath: "/data/mysql/data" - name: mysql-log mountPath: "/data/mysql/log" - name: mysql-run mountPath: "/data/mysql/run" - name: mysql-tmp mountPath: "/data/mysql/tmp" - name: mysql-cnf mountPath: "/etc/my.cnf" volumes: - name: mysql-data hostPath: path: "/data/mysql/mysql_1/data" type: DirectoryOrCreate - name: mysql-log hostPath: path: "/data/mysql/mysql_1/log" type: DirectoryOrCreate - name: mysql-run hostPath: path: "/data/mysql/mysql_1/run" type: DirectoryOrCreate - name: mysql-tmp hostPath: path: "/data/mysql/mysql_1/tmp" type: DirectoryOrCreate - name: mysql-cnf hostPath: path: "/data/mysql/mysql_1/mysql_arm.cnf" type: FileOrCreate --- apiVersion: v1 kind: Pod metadata: labels: app: mysql-2 name: mysql-2 namespace: ns-mysql-test annotations: ovn.kubernetes.io/ip_address: 10.99.0.16 ovn.kubernetes.io/mac_address: 00:00:00:53:6B:B7 spec: nodeSelector: test: "mysql-test-2" hostAliases: - ip: "10.99.0.15" hostnames: - "mysql-1" - ip: "10.99.0.16" hostnames: - "mysql-2" - ip: "10.99.0.17" hostnames: - "mysql-3" containers: - name: mysql-2 image: mymysql/centos8-mysql-arm:8.0.19 resources: limits: cpu: 16 memory: 64Gi ports: - name: mysql-port containerPort: 3306 - name: copy-port containerPort: 33061 env: - name: MYSQL_ROOT_PASSWORD value: test123 - name: MYSQL_ROOT_HOST value: "%" volumeMounts: - name: mysql-data mountPath: "/data/mysql/data" - name: mysql-log mountPath: "/data/mysql/log" - name: mysql-run mountPath: "/data/mysql/run" - name: mysql-tmp mountPath: "/data/mysql/tmp" - name: mysql-cnf mountPath: "/etc/my.cnf" volumes: - name: mysql-data hostPath: path: "/data/mysql/mysql_2/data" type: DirectoryOrCreate - name: mysql-log hostPath: path: "/data/mysql/mysql_2/log" type: DirectoryOrCreate - name: mysql-run hostPath: path: "/data/mysql/mysql_2/run" type: DirectoryOrCreate - name: mysql-tmp hostPath: path: "/data/mysql/mysql_2/tmp" type: DirectoryOrCreate - name: mysql-cnf hostPath: path: "/data/mysql/mysql_2/mysql_arm.cnf" type: FileOrCreate --- apiVersion: v1 kind: Pod metadata: labels: app: mysql-3 name: mysql-3 namespace: ns-mysql-test annotations: ovn.kubernetes.io/ip_address: 10.99.0.17 ovn.kubernetes.io/mac_address: 00:00:00:53:6B:B8 spec: nodeSelector: test: "mysql-test-3" hostAliases: - ip: "10.99.0.15" hostnames: - "mysql-1" - ip: "10.99.0.16" hostnames: - "mysql-2" - ip: "10.99.0.17" hostnames: - "mysql-3" containers: - name: mysql-3 image: mymysql/centos8-mysql-arm:8.0.19 resources: limits: cpu: 16 memory: 64Gi ports: - name: mysql-port containerPort: 3306 - name: copy-port containerPort: 33061 env: - name: MYSQL_ROOT_PASSWORD value: test123 - name: MYSQL_ROOT_HOST value: "%" volumeMounts: - name: mysql-data mountPath: "/data/mysql/data" - name: mysql-log mountPath: "/data/mysql/log" - name: mysql-run mountPath: "/data/mysql/run" - name: mysql-tmp mountPath: "/data/mysql/tmp" - name: mysql-cnf mountPath: "/etc/my.cnf" volumes: - name: mysql-data hostPath: path: "/data/mysql/mysql_3/data" type: DirectoryOrCreate - name: mysql-log hostPath: path: "/data/mysql/mysql_3/log" type: DirectoryOrCreate - name: mysql-run hostPath: path: "/data/mysql/mysql_3/run" type: DirectoryOrCreate - name: mysql-tmp hostPath: path: "/data/mysql/mysql_3/tmp" type: DirectoryOrCreate - name: mysql-cnf hostPath: path: "/data/mysql/mysql_3/mysql_arm.cnf" type: FileOrCreate --- apiVersion: v1 kind: Service metadata: name: mysql-1-service namespace: ns-mysql-test spec: type: NodePort selector: app: mysql-1 ports: - name: dbport protocol: TCP port: 3306 targetPort: 3306 nodePort: 30001 - name: cpport protocol: TCP port: 33061 targetPort: 33061 nodePort: 30061 --- apiVersion: v1 kind: Service metadata: name: mysql-2-service namespace: ns-mysql-test spec: type: NodePort selector: app: mysql-2 ports: - name: dbport protocol: TCP port: 3306 targetPort: 3306 nodePort: 30002 - name: cpport protocol: TCP port: 33061 targetPort: 33061 nodePort: 30062 --- apiVersion: v1 kind: Service metadata: name: mysql-3-service namespace: ns-mysql-test spec: type: NodePort selector: app: mysql-3 ports: - name: dbport protocol: TCP port: 3306 targetPort: 3306 nodePort: 30003 - name: cpport protocol: TCP port: 33061 targetPort: 33061 nodePort: 30063 |
在该配置文件中配置了:
1 | kubectl get nodes |
1 2 3 4 5 | NAME STATUS ROLES AGE VERSION centos-10 Ready master 42h v1.18.5 node-test-1 Ready <none> 42h v1.18.5 node-test-2 Ready <none> 42h v1.18.5 node-test-3 Ready <none> 42h v1.18.5 |
本文以将三个MySQL Pod分别部署到三个node-test-1、node-test-2、node-test-3三个节点为例,使用kubectl label nodes命令分别配置三个节点的标签:
1 2 3 | kubectl label nodes node-test-1 test=mysql-test-1 kubectl label nodes node-test-2 test=mysql-test-2 kubectl label nodes node-test-3 test=mysql-test-3 |
应用yaml文件的部署,即可将三个MySQL Pod分别部署到三个不同的node上。