(22) Kubernetes - Service

1. Concept

Through the previous study, it is possible to create a set of pods through deployment to provide highly available services. Although each pod will assign a ip, however, have the following problems:
1) With the reconstruction pod pod ip will vary
2) pod ip is only visible within the cluster virtual ip, external can access
this service with access to this It is difficult, therefore, kubernetes designed service to solve this problem.
Service can be regarded as a group of similar pods' external access interfaces. With the help of service, applications can easily realize service discovery and load balancing.
Insert picture description here
A request arrives at the service first, and the service matches the pod according to the tag selector, and then forwards it to the pod.

Two, service creation

Once the service is created, there will be an IP. If the IP of the service changes, the IP used to access the pod through the service will also change.

  1. Create an accessible service inside the cluster
#暴露service
# expose 暴露,就是把上图所示的三个pod暴露出来
# deploy nginx 通过deployment 来寻找pod
# name 名称
# type 类型,ClusterIp这个类型是只有在集群内部才可以访问的
# port service 上的端口
# target-port 对应到pod上的端口
kubectl expose deploy nginx --name=svc-nginx1 --type=ClusterIp --port=80 --target-port=80 -n dev
#查询service, service可以简写为svc
kubectl get service -n dev

A CLUSTER-IP is generated here, which is the ip of the service. During the life cycle of the service, this address will not change, and the pod corresponding to the current service can be accessed through this ip:

curl 10.109.179.231:80
  1. Create a service that can also be accessed outside the cluster
#上面创建的service的type类型为ClusterIp,这个ip地址只可以在集群内部访问,如果需要创建集群外部也可以访问的service,需要修改type的类型为NodePort
kubectl expose deploy nginx --name=svc-ningx2 --type=NodePort --port=80 --target-port=80 -n dev
#这时候的port格式80:30539,30539对应的是node节点上的端口,访问node节点的30740会转发到此service的80端口,可以在本机访问:

Insert picture description here

  1. Delete servie
kubectl delete svc svc-nginx1 -n dev
  1. Configuration method
    Create a svc-nginx.yaml
apiVersion: v1
kind: Service
metadata:
	name: svc-nginx
	namespace: dev
spec:
	clusterIp: 10.109.179.231
	ports:
		- port: 80
		  protocol: TCP
		  targetPort: 80
	selector:
		run: nginx
	type: ClusterIp
		

If ClusterIp is configured, then ClusterIp can be configured without configuration. If configured, it is configured with ip. If not configured, it is configured with ip by default.

#创建service
kubectl create -f svc-nginx.yaml
#删除
kubectl delete -f svc-nginx.yaml

》》》Bloggers update their learning experience for a long time, recommend likes and follow! ! !
》》》If there is something wrong, please leave a message in the comment area, thank you! ! !

Guess you like

Origin blog.csdn.net/qq_41622739/article/details/114223100