Skip to content

Commit

Permalink
Merge pull request #113 from banzaicloud/centos8-support
Browse files Browse the repository at this point in the history
CentOS 8 support
  • Loading branch information
sagikazarmark authored Oct 7, 2020
2 parents 135c8e7 + 0f172ef commit b3ca246
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 5 deletions.
83 changes: 83 additions & 0 deletions Vagrantfile-centos8
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
$enable_serial_logging = false

raise "vagrant-vbguest plugin must be installed" unless Vagrant.has_plugin? "vagrant-vbguest"

Vagrant.configure("2") do |config|
# Sync time with the local host
config.vm.provider 'virtualbox' do |vb|
vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 1000 ]
end

# sync build folder
config.vm.synced_folder '.', '/vagrant', disabled: true
config.vm.synced_folder 'scripts/vagrant/', '/scripts/', create: true
config.vm.synced_folder 'build/', '/banzaicloud/', create: true

$num_instances = 4

# centos 7 nodes
(1..$num_instances).each do |n|
config.vm.define "centos#{n}" do |node|
node.vm.box = "centos/8"
node.vm.network "private_network", ip: "192.168.64.#{n+10}"
node.vm.hostname = "centos#{n}"

# Monkey patch for https://github.com/dotless-de/vagrant-vbguest/issues/367
class Foo < VagrantVbguest::Installers::CentOS
def has_rel_repo?
unless instance_variable_defined?(:@has_rel_repo)
rel = release_version
@has_rel_repo = communicate.test("yum repolist")
end
@has_rel_repo
end

def install_kernel_devel(opts=nil, &block)
cmd = "yum update kernel -y"
communicate.sudo(cmd, opts, &block)

cmd = "yum install -y kernel-devel"
communicate.sudo(cmd, opts, &block)

cmd = "shutdown -r now"
communicate.sudo(cmd, opts, &block)

begin
sleep 5
end until @vm.communicate.ready?
end
end
node.vbguest.installer = Foo

node.vm.provider "virtualbox" do |vb|
vb.name = "centos#{n}"
vb.memory = "2048"
vb.cpus = "2"
vb.customize ["modifyvm", :id, "--audio", "none"]
vb.customize ["modifyvm", :id, "--memory", "2048"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end

node.vm.provision "shell" do |s|
s.inline = <<-SHELL
dnf install -y yum-utils wget curl chrony vim net-tools socat
echo 'sync time'
systemctl enable --now chronyd
swapoff -a

echo 'set host name resolution'
cat >> /etc/hosts <<EOF
192.168.64.11 centos1
192.168.64.12 centos2
192.168.64.13 centos3
192.168.64.14 centos4
EOF
cat /etc/hosts

hostnamectl set-hostname centos#{n}

SHELL
end
end
end
end
54 changes: 54 additions & 0 deletions centos8-multi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash -e

# prerequisitesSkipping phase due to missing Pipeline API endpoint credentials
jq --version || (echo "Please install jq command line tool. https://stedolan.github.io/jq/" && exit 1)

# build latest pke tool
GOOS=linux make pke

KUBERNETES_VERSION="${1:-v1.19.2}"
export VAGRANT_VAGRANTFILE=Vagrantfile-centos8

# install first master node
echo ""
echo "= centos1 ========================================================================"
vagrant up centos1
vagrant ssh centos1 -c "sudo sh -c 'echo -n "LANG=en_US.utf-8\nLC_ALL=en_US.utf-8" > /etc/environment'"
vagrant ssh centos1 -c "sudo /scripts/pke-multi-master1.sh '$KUBERNETES_VERSION' '192.168.64.11:6443'"
vagrant ssh centos1 -c 'sudo cat /etc/kubernetes/admin.conf' > pke-multi-config.yaml
vagrant ssh centos1 -c "sudo /banzaicloud/pke token list -o json" > build/token.json


TOKEN=`jq -r '.tokens[] | select(.expired == false) | .token' build/token.json`
CERTHASH=`jq -r '.tokens[] | select(.expired == false) | .hash' build/token.json`

echo ""
echo "Using '$TOKEN' and '$CERTHASH' to join other nodes to the cluster"

# install second master node
echo ""
echo "= centos2 ========================================================================"
vagrant up centos2
vagrant ssh centos2 -c "sudo /scripts/pke-multi-mastern.sh '$KUBERNETES_VERSION' '192.168.64.11:6443' '$TOKEN' '$CERTHASH' 192.168.64.12"

# install third master node
echo ""
echo "= centos3 ========================================================================"
vagrant up centos3
vagrant ssh centos3 -c "sudo /scripts/pke-multi-mastern.sh '$KUBERNETES_VERSION' '192.168.64.11:6443' '$TOKEN' '$CERTHASH' 192.168.64.13"

# install worker node
echo ""
echo "= centos4 ========================================================================"
vagrant up centos4
vagrant ssh centos4 -c "sudo /scripts/pke-multi-worker.sh '$KUBERNETES_VERSION' '192.168.64.11:6443' '$TOKEN' '$CERTHASH'"

export KUBECONFIG=$PWD/pke-multi-config.yaml

echo ""
echo "You can access your PKE cluster either:"
echo "- from your host machine accessing the cluster via kubectl. Please run:"
echo "export KUBECONFIG=$PWD/pke-multi-config.yaml"
echo ""
echo "- or starting a shell in the virtual machine. Please run:"
echo "vagrant ssh centos1 -c 'sudo -s'"
21 changes: 21 additions & 0 deletions centos8-single.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash -e

# build latest pke tool
GOOS=linux make pke

KUBERNETES_VERSION="${1:-v1.19.2}"

export VAGRANT_VAGRANTFILE=Vagrantfile-centos8
vagrant up centos1
vagrant ssh centos1 -c "sudo /scripts/pke-single.sh '$KUBERNETES_VERSION' '192.168.64.11:6443' containerd cilium"
vagrant ssh centos1 -c 'sudo cat /etc/kubernetes/admin.conf' > pke-single-config.yaml

export KUBECONFIG=$PWD/pke-single-config.yaml

echo ""
echo "You can access your PKE cluster either:"
echo "- from your host machine accessing the cluster via kubectl. Please run:"
echo "export KUBECONFIG=$PWD/pke-single-config.yaml"
echo ""
echo "- or starting a shell in the virtual machine. Please run:"
echo "vagrant ssh centos1 -c 'sudo -s'"
16 changes: 12 additions & 4 deletions cmd/pke/app/util/linux/autodetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ func KubernetesPackagesImpl(out io.Writer) (KubernetesPackages, error) {
}
if err == nil {
v, _ := semver.NewVersion(ver)
c, _ := semver.NewConstraint("7.x-0 || 8.x-0")
if c.Check(v) {
c7, _ := semver.NewConstraint("7.x-0")
c8, _ := semver.NewConstraint("8.x-0")
if c7.Check(v) {
return NewYumInstaller(), nil
}
if c8.Check(v) {
return NewDnfInstaller(), nil
}
}

if distro, err := LSBReleaseDistributorID(out); err == nil {
Expand All @@ -56,10 +60,14 @@ func ContainerdPackagesImpl(out io.Writer) (ContainerdPackages, error) {
}
if err == nil {
v, _ := semver.NewVersion(ver)
c, _ := semver.NewConstraint("7.x-0 || 8.x-0")
if c.Check(v) {
c7, _ := semver.NewConstraint("7.x-0")
c8, _ := semver.NewConstraint("8.x-0")
if c7.Check(v) {
return NewYumInstaller(), nil
}
if c8.Check(v) {
return NewDnfInstaller(), nil
}
}

if distro, err := LSBReleaseDistributorID(out); err == nil {
Expand Down
132 changes: 132 additions & 0 deletions cmd/pke/app/util/linux/dnf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright © 2020 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package linux

import (
"fmt"
"io"
"os"

"emperror.dev/errors"
"github.com/banzaicloud/pke/cmd/pke/app/util/file"
"github.com/banzaicloud/pke/cmd/pke/app/util/runner"
)

const (
cmdDnf = "/bin/dnf"
k8sRPMRepoDnf = `[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg`
)

func DnfInstall(out io.Writer, packages []string) error {
_, err := runner.Cmd(out, cmdDnf, append([]string{"install", "-y"}, packages...)...).CombinedOutputAsync()
if err != nil {
return err
}

for _, pkg := range packages {
if pkg == "" {
continue
}
if pkg[:1] == "-" {
continue
}

name, ver, rel, arch, err := rpmQuery(out, pkg)
if err != nil {
return err
}
if name == pkg ||
name+"-"+ver == pkg ||
name+"-"+ver+"-"+rel == pkg ||
name+"-"+ver+"-"+rel+"."+arch == pkg {
continue
}
return errors.New(fmt.Sprintf("expected package version after installation: %q, got: %q", pkg, name+"-"+ver+"-"+rel+"."+arch))
}

return nil
}

var _ ContainerdPackages = (*DnfInstaller)(nil)
var _ KubernetesPackages = (*DnfInstaller)(nil)

type DnfInstaller struct{}

func NewDnfInstaller() *DnfInstaller {
return &DnfInstaller{}
}

func (y *DnfInstaller) InstallKubernetesPrerequisites(out io.Writer, kubernetesVersion string) error {
if err := SwapOff(out); err != nil {
return err
}

if err := ModprobeKubeProxyIPVSModules(out); err != nil {
return err
}

if err := SysctlLoadAllFiles(out); err != nil {
return errors.Wrapf(err, "unable to load all sysctl rules from files")
}

if _, err := os.Stat(banzaiCloudRPMRepo); err != nil {
err = file.Overwrite(k8sRPMRepoFile, k8sRPMRepoDnf)
if err != nil {
return err
}
}

if _, err := runner.Cmd(out, cmdDnf, "update", "-y").CombinedOutputAsync(); err != nil {
return err
}

return nil
}

func (y *DnfInstaller) InstallKubernetesPackages(out io.Writer, kubernetesVersion string) error {
// dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
p := []string{
"kubelet-" + kubernetesVersion,
"kubeadm-" + kubernetesVersion,
"kubectl-" + kubernetesVersion,
}

return DnfInstall(out, p)
}

func (y *DnfInstaller) InstallKubeadmPackage(out io.Writer, kubernetesVersion string) error {
// dnf install -y kubeadm --disableexcludes=kubernetes
pkg := []string{
"kubelet-" + kubernetesVersion,
"kubeadm-" + kubernetesVersion,
}

return DnfInstall(out, pkg)
}

func (y *DnfInstaller) InstallContainerdPrerequisites(out io.Writer, containerdVersion string) error {
// dnf install -y libseccomp
if err := DnfInstall(out, []string{"libseccomp"}); err != nil {
return errors.Wrap(err, "unable to install libseccomp package")
}

return nil
}
4 changes: 3 additions & 1 deletion scripts/vagrant/pke-single.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
KUBERNETES_VERSION=$1
APISERVER_ADDRESS="${2:-192.168.64.11:6443}"
CONTAINER_RUNTIME="${3:-containerd}"
NETWORK_PROVIDER="${4:-cilium}"

systemctl is-active kubelet || ( \
/banzaicloud/pke version -o yaml || ( \
Expand All @@ -14,7 +15,8 @@ systemctl is-active kubelet || ( \
--kubernetes-version="${KUBERNETES_VERSION}" \
--kubernetes-container-runtime="${CONTAINER_RUNTIME}" \
--kubernetes-advertise-address="${APISERVER_ADDRESS}" \
--kubernetes-api-server="${APISERVER_ADDRESS}" && \
--kubernetes-api-server="${APISERVER_ADDRESS}" \
--kubernetes-network-provider="${NETWORK_PROVIDER}" && \
mkdir -p $HOME/.kube && \
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config && \
chown $(id -u):$(id -g) $HOME/.kube/config
Expand Down

0 comments on commit b3ca246

Please sign in to comment.