Kubernetes的安装和使用(一)
Kubernetes的安装和使用(二)
Kubernetes的安装和使用(三)
在前面的例子中我们使用port-forward进行端口转发,这样会存在两个问题:
k8s的Service就是用来解决这个问题的,它包含ClusterIP、NodePort和Headless等模块。
ClusterIP就是将多个pod用一个ip进行访问的服务,这个ip只能在集群内访问,我们创建如下的程序
1 | package main |
我们将如上程序打包成镜像
docker build . -t derobukal/hellok8s:v3_hostnamedocker 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) AGEkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d1hservice-hellok8s-clusterip ClusterIP 10.111.240.227 <none> 3000/TCP 10s~ kc get endpointsNAME ENDPOINTS AGEkubernetes 192.168.49.2:8443 7d1hservice-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 podsNAME READY STATUS RESTARTS AGEhellok8s-go-http-6f5d68bc64-6xrdx 1/1 Running 0 23mhellok8s-go-http-6f5d68bc64-b7wq2 1/1 Running 0 23mhellok8s-go-http-6f5d68bc64-rk59k 1/1 Running 0 23mnginx 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-b7wq2root@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59kroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59kroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-b7wq2root@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-6xrdxroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-b7wq2root@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59kroot@nginx:/#
上面通过curl测试service,可以看到每次访问的后端pod都是不一样的。接下来我们增加pod的数量,可以看到endpoint的数量也发生了变化,同时测试请求也打到了新的pod上面
~ kc scale deployment/hellok8s-go-http --replicas=10deployment.apps/hellok8s-go-http scaled~ kc get endpoints NAME ENDPOINTS AGEkubernetes 192.168.49.2:8443 7d1hservice-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 AGEhellok8s-go-http-6f5d68bc64-5lxvs 1/1 Running 0 27shellok8s-go-http-6f5d68bc64-6xrdx 1/1 Running 0 29mhellok8s-go-http-6f5d68bc64-b7wq2 1/1 Running 0 29mhellok8s-go-http-6f5d68bc64-cl56f 1/1 Running 0 27shellok8s-go-http-6f5d68bc64-gbn9v 1/1 Running 0 27shellok8s-go-http-6f5d68bc64-k7db4 1/1 Running 0 27shellok8s-go-http-6f5d68bc64-m4h5s 1/1 Running 0 27shellok8s-go-http-6f5d68bc64-rk59k 1/1 Running 0 29mhellok8s-go-http-6f5d68bc64-whpht 1/1 Running 0 27shellok8s-go-http-6f5d68bc64-xnvk2 1/1 Running 0 27snginx 1/1 Running 0 8m13s~ kc exec nginx -it -- bashroot@nginx:/# curl 10.111.240.227:3000 [v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-6xrdxroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-b7wq2root@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-m4h5sroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-whphtroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-whphtroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-cl56froot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-xnvk2root@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-cl56froot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-cl56froot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-m4h5sroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-rk59kroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-whphtroot@nginx:/# curl 10.111.240.227:3000[v3] Hello, Kubernetes!, From host: hellok8s-go-http-6f5d68bc64-k7db4root@nginx:/#
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) AGEkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d1hservice-hellok8s-clusterip ClusterIP 10.111.240.227 <none> 3000/TCP 28mservice-hellok8s-nodeport NodePort 10.106.138.169 <none> 3000:30000/TCP 17s
因为使用minikube创建服务,而非创建了k8s的集群,因此暂时不太方便测试该功能。