Kubernetes,k8s如何制作服务网址?

时间:2018-05-23 06:53:55

标签: kubernetes kubernetes-ingress

我正在学习k8s。我的问题是如何让k8s获得服务网址作为minikube命令“minikube get service xxx --url”吗? 我问的原因是因为当pod被关闭和启动/创建/再次启动时,无需通过访问服务URL来更改URL。而 我将pod部署为NodePort,我可以使用主机IP和端口访问pod,但如果再次重新创建/创建,则端口会更改。

我的情况说明如下:我有

one master(172.16.100.91) and 
one node(hostname node3, 172.16.100.96) 

我创建pod和服务如下,helllocomm部署为NodePort,helloext部署为ClusterIP。 hellocomm和helloext都是 spring boot你好世界的应用。

docker build -t jshenmaster2/hellocomm:0.0.2 .
kubectl run hellocomm --image=jshenmaster2/hellocomm:0.0.2 --port=8080
kubectl expose deployment hellocomm --type NodePort

docker build -t jshenmaster2/helloext:0.0.1 .
kubectl run helloext --image=jshenmaster2/helloext:0.0.1 --port=8080
kubectl expose deployment helloext --type ClusterIP


[root@master2 shell]# kubectl get service -o wide
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
hellocomm    NodePort    10.108.175.143   <none>        8080:31666/TCP   8s        run=hellocomm
helloext     ClusterIP   10.102.5.44      <none>        8080/TCP         2m        run=helloext

[root@master2 hello]# kubectl get pods -o wide
NAME                         READY     STATUS    RESTARTS   AGE       IP              NODE
hellocomm-54584f59c5-7nxp4   1/1       Running   0          18m       192.168.136.2   node3
helloext-c455859cc-5zz4s     1/1       Running   0          21m       192.168.136.1   node3

在上面,我的pod部署在node3(172.16.100.96),所以我可以通过172.16.100.96:31666/hello访问hellocomm, 在这种情况下,人们可以很容易地看到,当node3关闭时,会创建/启动新的pod,端口也会更改。 这样我的客户就失去了联系。我不想要这个解决方案。

我目前的问题是,将helloext部署为ClusteriP,它也是一项服务,如上所示。这是否意味着ClusterIP 10.102.5.44和端口8080将是服务URL,http://10.102.5.44:8080/hello

我是否需要再次通过yaml文件创建服务?与命令对yaml文件创建的服务有什么区别?怎么写 如果我必须通过yaml创建服务,请关注yaml文件?

下面是我需要填写的yaml定义模板,如何填写?

apiVersion: v1
kind: Service
matadata:
  name: string         helloext      
  namespace: string    default
  labels:              
    - name: string     helloext
  annotations:
    - name: string     hello world   
spec:
  selector: []            ?
  type: string            ?
  clusterIP: string       anything I could give?  
  sessionAffinity: string  ? (yes or no) 
  ports:
  - name: string          helloext  
    protocol: string      tcp  
    port: int             8081? (port used by host machine)
    targetPort: int       8080? (spring boot uses 8080)
    nodePort: int         ? 
  status:                 since I am not using loadBalancer in deploymennt, I could forget this.  
    loadBalancer:
      ingress:
        ip: string
        hostname: string

2 个答案:

答案 0 :(得分:2)

NodePort,顾名思义,直接在节点上打开一个端口(实际上在群集中的所有节点上),以便您可以访问您的服务。默认情况下它是随机的 - 这就是为什么当一个pod死掉时,它会为你生成一个新的。但是,您也可以指定端口(第3段here) - 即使重新创建了pod,您也可以在同一端口上访问。

clusterIP只能在群集内访问,因为它是私有IP。这意味着,在默认情况下,您可以从群集内的另一个容器/节点访问此服务。您可以exec / ssh进入任何正在运行的容器/节点并进行试用。

Yaml文件可以是版本控制,记录,模板化(Helm)等。

检查https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#servicespec-v1-core以了解每个字段的详细信息。

修改: 有关服务的更详细信息,请访问:https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0

答案 1 :(得分:1)

如何创建入口并将其指向服务以在群集外部访问它?