简体   繁体   中英

How to get AWS kops based kubernetes cluster IP address to connect with gitlab CICD pipeline

I am trying to create basic gitlab CICD pipeline which will deploy my node.js based backend to AWS kops based k8s cluster.For that I have created gitlab-ci.yml file which will use for deploy whole CICD pipeline, however I am getting confused with how to get kubernetes cluster IP address so I can use it in gitlab-ci.yml to set as - kubectl config set-cluster k8s --server="$CLUSTER_ADDRESS" where I want CLUSTER_ADDRESS to configure with gitlab in gitlab-ci.yml.

Any help would be appreciated.

variables:
  DOCKER_DRIVER: overlay2
  REGISTRY: $CI_REGISTRY
  IMAGE_TAG: $CI_REGISTRY_IMAGE
  K8S_DEPLOYMENT_NAME: deployment/$CI_PROJECT_NAME
  CONTAINER_NAME: $CI_PROJECT_NAME

stages:
  - build
  - build-docker
  - deploy


build-docker:
  image: docker:latest
  stage: build-docker
  services:
    - docker:dind
  tags:
    - privileged
  only:
    - Test
  script:
    script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $REGISTRY
    - docker build --network host -t $IMAGE_NAME:$IMAGE_TAG -t $IMAGE_NAME:latest .
    - docker push $IMAGE_NAME:$IMAGE_TAG
    - docker push $IMAGE_NAME:latest
deploy-k8s-(stage):
  image:
    name: kubectl:latest
    entrypoint: [""]
  stage: deploy
  tags:
    - privileged
  # Optional: Manual gate
  when: manual
  dependencies:
    - build-docker
  script:
    - kubectl config set-cluster k8s --server="$CLUSTER_ADDRESS"
    - kubectl config set clusters.k8s.certificate-authority-data $CA_AUTH_DATA
    - kubectl config set-credentials gitlab-service-account --token=$K8S_TOKEN
    - kubectl config set-context default --cluster=k8s --user=gitlab-service-account --namespace=default
    - kubectl config use-context default
    - kubectl set image $K8S_DEPLOYMENT_NAME $CI_PROJECT_NAME=$IMAGE_TAG
    - kubectl rollout restart $K8S_DEPLOYMENT_NAME

If your current kubeconfig context is set to the cluster in question, you can run the following to get the cluster address you want:

kubectl config view --minify --raw \
    --output 'jsonpath={.clusters[0].cluster.server}'

You can add --context <cluster name> if not.

In most cases this will be https://api.<cluster name> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM