Skip to content

Commit

Permalink
Merge pull request #61 from galexrt/docker_install_from_officialrepo
Browse files Browse the repository at this point in the history
Docker install from officialrepo
  • Loading branch information
galexrt authored Mar 29, 2020
2 parents 63f6379 + 1a01b8f commit bb8e265
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 44 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ MFILECWD = $(shell pwd)
REVERSE_LINES=sed -e '1!G;h;$$!d'

# === BEGIN USER OPTIONS ===
KUBECTL ?= kubectl
# Vagrantfile set to use.
BOX_OS ?= fedora
# Vagrant Provider
Expand Down Expand Up @@ -95,12 +96,16 @@ endif

up: preflight ## Start Kubernetes Vagrant multi-node cluster. Creates, starts and bootsup the master and node VMs.
@$(MAKE) start
@echo
$(KUBECTL) get nodes
@echo
@echo "Your k8s-vagrant-multi-node Kuberenetes cluster should be ready now."

start: preflight pull
ifeq ($(VAGRANT_DEFAULT_PROVIDER), "virtualbox")
@$(MAKE) start-master start-nodes
else
# Need to start master and nodes separately due to some weird IP assignment side effects
# Need to start master and nodes separately due to some weird IP assignment side effects (at least on my machine)
@$(MAKE) start-master
@$(MAKE) start-nodes
endif
Expand Down Expand Up @@ -128,14 +133,14 @@ kubectl: ## Configure kubeconfig context for the cluster using `kubectl config`
> $(CLUSTERCERTSDIR)/client-key.key

# kubectl create cluster
kubectl \
$(KUBECTL) \
config set-cluster \
$(CLUSTER_NAME) \
--embed-certs=true \
--server=https://$(MASTER_IP):6443 \
--certificate-authority=$(CLUSTERCERTSDIR)/ca.crt
# kubectl create user
kubectl \
$(KUBECTL) \
config set-credentials \
$(CLUSTER_NAME)-kubernetes-admin \
--embed-certs=true \
Expand All @@ -144,7 +149,7 @@ kubectl: ## Configure kubeconfig context for the cluster using `kubectl config`
--client-key=$(CLUSTERCERTSDIR)/client-key.key
@rm -rf $(CLUSTERCERTSDIR)
# kubectl create context
kubectl \
$(KUBECTL) \
config set-context \
$(CLUSTER_NAME) \
--cluster=$(CLUSTER_NAME) \
Expand Down
1 change: 1 addition & 0 deletions vagrantfiles/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Vagrant.configure('2') do |config|
subconfig.vm.provision :shell, inline: $osPrepareScript
end
subconfig.vm.provision :shell, inline: $prepareScript
subconfig.vm.provision :shell, inline: $containerRuntimeInstallScript
subconfig.vm.provision :shell, inline: $baseInstallScript
subconfig.vm.provision :shell, inline: $verifyNodeScript
# Run diskandreboot plugin to add VirtualBox disks to VMs
Expand Down
66 changes: 44 additions & 22 deletions vagrantfiles/Vagrantfile_vars
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ KUBE_PROXY_IPVS = ENV['KUBE_PROXY_IPVS'] || false
# Generate new using steps in README
KUBETOKEN = ENV['KUBETOKEN'] || 'b029ee.968a33e8d8e6bb0d'

if $kubeletExtraArgsFile.nil? || $kubeletExtraArgsFile == ""
$kubeletExtraArgsFile = '/etc/sysconfig/kubelet'
end

# The controller name is different per OS it seems (this is the case for at least Ubuntu right now).
# For more information, see https://github.com/kusnier/vagrant-persistent-storage/issues/33.
$storagecontroller = 'k8svmSATAController'
Expand Down Expand Up @@ -111,6 +115,22 @@ kubelet --version || { echo "Something wrong with kubelet"; exit 1; }

systemctl enable kubelet

mkdir -p /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF

mkdir -p /etc/systemd/system/docker.service.d
SCRIPT

$kubeMasterScript = <<SCRIPT
Expand All @@ -119,32 +139,32 @@ set -x
kubeadm reset -f

retries=5
for ((i=0; i<retries; i++)); do
# For now ignore SystemVerification error
for ((i=1; i<=retries; i++)); do
# For now ignore preflight SystemVerification error
kubeadm init \
#{$kubeadm_init_flags} \
--ignore-preflight-errors=SystemVerification \
--apiserver-advertise-address=#{MASTER_IP} \
--pod-network-cidr=#{POD_NW_CIDR} \
--token "#{KUBETOKEN}" \
--token-ttl 0 && \
break
break

[[ $retries -eq i ]] && { echo "Failed to run kubeadm init after 5 tries"; exit 1; }
echo "kubeadm join failed, trying again in 3 seconds (try ${i}/${retries})..."
sleep 3
done
[[ $retries -eq i ]] && { echo "Failed to run kubeadm init after 5 tries"; exit 1; }

KUBELET_EXTRA_ARGS_FILE=/etc/sysconfig/kubelet
if [ -f /etc/default/kubelet ]; then
KUBELET_EXTRA_ARGS_FILE=/etc/default/kubelet
else
KUBELET_EXTRA_ARGS_FILE=/etc/sysconfig/kubelet
fi
KUBELET_EXTRA_ARGS_FILE="#{$kubeletExtraArgsFile}"

if grep -q -- '--node-ip=' "$KUBELET_EXTRA_ARGS_FILE"; then
sed -ri -e 's|KUBELET_EXTRA_ARGS=--node-ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|KUBELET_EXTRA_ARGS=|' "$KUBELET_EXTRA_ARGS_FILE"
if [ -f "$KUBELET_EXTRA_ARGS_FILE" ]; then
if grep -q -- '--node-ip=' "$KUBELET_EXTRA_ARGS_FILE"; then
sed -ri -e 's|KUBELET_EXTRA_ARGS=--node-ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|KUBELET_EXTRA_ARGS=|' "$KUBELET_EXTRA_ARGS_FILE"
fi
sed -i 's|KUBELET_EXTRA_ARGS=|KUBELET_EXTRA_ARGS=--node-ip=#{NODE_IP} --cni-bin-dir=/opt/cni/bin,/usr/libexec/cni |' "$KUBELET_EXTRA_ARGS_FILE"
else
echo "KUBELET_EXTRA_ARGS=--node-ip=#{NODE_IP} " > "$KUBELET_EXTRA_ARGS_FILE"
fi
sed -i 's|KUBELET_EXTRA_ARGS=|KUBELET_EXTRA_ARGS=--node-ip=#{NODE_IP} --cni-bin-dir=/opt/cni/bin,/usr/libexec/cni |' "$KUBELET_EXTRA_ARGS_FILE"

systemctl daemon-reload
systemctl restart kubelet.service
Expand Down Expand Up @@ -185,7 +205,7 @@ set -x
kubeadm reset -f

retries=5
for ((i=0; i<retries; i++)); do
for ((i=1; i<=retries; i++)); do
# For now ignore SystemVerification error
kubeadm join \
#{$kubeadm_join_flags} \
Expand All @@ -194,20 +214,22 @@ for ((i=0; i<retries; i++)); do
--token "#{KUBETOKEN}" \
"#{MASTER_IP}:6443" && \
break

[[ $retries -eq i ]] && { echo "Failed to run kubeadm join after 5 tries"; exit 1; }
echo "kubeadm join failed, trying again in 3 seconds (try ${i}/${retries})..."
sleep 3
done
[[ $retries -eq i ]] && { echo "Failed to run kubeadm join after 5 tries"; exit 1; }

KUBELET_EXTRA_ARGS_FILE=/etc/sysconfig/kubelet
if [ ! -f /etc/sysconfig/kubelet ]; then
KUBELET_EXTRA_ARGS_FILE=/etc/default/kubelet
fi
KUBELET_EXTRA_ARGS_FILE="#{$kubeletExtraArgsFile}"

if grep -q -- '--node-ip=' "$KUBELET_EXTRA_ARGS_FILE"; then
sed -ri -e 's/KUBELET_EXTRA_ARGS=--node-ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/KUBELET_EXTRA_ARGS=/' "$KUBELET_EXTRA_ARGS_FILE"
if [ -f "$KUBELET_EXTRA_ARGS_FILE" ]; then
if grep -q -- '--node-ip=' "$KUBELET_EXTRA_ARGS_FILE"; then
sed -ri -e 's|KUBELET_EXTRA_ARGS=--node-ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|KUBELET_EXTRA_ARGS=|' "$KUBELET_EXTRA_ARGS_FILE"
fi
sed -i 's|KUBELET_EXTRA_ARGS=|KUBELET_EXTRA_ARGS=--node-ip=#{NODE_IP} --cni-bin-dir=/opt/cni/bin,/usr/libexec/cni |' "$KUBELET_EXTRA_ARGS_FILE"
else
echo "KUBELET_EXTRA_ARGS=--node-ip=#{NODE_IP} " > "$KUBELET_EXTRA_ARGS_FILE"
fi
sed -i 's|KUBELET_EXTRA_ARGS=|KUBELET_EXTRA_ARGS=--node-ip=#{NODE_IP} --cni-bin-dir=/opt/cni/bin,/usr/libexec/cni |' "$KUBELET_EXTRA_ARGS_FILE"

systemctl daemon-reload
systemctl restart kubelet.service
Expand Down
19 changes: 19 additions & 0 deletions vagrantfiles/centos/common
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
$box_image = ENV['BOX_IMAGE'] || 'centos/7'.freeze

# Stop and disable firewalld service
$osPrepareScript = <<SCRIPT
set -x

systemctl stop firewalld || true
systemctl disable firewalld || true
systemctl mask firewalld || true
SCRIPT

$containerRuntimeInstallScript = <<SCRIPT
set -x

retries=5
for ((i=1; i<=retries; i++)); do
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io && \
break

[[ $retries -eq i ]] && { echo "Failed to install docker-ce after 5 tries"; exit 1; }
done
SCRIPT
26 changes: 25 additions & 1 deletion vagrantfiles/fedora/common
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,32 @@ $box_image = ENV['BOX_IMAGE'] || 'fedora/30-cloud-base'.freeze

# Use iptables-legacy for iptables
$osPrepareScript = <<SCRIPT
set -x

if [ ! -f /usr/sbin/iptables-legacy ]; then
dnf install -y iptables
retries=5
for ((i=1; i<=retries; i++)); do
dnf install -y iptables && \
break

[[ $retries -eq i ]] && { echo "Failed to install iptables after 5 tries"; exit 1; }
done
fi
update-alternatives --set iptables /usr/sbin/iptables-legacy
SCRIPT

$containerRuntimeInstallScript = <<SCRIPT
set -x

retries=5
for ((i=1; i<retries; i++)); do
dnf -y install dnf-plugins-core
dnf config-manager \
--add-repo \
https://download.docker.com/linux/fedora/docker-ce.repo
dnf install -y docker-ce docker-ce-cli containerd.io && \
break

[[ $retries -eq i ]] && { echo "Failed to install docker-ce after 5 tries"; exit 1; }
done
SCRIPT
2 changes: 1 addition & 1 deletion vagrantfiles/fedora/master
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config

# Use yum so we can use this for CentOS too
yum clean expire-cache
yum install --nogpgcheck -y net-tools screen tree telnet conntrack socat docker rsync ${KUBERNETES_PACKAGES}
yum install --nogpgcheck -y net-tools screen tree telnet conntrack socat rsync ${KUBERNETES_PACKAGES}

systemctl enable docker && systemctl start docker
SCRIPT
19 changes: 19 additions & 0 deletions vagrantfiles/ubuntu/common
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,22 @@ if $kubernetes_pkg_version_suffix == ''
end

$docker_version = '5:18.09.1~3-0~ubuntu-bionic'

$containerRuntimeInstallScript = <<SCRIPT
set -x

curl --retry 5 -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

retries=5
for ((i=1; i<=retries; i++)); do
apt-get update
apt-get -y install docker-ce=#{$docker_version} && \
break

[[ $retries -eq i ]] && { echo "Failed to install docker-ce after 5 tries"; exit 1; }
done
SCRIPT
22 changes: 6 additions & 16 deletions vagrantfiles/ubuntu/master
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
$baseInstallScript = <<SCRIPT
set -x

retries=5
for ((i=0; i<retries; i++)); do
apt-get update
apt-get -y install apt-transport-https curl software-properties-common ca-certificates && \
break
done
[[ $retries -eq i ]] && { echo "Failed to install apt-transport-https and other dependenices after 5 tries"; exit 1; }

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
curl curl --retry 5 --fail -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
Expand All @@ -26,15 +13,18 @@ else
fi

retries=5
for ((i=0; i<retries; i++)); do
for ((i=1; i<=retries; i++)); do
apt-get update
apt-get -y install screen telnet conntrack socat docker-ce=#{$docker_version} ${KUBERNETES_PACKAGES} && \
apt-get -y install screen telnet conntrack socat ${KUBERNETES_PACKAGES} && \
break

[[ $retries -eq i ]] && { echo "Failed to install kubelet, kubeadm and other dependenices after 5 tries"; exit 1; }
done
[[ $retries -eq i ]] && { echo "Failed to install docker, kubelet, kubeadm and other dependenices after 5 tries"; exit 1; }

apt-mark hold kubelet kubeadm kubectl

# Increase /run dir as it is/was a problem on Ubuntu 18.04 (at least on bento box image)
echo 'tmpfs /run tmpfs rw,nosuid,noexec,relatime,size=614460k,mode=755' >> /etc/fstab

systemctl enable docker && systemctl start docker
SCRIPT
12 changes: 12 additions & 0 deletions vagrantfiles/ubuntu/pre
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
$kube_network = 'canal'

$osPrepareScript = <<SCRIPT
set -x

sed -i 's/^DNS=.*/DNS=8.8.8.8 8.8.4.4/' /etc/systemd/resolved.conf

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
sudo update-alternatives --set arptables /usr/sbin/arptables-legacy
sudo update-alternatives --set ebtables /usr/sbin/ebtables-legacy

retries=5
for ((i=1; i<=retries; i++)); do
apt-get update
apt-get -y install apt-transport-https curl software-properties-common ca-certificates && \
break

[[ $retries -eq i ]] && { echo "Failed to install apt-transport-https and other dependenices after 5 tries"; exit 1; }
done
SCRIPT

$kubeletExtraArgsFile = '/etc/default/kubelet'

0 comments on commit bb8e265

Please sign in to comment.