CCE Service 类型与网络访问
1. Service Types and Application Scenarios
Section titled “1. Service Types and Application Scenarios”Services of the ClusterIP, NodePort, LoadBalancer, and Headless Service types offer different functions.
- ClusterIP: used to make the Service only reachable within a cluster.
- NodePort: used for access from outside a cluster. A NodePort Service is accessed through the port on the node. For details, see NodePort Services.
- LoadBalancer: used for access from outside a cluster. It is an extension of NodePort, to which a load balancer routes, and external systems only need to access the load balancer. For details, see LoadBalancer Services.
- Headless Service: used by pods to discover each other. No separate cluster IP address will be allocated to this type of Service, and the cluster will not balance loads or perform routing for it. You can create a headless Service by setting the spec.clusterIP value to None. For details, see Headless Services.
we can use kubectl expose command setting this, And we can setting use console also.
# When using the NodePort type, the port here is meaningless. When accessing through a node, you need to actually view the corresponding NodePort.# This port parameter is only use for internal access when set the nodeportkubectl expose deployment deployment-name --port=30080 --target-port=80 --name=service-name --type=NodePortkubectl expose --type=NodePort deployment nginx --port 80 --name nginx-nodeport --overrides '{ "apiVersion": "v1","spec":{"ports": [{"port":80,"protocol":"TCP","targetPort":80,"nodePort":30080}]}}'
kubectl expose deployment deployment-name --port=80 --target-port=80 --name=service-name --type=ClusterIPkubectl expose deployment hello-world --port 80 --type=LoadBalancer
# we can create a service and modify the selectorkubectl create service nodeport myservice --node-port=31000 --tcp=3000:80Manage clusters via console or commands
Section titled “Manage clusters via console or commands”We can see the Cluster Information —> Resources —> Connection Information —> kubectl, And then get the more details informations about configure kubectl.


When we finished all the steps, we can use commands operate the CCE cluster:
[root@ecs-c5e5 ~]# kubectl get nsNAME STATUS AGEdefault Active 32hkube-node-lease Active 32hkube-public Active 32hkube-system Active 32hKubectl autocomplete
[root@ecs-c5e5 ~]# yum -y install bash-completion[root@ecs-c5e5 ~]# source <(kubectl completion bash)[root@ecs-c5e5 ~]# echo "source <(kubectl completion bash)" >> ~/.bashrcYou can also use a shorthand alias for kubectl that also works with completion:
alias k=kubectlcomplete -o default -F __start_kubectl kClusterIP
Section titled “ClusterIP”[root@ecs-c5e5 ~]# kubectl apply -f nginx.yaml -f tomcat.yamldeployment.apps/nginx createddeployment.apps/tomcat created[root@ecs-c5e5 ~]# kubectl expose deployment nginx --type=ClusterIP --name=nginx-service --port=80 --target-port=80service/nginx-service exposed[root@ecs-c5e5 ~]#[root@ecs-c5e5 ~]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-698c667dd7-zrnrx 1/1 Running 0 2m8s 172.16.0.13 192.168.34.211 <none> <none>tomcat-77b6db4bdd-x295j 1/1 Running 0 2m8s 172.16.0.12 192.168.34.211 <none> <none>[root@ecs-c5e5 ~]# kubectl get svc -o wideNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTORkubernetes ClusterIP 10.247.0.1 <none> 443/TCP 33h <none>nginx-service ClusterIP 10.247.156.240 <none> 80:80/TCP 36s app=nginx[root@ecs-c5e5 ~]# kubectl get endpointsNAME ENDPOINTS AGEkubernetes 192.168.34.240:5444 33hnginx-service 172.16.0.13:80 49s[root@ecs-c5e5 ~]# kubectl scale deployment nginx --replicas=2deployment.apps/nginx scaled[root@ecs-c5e5 ~]# kubectl get endpointsNAME ENDPOINTS AGEkubernetes 192.168.34.240:5444 33hnginx-service 172.16.0.13:80,172.16.0.14:80 64sAccess test like this:
[root@ecs-c5e5 ~]# kubectl exec -it tomcat-77b6db4bdd-x295j -- /bin/bashroot@tomcat-77b6db4bdd-x295j:/usr/local/tomcat#
# Access USE Pod IProot@tomcat-77b6db4bdd-x295j:/usr/local/tomcat# curl 172.16.0.13<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>
<p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p></body></html>
# Access use ClusterIProot@tomcat-77b6db4bdd-x295j:/usr/local/tomcat# curl 10.247.156.240<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>
<p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p></body></html>
# Access use service nameroot@tomcat-77b6db4bdd-x295j:/usr/local/tomcat# curl nginx-service<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>
<p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p></body></html>
# Access use Domainroot@tomcat-77b6db4bdd-x295j:/usr/local/tomcat# cat /etc/resolv.confsearch default.svc.cluster.local svc.cluster.local cluster.localnameserver 10.247.3.10options ndots:5 timeout:2 single-request-reopenroot@tomcat-77b6db4bdd-x295j:/usr/local/tomcat# curl nginx-service.default.svc.cluster.local<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>
<p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p></body></html>nodeport
Section titled “nodeport”
[root@ecs-c5e5 ~]# kubectl get svc nginx-service -o yamlapiVersion: v1kind: Servicemetadata: creationTimestamp: "2023-08-27T05:13:44Z" labels: app: nginx name: nginx-service namespace: default resourceVersion: "86260" uid: fac3c03a-fb5d-4341-812f-cff661c9ebe2spec: clusterIP: 10.247.156.240 clusterIPs: - 10.247.156.240 externalTrafficPolicy: Cluster internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - nodePort: 31428 port: 80 protocol: TCP targetPort: 80 selector: app: nginx sessionAffinity: None type: NodePortstatus: loadBalancer: {}loadbalancer
Section titled “loadbalancer”
[root@ecs-c5e5 ~]# kubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.247.0.1 <none> 443/TCP 47hnginx ClusterIP 10.247.80.80 <none> 80/TCP 13hnginx-elb LoadBalancer 10.247.155.17 110.238.103.45,192.168.34.190 80:32337/TCP 3m24snginx-node NodePort 10.247.183.147 <none> 30080:32180/TCP 13h[root@ecs-c5e5 ~]# kubectl get svc nginx-elb -o yamlapiVersion: v1kind: Servicemetadata: annotations: kubernetes.io/elb.autocreate: '{"name":"elb-test","type":"public","bandwidth_name":"cce-bandwidth-1693163374519","bandwidth_chargemode":"traffic","bandwidth_size":5,"bandwidth_sharetype":"PER","eip_type":"5_bgp"}' kubernetes.io/elb.class: union kubernetes.io/elb.eip-id: fba071e4-58aa-407a-bf00-4f5fc28cd73d kubernetes.io/elb.health-check-flag: "on" kubernetes.io/elb.health-check-option: '{"protocol":"TCP","delay":"5","timeout":"10","max_retries":"3"}' kubernetes.io/elb.id: a68106b8-f754-4369-8618-af21e8143dc3 kubernetes.io/elb.lb-algorithm: ROUND_ROBIN kubernetes.io/elb.mark: "0" creationTimestamp: "2023-08-27T19:10:37Z" finalizers: - service.kubernetes.io/load-balancer-cleanup labels: app: nginx name: nginx-elb namespace: default resourceVersion: "119775" uid: 53191067-b9aa-4cc3-84df-ec4d5cbada74spec: allocateLoadBalancerNodePorts: true clusterIP: 10.247.155.17 clusterIPs: - 10.247.155.17 externalTrafficPolicy: Cluster internalTrafficPolicy: Cluster ipFamilies: - IPv4 ipFamilyPolicy: SingleStack loadBalancerIP: 110.238.103.45 ports: - name: cce-service-0 nodePort: 32337 port: 80 protocol: TCP targetPort: 80 selector: app: nginx sessionAffinity: None type: LoadBalancerstatus: loadBalancer: ingress: - ip: 110.238.103.45 - ip: 192.168.34.1902. ELB Ingress & Nginx Ingress
Section titled “2. ELB Ingress & Nginx Ingress”ELB Ingress
Section titled “ELB Ingress”
Access use the domain:

Nginx Ingress
Section titled “Nginx Ingress”Install the nginx-ingress plug-in and create a policy:

Because we didnot have the domain, so we can add the hosts record to verify. we need add the DNS A record if we have a real domain.
[root@ecs-c5e5 ~]# cat /etc/hosts::1 localhost localhost.localdomain localhost6 localhost6.localdomain6127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4127.0.0.1 ecs-c5e5 ecs-c5e5110.238.103.45 www.abc.com110.238.103.45 java.abc.com[root@ecs-c5e5 ~]# curl -I java.abc.comHTTP/1.1 200Date: Mon, 28 Aug 2023 11:41:41 GMTContent-Type: text/html;charset=UTF-8Content-Length: 3084Connection: keep-aliveLast-Modified: Mon, 28 Aug 2023 01:01:15 GMTAccept-Ranges: bytesContent-Language: en-US
[root@ecs-c5e5 ~]# curl www.abc.com<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>html { color-scheme: light dark; }body { width: 35em; margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>
<p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p></body></html>