Created
August 7, 2018 08:58
-
-
Save sgdan/405fc74adab7443677b0f2935bb4bf2e to your computer and use it in GitHub Desktop.
Create single node kubernetes cluster with kubeadm using vagrant and ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$script = <<-SCRIPT | |
#!/bin/sh | |
set -ex | |
apt-get update && apt-get install -y apt-transport-https | |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - | |
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee > /etc/apt/sources.list.d/kubernetes.list | |
apt-get update && apt-get install -y docker.io kubeadm | |
swapoff -a | |
sudo sed -i.bak '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab | |
systemctl enable docker.service | |
sudo kubeadm config images pull | |
IP_ADDRESS=`ifconfig eth1 | grep 'inet ' | awk '{print $2}'` | |
sudo kubeadm init --apiserver-cert-extra-sans=$IP_ADDRESS --node-name=`hostname` | |
# configure kubectl | |
mkdir -p /home/vagrant/.kube | |
sudo cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config | |
sudo chown $(id -u):$(id -g) /home/vagrant/.kube/config | |
# allow pods to run on this master node | |
kubectl taint nodes --all node-role.kubernetes.io/master- | |
# install network plugin | |
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/kubeadm/1.7/calico.yaml | |
# install dashboard | |
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml | |
grep 'client-certificate-data' ~/.kube/config | awk '{print $2}' | base64 -d >> kubecfg.crt | |
grep 'client-key-data' ~/.kube/config | awk '{print $2}' | base64 -d >> kubecfg.key | |
#openssl pkcs12 -export -clcerts -inkey kubecfg.key -in kubecfg.crt -out kubecfg.p12 -name "kubernetes-client" | |
# create service account | |
cat <<EOF | kubectl create -f - | |
apiVersion: v1 | |
kind: ServiceAccount | |
metadata: | |
name: admin-user | |
namespace: kube-system | |
EOF | |
# create ClusterRoleBinding | |
cat <<EOF | kubectl create -f - | |
apiVersion: rbac.authorization.k8s.io/v1 | |
kind: ClusterRoleBinding | |
metadata: | |
name: admin-user | |
roleRef: | |
apiGroup: rbac.authorization.k8s.io | |
kind: ClusterRole | |
name: cluster-admin | |
subjects: | |
- kind: ServiceAccount | |
name: admin-user | |
namespace: kube-system | |
EOF | |
echo Dashboard bearer token: | |
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}') | |
SCRIPT | |
Vagrant.configure("2") do |config| | |
config.vm.provider "virtualbox" do |v| | |
v.name = "k8s" | |
v.cpus = 4 | |
v.memory = 6144 | |
end | |
config.vm.box = "bento/ubuntu-18.04" | |
config.vm.hostname = "k8s" | |
config.vm.network :private_network, ip: "192.168.66.100" | |
config.vm.provision "shell", inline: $script | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment