北京好的网站制作网站建设 主要学是么
k8s入门实战-Service
Service 和 Label
Service 通过一组 Pod 路由通信。Service 是一种抽象,它允许 Pod 死亡并在 Kubernetes 中复制,而不会影响应用程序。在依赖的 Pod (如应用程序中的前端和后端组件)之间进行发现和路由是由Kubernetes Service 处理的。
Service 匹配一组 Pod 是使用 标签(Label)和选择器(Selector), 它们是允许对 Kubernetes 中的对象进行逻辑操作的一种分组原语。标签(Label)是附加在对象上的键/值对,可以以多种方式使用:
 
- 指定用于开发,测试和生产的对象
 - 嵌入版本标签
 - 使用 Label 将对象进行分类

 
执行yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:name: nginxnamespace: dev
spec:replicas: 3selector:matchLabels:run: nginxtemplate:metadata:labels:run: nginxspec:containers:- image: nginx:1.17.1name: nginxports:- containerPort: 80protocol: TCP
 
[root@master ~]# kubectl create -f deploy-nginx.yaml
deployment.apps/nginx created
[root@master ~]# kubectl get pods -n dev
NAME                     READY   STATUS    RESTARTS   AGE
nginx-64777cd554-h6rjh   1/1     Running   0          11s
nginx-64777cd554-sffwm   1/1     Running   0          11s
nginx-64777cd554-zw66q   1/1     Running   0          11s
 
查询每个podIP地址
[root@master ~]# kubectl get pods -n dev -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP            NODE    NOMINATED NODE   READINESS GATES
nginx-64777cd554-h6rjh   1/1     Running   0          117s   10.244.2.17   node2   <none>           <none>
nginx-64777cd554-sffwm   1/1     Running   0          117s   10.244.2.18   node2   <none>           <none>
nginx-64777cd554-zw66q   1/1     Running   0          117s   10.244.1.10   node1   <none>           <none>
 
删除pod,查看新pod Ip地址是否改变
[root@master ~]# kubectl delete pod nginx-64777cd554-h6rjh -n dev
pod "nginx-64777cd554-h6rjh" deleted
[root@master ~]# kubectl get pods -n dev -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP            NODE    NOMINATED NODE   READINESS GATES
nginx-64777cd554-qdjn8   1/1     Running   0          45s     10.244.1.11   node1   <none>           <none>
nginx-64777cd554-sffwm   1/1     Running   0          4m59s   10.244.2.18   node2   <none>           <none>
nginx-64777cd554-zw66q   1/1     Running   0          4m59s   10.244.1.10   node1   <none>           <none>
 
删除pod后,新建pod IP改变
-  
Pod IP在Pod重建时会更改
 -  
Pod IP 只能在集群内使用,外部无法访问
 
Kubernetes 中 Service 是 将运行在一个或一组 Pod 上的网络应用程序公开为网络服务的方法。
Service在生命周期内,IP地址不会改变
创建集群内部可访问的Service
#暴露Service  
[root@master ]# kubectl expose deployment nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev
service/svc-nginx1 exposed 
[root@master ~]# kubectl get svc -n dev
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
svc-nginx1   ClusterIP   10.101.232.58   <none>        80/TCP    5m55s
#访问service IP+端口 
 
这里我无法ping通Service IP
错误:ServiceIP无法Ping通
我的做法尝试重新启动Pod,重新创建一个Service
然后编辑文件
kubectl -n kube-system edit configmap kube-proxy
 
修改这段
  kind: KubeProxyConfigurationmetricsBindAddress: ""mode: "ipvs"nodePortAddresses: null
 
把mode添加为ipvs
重启主机即可
然后查看下新的ServiceIP,访问80端口,成功访问,但是很慢,不报错的话你等个一分钟看看
[root@master ~]# kubectl get pods,svc,deploy -n dev
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-64777cd554-bhs6f   1/1     Running   0          4m39s
pod/nginx-64777cd554-gkjqk   1/1     Running   0          4m39s
pod/nginx-64777cd554-x5szs   1/1     Running   0          4m39sNAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
service/svc-nginx1   ClusterIP   10.108.90.241   <none>        80/TCP    3m7sNAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           4m39s
[root@master ~]# curl 10.108.90.241:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>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 and
working. 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>
 
创建集群外部也可以访问的Service
# 上面创建的Service的type类型为ClusterIP,这个ip地址只用集群内部可访问
# 如果需要创建外部也可以访问的Service,需要修改type为NodePort
[root@master ~]# kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev
service/svc-nginx2 exposed  [root@master ~]# kubectl get svc -n dev
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
svc-nginx1   ClusterIP   10.108.90.241   <none>        80/TCP         9m27s
svc-nginx2   NodePort    10.96.231.201   <none>        80:32400/TCP   18s
 
尝试浏览器访问:
 
删除Service
#删除所有
[root@master ~]# kubectl delete svc -n dev --all
service "svc-nginx1" deleted
service "svc-nginx2" deleted#删除单个
kubectl delete svc [Service名称] -n [名称空间] 
 
配置方式
创建一个svc-nginx.yaml
apiVersion: v1
kind: Service
metadata:name: svc-nginxnamespace: dev
spec:clusterIP: 10.109.179.231ports:- port: 80protocol: TCPtargetPort: 80selector:run: nginxtype: ClusterIP
 
#创建
[root@master ~]# kubectl create -f svc-nginx.yaml
service/svc-nginx created
[root@master ~]# kubectl get svc -n dev
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
svc-nginx   ClusterIP   10.109.179.231   <none>        80/TCP    49s
#删除
[root@master ~]# kubectl delete -f svc-nginx.yaml
service "svc-nginx" deleted
[root@master ~]# kubectl get svc -n dev
No resources found in dev namespace.