Create a small infra for kubernetes
1 #On your Azure CLI
2 az --version # Version expected 2.1.0 or higher
3
4 az group delete --name kubernetes -y
5
6 az group create -n kubernetes -l westeurope
7
8 az network vnet create -g kubernetes \
9 -n kubernetes-vnet \
10 --address-prefix 10.240.0.0/24 \
11 --subnet-name kubernetes-subnet
12
13 az network nsg create -g kubernetes -n kubernetes-nsg
14
15 az network vnet subnet update -g kubernetes \
16 -n kubernetes-subnet \
17 --vnet-name kubernetes-vnet \
18 --network-security-group kubernetes-nsg
19
20 az network nsg rule create -g kubernetes \
21 -n kubernetes-allow-ssh \
22 --access allow \
23 --destination-address-prefix '*' \
24 --destination-port-range 22 \
25 --direction inbound \
26 --nsg-name kubernetes-nsg \
27 --protocol tcp \
28 --source-address-prefix '*' \
29 --source-port-range '*' \
30 --priority 1000
31
32 az network nsg rule create -g kubernetes \
33 -n kubernetes-allow-api-server \
34 --access allow \
35 --destination-address-prefix '*' \
36 --destination-port-range 6443 \
37 --direction inbound \
38 --nsg-name kubernetes-nsg \
39 --protocol tcp \
40 --source-address-prefix '*' \
41 --source-port-range '*' \
42 --priority 1001
43
44 az network nsg rule list -g kubernetes --nsg-name kubernetes-nsg --query "[].{Name:name, Direction:direction, Priority:priority, Port:destinationPortRange}" -o table
45
46 az network lb create -g kubernetes --sku Standard \
47 -n kubernetes-lb \
48 --backend-pool-name kubernetes-lb-pool \
49 --public-ip-address kubernetes-pip \
50 --public-ip-address-allocation static
51
52 az network public-ip list --query="[?name=='kubernetes-pip'].{ResourceGroup:resourceGroup, Region:location,Allocation:publicIpAllocationMethod,IP:ipAddress}" -o table
53 #For Ubuntu
54 # az vm image list --location westeurope --publisher Canonical --offer UbuntuServer --sku 18.04-LTS --all -o table
55 # For Redhat
56 # az vm image list --location westeurope --publisher RedHat --offer RHEL --sku 8 --all -o table
57 # => choosen one : 8-lvm-gen2
58 WHICHOS="RedHat:RHEL:8-lvm-gen2:8.5.2022032206"
59
60 # K8s Controller
61 az vm availability-set create -g kubernetes -n controller-as
62
63 for i in 0 1 2; do
64 echo "[Controller ${i}] Creating public IP..."
65 az network public-ip create -n controller-${i}-pip -g kubernetes --sku Standard > /dev/null
66 echo "[Controller ${i}] Creating NIC..."
67 az network nic create -g kubernetes \
68 -n controller-${i}-nic \
69 --private-ip-address 10.240.0.1${i} \
70 --public-ip-address controller-${i}-pip \
71 --vnet kubernetes-vnet \
72 --subnet kubernetes-subnet \
73 --ip-forwarding \
74 --lb-name kubernetes-lb \
75 --lb-address-pools kubernetes-lb-pool >/dev/null
76
77 echo "[Controller ${i}] Creating VM..."
78 az vm create -g kubernetes \
79 -n controller-${i} \
80 --image ${WHICHOS} \
81 --nics controller-${i}-nic \
82 --availability-set controller-as \
83 --nsg '' \
84 --admin-username 'kuberoot' \
85 --admin-password 'Changeme!' \
86 --size Standard_B2s \
87 --storage-sku StandardSSD_LRS
88 #--generate-ssh-keys > /dev/null
89 done
90
91 #K8s Worker
92 az vm availability-set create -g kubernetes -n worker-as
93 for i in 0 1; do
94 echo "[Worker ${i}] Creating public IP..."
95 az network public-ip create -n worker-${i}-pip -g kubernetes --sku Standard > /dev/null
96 echo "[Worker ${i}] Creating NIC..."
97 az network nic create -g kubernetes \
98 -n worker-${i}-nic \
99 --private-ip-address 10.240.0.2${i} \
100 --public-ip-address worker-${i}-pip \
101 --vnet kubernetes-vnet \
102 --subnet kubernetes-subnet \
103 --ip-forwarding > /dev/null
104 echo "[Worker ${i}] Creating VM..."
105 az vm create -g kubernetes \
106 -n worker-${i} \
107 --image ${WHICHOS} \
108 --nics worker-${i}-nic \
109 --tags pod-cidr=10.200.${i}.0/24 \
110 --availability-set worker-as \
111 --nsg '' \
112 --generate-ssh-keys \
113 --size Standard_B2s \
114 --storage-sku StandardSSD_LRS \
115 --admin-username 'kuberoot'> /dev/null \
116 --admin-password 'Changeme!' \
117 done
118
119 #Summarize
120 az vm list -d -g kubernetes -o table
Comments