1. 首页 > Linux教程 > 正文

Linux教程FG436-Kubernetes Service

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

from PG视频:www.itpux.com

本文档介绍Kubernetes Service的创建和管理方法。

Part01-Service类型

1.1 Service类型概述

# Kubernetes Service类型
[root@k8s-master ~]# cat > /root/k8s-service-types.txt << 'EOF' Kubernetes Service类型 ===================== 1.学习交流加群风哥微信: itpux-com ClusterIP - 默认类型 - 集群内部访问 - 虚拟IP地址 2. NodePort - 在每个节点开放端口 - 外部可访问 - 端口范围: 30000-32767 3. LoadBalancer - 云厂商负载均衡器 - 自动分配外部IP - 需要云平台支持 4. ExternalName - 映射外部服务 - DNS CNAME记录 - 无需代理 EOF

Part02-ClusterIP Service

2.1 创建ClusterIP Service

# 创建ClusterIP Service YAML
[root@k8s-master ~]# cat > fgedu-service-clusterip.yaml << 'EOF' apiVersion: v1 kind: Service metadata: name: fgedu-web labels: app: fgedu-web spec: type: ClusterIP selector: app: fgedu-web ports: - name: http port: 80 targetPort: 80 protocol: TCP EOF # 创建Service [root@k8s-master ~]# kubectl apply -f fgedu-service-clusterip.yaml service/fgedu-web created # 查看Service [root@k8s-master ~]# kubectl get svc fgedu-web NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE fgedu-web ClusterIP 10.96.100.100 80/TCP 10s

# 查看Service详情
[root@k8s-master ~]# kubectl describe svc fgedu-web
Name: fgedu-web
Namespace: default
Labels: app=fgedu-web
Annotations:
Selector: app=fgedu-web
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.96.100.100
IPs: 10.96.100.100
Port: http 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.10:80,10.244.2.10:80,10.244.2.11:80
Session Affinity: None
Events:

# 测试Service访问
[root@k8s-master ~]# kubectl run test –image=busybox –rm -it — wget -qO- http://fgedu-web
If you don’t see a command prompt, try pressing enter.

Welcome to nginx!

If you see this page,更多视频教程www.fgedu.net.cn the nginx web server is successfully installed and
working. Further configuration is required.

Session ended, delete resource: pod “test”

Part03-NodePort Service

3.1 创建NodePort Service

# 创建NodePort Service YAML
[root@k8s-master ~]# cat > fgedu-service-nodeport.yaml << 'EOF' apiVersion: v1 kind: Service metadata: name: fgedu-web-nodeport labels: app: fgedu-web spec: type: NodePort selector: app: fgedu-web ports: - name: http port: 80 targetPort: 80 nodePort: 30080 protocol: TCP EOF # 创建Service [root@k8s-master ~]# kubectl apply -f fgedu-service-nodeport.yaml service/fgedu-web-nodeport created # 查看Service [root@k8s-master ~]# kubectl get svc fgedu-web-nodeport NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE fgedu-web-nodeport NodePort 10.96.100.101 80:30080/TCP 10s

# 测试NodePort访问
[root@k8s-master ~]# curl http://192.168.1.100:30080

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

Part04-LoadBalancer Service

4.1 创建LoadBalancer Service

# 创建LoadBalancer Service YAML
[root@k8s-master ~]# cat > fgedu-service-lb.yaml << 'EOF' apiVersion: v1 kind: Service metadata: name: fgedu-web-lb labels: app: fgedu-web spec: type: LoadBalancer selector: app: fgedu-web ports: - name: http port: 80 targetPort: 80 protocol: TCP EOF # 创建Service [root@k8s-master ~]# kubectl apply -f fgedu-service-lb.yaml service/fgedu-web-lb created # 查看Service [root@k8s-master ~]# kubectl get svc fgedu-web-lb NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE fgedu-web-lb LoadBalancer 10.96.100.102 192.168.1.200 80:31234/TCP 10s # 测试LoadBalancer访问 [root@k8s-master ~]# curl http://192.168.1.200

Welcome to nginx!

If you see this page, the nginx web s

风哥提示:

erver is successfully installed and
working. Further configuration is required.

风哥针对Service管理建议:

  • 根据需求选择Service类型
  • 使用标签选择器关联Pod
  • 配置健康检查
  • 使用命名规范管理Service
  • 配置会话保持策略

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息