-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vagrantfile
331 lines (261 loc) · 10.9 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
# Minikube
KUBERNETES_VERSION = ENV['KUBERNETES_VERSION'] || "1.16.3" # OK
$ubuntu_docker_script = <<-SCRIPT
# vg-minikube01: Package 'docker.io' is not installed, so not removed
# vg-minikube01: E: Unable to locate package docker
# vg-minikube01: E: Unable to locate package docker-engine
# The SSH command responded with a non-zero exit status. Vagrant
# assumes that this means the command failed. The output for this command
# should be in the log above. Please read the output to determine what
# went wrong.
# Uninstall old versions
# apt-get remove docker docker-engine docker.io containerd runc -y
# Set up the repository
apt-get update -y
apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release -y
# Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# set up the stable repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install Docker Engine
apt-get update -y
apt-get install \
docker-ce \
docker-ce-cli \
containerd.io -y
docker --version
# Verify that Docker Engine is installed correctly
docker run hello-world
# Post-installation steps for Linux
# Manage Docker as a non-root user
# Create the docker group
groupadd docker
# Add your user to the docker group
# usermod -aG docker $USER # by default run by root
usermod -aG docker vagrant
SCRIPT
$installer = <<SCRIPT
#!/bin/bash
sudo apt-get -y update
sudo apt-get install -y zip unzip curl wget socat ebtables
SCRIPT
$docker = <<SCRIPT
#!/bin/bash
#curl -fsSL https://apt.dockerproject.org/gpg | sudo apt-key add -
#sudo apt-add-repository "deb https://apt.dockerproject.org/repo ubuntu-xenial main"
#sudo apt-get install -y docker-engine=17.03.1~ce-0~ubuntu-xenial
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-get install -y docker-ce
sudo systemctl start docker
sudo usermod -a -G docker vagrant
SCRIPT
$minikubescript = <<SCRIPT
#!/bin/bash
echo "current user is $(whoami)"
echo "current directory is $(pwd)"
#Install minikube
echo "Downloading Minikube"
curl -q -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 2>/dev/null
chmod +x minikube
sudo mv minikube /usr/local/bin/
stat /usr/local/bin/minikube # verify
#https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/
#Install kubectl
echo "Downloading Kubectl"
curl -q -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v${KUBERNETES_VERSION}/bin/linux/amd64/kubectl 2>/dev/null
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
stat /usr/local/bin/kubectl # verify
# Install crictl
curl -qL https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.16.1/crictl-v1.16.1-linux-amd64.tar.gz 2>/dev/null | tar xzvf -
chmod +x crictl
sudo mv crictl /usr/local/bin/
stat /usr/local/bin/crictl # verify
#Install stern
# TODO: Check sha256sum
echo "Downloading Stern"
curl -q -Lo stern https://github.com/wercker/stern/releases/download/1.10.0/stern_linux_amd64 2>/dev/null
chmod +x stern
sudo mv stern /usr/local/bin/
stat /usr/local/bin/stern # verify
#Install kubecfg
# TODO: Check sha256sum
echo "Downloading Kubecfg"
curl -q -Lo kubecfg https://github.com/ksonnet/kubecfg/releases/download/v0.9.0/kubecfg-linux-amd64 2>/dev/null
chmod +x kubecfg
sudo mv kubecfg /usr/local/bin/
stat /usr/local/bin/kubecfg # verify
#Setup minikube
echo "127.0.0.1 minikube minikube." | sudo tee -a /etc/hosts
mkdir -p $HOME/.minikube
mkdir -p $HOME/.kube
touch $HOME/.kube/config
export KUBECONFIG=$HOME/.kube/config
# Permissions
echo "USER...:$USER"
echo "HOME...:$HOME"
sudo chown -R $USER:$USER $HOME/.kube
sudo chown -R $USER:$USER $HOME/.minikube
export MINIKUBE_WANTUPDATENOTIFICATION=false
export MINIKUBE_WANTREPORTERRORPROMPT=false
export MINIKUBE_HOME=$HOME
export CHANGE_MINIKUBE_NONE_USER=true
export KUBECONFIG=$HOME/.kube/config
# v1.24.3
apt-get update && apt-get -qq -y install conntrack #http://conntrack-tools.netfilter.org/
# Disable SWAP since is not supported on a kubernetes cluster
sudo swapoff -a
## Start minikube
sudo -E minikube start \
-v 4 --vm-driver none \
--kubernetes-version v${KUBERNETES_VERSION} \
--bootstrapper kubeadm
minikube version --short
minikube version --components
minikube status
#post-install checks
kubectl cluster-info
kubectl get nodes
kubectl get pod
## Addons
sudo -E minikube addons enable ingress
minikube addons list #verify
# Permissions
sudo chown -R $USER:$USER $HOME/.kube
sudo chown -R $USER:$USER $HOME/.minikube
# Enforce sysctl
sudo sysctl -w vm.max_map_count=262144
sudo echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.d/90-vm_max_map_count.conf
kubectl get pod -n default -o wide --all-namespaces
kubectl -n default get services
#https://kubernetes.io/docs/tutorials/hello-minikube/
echo "================================"
echo "Running Hello Minikube tutorial"
echo "================================"
#Create a Deployment
#A Kubernetes Pod is a group of one or more Containers, tied together for the purposes of administration and networking
#The Pod in this tutorial has only one Container.
#A Kubernetes Deployment checks on the health of your Pod and restarts the Pod's Container if it terminates.
#Deployments are the recommended way to manage the creation and scaling of Pods.
#create a Deployment that manages a Pod. The Pod runs a Container based on the provided Docker image
kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4
#View the Deployment
kubectl get deployments
#View the Pod
kubectl get pods
#View cluster events
kubectl get events
#View the kubectl configuration
kubectl config view
#Create a Service
#By default, the Pod is only accessible by its internal IP address within the Kubernetes cluster.
#To make the hello-node Container accessible from outside the Kubernetes virtual network
#expose the Pod as a Kubernetes Service.
#Expose the Pod to the public internet
#The --type=LoadBalancer flag indicates to expose Service outside of the cluster
#The application code inside the image k8s.gcr.io/echoserver only listens on TCP port 8080
#On minikube, the LoadBalancer type makes the Service accessible through the minikube service
kubectl expose deployment hello-node --type=LoadBalancer --port=8080
#View the Service
kubectl get services
minikube service hello-node
#Verify
kubectl get services
#Enable addons
#List the currently supported addons
minikube addons list
#Enable metrics-server addon
minikube addons enable metrics-server
#View the Pod and Service created
kubectl get pod,svc -n kube-system
#Disable metrics-server
# minikube addons disable metrics-server
#Clean up the resources
# kubectl delete service hello-node
# kubectl delete deployment hello-node
#stop the Minikube virtual machine (VM)
# echo "stopping minikube.."
# minikube stop
#delete the Minikube VM
# minikube delete
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox" do |vb|
# vb.gui = false
vb.memory = "1024"
vb.cpus = 2
# vb.customize ["modifyvm", :id, "--groups", "/kali-sandbox"] # create vbox group
end
config.vm.define "vg-minikube-01" do |kalicluster|
# https://app.vagrantup.com/ubuntu/boxes/hirsute64
# kalicluster.vm.box = "ubuntu/hirsute64" #21.04
# https://app.vagrantup.com/ubuntu/boxes/impish64
# kalicluster.vm.box = "ubuntu/impish64" #21.10
# https://app.vagrantup.com/ubuntu/boxes/focal64
kalicluster.vm.box = "ubuntu/focal64" #Official Ubuntu 20.04 LTS (Focal Fossa) builds
# https://app.vagrantup.com/ubuntu/boxes/xenial64
# kalicluster.vm.box = "ubuntu/xenial64" #16.04
kalicluster.vm.hostname = "vg-minikube-01"
#bridged network,DHCP disabled, manual IP assignment
# kalicluster.vm.network "public_network", ip: "10.10.8.67"
#bridged network,DHCP enabled,auto IP assignment
# kalicluster.vm.network "public_network"
kalicluster.vm.network "private_network", ip: "192.168.51.6"
# kalicluster.vm.network "forwarded_port", guest: 80, host: 81
#Disabling the default /vagrant share can be done as follows:
# kalicluster.vm.synced_folder ".", "/vagrant", disabled: true
kalicluster.vm.provider "virtualbox" do |vb|
vb.name = "vbox-minikube-01"
vb.cpus = 2
vb.memory = 4096
vb.gui = false
end
kalicluster.vm.provision "shell", inline: $installer, privileged: false
kalicluster.vm.provision "shell", inline: $docker, privileged: false #OK
kalicluster.vm.provision "shell", inline: $minikubescript, privileged: false, env: {"KUBERNETES_VERSION" => KUBERNETES_VERSION}
end
config.vm.define "vg-minikube-02" do |kalicluster|
# https://app.vagrantup.com/ubuntu/boxes/hirsute64
# kalicluster.vm.box = "ubuntu/hirsute64" #21.04
# https://app.vagrantup.com/ubuntu/boxes/impish64
# kalicluster.vm.box = "ubuntu/impish64" #21.10
# https://app.vagrantup.com/ubuntu/boxes/focal64
# kalicluster.vm.box = "ubuntu/focal64" #Official Ubuntu 20.04 LTS (Focal Fossa) builds
# https://app.vagrantup.com/ubuntu/boxes/xenial64
kalicluster.vm.box = "ubuntu/bionic64" #18.04
# https://app.vagrantup.com/ubuntu/boxes/xenial64
# kalicluster.vm.box = "ubuntu/xenial64" #16.04
kalicluster.vm.hostname = "vg-minikube-01"
#bridged network,DHCP disabled, manual IP assignment
# kalicluster.vm.network "public_network", ip: "10.10.8.67"
#bridged network,DHCP enabled,auto IP assignment
# kalicluster.vm.network "public_network"
kalicluster.vm.network "private_network", ip: "192.168.51.7"
# kalicluster.vm.network "forwarded_port", guest: 80, host: 81
#Disabling the default /vagrant share can be done as follows:
# kalicluster.vm.synced_folder ".", "/vagrant", disabled: true
kalicluster.vm.provider "virtualbox" do |vb|
vb.name = "vbox-minikube-02"
vb.cpus = 2
vb.memory = 4096
vb.gui = false
end
kalicluster.vm.provision "shell", inline: $installer, privileged: false
kalicluster.vm.provision "shell", inline: $ubuntu_docker_script
kalicluster.vm.provision "shell", inline: $minikubescript, privileged: false, env: {"KUBERNETES_VERSION" => KUBERNETES_VERSION}
end
end