diff --git a/cmd/init.go b/cmd/init.go index d842baf06..6980681b0 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -40,6 +40,31 @@ var contact = ` 常见问题:sealyun.com/faq ` +var exampleInit = ` + # init with password with three master one node + sealos init --passwd your-server-password \ + --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 \ + --node 192.168.0.5 --user root \ + --version v1.18.0 --pkg-url=/root/kube1.18.0.tar.gz + + # init with pk-file , when your server have different password + sealos init --pk /root/.ssh/id_rsa \ + --master 192.168.0.2 --node 192.168.0.5 --user root \ + --version v1.18.0 --pkg-url=/root/kube1.18.0.tar.gz + + # when use multi network. set a can-reach with --interface + sealos init --interface 192.168.0.254 \ + --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 \ + --node 192.168.0.5 --user root --passwd your-server-password \ + --version v1.18.0 --pkg-url=/root/kube1.18.0.tar.gz + + # when your interface is not "eth*|en*|em*" like. + sealos init --interface your-interface-name \ + --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 \ + --node 192.168.0.5 --user root --passwd your-server-password \ + --version v1.18.0 --pkg-url=/root/kube1.18.0.tar.gz +` + // initCmd represents the init command var initCmd = &cobra.Command{ Use: "init", @@ -47,6 +72,7 @@ var initCmd = &cobra.Command{ Long: `sealos init --master 192.168.0.2 --master 192.168.0.3 --master 192.168.0.4 \ --node 192.168.0.5 --user root --passwd your-server-password \ --version v1.18.0 --pkg-url=/root/kube1.18.0.tar.gz`, + Example: exampleInit, Run: func(cmd *cobra.Command, args []string) { c := &install.SealConfig{} // 没有重大错误可以直接保存配置. 但是apiservercertsans为空. 但是不影响用户 clean @@ -86,7 +112,7 @@ func init() { initCmd.Flags().StringVar(&install.Repo, "repo", "k8s.gcr.io", "choose a container registry to pull control plane images from") initCmd.Flags().StringVar(&install.PodCIDR, "podcidr", "100.64.0.0/10", "Specify range of IP addresses for the pod network") initCmd.Flags().StringVar(&install.SvcCIDR, "svccidr", "10.96.0.0/12", "Use alternative range of IP address for service VIPs") - initCmd.Flags().StringVar(&install.Interface, "interface", "eth.*|en.*|em.*", "name of network interface") + initCmd.Flags().StringVar(&install.Interface, "interface", "eth.*|en.*|em.*", "name of network interface, when use calico IP_AUTODETECTION_METHOD, set your ipv4 with can-reach=192.168.0.1") initCmd.Flags().BoolVar(&install.WithoutCNI, "without-cni", false, "If true we not install cni plugin") initCmd.Flags().StringVar(&install.Network, "network", "calico", "cni plugin, calico..") diff --git a/install/constants.go b/install/constants.go index d7f0d90c9..095f389f0 100644 --- a/install/constants.go +++ b/install/constants.go @@ -16,10 +16,14 @@ const ( // etcd backup ETCDSNAPSHOTDEFAULTNAME = "snapshot" ETCDDEFAULTBACKUPDIR = "/opt/sealos/ectd-backup" - ETCDDEFAULTRESTOREDIR = "/opt/sealos/ectd-restore" - ETCDDATADIR = "/var/lib/etcd" + ETCDDEFAULTRESTOREDIR = "/opt/sealos/ectd-restore" + ETCDDATADIR = "/var/lib/etcd" EtcdCacart = "/root/.sealos/pki/etcd/ca.crt" EtcdCert = "/root/.sealos/pki/etcd/healthcheck-client.crt" EtcdKey = "/root/.sealos/pki/etcd/healthcheck-client.key" TMPDIR = "/tmp" + + // kube file + KUBECONTROLLERCONFIGFILE = "/etc/kubernetes/controller-manager.conf" + KUBESCHEDULERCONFIGFILE = "/etc/kubernetes/scheduler.conf" ) diff --git a/install/etcd_save.go b/install/etcd_save.go index 1392303ba..c967ddccb 100644 --- a/install/etcd_save.go +++ b/install/etcd_save.go @@ -43,7 +43,6 @@ func GetEtcdBackFlags() *EtcdFlags { os.Exit(0) } // get Etcd host - e.EtcdHosts = e.Masters e.BackDir = EtcdBackDir e.Name = SnapshotName e.LongName = fmt.Sprintf("%s/%s", e.BackDir, e.Name) diff --git a/install/init.go b/install/init.go index 1703ab0c9..59277d24f 100644 --- a/install/init.go +++ b/install/init.go @@ -2,6 +2,7 @@ package install import ( "fmt" + "github.com/fanux/sealos/k8s" "io/ioutil" "os" @@ -145,6 +146,14 @@ func (s *SealosInstaller) InstallMaster0() { return } //cmd = `kubectl apply -f /root/kube/conf/net/calico.yaml || true` + + // can-reach is used by calico multi network + if k8s.IsIpv4(Interface) { + Interface = "can-reach=" + Interface + } else { + Interface = "interface=" + Interface + } + netyaml := net.NewNetwork(Network, net.MetaData{ Interface: Interface, CIDR: PodCIDR, @@ -165,4 +174,17 @@ func (s *SealosInstaller) SendKubeConfigs(masters []string, isMaster0 bool) { SendPackage(cert.SealosConfigDir+"/admin.conf", masters, cert.KubernetesDir, nil, nil) SendPackage(cert.SealosConfigDir+"/controller-manager.conf", masters, cert.KubernetesDir, nil, nil) SendPackage(cert.SealosConfigDir+"/scheduler.conf", masters, cert.KubernetesDir, nil, nil) + + // fix > 1.19.1 kube-controller-manager and kube-scheduler use the LocalAPIEndpoint instead of the ControlPlaneEndpoint. + if VersionToIntAll(Version) >= 1191 { + for _, v := range s.Masters { + ip := IpFormat(v) + // use grep -qF if already use sed then skip.... + cmd := fmt.Sprintf(`grep -qF "apiserver.cluster.local" %s && \ +sed -i 's/apiserver.cluster.local/%s/' %s && \ +sed -i 's/apiserver.cluster.local/%s/' %s`,KUBESCHEDULERCONFIGFILE, ip, KUBECONTROLLERCONFIGFILE, ip, KUBESCHEDULERCONFIGFILE) + SSHConfig.CmdAsync(v, cmd) + } + } + } diff --git a/install/send.go b/install/send.go index f1a68343f..39ca2d159 100644 --- a/install/send.go +++ b/install/send.go @@ -3,6 +3,7 @@ package install import ( "fmt" "path" + "sync" ) //SendPackage is @@ -15,13 +16,28 @@ func (s *SealosInstaller) SendPackage() { PkgUrl = SendPackage(PkgUrl, s.Hosts, "/root", nil, &kubeHook) + // override sealos to avoid old version problem + sealos := FetchSealosAbsPath() + beforeHook := "ps -ef |grep -v 'grep'|grep sealos >/dev/null || rm -rf /usr/bin/sealos" + afterHook := "chmod a+x /usr/bin/sealos" + var wg sync.WaitGroup + for _, node := range s.Hosts { + wg.Add(1) + go func(node string) { + defer wg.Done() + SSHConfig.CmdAsync(node,beforeHook) + SSHConfig.CopyLocalToRemote(node, sealos, "/usr/bin/sealos") + SSHConfig.CmdAsync(node, afterHook) + }(node) + } + wg.Wait() } // SendSealos is send the exec sealos to /usr/sbin/sealos func (s *SealosInstaller) SendSealos() { // send sealos first to avoid old version sealos := FetchSealosAbsPath() - beforeHook := "ps -ef |grep -v 'grep'|grep sealos >/dev/null || rm -rf /usr/bin/sealos" + beforeHook := "ps -ef |grep -v 'grep'|grep sealos >/dev/null || rm -rf /usr/sbin/sealos" afterHook := "chmod a+x /usr/sbin/sealos" SendPackage(sealos, s.Hosts, "/usr/sbin", &beforeHook, &afterHook) } \ No newline at end of file diff --git a/install/utils.go b/install/utils.go index f46649231..683463c5a 100644 --- a/install/utils.go +++ b/install/utils.go @@ -133,6 +133,19 @@ func VersionToInt(version string) int { return 0 } +//VersionToIntAll v1.19.1 ==> 1191 +func VersionToIntAll(version string) int { + version = strings.Replace(version,"v","", -1) + arr := strings.Split(version,".") + if len(arr) >= 3 { + str := arr[0] + arr[1] + arr[2] + if i, err := strconv.Atoi(str) ;err == nil { + return i + } + } + return 0 +} + //IpFormat is func IpFormat(host string) string { ipAndPort := strings.Split(host, ":") diff --git a/install/utils_test.go b/install/utils_test.go index 726ad284d..f159e3cc0 100644 --- a/install/utils_test.go +++ b/install/utils_test.go @@ -144,3 +144,24 @@ func TestFileExist(t *testing.T) { }) } } + +func TestVersionToIntAll(t *testing.T) { + type args struct { + version string + } + tests := []struct { + name string + args args + want int + }{ + {"test01", args{"v1.19.1"}, 1191}, + {"test01", args{"v1.15.1"}, 1151}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := VersionToIntAll(tt.args.version); got != tt.want { + t.Errorf("VersionToInt() = %v, want %v", got, tt.want) + } + }) + } +} \ No newline at end of file diff --git a/net/calico.go b/net/calico.go index 29a2569f1..2543b0638 100644 --- a/net/calico.go +++ b/net/calico.go @@ -9,7 +9,7 @@ func (c Calico) Manifests(template string) string { template = c.Template() } if c.metadata.Interface == "" { - c.metadata.Interface = defaultInterface + c.metadata.Interface = "interface=" + defaultInterface } if c.metadata.CIDR == "" { c.metadata.CIDR = defaultCIDR @@ -627,7 +627,7 @@ spec: - name: IP value: "autodetect" - name: IP_AUTODETECTION_METHOD - value: "interface={{ .Interface }}" + value: "{{ .Interface }}" # Enable IPIP - name: CALICO_IPV4POOL_IPIP value: "{{if not .IPIP }}Off{{else}}Always{{end}}" diff --git a/net/net_test.go b/net/net_test.go index bdd6fb932..b2d58436d 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -7,7 +7,15 @@ import ( func TestNewNetwork(t *testing.T) { netyaml := NewNetwork("calico", MetaData{ - Interface: "en.*|eth.*", + Interface: "interface=en.*|eth.*", + CIDR: "10.1.1.1/24", + IPIP: true, + MTU: "1440", + }).Manifests("") + fmt.Println(netyaml) + + netyaml = NewNetwork("calico", MetaData{ + Interface: "can-reach=192.168.160.1", CIDR: "10.1.1.1/24", IPIP: true, MTU: "1440",