fabric8 kubernetes的nodeAffinity实现

这两天在研究如何在启动部署服务的时候指定特定的节点机器,看了很多文章,先列几篇好的

deployment上指定node
http://blog.csdn.net/stonexmx/article/details/73790816

官网上assign pod到node
https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#step-two-add-a-nodeselector-field-to-your-pod-configuration

nodeAffinity的一些解读
http://blog.csdn.net/yevvzi/article/details/54585686

指定单/多节点
https://segmentfault.com/a/1190000004470355

总结说来,指定单节点的场景一般比较少,多是多节点指定,所以对label的要求比较高。而现有系统(公司产品)里面对节点没有很好的label机制,导致很难通过算法很准的找到要目标机器。

在这种情况下,通过NodeAffinity中label键值对单key对多value的特性,可以通过K8s自带的“kubernetes.io/hostname”label来直接指定多个节点机。然而尝试了kubernetes-model-1.1.0.jar以及kubernetes-client-2.5.6.jar两个jar包之后,依然没有办法添加

apiVersion: apps/v1beta1 # for versions before 1.6.0 use extensions/v1beta1
kind: Deployment
metadata:
  name: web-server
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: web-store
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - store
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: web-app

像这里想在spec下面添加affinity,就没有对应的方法

之前误把kind为pod教程的方法用在了deployment上:

apiVersion: v1  
kind: Pod  
metadata:  
  name: with-node-affinity  
  annotations:  
    scheduler.alpha.kubernetes.io/affinity: >  
      {  
        "nodeAffinity": {  
          "requiredDuringSchedulingIgnoredDuringExecution": {  
            "nodeSelectorTerms": [  
              {  
                "matchExpressions": [  
                  {  
                    "key": "kubernetes.io/e2e-az-name",  
                    "operator": "In",  
                    "values": ["e2e-az1", "e2e-az2"]  
                  }  
                ]  
              }  
            ]  
          }  
        }  
      }  
    another-annotation-key: another-annotation-value  
spec:  
  containers:  
  - name: with-node-affinity  
    image: gcr.io/google_containers/pause:2.0 

通过不断地套接pojo来拼出一个map的value来对应值为scheduler.alpha.kubernetes.io/affinity的key。这里其实做的很有问题,虽然值传进去了,但是范例中的“>”不知为何物,而且通过-o yaml出来的值多了副单引号,而-o json出来的配置因为又一次的json转换变成了带了很多“/”的样子

//yaml的output
annotations:
      deployment.kubernetes.io/revision: "1"
      scheduler.alpha.kubernetes.io/affinity: '{"nodeAffinity":{"requiredDuringSchedulingIgnoredDuringExecution":{"nodeSelectorTerms":{"matchExpressions":[{"affinityLabelExpressions":[{"key":"kubernetes.io/hostname","operator":"In","values":["10.1.235.165"]}]}]}}}}'


//json的output
"annotations": {
                    "deployment.kubernetes.io/revision": "1",
                    "scheduler.alpha.kubernetes.io/affinity": "{\"nodeAffinity\":{\"requiredDuringSchedulingIgnoredDuringExecution\":{\"nodeSelectorTerms\":{\"matchExpressions\":[{\"affinityLabelExpressions\":[{\"key\":\"kubernetes.io/hostname\",\"operator\":\"In\",\"values\":[\"10.1.235.165\"]}]}]}}}}"
                }

到底能不能用注解的方式对deployment中的pod进行指定,需要大神来尝试,但是使用io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder来java拼接貌似是不行的

最后还是通过指定机器之后先打标签然后贴nodeSelector来实现

最后贴几个地址
Github
https://github.com/fabric8io/kubernetes-client
https://github.com/fabric8io/kubernetes-model

jar包下载
http://mvnrepository.com/artifact/io.fabric8/kubernetes-model
http://mvnrepository.com/artifact/io.fabric8/kubernetes-client

一些fabric8的使用例子
https://github.com/fabric8io/kubernetes-client/tree/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples

你可能感兴趣的:(Kubernetes)