duanhe8280 2018-06-25 05:05
浏览 126
已采纳

通过操作员框架对K8s Pod进行负载平衡

I built a simple operator, by tweaking the memcached example. The only major difference is that I need two docker images in my pods. Got the deployment running. My test.yaml used to deploy with kubectl.

apiVersion: "cache.example.com/v1alpha1"
kind: "Memcached"
metadata:
  name: "solar-demo"
spec:
  size: 3
  group: cache.example.com
  names:
    kind: Memcached
    listKind: MemcachedList
    plural: solar-demos
    singular: solar-demo
  scope: Namespaced
  version: v1alpha1

I am still missing one piece though - load-balancing part. Currently, under Docker we are using the nginx image working as a reverse-proxy configured as:

upstream api_microservice {
  server api:3000;
}
upstream solar-svc_microservice {
  server solar-svc:3001;
}
server {
  listen $NGINX_PORT default;

  location /city {
    proxy_pass http://api_microservice;
  }

  location /solar {
    proxy_pass http://solar-svc_microservice;
  }

  root /html;
  location / {
    try_files /$uri /$uri/index.html /$uri.html /index.html=404;
  }
}

I want my cluster to expose the port 8080 and forward to ports 3000 and 3001 to my images running inside Pods.

My deployment:

dep := &appsv1.Deployment{
    TypeMeta: metav1.TypeMeta{
        APIVersion: "apps/v1",
        Kind:       "Deployment",
    },
    ObjectMeta: metav1.ObjectMeta{
        Name:      m.Name,
        Namespace: m.Namespace,
    },
    Spec: appsv1.DeploymentSpec{
        Replicas: &replicas,
        Selector: &metav1.LabelSelector{
            MatchLabels: ls,
        },
        Template: v1.PodTemplateSpec{
            ObjectMeta: metav1.ObjectMeta{
                Labels: ls,
            },
            Spec: v1.PodSpec{
                Containers: []v1.Container{
                    {
                        Image:   "shmukler/docker_solar-svc",
                        Name:    "solar-svc",
                        Command: []string{"npm", "run", "start-solar-svc"},
                        Ports: []v1.ContainerPort{{
                            ContainerPort: 3001,
                            Name:          "solar-svc",
                        }},
                    },
                    {
                        Image:   "shmukler/docker_solar-api",
                        Name:    "api",
                        Command: []string{"npm", "run", "start-api"},
                        Ports: []v1.ContainerPort{{
                            ContainerPort: 3000,
                            Name:          "solar-api",
                        }},
                    },
                },
            },
        },
    }

What do I need to add have ingress or something running in front of my pods?

Thank you

展开全部

  • 写回答

1条回答 默认 最新

  • doukuo9116 2018-06-26 07:01
    关注

    What do I need to add have ingress or something running in front of my pods?

    Yes, Ingress is designed for that kind of tasks.

    Ingress has a path-based routing, which will be able to set up the same configuration as you mentioned in your example with Nginx. Moreover, one of the most popular implementations of Ingress is Nginx as a proxy.

    Ingress is basically a set of rules that allows traffic, otherwise dropped or forwarded elsewhere, to reach the cluster services.
    Here is an example of an Ingress configuration:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: my-app
    spec:
      rules:
      - host: '' # Empty value means ‘any host’
        http:
          paths:
          - path: /city
            backend:
              serviceName: myapp
              servicePort: 3000
          - path: /solar
            backend:
              serviceName: myapp
              servicePort: 3001
    

    Also, because a Pod is not a static thing, you should create a Service object which will be a static entry point of your application for Ingress.

    Here is an example of the Service:

    kind: Service
    apiVersion: v1
    metadata:
      name: myapp
    spec:
      selector:
        app: "NAME_OF_YOUR_DEPLOYMENT"
      ports:
      - name: city
        protocol: TCP
        port: 3000
        targetPort: 3000
      - name: solar
        protocol: TCP
        port: 3001
        targetPort: 3001
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 图片是GET请求,后面是图片的id,Glide如何缓存?
  • ¥15 现代密码学问题求解答
  • ¥15 关于C#与NICAN类库通信的问题
  • ¥15 cytoscape问题
  • ¥15 putty实现机器学习猫狗识别
  • ¥15 STS/eclipse导入gradle项目时报错如下
  • ¥15 centos7.6进不去系统,卡在数字7界面
  • ¥15 Tensorflow采用interpreter.allocate_tensors()分配内存出现ValueError: vector too long报错
  • ¥15 使用CGenFF在线生成血红素辅基拓扑结构遇到问题
  • ¥20 matlab代码实现可达矩阵形成骨骼矩阵
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部