Kubernetes 的安装和使用(三)
目录
Kubernetes 的安装和使用(一)
Kubernetes 的安装和使用(二)
Kubernetes 的安装和使用(三)
k8s 使用 Service
在前面的例子中我们使用 port-forward 进行端口转发,这样会存在两个问题:
- pod 重启后 ip 地址发生了变化怎么办
- 如何进行负载均衡
k8s 的 Service 就是用来解决这个问题的,它包含 ClusterIP、NodePort 和 Headless 等模块。
ClusterIP
ClusterIP 就是将多个 pod 用一个 ip 进行访问的服务,这个 ip 只能在集群内访问,我们创建如下的程序
1 | package main |
我们将如上程序打包成镜像
docker build . -t derobukal/hellok8s:v3_hostname
docker push derobukal/hellok8s:v3_hostname
然后启动 k8s 的 deployment,可以得到 3 个运行的 pod
1 | apiVersion: apps/v1 |
之后我们创建 service-clusterip.yaml
1 | apiVersion: v1 |
随后启动 service,并查看 service 所生成的 clusterIp 的值,以及 clusterIp 后面的 endpoints 的详细信息
~ kc apply -f service-clusterip.yaml
service/service-hellok8s-clusterip created
~ kc get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d1h
service-hellok8s-clusterip ClusterIP 10.111.240.227 <none> 3000/TCP 10s
~ kc get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.49.2:8443 7d1h
service-hellok8s-clusterip 10.244.0.67:3000,10.244.0.68:3000,10.244.0.69:3000 16s
有了 service 之后,我们就可以很方便的通过 clusterIp 的地址访问服务了。因为我们使用的是 minikube,为了能正常访问 clusterIp,我们先创建一个 nginx 的 pod
1 | apiVersion: v1 |
之后我们进入 nginx 的 pod 测试 clusterIp
~ kc apply -f pod_nginx.yaml
pod/nginx created
~ kc get pods
NAME READY STATUS RESTARTS AGE
hellok8s-go-http-6f5d68bc64-6xrdx 1/1 Running 0 23m
hellok8s-go-http-6f5d68bc64-b7wq2 1/1 Running 0 23m
hellok8s-go-http-6f5d68bc64-rk59k 1/1 Running 0 23m
nginx 1/1 Running 0 98s
~ kc exec nginx -it -- bash
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-b7wq2
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59k
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59k
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-b7wq2
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-6xrdx
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-b7wq2
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59k
root@nginx:/#
上面通过 curl 测试 service,可以看到每次访问的后端 pod 都是不一样的。接下来我们增加 pod 的数量,可以看到 endpoint 的数量也发生了变化,同时测试请求也打到了新的 pod 上面
~ kc scale deployment/hellok8s-go-http --replicas=10
deployment.apps/hellok8s-go-http scaled
~ kc get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.49.2:8443 7d1h
service-hellok8s-clusterip 10.244.0.67:3000,10.244.0.68:3000,10.244.0.69:3000 + 7 more... 22m
~ kc get pods
NAME READY STATUS RESTARTS AGE
hellok8s-go-http-6f5d68bc64-5lxvs 1/1 Running 0 27s
hellok8s-go-http-6f5d68bc64-6xrdx 1/1 Running 0 29m
hellok8s-go-http-6f5d68bc64-b7wq2 1/1 Running 0 29m
hellok8s-go-http-6f5d68bc64-cl56f 1/1 Running 0 27s
hellok8s-go-http-6f5d68bc64-gbn9v 1/1 Running 0 27s
hellok8s-go-http-6f5d68bc64-k7db4 1/1 Running 0 27s
hellok8s-go-http-6f5d68bc64-m4h5s 1/1 Running 0 27s
hellok8s-go-http-6f5d68bc64-rk59k 1/1 Running 0 29m
hellok8s-go-http-6f5d68bc64-whpht 1/1 Running 0 27s
hellok8s-go-http-6f5d68bc64-xnvk2 1/1 Running 0 27s
nginx 1/1 Running 0 8m13s
~ kc exec nginx -it -- bash
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-6xrdx
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-b7wq2
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-m4h5s
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-whpht
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-whpht
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-cl56f
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-xnvk2
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-cl56f
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-cl56f
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-m4h5s
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59k
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-whpht
root@nginx:/# curl 10.111.240.227:3000
[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-k7db4
root@nginx:/#
NodePort
clusterIp 只能在集群内部进行访问,NodePort 在 clusterIp 的基础上,还支持了通过 k8s 集群的节点进行访问。有如下的配置
1 | apiVersion: v1 |
之后启动 nodeport 的服务,就可以在集群的节点上访问服务了
~ kc apply -f service-nodeport.yaml
service/service-hellok8s-nodeport created
~ kc get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d1h
service-hellok8s-clusterip ClusterIP 10.111.240.227 <none> 3000/TCP 28m
service-hellok8s-nodeport NodePort 10.106.138.169 <none> 3000:30000/TCP 17s
因为使用 minikube 创建服务,而非创建了 k8s 的集群,因此暂时不太方便测试该功能。