• Stars
    star
    391
  • Rank 110,003 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 6 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Assign static public IPs to Kubernetes nodes (GKE, EKS)

ci Go Report Card Docker Pulls

What is kubeIP?

Many applications need to be whitelisted by users based on a Source IP Address. As of today, Google Kubernetes Engine doesn't support assigning a static pool of IP addresses to the GKE cluster. Using kubeIP, this problem is solved by assigning GKE nodes external IP addresses from a predefined list. kubeIP monitors the Kubernetes API for new/removed nodes and applies the changes accordingly.

Deploy kubeIP (without building from source)

If you just want to use kubeIP (instead of building it yourself from source), please follow the instructions in this section. Youโ€™ll need Kubernetes version 1.10 or newer. You'll also need the Google Cloud SDK. You can install the Google Cloud SDK (which also installs kubectl).

To configure your Google Cloud SDK, set default project as:

gcloud config set project {your project_id}

Set the environment variables and make sure to configure before continuing:

export GCP_REGION=<gcp-region>
export GCP_ZONE=<gcp-zone>
export GKE_CLUSTER_NAME=<cluster-name>
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
export KUBEIP_NODEPOOL=<nodepool-with-static-ips>
export KUBEIP_SELF_NODEPOOL=<nodepool-for-kubeip-to-run-in>

Creating an IAM Service Account and obtaining the Key in JSON format

Create a Service Account with this command:

gcloud iam service-accounts create kubeip-service-account --display-name "kubeIP"

Create and attach a custom kubeIP role to the service account by running the following commands:

gcloud iam roles create kubeip --project $PROJECT_ID --file roles.yaml

gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member=serviceAccount:kubeip-service-account@$PROJECT_ID.iam.gserviceaccount.com \
    --role=projects/$PROJECT_ID/roles/kubeip \
    --condition=None

Generate the Key using the following command:

gcloud iam service-accounts keys create key.json \
    --iam-account kubeip-service-account@$PROJECT_ID.iam.gserviceaccount.com

Create Kubernetes Secret Objects

Get your GKE cluster credentaials with (replace $GKE_CLUSTER_NAME with your real GKE cluster name):

gcloud container clusters get-credentials $GKE_CLUSTER_NAME \
    --region $GCP_ZONE \
    --project $PROJECT_ID

Create a Kubernetes secret object by running:

kubectl create secret generic kubeip-key --from-file=key.json -n kube-system

Get RBAC permissions with:

kubectl create clusterrolebinding cluster-admin-binding \
    --clusterrole cluster-admin --user `gcloud config list --format 'value(core.account)'`

Create Static, Reserved IP Addresses:

Create as many static IP addresses for the number of nodes in your GKE cluster (this example creates 10 addresses) so you will have enough addresses when your cluster scales up (manually or automatically):

for i in {1..10}; do gcloud compute addresses create kubeip-ip$i --project=$PROJECT_ID --region=$GCP_REGION; done

Add labels to reserved IP addresses. A common practice is to assign a unique value per cluster (for example cluster name):

for i in {1..10}; do gcloud beta compute addresses update kubeip-ip$i --update-labels kubeip=$GKE_CLUSTER_NAME --region $GCP_REGION; done
sed -i -e "s/reserved/$GKE_CLUSTER_NAME/g" -e "s/default-pool/$KUBEIP_NODEPOOL/g" deploy/kubeip-configmap.yaml

Make sure the deploy/kubeip-configmap.yaml file contains the correct values:

  • The KUBEIP_LABELVALUE should be your GKE's cluster name
  • The KUBEIP_NODEPOOL should match the name of your GKE node-pool on which kubeIP will operate
  • The KUBEIP_FORCEASSIGNMENT - controls whether kubeIP should assign static IPs to existing nodes in the node-pool and defaults to true

We recommend that KUBEIP_NODEPOOL should NOT be the same as KUBEIP_SELF_NODEPOOL

If you would like to assign addresses to other node pools, then KUBEIP_NODEPOOL can be added to this nodepool KUBEIP_ADDITIONALNODEPOOLS as a comma separated list. You should tag the addresses for this pool with the KUBEIP_LABELKEY value + -node-pool and assign the value of the node pool a name i.e., kubeip-node-pool=my-node-pool

sed -i -e "s/pool-kubip/$KUBEIP_SELF_NODEPOOL/g" deploy/kubeip-deployment.yaml

Deploy kubeIP by running:

kubectl apply -f deploy/.

Once youโ€™ve assigned an IP address to a node kubeIP, a label will be created for that node kubip_assigned with the value of the IP address (. are replaced with -):

172.31.255.255 ==> 172-31-255-255

Ordering IPs

KubeIP can order IPs based on the numeric value identified by KUBEIP_ORDERBYLABELKEY.

IPs are ordered in descending order if KUBEIP_ORDERBYDESC is set to true, ascending order otherwise.

Missing KUBEIP_ORDERBYLABELKEY or invalid values present on KUBEIP_ORDERBYLABELKEY will be assigned the lowest priority.

When nodes are added, deleted or on tick, kubeIP will check whether the nodes have the most optimal IP assignment. What does this mean?

E.g. Let's assume Node1 has IP_A, Node2 has IP_B and IP_A > IP_B, when we scale the cluster down the cluster two things might happen

  1. Node 1 is deleted which results in a sub-optimal IP assignment since Node2 has IP_B and IP_A > IP_B
  2. Node 2 is deleted maintaining optimal order.

In the first case Node 2 is re-assigned IP_A.

To order the IPs reserved above in asc order use

for i in {1..10}; do gcloud beta compute addresses update kubeip-ip$i --update-labels priority=$i --region=$GCP_REGION; done

and set

KUBEIP_ORDERBYLABELKEY: "priority"
KUBEIP_ORDERBYDESC: "false"

Copy Labels

KubeIP will also copy all labels from the IP being assigned over to the node if KUBEIP_COPYLABELS is set to true.

This is typically helpful when we want to have node selection not based on IP but more semantic label keys and values.

As an example let's label kubeip-ip1 with platform_whitelisted=true, to do this we execute the following command

gcloud beta compute addresses update kubeip-ip1 --update-labels "platform_whitelisted=true" --region=$GCP_REGION;

Now, when a node is assigned the IP address of kubeip-ip1 it will also be labelled with platform_whitelisted=true as well as the default kubip_assigned.

An IP can have multiple labels, all will be copied over.

Clear Labels

When IPs get assigned or re-assigned to achieve optimal IP assignment we can configure the system to clear any previous labels. Set KUBEIP_CLEARLABELS flag to true if you want this behaviour.

This feature is required when labels are not overlapping. E.g. let's assume we have the following tagged IPs; IP_A and IP_B, order by priority

IP_A test_a=value_a,test_b=value_b,priority=1
IP_B test_c=value_c,priority=2

Let's assume that the assignment was as follows

IP_A => NodeA
IP_B => NodeB

At this point NodeA has labels test_a=value_a,test_b=value_b and NodeB has labels test_c=value_c. Note priority is not copied over.

If NodeA is deleted a re-assignment needs to happen (due to the fact that IP_A > IP_B) and NodeB would have

  • test_a=value_a,test_b=value_b,test_c=value_c if KUBEIP_CLEARLABELS="false" and
  • test_a=value_a,test_b=value_b if KUBEIP_CLEARLABELS="true"

Note that test_c is not an overlapping label and hence might cause problems if KUBEIP_CLEARLABELS is not set to true.

Dry Run Mode

Dry run mode allows debugging the operations performed by KubeIP without actually performing the operations.

ONLY use this mode during development of new features on KubeIP.

Deploy & Build From Source

You need Kubernetes version 1.10 or newer. You also need Docker version and kubectl 1.10.x or newer installed on your machine, as well as the Google Cloud SDK. You can install the Google Cloud SDK (which also installs kubectl).

Clone Git Repository

Make sure your $GOPATH is configured. You'll need to clone this repository to your $GOPATH/src folder.

mkdir -p $GOPATH/src/doitintl/kubeip
git clone https://github.com/doitintl/kubeip.git $GOPATH/src/doitintl/kubeip
cd $GOPATH/src/doitintl/kubeip

Set Environment Variables

Replace us-central1 with the region where your GKE cluster resides and kubeip-cluster with your real GKE cluster name

export GCP_REGION=us-central1
export GCP_ZONE=us-central1-b
export GKE_CLUSTER_NAME=kubeip-cluster
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')

Develop kubeIP Locally

Compile the kubeIP binary and run tests

make

Build kubeIP's Container Image

Compile the kubeIP binary and build the Docker image as following:

make image

Tag the image using:

docker tag kubeip gcr.io/$PROJECT_ID/kubeip

Finally, push the image to Google Container Registry with:

docker push gcr.io/$PROJECT_ID/kubeip

Alternatively, you can export REGISTRY to gcr.io/$PROJECT_ID and run the script build-all-and-push.sh which builds and publishes the docker image.

Create IAM Service Account and obtain the Key in JSON format

Create a Service Account with this command:

gcloud iam service-accounts create kubeip-service-account --display-name "kubeIP"

Create and attach the custom kubeIP role to the service account by running the following commands:

gcloud iam roles create kubeip --project $PROJECT_ID --file roles.yaml

gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:kubeip-service-account@$PROJECT_ID.iam.gserviceaccount.com --role projects/$PROJECT_ID/roles/kubeip

Generate the Key using the following command:

gcloud iam service-accounts keys create key.json \
  --iam-account kubeip-service-account@$PROJECT_ID.iam.gserviceaccount.com

Create Kubernetes Secret

Get your GKE cluster credentaials with (replace cluster_name with your real GKE cluster name):

gcloud container clusters get-credentials $GKE_CLUSTER_NAME \
    --region $GCP_ZONE \
    --project $PROJECT_ID

Create a Kubernetes secret by running:

kubectl create secret generic kubeip-key --from-file=key.json -n kube-system

We need to get RBAC permissions first with

kubectl create clusterrolebinding cluster-admin-binding \
    --clusterrole cluster-admin --user `gcloud config list --format 'value(core.account)'`

Create static reserved IP addresses:

Create as many static IP addresses for the number of nodes in your GKE cluster (this example creates 10 addresses) so you will have enough addresses when your cluster scales up (automatically or manually):

for i in {1..10}; do gcloud compute addresses create kubeip-ip$i --project=$PROJECT_ID --region=$GCP_REGION; done

Add labels to reserved IP addresses. A common practice is to assign a unique value per cluster. You can use your cluster name for example:

for i in {1..10}; do gcloud beta compute addresses update kubeip-ip$i --update-labels kubeip=$GKE_CLUSTER_NAME --region $GCP_REGION; done

Adjust the deploy/kubeip-configmap.yaml with your GKE cluster name (replace the GKE-cluster-name with your real GKE cluster name):

sed -i -e "s/reserved/$GKE_CLUSTER_NAME/g" deploy/kubeip-configmap.yaml

Adjust the deploy/kubeip-deployment.yaml to reflect your real container image path:

  • Edit the image to match your container image path, i.e. gcr.io/$PROJECT_ID/kubeip

By default, kubeIP will only manage the nodes in default-pool nodepool. If you'd like kubeIP to manage another node-pool, please update the KUBEIP_NODEPOOL setting in deploy/kubeip-configmap.yaml file before deploying. You can also update the KUBEIP_LABELKEY and KUBEIP_LABELVALUE to control which static external IP addresses the kubeIP will look for to assign to your nodes.

The KUBEIP_FORCEASSIGNMENT (which defaults to true) will check on startup and every five minutes if there are nodes in the node-pool that are not assigned to a reserved address. If such nodes are found, then kubeIP will assign a reserved address (if one is available to them):

Deploy kubeIP by running

kubectl apply -f deploy/.

References:

  • Event listening code was take from kubewatch

More Repositories

1

kube-no-trouble

Easily check your clusters for use of deprecated APIs
Go
3,126
star
2

bigquery-grafana

Google BigQuery Datasource Plugin for Grafana. (NO LONGER MAINTAINED)
TypeScript
242
star
3

gcpinstances.info

GCPinstances.info source code
Python
173
star
4

secrets-init

minimalistic init system for containers with AWS/GCP secrets support
Go
161
star
5

kube-secrets-init

Kubernetes mutating webhook for `secrets-init` injection
Go
148
star
6

zorya

Google Cloud Instance Scheduler helping to reduce costs by 60% on average for non-production environments.
JavaScript
141
star
7

secure-gcp-reference

Best practice example for secure and compliant Google Cloud Platform infrastructure
99
star
8

gtoken

Securely access AWS services from GKE cluster
Go
73
star
9

gSlack

Get Slack notifications from Google Cloud Platform
JavaScript
72
star
10

iris3

An upgraded and improved version of the Iris automatic GCP-labeling project
Python
71
star
11

gpu-finder

Python
69
star
12

SafeScrub

Safely delete unwanted resources in a GCP project, clearing clutter and saving money.
Shell
68
star
13

bigquery-optimization-queries

Queries to assist with BigQuery cost and performance.
Python
61
star
14

banias

Opinionated serverless event analytics pipeline
Go
43
star
15

janus

Janus is a simple way to assume AWS Role with Google Cloud Service Account
Python
40
star
16

ClusterCloner

Clone Kubernetes clusters (VM infrastructure, not K8s objects) to/from AWS EKS, GCP GKE, and Azure EKS.
Go
33
star
17

doit-composer-airflow-training

Getting started with Apache Airflow on Cloud Composer
Python
28
star
18

gke-fundamentals-workshop

Shell
25
star
19

bqtop

Visualizing BigQuery query jobs with Cloud Functions, Firebase andย Pub/Sub
JavaScript
25
star
20

CloudBlaster

Kotlin
22
star
21

Cloud-Tasks-In-Process-Emulator

Google doesn't offer an emulator for the Cloud Tasks API, as it does for Datastore or PubSub. This project answers that need with a single short Python module intended to be copied to your codebase.
Python
20
star
22

AWSlack

Get Slack notifications on AWS CloudWatch events
JavaScript
19
star
23

gcp-monitoring-metric-exporter

Python
18
star
24

workload-identity-analyzer

A tool to analyze a workload running in GKE and make sure that Workload Identity is configured properly
Python
16
star
25

doit-easily-marketplace

Python
12
star
26

dataflow-kafka-to-bq

Dataflow template which read data from Kafka (Support SSL), transform, and outputs the resulting records to BigQuery
Java
12
star
27

cloud-catalog

Extract categories and services (as unified JSON) for major public cloud services.
Python
11
star
28

gcs-stats

Easily analyze the size of Google Cloud Storage buckets regardless of their size
JavaScript
10
star
29

terraform-iac-demo

GitOps demo of Terraform infrastructure as code (IaC)
HCL
9
star
30

aws-eks-sample-templates

The repository contains the sample templates to get started with AWS EKS quickly
9
star
31

secrets-consumer-webhook

Kubernetes mutation webhook for secrets-consumer-env - Automatically inject secrets to Pod
Go
9
star
32

gke-https-redirect

Demonstration how to use the newly introduced https redirect support in native GKE ingress resources.
9
star
33

gcp-discover-orphaned-firewall-rules

Find orphaned firewall rules that are not applied to any VM instances in a shared VPC
Go
8
star
34

esop

Employee Stock Option Plan
8
star
35

eks-spot-to-ondemand-fallback

7
star
36

terraform-bq-scheduled-queries

This is a demo project to use Terraform to manage BigQuery scheduled queries with Cloud Build CI/CD
HCL
7
star
37

bi-engine-statistics

a opinionated bi engine statistics dashboard for dashboard
LookML
7
star
38

ignite-gke

Running Apache Ignite on GKE the "right" way
Java
6
star
39

bq-snitch-app

Get visibility into expensive Google BigQuery queries on Slack
Python
6
star
40

DoiT-AdminPulse-for-Workspace

A tool for Workspace administrators to review their security posture and inventory the admin SDK.
JavaScript
6
star
41

terraform-gcp-templates

Generic Terraform GCP templates
HCL
5
star
42

validating-admission-policy-playground

5
star
43

azure-instances.info

HTML
5
star
44

QuickQuickstarts

The simplest quickstart scripts for running multiple web backend infrastructures in AWS and GCP.
Shell
5
star
45

dataflow-bigquery-schema-migrator-insert

Dataflow Bigquery Schema Migrator Insert
Java
5
star
46

spotzero

Update EC2 Auto Scaling groups in AWS account to use Spot instances.
Go
4
star
47

galactus

A tool for detecting unused Service Accounts and Service Account Keys on GCP
Python
4
star
48

gcp-alerting-cis-benchmarks

Configuration instructions for Cloud Monitoring alerts on Google Cloud Platform for additional security based on CIS benchmarks
4
star
49

private_cloud_sql

4
star
50

intercloud-throughput

Python
4
star
51

secrets-consumer-env

Consume secrets securely from AWS, GCP and Hashicorp Vault secret managers
Go
3
star
52

ec2-auto-tag

Python
3
star
53

elasticsearch-gke

Blueprint for creating production-grade ElasticSearch deployments with Elastic K8s Operator
Makefile
3
star
54

ferent

Clojure
3
star
55

calculate-cloudfront-aos

Calculate CloudFront Average Object Size (AOS) using Cost Explorer API
Python
3
star
56

cre-playbooks

A collection of playbooks to help CREs solve problems more efficiently
3
star
57

docs-gitbook-cmp

ARCHIVED: GitBook repository for the DoiT Cloud Management Platform (CMP) User Documentation
Shell
3
star
58

docops

Common resources for doing DocOps at DoiT
Python
3
star
59

terraform-provider-doit-console

Terraform provider for DoiT API platform
Go
3
star
60

doit-eks-lens-helm-chart

Smarty
3
star
61

docops-devcontainer

DocOps devcontainer
2
star
62

next23-genai-demo

Jupyter Notebook
2
star
63

gke-ssh

HCL
2
star
64

platform-iac

DoiT platform infrastructure as a code templates
HCL
2
star
65

cloud-run-go-boilerplate

Go
2
star
66

gceinstances

Inspired by ec2instances.info, this is a summary page for Google Compute Engine instances
HTML
2
star
67

docops-python

DoiT International DocOps Python library and CLI program
Shell
2
star
68

mysql-57-eol

Documents to help guide customers through MySQL 5.7 End of life
2
star
69

tf-fundamentals-workshop-101

Basic workshop on the topic of Terraform in the context of AWS
HCL
2
star
70

looker-cph-event

LookML
1
star
71

robust-multicloud

Kotlin
1
star
72

help

DoiT International Help Center and product documentation
JavaScript
1
star
73

secret-manager-nodejs-example

JavaScript
1
star
74

assembly-pipeline

Shell
1
star
75

cloudbuild-demo

JavaScript
1
star
76

demo-gke-pubsub-consumer

JavaScript
1
star
77

clojure-exercises

Clojure
1
star
78

poc-gcp-nextflow

Nextflow + GCP + LifeSciencesAPI + Compute Engine + NextflowTower + Workflows
1
star
79

app-engine-firebase-identity

BYO Identity through Firebase
Python
1
star
80

dynamodb-lens

Python
1
star
81

cloudrun-cloudsql-psc

Accessing CloudSQL with Private Service Connect enabled from Cloud Run
1
star
82

zen-dog

Tool to sync crucial zendesk configuration from code
1
star
83

gcp-auto-tag

Python
1
star
84

locust-demo

Python
1
star
85

gke-node-autoscheduler-poc

HCL
1
star
86

eks-lens-agent

Go
1
star
87

gcp-qms

Quota Monitoring Solution support files for Google Cloud Platform
Shell
1
star
88

avoxi-workshop-public

Jupyter Notebook
1
star
89

developer-envs

HCL
1
star
90

simple-cloud-run

Sometimes it's amazing how simple Google Cloud can be - this time Cloud Run
Go
1
star
91

terraform-provider-doit

Terraform provider for DoiT API platform
Go
1
star
92

next24-genai-demo

Gen AI Demo for Google Next'24. This is a RAG system with Agents to retrieve project specific cost and combine it with DoiT blog posts to deliver a analysis on how to reduce cost.
Jupyter Notebook
1
star
93

aws-dms-to-hudi

Example AWS DMS ingestion pipeline to Apache Hudi tables in S3
Python
1
star
94

bigquery-list-tables-org-wide

A script that will list all tables (and potentially relevant metadata) across your whole Google Cloud organisation.
Python
1
star
95

LLM-Liftoff-Bedrock-Workshop

Python
1
star
96

ticket-review

An app for performing ticket reviews.
HCL
1
star
97

aws-help

Help Docs for DoIT AWS Customers
1
star