集群内访问ipquery

目标: 通过apply创建了 ipquery 对应的deployment,它具有4个副本。通过get pods 可以查询4个副本的状态。 同一个命名空间的其他Pods通过 ipquery-v1:9999 访问ipquery服务

(1) 创建deployment声明文件 ipquery-service-clusterip.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ipquery-v1
  labels:
    app: ipquery-v1
spec:
  replicas: 4
  selector:
    matchLabels:
      app: ipquery-v1
  template:
    metadata:
      labels:
        app: ipquery-v1
    spec:
      containers:
      - name: ipquery-k8s
        image: lorahz/ipquery:1.10
---
apiVersion: v1
kind: Service
metadata:
  name: ipquery-v1
  labels:
    app: ipquery-v1
spec:
  selector:
    app: ipquery-v1
  ports:
  - name: ipquery-v1
    protocol: TCP
    port: 9999
    targetPort: 8888
  type: ClusterIP

(2) 创建deployment声明文件

//创建deployment
[root@t1 ipquery]# kubectl apply -f ipquery-service-clusterip.yaml 
deployment.apps/ipquery-v1 created
service/ipquery-v1 created

[root@t1 ipquery]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
ipquery-v1   ClusterIP   10.96.182.252   <none>        9999/TCP   27s

(3) 集群内访问

临时启动一个容器
[root@t1 ipquery]kubectl run -i --tty centos --image=centos --restart=Never

[root@centos /]# curl ipquery-v1:9999   
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"25a86ed3-cc47-4f7e-ae0a-3b0eaeb0dde2"}
[root@centos /]# curl ipquery-v1:9999
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"b88aeebe-222e-40bb-949e-0610ff451a83"}
[root@centos /]# curl ipquery-v1:9999
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"25a86ed3-cc47-4f7e-ae0a-3b0eaeb0dde2"}

删除临时容器
kubectl delete pod centos 

外网访问ipquery

目标: 通过apply创建了 ipquery 对应的deployment,它具有4个副本。通过get pods 可以查询4个副本的状态。 并且通过IP地址访问 ipquery

http://[任意节点的IP]:31255/

(1) 创建deployment声明文件

ipquery-service-nodeport.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ipquery-v1
  labels:
    app: ipquery-v1
spec:
  replicas: 4
  selector:
    matchLabels:
      app: ipquery-v1
  template:
    metadata:
      labels:
        app: ipquery-v1
    spec:
      containers:
      - name: ipquery-k8s
        image: lorahz/ipquery:1.10
---
apiVersion: v1
kind: Service
metadata:
  name: ipquery-v1
  labels:
    app: ipquery-v1
spec:
  selector:
    app: ipquery-v1
  ports:
  - name: ipquery-v1
    protocol: TCP
    port: 9999
    targetPort: 8888
  type: NodePort

(2) 使用kubctl apply 或者 kubctl create创建deployment


//创建deployment
[root@t1 ipquery]# kubectl apply -f ipquery-service-nodeport.yaml 
deployment.apps/ipquery-v1 created
service/ipquery-v1 created

[root@t1 ipquery]# kubectl get svc
NAME         TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
ipquery-v1   NodePort   10.96.208.29   <none>        9999:31255/TCP   16s

(3) 如何访问Pod

查询Pod对应IP地址,通过curl命名访问

[root@t1 ipquery]# curl http://[任意节点的IP]:31255/
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"49dba3b1-41b0-4cf1-933c-5b023ca31c72"}
[root@t1 ipquery]# curl http://[任意节点的IP]:31255/
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"01fb4154-4984-4350-b0b7-c6bd83de68a1"}
[root@t1 ipquery]# curl http://[任意节点的IP]:31255/
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"01fb4154-4984-4350-b0b7-c6bd83de68a1"}
[root@t1 ipquery]# curl http://[任意节点的IP]:31255/
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"75f74058-1dc3-44ff-b500-84f494a76e05"}

Service和pod关系图

效果图