K8S 创建 Service
1、创建 deploymentkubectl run nginx --image=nginx --replicas=22、创建 servicekubectl expose deployment nginx --type=ClusterIP --target-port=80 --port=80expose 创建 service--type=ClusterIP 指定 service 类型--targe
Service是k8s中的一个重要概念,可以看作是一组同类Pod对外的访问接口,可以方便的实现负载均衡和服务自动发现
一、通过命令行创建
1、创建 deployment
kubectl run nginx --image=nginx --replicas=2
2、创建 service
kubectl expose deployment nginx --type=ClusterIP --port=80 --target-port=80
expose 创建 service
--type=ClusterIP 指定 service 类型
--port=80 指定 service 端口
--target-port=80 指定 pod 端口
3、查看 service
[root@master ~]# kubectl get service 或者 kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h42m
nginx ClusterIP 10.98.107.137 <none> 80/TCP 13m
二、通过 yaml 资源清单文件创建 CluserIP
1、创建文件
vim service-nginx.yaml
# 文件内容
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-app-svc
spec:
type: ClusterIP
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
app: nginx
2、创建 service
kubectl apply -f service-nginx.yaml
3、查看 deployment
[root@master yaml]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 2/2 2 2 22s
4、查看 service
[root@master yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h4m
nginx-app-svc ClusterIP 10.96.22.84 <none> 80/TCP 16s
5、查看 endpoints
[root@master yaml]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.2.100:6443 5h18m
nginx-app-svc 10.244.1.8:80,10.244.2.9:80 14m
6、通过 service 直接访问
[root@master yaml]# curl http://10.96.22.84
.....
<h1>Welcome to nginx!</h1>
......
<html>
7、查看 ipvs
[root@master ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.96.22.84:80 rr
-> 10.244.1.8:80 Masq 1 0 0
-> 10.244.2.9:80 Masq 1 0 0
8、关闭 flannel 网卡发送校验
当 Service 类型为 ClusterIP 时,会出现集群内访问 ClusterIP 非常慢,原因是 flannel 的网络设置将发送端的 checksum 打开了,而实际应该关闭,从而让物理网卡校验,(所有主机都要操作)
# 临时关闭
ethtool -K flannel.1 tx-checksum-ip-generic off
# 创建服务关闭
cat <<EOF > /etc/systemd/system/k8s-flannel-tx-checksum-off.service
[Unit]
Description=Turn off checksum offload on flannel.1
After=sys-devices-virtual-net-flannel.1.device
[Install]
WantedBy=sys-devices-virtual-net-flannel.1.device
[Service]
Type=oneshot
ExecStart=/sbin/ethtool -K flannel.1 tx-checksum-ip-generic off
EOF
# 开机启动
systemctl start k8s-flannel-tx-checksum-off
systemctl enable k8s-flannel-tx-checksum-off
出现如下表示关闭成功
ethtool -k flannel.1 | grep checksum
# 结果
rx-checksumming: on
tx-checksumming: off # 当前为 off
tx-checksum-ipv4: off [fixed]
tx-checksum-ip-generic: off # 当前为 off
tx-checksum-ipv6: off [fixed]
tx-checksum-fcoe-crc: off [fixed]
tx-checksum-sctp: off [fixed]
三、负载均衡
1、查看 pod
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app-85ff79dd56-5t2ns 1/1 Running 1 4d3h
nginx-app-85ff79dd56-bp6x5 1/1 Running 1 4d3h
2、修改 2 个 pod 中的 nginx 文件
# pod 1
[root@master ~]# kubectl exec -it nginx-app-85ff79dd56-5t2ns /bin/bash
root@nginx-app-85ff79dd56-5t2ns:/# cd /usr/share/nginx/html
root@nginx-app-85ff79dd56-5t2ns:/usr/share/nginx/html# echo 'Welcome to nginx-pod!' > index.html
# pod 2
[root@master ~]# kubectl exec -it nginx-app-85ff79dd56-bp6x5 /bin/bash
root@nginx-app-85ff79dd56-bp6x5:/# cd /usr/share/nginx/html
root@nginx-app-85ff79dd56-bp6x5:/usr/share/nginx/html# echo 'Welcome to nginx-pod2!' > index.html
3、测试成功
[root@master ~]# curl http://10.96.22.84
Welcome to nginx-pod!
[root@master ~]# curl http://10.96.22.84
Welcome to nginx-pod2!
四、通过 yaml 资源清单文件创建 NodePort
1、创建
vim service-nodeport-nginx.yaml
# 内容
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-app-svc
spec:
type: NodePort
ports:
- protocol: TCP
nodePort: 30003
port: 80
targetPort: 80
selector:
app: nginx
2、创建 service
kubectl apply -f service-nodeport-nginx.yaml
3、查看 svc
[root@master yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d8h
nginx-app-svc NodePort 10.97.51.113 <none> 80:30003/TCP 24s
4、现在可以通过 节点主机的物理IP 加端口就可以访问了
[root@master yaml]# curl http://192.168.2.100:30003
<!DOCTYPE html>
....
<title>Welcome to nginx!</title>
....
</html>
# 其它节点主机也都可以访问
curl http://192.168.2.101:30003
curl http://192.168.2.102:30003
更多推荐
所有评论(0)