k8s常用资源之svc资源(五)

it2024-02-19  66

k8s常用资源之svc资源

1.k8s资源常见操作

创建一个资源 kubectl create -f xxx.yaml 查看一个资源 kubectl get pod | rc 查看一个资源的详细过程 kubectl describe pos pod_name 删除一个资源 kubectl delete pod pod_name kubectl delete -f xxx.yaml 编辑一个资源的配置文件 kubectl edit pod pod_name

2.svc资源

svc资源可以调用kube-proxy组件,将容器进行端口映射,使外网用户可以访问到容器中的资源

svc利用的是vip

k8sip类型:nodeip(节点ip)、podip(pod资源的ip)、vip(虚拟ip)

之前都是一个pod映射一个端口,如果有多个pod,这时就要采用负载均衡,但是负载均衡里面的地址不能写死,因为有时可能pod会死,pod重新上来一个后端口和ip都会发生改变,这是就要采用k8s的svc资源和虚拟ip来解决此问题,写死虚拟ip即可,当pod死了之后,svc会根据设置的标签去选择pod资源,并将其加入负载均衡集群,不需要人为手动干预

svc资源创建的容器可以使用任意节点的ip+端口即可访问

svc资源指定的端口默认从30000开始,如果要修改请编辑/etc/kubenetes/apiserver配置文件,默认地址为10.254.0.0/16

svc默认采用iptables进行负载均衡使用iptables -t nat -nL

svc默认类型是ClusterIP,使用vip,外界无法访问,常用于数据库,因为数据库不需要被外界访问

2.1.编写svc资源文件

准备svc资源yaml文件 [root@k8s-master svc]# vim k8s_svc.yaml apiVersion: v1 //api版本 kind: Service //资源类型 metadata: //资源属性 name: myweb //资源的名称 spec: type: NodePort //src资源的类型,nodeport表示端口映射 ports: - port: 80 //虚拟ip的端口 nodePort: 30000 //节点映射后的端口 targetPort: 80 //pod资源的端口 selector: app: myweb2 //根据那个标签去控制pod

2.2.创建svc资源

1)创建svc资源 [root@k8s-master svc]# kubectl create -f k8s_svc.yaml service "myweb" created 2)查看创建的svc资源 [root@k8s-master svc]# kubectl get all -o wide NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR rc/nginxrc 5 5 5 23h myweb 192.168.81.240/k8s/nginx:1.13 app=myweb NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR svc/kubernetes 10.254.0.1 <none> 443/TCP 9d <none> svc/myweb 10.254.137.46 <nodes> 80:30000/TCP 48s app=myweb2 NAME READY STATUS RESTARTS AGE IP NODE po/nginxrc-35qbl 1/1 Running 0 23h 172.16.16.3 192.168.81.230 po/nginxrc-49zvz 1/1 Running 0 23h 172.16.56.2 192.168.81.220 po/nginxrc-82bcf 1/1 Running 0 23h 172.16.56.3 192.168.81.220 po/nginxrc-kjrjd 1/1 Running 0 23h 172.16.56.4 192.168.81.220 po/nginxrc-kwbwp 1/1 Running 0 23h 172.16.16.2 192.168.81.230 3)查看svc的详细信息,发现没有任何节点 [root@k8s-master svc]# kubectl describe svc myweb Name: myweb Namespace: default Labels: <none> Selector: app=myweb2 Type: NodePort IP: 10.254.137.46 Port: <unset> 80/TCP NodePort: <unset> 30000/TCP Endpoints: <none> Session Affinity: None No events. 4)没有节点配置是由于svc的标签和rc资源的标签不一致导致的,我们将svc的标签修改为rc的标签即可 [root@k8s-master svc]# kubectl edit svc myweb app: myweb 22行 service "myweb" edited 5)再次查看svc资源的过程 [root@k8s-master svc]# kubectl describe svc myweb Name: myweb Namespace: default Labels: <none> Selector: app=myweb Type: NodePort IP: 10.254.137.46 Port: <unset> 80/TCP NodePort: <unset> 30000/TCP Endpoints: 172.16.16.2:80,172.16.16.3:80,172.16.56.2:80 + 2 more... Session Affinity: None No events. 6)访问即可

2.3.访问资源

[root@k8s-master svc]# curl -I 192.168.81.230:30000 HTTP/1.1 200 OK Server: nginx/1.13.12 Date: Mon, 20 Jul 2020 15:46:06 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT Connection: keep-alive ETag: "5acb8e45-264" Accept-Ranges: bytes [root@k8s-master svc]# curl -I 192.168.81.220:30000 HTTP/1.1 200 OK Server: nginx/1.13.12 Date: Mon, 20 Jul 2020 15:46:09 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT Connection: keep-alive ETag: "5acb8e45-264" Accept-Ranges: bytes

2.5.修改svc端口范围

1)修改端口范围并重启 [root@k8s-master svc]# vim /etc/kubernetes/apiserver KUBE_API_ARGS="--service-node-port-range=3000-50000" [root@k8s-master svc]# systemctl restart kube-apiserver.service 2)创建一个svc资源,设置node端口为3000 [root@k8s-master svc]# vim k8s_svc2.yaml apiServer: v1 kind: Service metadata: name: myweb2 spec: type: NodePort ports: - port: 80 nodePort: 3000 targetPort: 80 selector: app: myweb2 [root@k8s-master svc]# kubectl create -f k8s_svc2.yaml service "myweb2" created 3)修改svc标签,可以一次性写好,一次性写好可以省略此步操作 [root@k8s-master svc]# kubectl edit svc myweb2 app: myweb 22行 service "myweb2" edited 4)再次查看节点 [root@k8s-master svc]# kubectl describe svc myweb2 Name: myweb2 Namespace: default Labels: <none> Selector: app=myweb Type: NodePort IP: 10.254.250.66 Port: <unset> 80/TCP NodePort: <unset> 3000/TCP Endpoints: 172.16.16.2:80,172.16.16.3:80,172.16.56.2:80 + 2 more... Session Affinity: None No events. 5)访问 [root@k8s-master svc]# curl -I 192.168.81.220:3000 HTTP/1.1 200 OK [root@k8s-master svc]# curl -I 192.168.81.230:3000 HTTP/1.1 200 OK

iptables负载均衡规则

4.6.删除svc

[root@k8s-master svc]# kubectl delete svc myweb service "myweb" deleted
最新回复(0)