1. 程式人生 > >kubernetes/k8s接合阿里雲LoadBalancer/負載均衡

kubernetes/k8s接合阿里雲LoadBalancer/負載均衡

在生產環境中kubernetes如果僅提供http或https服務,則可以使用nginx-ingress或者traefik之類的七層負載均衡軟體。 當然,也可以通過NodePort的方式暴露http或tcp服務。如果不想直接使用NodePort暴露出的那個埠號,而伺服器又是執行在阿里雲上,那麼可以使用阿里雲提供的Kubernetes Cloud Controller Manager來對外暴露TCP服務或http/https服務。

前提

1.在阿里雲上購買了幾臺ecs

2.在阿里雲的ecs上搭建好了一個kubernetes叢集,這裡以kubernetes1.9.0為例

3.在阿里雲上購買一個公網的負載均衡

就可以開始配置阿里雲的負載均衡與kubernetes整合了。以下是配置步驟:

  1. 先獲取每臺ecs對應的regionId和ecsId,可以使用命令echo `curl -s http://100.100.100.200/latest/meta-data/region-id`.`curl -s http://100.100.100.200/latest/meta-data/instance-id`獲取。
  2. 把apiserver, controller-manager和kubelet都新增啟動引數 --cloud-provider=external,並且在kubelet中新增--provider-id=上面得到的<REGION_ID>.<ECS_ID>
  3. 先建立一個secret用來儲存阿里的Access Key Id和Access Key Secret(這兩項值在阿里的控制檯可以查到),編寫一個alicloud-secret.yaml檔案,如下
    apiVersion: v1
    kind: Secret
    metadata:
      name: alicloud-config
      namespace: kube-system
    data:
      # insert your base64 encoded AliCloud access id and key here, ensure there's no trailing newline:
      # to base64 encode your token run:
      #      echo -n "abc123abc123doaccesstoken" | base64
      access-key-id: "<ACCESS_KEY_ID>"
      access-key-secret: "<ACCESS_KEY_SECRET>"
    這裡的id和secret就是把從阿里控制檯中得到的用base64編碼,然後替換到上面檔案中就可以了。然後用kubectl apply -f alicloud-secret.yaml建立對應secret.
  4. 接下來再編寫alicloud-controller-manager.yaml檔案,如下
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: alicloud-controller-manager
      namespace: kube-system
    spec:
      replicas: 1
      revisionHistoryLimit: 2
      template:
        metadata:
          labels:
            app: alicloud-controller-manager
        spec:
          dnsPolicy: Default
          tolerations:
            # this taint is set by all kubelets running `--cloud-provider=external`
            - key: "node.cloudprovider.kubernetes.io/uninitialized"
              value: "true"
              effect: "NoSchedule"
          containers:
          - image: registry.cn-hangzhou.aliyuncs.com/kube-test/alicloud-controller-manager:v0.1.0
            name: alicloud-controller-manager
            command:
              - /alicloud-controller-manager
              # set leader-elect=true if you have more that one replicas
              - --leader-elect=false
              - --allocate-node-cidrs=true
              # set this to what you set to controller-manager or kube-proxy
              - --cluster-cidr=10.0.6.0/24
              # if you want to use a secure endpoint or deploy in a kubeadm deployed cluster, you need to use a kubeconfig instead.
              - --master=10.0.0.10:8080
            env:
              - name: ACCESS_KEY_ID
                valueFrom:
                  secretKeyRef:
                    name: alicloud-config
                    key: access-key-id
              - name: ACCESS_KEY_SECRET
                valueFrom:
                  secretKeyRef:
                    name: alicloud-config
                    key: access-key-secret
    注意上面紅字部分,根據自己的配置情況替換。同樣,使用kubectl apply -f alicloud-controller-manager.yaml來建立對應的deployment
  5. 然後就可以建立一個service來使用它了,例如我下面建立一個zookeeper的服務
    apiVersion: v1
    kind: Service
    metadata:
      name: zookeeper-master
      labels:
        app: zookeeper-master
      namespace: xxxxx
    spec:
      ports:
      - port: 2181
        targetPort: 2181
        protocol: TCP
        name: main-port
      selector:
        app: zookeeper-master
      type: LoadBalancer
    這時可以通過kubectl get svc看到對應的service已經有了external-ip
  6. 要在阿里控制檯把負載均衡設定一下後端伺服器,把k8s的node節點都新增進去。