8

I'm using Azure DevOps, to handle PBI, repos, PRS, and builds, but all my infrastructure, including Kubernetes is managed by AWS.

There's not documentation, neither "the right and easy way" of how to deploy to AWS EKS using Azure DevOps Tasks.

I found this solution, its a good solution, but would be awesome to know how you guys resolve it, or if there are more approaches.

David Noreña
  • 3,951
  • 1
  • 28
  • 43

3 Answers3

19

After a research and try and failure, I found another way to do it, without messing around with shell scripts.

You just need to apply the following to Kubernetes, It will create a ServiceAccount and bind it to a custom Role, that role will have the permissions to create/delete deployments and pods (tweak it for services permissions).

deploy-robot-conf.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: deploy-robot
automountServiceAccountToken: false
---
apiVersion: v1
kind: Secret
metadata:
  name: deploy-robot-secret
  annotations:
    kubernetes.io/service-account.name: deploy-robot
type: kubernetes.io/service-account-token
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: deploy-robot-role
  namespace: default
rules: # ## Customize these to meet your requirements ##
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["create", "delete"]
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["create", "delete"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: global-rolebinding
  namespace: default
subjects:
- kind: ServiceAccount
  name: deploy-robot
  namespace: default
roleRef:
  kind: Role
  name: deploy-robot-role
  apiGroup: rbac.authorization.k8s.io

This will have the minimum permissions needed for Azure DevOps be able to deploy to the cluster.

Note: Please tweak the rules at the role resource to meet your need, for instance services resources permissions.

Then go to your release and create a Kubernetes Service Connection:

Kubernetes Service Connection

Fill the boxes, and follow the steps required to get your secret from the service account, remember that is deploy-robot if you didn't change the yaml file.

Kubernetes Service Connection

And then just use your Kubernetes Connection:

Release with Kubernetes Connection

David Noreña
  • 3,951
  • 1
  • 28
  • 43
  • 1
    Yeah I still use it nowadays, and its amazing, because I dont have to change anything in the pipeline configuration if I want to change from aws to azure kubernetes i think is a really clean way to do it. Glad it worked for you @Khoa – David Noreña May 20 '20 at 14:43
  • I have followed the same process. I am getting error like secret don't have any data while running the "kubectl get secret deploy-robot-secret-o yaml" at service connection configuration. Can you guys please guide me if I missed anything?.Do we need to create secret with any data? – BSG Jun 18 '20 at 07:20
  • remember the secret is deploy-robot-secret line 10 of my example. – David Noreña Jun 18 '20 at 17:39
  • which kind of data(credentials or role details or any token) does the secret should contain? or else here secret automatically taking the service account token while creating. – BSG Jun 19 '20 at 12:48
  • 1
    This is great and clean! Tks. – Reginaldo Santos Jun 07 '21 at 23:17
2

Another option would be to use 'kubeconf' based authentication, where 'kubeconf' file can be obtained with following AWS CLI command:

aws eks --region region update-kubeconfig --name cluster_name --kubconfig ~/.kube/AzureDevOpsConfig
Nepomucen
  • 4,449
  • 3
  • 9
  • 24
  • could you give a more detailed example of how to use it with Azure DevOps tasks ?, I tried that on New Kubernetes Connection, but it doesn't recognize my credentials. – David Noreña Jul 08 '19 at 15:17
  • Hint: your Azure DevOps - Deploy to Kubernetes task, should run on the Azure Pipeline agent equipped with aws-iam-authenticator, and configured properly as explained here: https://docs.aws.amazon.com/eks/latest/userguide/install-aws-iam-authenticator.html – Nepomucen Jul 11 '19 at 11:22
  • 1
    This worked for me, had to install aws cli in a bash task, then aws sts get-caller-identity in an aws cli task. aws eks update config as stated above in an aws cli task. lastly run kubectl within an aws shell script task – RussellEast Aug 27 '19 at 13:37
  • I tried this one too, but you have to create scripts to update the token so its more work – David Noreña May 20 '20 at 14:36
0

found a new solution with credentials for kubernetes service connection type and kubeconfig option:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: xxx
    server: https://xxx.amazonaws.com
  name: arn:aws:eks:xxx:nnn:cluster/NNN
- cluster:
    certificate-authority-data: xxx
    server: https://xxx.amazonaws.com
  name: arn:aws:eks:xxx:nnn:cluster/NNN
contexts:
- context:
    cluster: arn:aws:eks:xxx:nnn:cluster/NNN
    user: arn:aws:eks:xxx:nnn:cluster/NNN
  name: arn:aws:eks:xxx:nnn:cluster/NNN
- context:
    cluster: arn:aws:eks:xxx:nnn:cluster/NNN2
    user: arn:aws:eks:xxx:nnn:cluster/NNN2
  name: arn:aws:eks:xxx:nnn:cluster/NNN2
current-context: arn:aws:eks:xxx:nnn:cluster/NNN2
kind: Config
preferences: {}
users:
- name: arn:aws:eks:xxx:nnn:cluster/NNN
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      args:
      - token
      - -i
      - <cluster-name>
      command: aws-iam-authenticator
      env:
      - name: AWS_ACCESS_KEY_ID
        value: "XXX"
      - name: AWS_SECRET_ACCESS_KEY
        value: "XXX"
      interactiveMode: IfAvailable
      provideClusterInfo: false
- name: arn:aws:eks:xxx:nnn:cluster/NNN2
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      args:
      - token
      - -i
      - <cluster-name-2>
      command: aws-iam-authenticator
      env:
      - name: AWS_ACCESS_KEY_ID
        value: "xxx"
      - name: AWS_SECRET_ACCESS_KEY
        value: "xxx"
      interactiveMode: IfAvailable
      provideClusterInfo: false
    ```
this is a kubeconfig for azure with credentials for two clusters.
kumarich
  • 31
  • 6
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 01 '23 at 12:20