@@ -117,7 +117,7 @@ class Kubinator
117117 vars = OpenStruct . new
118118 vars . box = @box
119119 vars . nodes = specs . map { |x | ' ' + x . to_s } * ",\n "
120- FileUtils . cp ( 'vagrant.tpl' , 'Vagrantfile' )
120+ FileUtils . cp ( 'templates/ vagrant.tpl' , 'Vagrantfile' )
121121 FileUtils . resolve ( 'Vagrantfile' , vars )
122122
123123 # Choose a vagrant box to use and update registry if needed
@@ -175,6 +175,7 @@ class Kubinator
175175 ips = getnodes . map { |x | x . ip }
176176 all = cmd . include? ( 'all' )
177177 init = cmd . include? ( 'init' )
178+ config = cmd . include? ( 'config' )
178179 dashboard = cmd . include? ( 'dashboard' )
179180 helm = cmd . include? ( 'helm' )
180181 weave = cmd . include? ( 'weave' )
@@ -233,39 +234,14 @@ class Kubinator
233234 # Configure kubelet for each node
234235 kubelet = '/etc/default/kubelet'
235236 if not ssh . exec! ( "cat #{ kubelet } " ) . include? ( ip )
236- extra_args = [
237-
238- # Configure ip address for node
239- "--node-ip=#{ ip } " ,
240-
241- # Note this is the default
242- # Configure cluster dns to use
243- # Specific address in the kubeadm --service-cidr arg
244- #"--cluster-dns=10.96.0.10",
245- #"--cluster-domain=cluster.local",
246-
247- # Cgroup driver
248- # --cgroup-driver=systemd
249-
250- # Seems to cause joins to fail
251- #"--hostname-override=#{ip}"
252- ] * ' '
253- ssh . exec! ( "sudo sed -i -e 's/\\ (KUBELET_EXTRA_ARGS=\\ )\\ (.*$\\ )/\\ 1#{ extra_args } \\ 2/' #{ kubelet } " )
237+ args = [ "--node-ip=#{ ip } " ] * ' '
238+ ssh . exec! ( "sudo sed -i -e 's/\\ (KUBELET_EXTRA_ARGS=\\ )\\ (.*$\\ )/\\ 1#{ args } \\ 2/' #{ kubelet } " )
254239 ssh . exec! ( "sudo systemctl restart kubelet" )
255240 puts ( "#{ ip } : Configured Kubelet private network ip...done" . colorize ( :cyan ) )
256241 else
257242 puts ( "#{ ip } : Configure Kubelet private network ip...skipped" . colorize ( :cyan ) )
258243 end
259244
260- # # Configure flannel ip masquerade
261- # flanneld = '/etc/default/flanneld'
262- # if not ssh.exec!("cat #{flanneld}").include?("ip-masq")
263- # ssh.exec!("echo 'FLANNELD_OPTS=--ip-masq=true' | sudo tee #{flanneld}")
264- # puts("#{ip}: Configured flanneld defaults...done".colorize(:cyan))
265- # else
266- # puts("#{ip}: Configure flanneld defaults...skipped".colorize(:cyan))
267- # end
268-
269245 # Configure kernel for Elasticsearch
270246 sysctl_conf = '/etc/sysctl.d/10-cyberlinux.conf'
271247 if not ssh . exec! ( "cat #{ sysctl_conf } " ) . include? ( 'max_map_count' )
@@ -316,41 +292,52 @@ class Kubinator
316292 # ==========================================================================
317293 # https://kubernetes.io/docs/getting-started-guides/kubeadm
318294 # https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/
295+ # https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/#config-file
319296 # --------------------------------------------------------------------------
320297 join = nil
321298 if all or init
322299 Net ::SSH . start ( ips . first , @user , password :@pass , verify_host_key : :never ) { |ssh |
323300 if not ssh . exec! ( "docker ps" ) . include? ( "apiserver" )
324301 puts ( ":: Initialize master node '#{ ips . first } '" . colorize ( :cyan ) )
325-
326- # Initialize cluster via kubeadm
327- # --------------------------------------------------------------------
328- cmd = "sudo kubeadm init --kubernetes-version=v#{ @k8sver } "
329-
330- # Define api's location on our host-only network
331- cmd += "--apiserver-advertise-address=#{ ips . first } "
302+ ssh . exec! ( "mkdir -p ~/.config" )
332303
333304 # Cluster cidr is required for flannel and some other pod networks.
305+ # https://github.com/coreos/flannel/blob/master/Documentation/kubernetes.md
334306 # It is the same cidr range that gets assigned to --cluster-cidr if you check with
335307 # 'kubectl cluster-info dump | grep cidr' and thus the same value that should be used for
336308 # the kube-proxy --cluster-cidr argument as well.
337309 cluster_cidr = "10.32.0.0/12" if weave
338310 cluster_cidr = "10.244.0.0/16" if flannel
339311
340- # Note this is the default
341- # Range of IPs to use for service VIPs
342- # Must match range called out in kubelet --cluster-dns arg
343- #cmd += "--service-cidr=10.96.0.0/16 "
344-
345- # Flannel requires this to work
346- # Range of IP addresses for the pod network
347- # https://github.com/coreos/flannel/blob/master/Documentation/kubernetes.md
348- cmd += " --pod-network-cidr=#{ cluster_cidr } " if flannel
349-
312+ # Execute kubeadm with config template
313+ if config
314+ FileUtils . cp ( 'templates/kubeadm.tpl' , 'config/kubeadm.conf' )
315+ FileUtils . resolve ( 'config/kubeadm.conf' , {
316+ advertise_address : ips . first ,
317+ kubernetes_version : @k8sver ,
318+ kube_proxy_mode : "iptables" ,
319+ cgroup_driver : "cgroupfs" , # kubeadm default: cgroupfs
320+ cluster_domain : "cluster.local" , # kubeadm default: cluster.local
321+ cluster_dns : "10.96.0.10" , # kubeadm default: 10.96.0.10
322+ service_cidr : "10.96.0.0/12" , # kubeadm default: 10.96.0.0/12
323+ cluster_cidr : cluster_cidr # cidr for pod networking
324+ } )
325+ Net ::SCP . upload! ( ips . first , @user , 'config/kubeadm.conf' , '.config/kubeadm.conf' ,
326+ ssh :{ verify_host_key : :never , password : @pass } )
327+ cmd = "sudo kubeadm init --config .config/kubeadm.conf"
328+
329+ # Execute without config
330+ else
331+ cmd = "sudo kubeadm init --kubernetes-version=v#{ @k8sver } "
332+ cmd += "--apiserver-advertise-address=#{ ips . first } "
333+ cmd += "--pod-network-cidr=#{ cluster_cidr } "
334+ end
335+
336+ # Capture join for worker nodes
337+ # e.g. kubeadm join 192.168.56.10:6443 --token u6wor2.6kinrlvcbtxcoqo4 --discovery-token-ca-cert-hash
338+ # sha256:1112aafe50e27d54bccfd45d956f658a18b05e8b0ccf90c264ec17b026d01a8f
350339 ssh . exec! ( cmd ) { |c , s , o |
351340 puts ( o )
352- # e.g. kubeadm join 192.168.56.10:6443 --token u6wor2.6kinrlvcbtxcoqo4 --discovery-token-ca-cert-hash
353- # sha256:1112aafe50e27d54bccfd45d956f658a18b05e8b0ccf90c264ec17b026d01a8f
354341 join = o . split ( "\n " ) . find { |x | x . include? ( "kubeadm join" ) } . strip if o . include? ( "kubeadm join" )
355342 }
356343
@@ -399,9 +386,9 @@ class Kubinator
399386
400387 # If using iptables, SNAT all traffice sent via Service cluser IPs (not commonly needed)
401388 #mod_kube_proxy += " | jq '.spec.template.spec.containers[0].command |= .+ [\"--masquerade-all=false\"]'"
402- set_kube_proxy = "#{ get_kube_proxy } #{ mod_kube_proxy } | kubectl apply -f -"
403389
404390 # Update kube-proxy deployment configuration and restart the service
391+ set_kube_proxy = "#{ get_kube_proxy } #{ mod_kube_proxy } | kubectl apply -f -"
405392 puts ( "exec: #{ set_kube_proxy } " )
406393 ssh . exec! ( set_kube_proxy ) { |c , s , o |puts ( o ) }
407394 ssh . exec! ( "kubectl -n kube-system delete po -l 'k8s-app=kube-proxy'" ) { |c , s , o |puts ( o ) }
@@ -410,9 +397,20 @@ class Kubinator
410397 # CoreDNS will be in a pending state until pod networking is deployed
411398 # CoreDNS must be running before joining any worker nodes to the cluster
412399 # https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#pod-network
400+ # validate results: kubectl -n kube-system get ds kube-flannel-ds -o json | '.spec.template.spec.containers[0].args'"
413401 puts ( "Installing pod networking" . colorize ( :cyan ) )
414402 podnet = "https://cloud.weave.works/k8s/net?k8s-version=#{ @k8sver } " if weave
415- podnet = "https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml" if flannel
403+ if flannel
404+ FileUtils . cp ( 'templates/flannel.tpl' , 'config/flannel.conf' )
405+ FileUtils . resolve ( 'config/flannel.conf' , {
406+ iface : "enp0s8" , # the interface to use for inter-host communication
407+ backend_type : "vxlan" , # default: vxlan - backend type
408+ cluster_cidr : cluster_cidr # default: 10.244.0.0/16 - cidr for pod networking
409+ } )
410+ Net ::SCP . upload! ( ips . first , @user , 'config/flannel.conf' , '~/.config/flannel.conf' ,
411+ ssh :{ verify_host_key : :never , password : @pass } )
412+ podnet = ".config/flannel.yml"
413+ end
416414 ssh . exec! ( "#{ proxy_export } ; kubectl apply -f #{ podnet } " ) { |c , s , o |puts ( o ) }
417415 podready! ( 'coredns' , ssh :ssh )
418416 end
@@ -600,6 +598,7 @@ if __FILE__ == $0
600598 Option . new ( nil , 'Cluster command' , required :true , type :Array , allowed :{
601599 all : 'Init cluster and deploy extras' ,
602600 init : 'Initialize the Kubernetes cluster' ,
601+ config : 'Use the kubeadm config template' ,
603602 weave : 'Use the weave pod network' ,
604603 flannel : 'Use the flannel pod network' ,
605604 dashboard : 'Deploy K8s dashboard to cluster' ,
0 commit comments