How to use kops in AWS China Region

Getting Started

Kops used to only support Google Cloud DNS and Amazon Route53 to provision a kubernetes cluster. But since 1.6.2 gossip has been added which make it possible to provision a cluster without one of those DNS providers. Thanks to gossip, it's officially supported to provision a fully-functional kubernetes cluster in AWS China Region which doesn't have Route53 so far since 1.7. Should support both cn-north-1 and cn-northwest-1, but only cn-north-1 is tested.

Most of the following procedures to provision a cluster are the same with the guide to use kops in AWS. The differences will be highlighted and the similar parts will be omitted.

NOTE: THE FOLLOWING PROCEDURES ARE ONLY TESTED WITH KOPS 1.10.0, 1.10.1 AND KUBERNETES 1.9.11, 1.10.12

Install kops

Install kubectl

Setup your environment

AWS

When aws configure, remember to set the default region name to the correct one, e.g. cn-north-1.

  1. AWS Access Key ID [None]:
  2. AWS Secret Access Key [None]:
  3. Default region name [None]:
  4. Default output format [None]:

And export it correctly.

  1. export AWS_REGION=$(aws configure get region)

Configure DNS

As the note kindly pointing out, a gossip-based cluster can be easily created by having the cluster name end with .k8s.local. We will adopt this trick below. Rest of this section can be skipped safely.

Testing your DNS setup

Thanks to gossip, this section can be skipped safely as well.

Cluster State storage

Since we are provisioning a cluster in AWS China Region, we need to create a dedicated S3 bucket in AWS China Region.

  1. aws s3api create-bucket --bucket prefix-example-com-state-store --create-bucket-configuration LocationConstraint=$AWS_REGION

Creating your first cluster

Ensure you have a VPC which can access the internet NORMALLY

First of all, we have to solve the slow and unstable connection to the internet outside China, or the following processes won't work. One way to do that is to build a NAT instance which can route the traffic via some reliable connection. The details won't be discussed here.

Prepare kops ami

We have to build our own AMI because there is no official kops ami in AWS China Regions. There're two ways to accomplish so.

ImageBuilder RECOMMENDED

First, launch an instance in a private subnet which accesses the internet fast and stably.

Because the instance launched in a private subnet, we need to ensure it can be connected by using the private ip via a VPN or a bastion.

  1. SUBNET_ID=<subnet id> # a private subnet
  2. SECURITY_GROUP_ID=<security group id>
  3. KEY_NAME=<key pair name on aws>
  4.  
  5. AMI_ID=$(aws ec2 describe-images --filters Name=name,Values=debian-jessie-amd64-hvm-2016-02-20-ebs --query 'Images[*].ImageId' --output text)
  6. INSTANCE_ID=$(aws ec2 run-instances --image-id $AMI_ID --instance-type m3.medium --key-name $KEY_NAME --security-group-ids $SECURITY_GROUP_ID --subnet-id $SUBNET_ID --no-associate-public-ip-address --query 'Instances[*].InstanceId' --output text)
  7. aws ec2 create-tags --resources ${INSTANCE_ID} --tags Key=k8s.io/role/imagebuilder,Value=1

Now follow the documentation of ImageBuilder in kube-deploy to build the image.

  1. go get k8s.io/kube-deploy/imagebuilder
  2. cd ${GOPATH}/src/k8s.io/kube-deploy/imagebuilder
  3.  
  4. sed -i '' "s|publicIP := aws.StringValue(instance.PublicIpAddress)|publicIP := aws.StringValue(instance.PrivateIpAddress)|" pkg/imagebuilder/aws.go
  5. make
  6.  
  7. # cloud-init is failing due to urllib3 dependency. https://github.com/aws/aws-cli/issues/3678
  8. sed -i '' "s/'awscli'/'awscli==1.16.38'/g" templates/1.9-jessie.yml
  9.  
  10. # If the keypair specified is not `$HOME/.ssh/id_rsa`, the config yaml file need to be modified to add the full path to the private key.
  11. echo 'SSHPrivateKey: "/absolute/path/to/the/private/key"' >> aws-1.9-jessie.yaml
  12.  
  13. ${GOPATH}/bin/imagebuilder --config aws-1.9-jessie.yaml --v=8 --publish=false --replicate=false --up=false --down=false

Copy AMI from another region

Following the comment to copy the kops image from another region, e.g. ap-southeast-1.

Get the AMI id

No matter how to build the AMI, we get an AMI finally, e.g. k8s-1.9-debian-jessie-amd64-hvm-ebs-2018-07-18.

Prepare local environment

Set up a few environment variables.

  1. export NAME=example.k8s.local
  2. export KOPS_STATE_STORE=s3://prefix-example-com-state-store

Create cluster configuration

We will need to note which availability zones are available to us. AWS China (Beijing) Region only has two availability zones. It will have the same problem, like other regions having less than three AZs, that there is no true HA support in two AZs. You can add more master nodes to improve the reliability in one AZ.

  1. aws ec2 describe-availability-zones

Below is a create cluster command which will create a complete internal cluster in an existing VPC. The below command will generate a cluster configuration, but not start building it. Make sure that you have generated SSH key pair before creating the cluster.

  1. VPC_ID=<vpc id>
  2. VPC_NETWORK_CIDR=<vpc network cidr> # e.g. 172.30.0.0/16
  3. AMI=<owner id/ami name> # e.g. 123456890/k8s-1.9-debian-jessie-amd64-hvm-ebs-2018-07-18
  4.  
  5. kops create cluster \
  6. --zones ${AWS_REGION}a \
  7. --vpc ${VPC_ID} \
  8. --network-cidr ${VPC_NETWORK_CIDR} \
  9. --image ${AMI} \
  10. --associate-public-ip=false \
  11. --api-loadbalancer-type internal \
  12. --topology private \
  13. --networking calico \
  14. ${NAME}

Customize Cluster Configuration

Now we have a cluster configuration, we adjust the subnet config to reuse shared subnets by editing the description.

  1. kops edit cluster $NAME

Then change the corresponding subnets to specify the id and remove the cidr, e.g.

  1. spec:
  2. subnets:
  3. - id: subnet-12345678
  4. name: cn-north-1a
  5. type: Private
  6. zone: cn-north-1a
  7. - id: subnet-87654321
  8. name: utility-cn-north-1a
  9. type: Utility
  10. zone: cn-north-1a

Another tweak we can adopt here is to add a docker section to change the mirror to the official registry mirror for China. This will increase stability and download speed of pulling images from docker hub.

  1. spec:
  2. docker:
  3. registryMirrors:
  4. - https://registry.docker-cn.com

Please note that this mirror MIGHT BE not suitable for some cases. It's can be replaced by any other registry mirror as long as it's compatible with the docker api.

Build the Cluster

Use the Cluster

Delete the Cluster

What's next?

Add more master nodes

In one AZ

To achieve this, we can add more parameters to kops create cluster.

  1. --master-zones ${AWS_REGION}a --master-count 3 \
  2. --zones ${AWS_REGION}a --node-count 2 \

In two AZs

  1. --master-zones ${AWS_REGION}a,${AWS_REGION}b --master-count 3 \
  2. --zones ${AWS_REGION}a,${AWS_REGION}b --node-count 2 \

Please note that this will still have 50% chance to break the cluster when one of the AZs are down.

Offline mode

Here is a naive, uncompleted attempt to provision a cluster in a way minimizing the requirements to the internet because even with some kind of proxies or VPN it's still not that fast and it's always much more expensive than downloading from S3.

  1. ## Setup vars
  2.  
  3. KUBERNETES_VERSION=$(curl -fsSL --retry 5 "https://dl.k8s.io/release/stable.txt")
  4. KOPS_VERSION=$(curl -fsSL --retry 5 "https://api.github.com/repos/kubernetes/kops/releases/latest" | grep 'tag_name' | cut -d\" -f4)
  5. ASSET_BUCKET="some-asset-bucket"
  6. ASSET_PREFIX=""
  7.  
  8. # Please note that this filename of cni asset may change with kubernetes version
  9. # Find this in https://github.com/kubernetes/kops/blob/master/upup/pkg/fi/cloudup/networking.go
  10. CNI_FILENAME=cni-plugins-amd64-v0.6.0.tgz
  11.  
  12.  
  13. export KOPS_BASE_URL=https://s3.cn-north-1.amazonaws.com.cn/$ASSET_BUCKET/kops/$KOPS_VERSION/
  14. export CNI_VERSION_URL=https://s3.cn-north-1.amazonaws.com.cn/$ASSET_BUCKET/kubernetes/network-plugins/$CNI_FILENAME
  15. export CNI_ASSET_HASH_STRING=d595d3ded6499a64e8dac02466e2f5f2ce257c9f
  16.  
  17. ## Download assets
  18.  
  19. KUBERNETES_ASSETS=(
  20. network-plugins/$CNI_FILENAME
  21. release/$KUBERNETES_VERSION/bin/linux/amd64/kube-apiserver.tar
  22. release/$KUBERNETES_VERSION/bin/linux/amd64/kube-controller-manager.tar
  23. release/$KUBERNETES_VERSION/bin/linux/amd64/kube-proxy.tar
  24. release/$KUBERNETES_VERSION/bin/linux/amd64/kube-scheduler.tar
  25. release/$KUBERNETES_VERSION/bin/linux/amd64/kubectl
  26. release/$KUBERNETES_VERSION/bin/linux/amd64/kubelet
  27. )
  28. for asset in "${KUBERNETES_ASSETS[@]}"; do
  29. dir="kubernetes/$(dirname "$asset")"
  30. mkdir -p "$dir"
  31. url="https://storage.googleapis.com/kubernetes-release/$asset"
  32. wget -P "$dir" "$url"
  33. [ "${asset##*.}" != "gz" ] && wget -P "$dir" "$url.sha1"
  34. [ "${asset##*.}" == "tar" ] && wget -P "$dir" "${url%.tar}.docker_tag"
  35. done
  36.  
  37. KOPS_ASSETS=(
  38. "images/protokube.tar.gz"
  39. "linux/amd64/nodeup"
  40. "linux/amd64/utils.tar.gz"
  41. )
  42. for asset in "${KOPS_ASSETS[@]}"; do
  43. kops_path="kops/$KOPS_VERSION/$asset"
  44. dir="$(dirname "$kops_path")"
  45. mkdir -p "$dir"
  46. url="https://kubeupv2.s3.amazonaws.com/kops/$KOPS_VERSION/$asset"
  47. wget -P "$dir" "$url"
  48. wget -P "$dir" "$url.sha256"
  49. done
  50.  
  51. ## Upload assets
  52.  
  53. ## Get default S3 multipart_threshold
  54.  
  55. AWS_S3_DEFAULT_MULTIPART_THRESHOLD=$(aws configure get default.s3.multipart_threshold)
  56.  
  57. if [ ! -n "$AWS_S3_DEFAULT_MULTIPART_THRESHOLD" ]; then
  58. AWS_S3_DEFAULT_MULTIPART_THRESHOLD=8MB
  59. fi
  60.  
  61. ## Set multipart_threshold to 1024MB to prevent Etag not returns MD5 when upload multipart
  62.  
  63. aws configure set default.s3.multipart_threshold 1024MB
  64.  
  65. aws s3api create-bucket --bucket $ASSET_BUCKET --create-bucket-configuration LocationConstraint=$AWS_REGION
  66. for dir in "kubernetes" "kops"; do
  67. aws s3 sync --acl public-read "$dir" "s3://$ASSET_BUCKET/$ASSET_PREFIX$dir"
  68. done
  69.  
  70. aws configure set default.s3.multipart_threshold $AWS_S3_DEFAULT_MULTIPART_THRESHOLD

When create the cluster, add these parameters to the command line.

  1. --kubernetes-version https://s3.cn-north-1.amazonaws.com.cn/$ASSET_BUCKET/kubernetes/release/$KUBERNETES_VERSION

Now most of the assets required to provision a cluster by kops and kubernetes will be downloaded from the specified S3 bucket except images like pause-amd64, dns related, etc. These images not mirrored by the docker hub mirror since they are hosted on gcr.io. There will be some problem if the connection is not good.

Assets API

It hasn't been tested as this approach was only a PR when the author experimenting with provisioning a cluster in AWS China Region. It's the official way to achieve offline mode and should be superior to the previous naive attempt.