该篇文章已经被专栏《从零开始学k8s》收录
上一篇文章:Kubernetes核心技能Service实战
继承我们上一章没讲完的内容!
创建Service:type类型是NodePort
- 1、创建一个 pod 资源
- [root@k8smaster service]# vim pod_nodeport.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- name: my-nginx-nodeport
- spec:
- selector:
- matchLabels:
- run: my-nginx-nodeport
- replicas: 2
- template:
- metadata:
- labels:
- run: my-nginx-nodeport
- spec:
- containers:
- - name: my-nginx-nodeport-container
- image: nginx
- imagePullPolicy: IfNotPresent
- ports:
- - containerPort: 80
- #更新资源清单文件
- [root@k8smaster service]# kubectl apply -f pod_nodeport.yaml
- deployment.apps/my-nginx-nodeport created
- #查看 pod 是否创建成功
- [root@k8smaster service]# kubectl get pods -l run=my-nginx-nodeport
- NAME READY STATUS RESTARTS AGE
- my-nginx-nodeport-5fccbb754b-jdj67 1/1 Running 0 19s
- my-nginx-nodeport-5fccbb754b-w5f8l 1/1 Running 0 19s
-
- 2、创建 service,代理 pod
- [root@xianchaomaster1 ~]# vim service_nodeport.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: my-nginx-nodeport
- labels:
- run: my-nginx-nodeport
- spec:
- type: NodePort
- ports:
- - port: 80
- protocol: TCP
- targetPort: 80
- nodePort: 30380
- selector:
- run: my-nginx-nodeport
-
- #更新资源清单文件
- [root@k8smaster service]# kubectl apply -f service_nodeport.yaml
- service/my-nginx-nodeport created
- #查看刚才创建的 service
- [root@k8smaster service]# kubectl get svc -l run=my-nginx-nodeport
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- my-nginx-nodeport NodePort 10.97.89.147 <none> 80:30380/TCP 111s
- [root@k8smaster service]# kubectl get pods -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED
- my-nginx-nodeport-5fccbb754b-jdj67 1/1 Running 0 9m14s 10.244.1.37 k8snode2 <none>
- my-nginx-nodeport-5fccbb754b-w5f8l 1/1 Running 0 9m14s 10.244.2.38 k8snode <none>
- [root@k8smaster service]# kubectl describe svc my-nginx-nodeport
- Name: my-nginx-nodeport
- Namespace: default
- Labels: run=my-nginx-nodeport
- Annotations: Selector: run=my-nginx-nodeport
- Type: NodePort
- IP: 10.97.89.147
- Port: <unset> 80/TCP
- TargetPort: 80/TCP
- NodePort: <unset> 30380/TCP
- Endpoints: 10.244.1.37:80,10.244.2.38:80
- Session Affinity: None
- External Traffic Policy: Cluster
- Events: <none>
- #ip一样的
- #访问 service
- [root@k8smaster service]# curl 10.97.89.147
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- 注意:
- 10.100.156.7 是 k8s 集群内部的 service ip 地址,只能在 k8s 集群内部访问,在集群外无法访问。
- 都是80端口也没事,不冲突,会有新的ip加入到防火墙规则。
- #在集群外访问 service
- [root@k8smaster service]# curl 192.168.11.139:30380
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- #在浏览器访问 service
复制代码
服务哀求走向
Client-node ip:30380->service ip:80->pod ip:container port
Client->192.168.11.139:30380->10.97.89.147:80->pod ip:80
创建Service:type类型是ExternalName
映射外部服务案例
k8s 集群引用外部的 mysql 数据库
- node2安装mysql
- [root@k8snode2 ~]# yum install mariadb-server.x86_64 -y
- [root@k8snode2 ~]# systemctl start mariadb
- [root@k8snode2 ~]# systemctl enable mariadb
- [root@k8smaster ~]# mkdir mysql
- [root@k8smaster ~]# cd mysql/
- [root@k8smaster mysql]# vim mysql_service.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: mysql
- spec:
- type: ClusterIP
- ports:
- - port: 3306
- [root@k8smaster mysql]# kubectl apply -f mysql_service.yaml
- service/mysql created
- [root@k8smaster mysql]# kubectl get svc | grep mysql
- mysql ClusterIP 10.103.7.164 <none> 3306/TCP 4s
- [root@k8smaster mysql]# kubectl describe svc mysql
- Name: mysql
- Namespace: default
- Labels: <none>
- Annotations: Selector: <none>
- Type: ClusterIP
- IP: 10.103.7.164
- Port: <unset> 3306/TCP
- TargetPort: 3306/TCP
- Endpoints: <none> #还没有 endpoint
- Session Affinity: None
- Events: <none>
- [root@k8smaster mysql]# vim mysql_endpoint.yaml endpoint和svc的名字保持一致
- apiVersion: v1
- kind: Endpoints
- metadata:
- name: mysql
- subsets:
- - addresses:
- - ip: 192.168.40.182
- ports:
- - port: 3306
- [root@k8smaster mysql]# kubectl apply -f mysql_endpoint.yaml
- endpoints/mysql created
- [root@k8smaster mysql]# kubectl describe svc mysql
- Name: mysql
- Namespace: default
- Labels: <none>
- Annotations: Selector: <none>
- Type: ClusterIP
- IP: 10.103.7.164
- Port: <unset> 3306/TCP
- TargetPort: 3306/TCP
- Endpoints: 192.168.40.182:3306 #这就是定义的外部数据库
- Session Affinity: None
- Events: <none>
- mysql.default.svc.cluster.local #这就是它的全局域名
复制代码 上面设置就是将外部 IP 地址和服务引入到 k8s 集群内部(其他节点),由 service 作为一个代理来到达可以或许访问外部服务的目的。
写在最后
创作不易,假如觉得内容对你有帮助,贫苦给个三连关注支持一下我!假如有错误,请在批评区指出,我会实时更改!
目前正在更新的系列:从零开始学k8s
感谢各位的观看,文章掺杂个人理解,如有错误请接洽我指出~
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |