Skip to content

Instantly share code, notes, and snippets.

@csiens
Last active January 26, 2020 11:36
Show Gist options
  • Save csiens/19284df452807b7e3bffa5384524d5eb to your computer and use it in GitHub Desktop.
Save csiens/19284df452807b7e3bffa5384524d5eb to your computer and use it in GitHub Desktop.
Script to install an RKE cluster with N masters and N workers with TungstenFabric as the CNI
#!/bin/sh
#
#This script will setup an RKE Kubernetes cluster with TungstenFabric as the CNI with one master and two worker nodes running Ubuntu 18.04
#
#Run this as root from the initial control plane node
#
#You will need to create an ssh keypair and place them in /root/.ssh/ and then use ssh-copy-id to distribute the public key to all other nodes
#
#Set the control_plane_ip and worker_ip variables and update the embedded cluster.yml to the correct ip addresses for your environment
#
#If you add additional nodes to the embedded cluster.yml you must add the nodes ip address to either the control_plane_ip or worker_ip variable
#Ssh user variable
user="root"
#Control plane ip address. Specify multiple nodes using this format 1.1.1.1,2.2.2.2,3.3.3.3
control_plane_ip="10.9.8.21"
#Worker ip address. Specify multiple nodes using this format 1.1.1.1,2.2.2.2,3.3.3.3
worker_ip='10.9.8.22,10.9.8.23'
#Setup allnodes and nodes variable
allnodes="$control_plane_ip,$worker_ip"
nodes=$(echo $allnodes | tr ',' ' ')
#Create rke-tf directory
mkdir /root/rke-tf ; cd /root/rke-tf
#Create rke cluster.yml Configure this file with your node IP addressess, hostname, and roles
cat << EOF > /root/rke-tf/cluster.yml
nodes:
- address: 10.9.8.21
port: "22"
role:
- controlplane
- worker
- etcd
hostname_override: test1
user: root
docker_socket: /var/run/docker.sock
ssh_key_path: ~/.ssh/id_rsa
- address: 10.9.8.22
port: "22"
role:
- worker
hostname_override: test2
user: root
docker_socket: /var/run/docker.sock
ssh_key_path: ~/.ssh/id_rsa
- address: 10.9.8.23
port: "22"
role:
- worker
hostname_override: test3
user: root
docker_socket: /var/run/docker.sock
ssh_key_path: ~/.ssh/id_rsa
services:
kube-api:
service_cluster_ip_range: 10.96.0.0/12
pod_security_policy: false
always_pull_images: false
kube-controller:
cluster_cidr: 10.32.0.0/12
service_cluster_ip_range: 10.96.0.0/12
kubelet:
cluster_domain: cluster.local
infra_container_image: ""
cluster_dns_server: 10.96.0.3
fail_swap_on: false
network:
plugin: none
options: {}
node_selector: {}
authentication:
strategy: x509
sans: []
webhook: null
ssh_key_path: ~/.ssh/id_rsa
ssh_agent_auth: false
authorization:
mode: rbac
options: {}
ignore_docker_version: false
EOF
#Create common.env file
cat << EOF > /root/rke-tf/common.env
CONTRAIL_REGISTRY="docker.io/opencontrailnightly"
CONTRAIL_CONTAINER_TAG="latest"
CLOUD_ORCHESTRATOR="kubernetes"
KUBERNETES_IP_FABRIC_FORWARDING="true"
KUBERNETES_IP_FABRIC_SNAT="true"
KUBERNETES_API_SECURE_PORT=6443
KUBERNETES_API_SERVER=control_plane_ip
CONTROLLER_NODES=control_plane_ip
WEBUI_VIP=control_plane_ip
ANALYTICS_NODES=control_plane_ip
ANALYTICSDB_NODES=control_plane_ip
ANALYTCIS_ALARM_NODES=control_plane_ip
ANLAYTICS_SNMP_NODES=control_plane_ip
KAFKA_NODES=control_plane_ip
ZOOKEEPER_NODES=control_plane_ip
AGENT_NODES=allnodes
EOF
#Sed control plane and worker ips in common.env
sed -i "s/control_plane_ip/$control_plane_ip/" /root/rke-tf/common.env
sed -i "s/allnodes/$allnodes/" /root/rke-tf/common.env
#Add nodes to local .ssh/known_hosts file
for host in ${nodes}; do
ssh-keyscan -H $host >> ~/.ssh/known_hosts
done
#Install ntp and docker.io on each node
for host in ${nodes}; do
ssh -l $user $host apt-get install ntp docker.io -yq
done
#Turn off swap on each node
for host in ${nodes}; do
ssh -l $user $host swapoff -a
done
#Disable firewall on each node
for host in ${nodes}; do
ssh -l $user $host ufw disable
done
#Install kubectl
snap install kubectl --classic
#Download rke binary
wget https://github.com/rancher/rke/releases/download/v0.3.0/rke_linux-amd64 -P /root/rke-tf/
#Rename, chmod, move, and test rke binary
mv /root/rke-tf/rke_linux-amd64 /root/rke-tf/rke ; chmod +x /root/rke-tf/rke ; mv /root/rke-tf/rke /usr/bin ; rke --version
#Run rke up with provided cluster.yml
rke up --config /root/rke-tf/cluster.yml
#Copy kubeconfig
mkdir /root/.kube ; cp /root/rke-tf/kube_config_cluster.yml /root/.kube/config
#Add /var/lib/contrail/ports/vm to rke kubelet container on each node
for host in ${nodes}; do
ssh -l $user $host docker exec kubelet mkdir -p /var/lib/contrail/ports/vm
done
#Git clone contrail container builder
git clone https://github.com/Juniper/contrail-container-builder /root/rke-tf/contrail-container-builder
#Copy common.env
cp /root/rke-tf/common.env /root/rke-tf/contrail-container-builder/common.env
#Create TF manifest
/root/rke-tf/contrail-container-builder/kubernetes/manifests/./resolve-manifest.sh /root/rke-tf/contrail-container-builder/kubernetes/manifests/contrail-standalone-kubernetes.yaml > /root/rke-tf/tf.yml
#Sed kernel container image for ubuntu
sed -i "s/kernel-init/kernel-build-init/" /root/rke-tf/tf.yml
#Label nodes
/root/rke-tf/contrail-container-builder/kubernetes/manifests/./set-node-labels.sh
#Install TF manifest
kubectl apply -f /root/rke-tf/tf.yml
#Fix coredns
kubectl get configmap -nkube-system coredns -o yaml >> /root/rke-tf/coredns-configmap.yaml
export coredns_line=" forward . 10.79.255.253"
sed -i "s/.*forward.*/$coredns_line/" /root/rke-tf/coredns-configmap.yaml
kubectl get deployment -nkube-system coredns -o yaml >> /root/rke-tf/coredns-deployment.yaml
sed -i "/ livenessProbe:/,+9d" /root/rke-tf/coredns-deployment.yaml
sed -i "/ readinessProbe:/,+8d" /root/rke-tf/coredns-deployment.yaml
kubectl apply -f /root/rke-tf/coredns-configmap.yaml
kubectl apply -f /root/rke-tf/coredns-deployment.yaml
#Installation complete
printf "Installation complete.\nCheck pod status with 'watch kubectl get pods -A -owide'.\nOnce all pods are running get TungstenFabric status with 'contrail-status'.\nLogin to TungstenFabric WebUI at https://control_plane_ip:8143 U: admin P: contrail123\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment