创建 Service

k8s 从逻辑上代表了一组 Pod,具体是哪些 Pod 则是由 label 来挑选。 Service 有自己的 IP,而且这个 IP 是不变的,客户端只需要访问 Service 的 IP, k8s 则负责建立和维护 Service 与 Pod 的映射关系,无论后端 Pod 如何变化,对客户端不会有任何影响,因为 Service 没有变。

创建 Deployment

[root@master service]# cat httpd.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-delpoy
  labels:
    run: apache
spec:
  replicas: 3
  selector:
    matchLabels:
      run: apache
  template:
    metadata:
      labels:
        run: apache     ## 与之后创建 service 对应
    spec:
      containers:
      - name: httpd
        image: httpd
        ports:
        - containerPort: 80

启动:

[root@master service]# kubectl apply  -f httpd.yml 
deployment.apps/httpd-delpoy created

查看并访问节点:

[root@master service]# kubectl  get pod -o wide 
NAME                            READY   STATUS    RESTARTS   AGE   IP             NODE    NOMINATED NODE   READINESS GATES
httpd-delpoy-6cbfc784b4-46dtz   1/1     Running   0          33s   10.244.1.138   node1   <none>           <none>
httpd-delpoy-6cbfc784b4-9tfkr   1/1     Running   0          33s   10.244.2.135   node2   <none>           <none>
httpd-delpoy-6cbfc784b4-gf7m7   1/1     Running   0          33s   10.244.1.139   node1   <none>           <none>
[root@master service]# curl 10.244.1.138
<html><body><h1>It works!</h1></body></html>
[root@master service]# curl 10.244.2.135
<html><body><h1>It works!</h1></body></html>
[root@master service]# curl 10.244.1.139
<html><body><h1>It works!</h1></body></html>

创建 service

[root@master service]# cat service_httpd.yml 
apiVersion: v1
kind: Service
metadata:
  name: httpd-svc
spec:
  selector:
    run: apache   ## 对应 Deployment 的 run
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80

v1是service的apiversion
kind 指明了当前资源类型为service。
service的名字为httpd-svc
selector指明挑选哪些label为run:apache的pod作为service的后端
将service的8080端口映射到pod的80端口,使用TCP协议

查看 service:

root@master service]# kubectl  get service -A
NAMESPACE     NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default       httpd-svc    ClusterIP   10.96.103.176   <none>        8080/TCP                 7m27s

查看 httpd-svc 与 Pod 的关系:

[root@master service]# kubectl  describe  services  httpd-svc 
Name:              httpd-svc
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"httpd-svc","namespace":"default"},"spec":{"ports":[{"port":8080,"...
Selector:          run=apache
Type:              ClusterIP
IP:                10.96.103.176
Port:              <unset>  8080/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.138:80,10.244.1.139:80,10.244.2.135:80
Session Affinity:  None
Events:            <none>

httpd-svc 分配到一个 ClusterIP 10.96.103.176,可以通过该 IP 访问后端的 httpd Pod

[root@master service]# curl 10.96.103.176:8080
<html><body><h1>It works!</h1></body></html>

DNS 访问 Service

kubeadm 部署时会默认安装 coredns 组件:

[root@master service]# kubectl  get deployments.apps  -n kube-system 
NAME      READY   UP-TO-DATE   AVAILABLE   AGE
coredns   2/2     2            2           10d

创建一个容器访问:

[root@master service]# kubectl run -it --rm --image=busybox:1.28 sh
/ # wget httpd-svc.default:8080
          服务名称  命名空间
Connecting to httpd-svc.default:8080 (10.96.103.176:8080)
index.html           100% |***********************************************|    45   0:00:00 ETA
/ # cat index.html 
<html><body><h1>It works!</h1></body></html>

外网访问 Service

修改 service 配置文件:

[root@master service]# cat service_httpd.yml 
apiVersion: v1
kind: Service
metadata:
  name: httpd-svc
spec:
  type: NodePort
  selector:
    run: apache
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80

添加 type: NodePort

访问 service:

[root@master service]# kubectl apply -f service_httpd.yml 
service/httpd-svc configured
[root@master service]# kubectl get svc httpd-svc 
NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
httpd-svc   NodePort   10.96.103.176   <none>        8080:32595/TCP   15m
[root@master service]# curl 192.168.19.160:32595
<html><body><h1>It works!</h1></body></html>
[root@master service]# curl 192.168.19.161:32595
<html><body><h1>It works!</h1></body></html>
[root@master service]# curl 192.168.19.162:32595
<html><body><h1>It works!</h1></body></html>

上面的是随机分配一个端口,我们也可以用 nodePort 指定一个端口

[root@master service]# cat service_httpd.yml 
apiVersion: v1
kind: Service
metadata:
  name: httpd-svc
spec:
  type: NodePort
  selector:
    run: apache
  ports:
  - protocol: TCP
    nodePort: 30001
    port: 8080
    targetPort: 80

访问 service:

[root@master service]# curl 192.168.19.160:30001
<html><body><h1>It works!</h1></body></html>
[root@master service]# curl 192.168.19.161:30001
<html><body><h1>It works!</h1></body></html>
[root@master service]# curl 192.168.19.162:30001
<html><body><h1>It works!</h1></body></html>
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐