-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
Β·174 lines (150 loc) Β· 4.66 KB
/
setup.sh
File metadata and controls
executable file
Β·174 lines (150 loc) Β· 4.66 KB
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
#!/bin/bash
set -e
echo "π Kubernetes Networking 24h - Setup Script"
echo "==========================================="
echo ""
# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Detect OS
OS="$(uname -s)"
ARCH="$(uname -m)"
echo "Detected: $OS $ARCH"
echo ""
# Check prerequisites
echo "π Checking prerequisites..."
# Check Docker
if ! command -v docker &> /dev/null; then
echo -e "${RED}β Docker not found. Please install Docker first.${NC}"
echo "Visit: https://docs.docker.com/get-docker/"
exit 1
fi
echo -e "${GREEN}β Docker found${NC}"
# Check if Docker is running
if ! docker info &> /dev/null; then
echo -e "${RED}β Docker daemon not running. Please start Docker.${NC}"
exit 1
fi
echo -e "${GREEN}β Docker daemon running${NC}"
# Install kubectl if not present
if ! command -v kubectl &> /dev/null; then
echo -e "${YELLOW}βοΈ Installing kubectl...${NC}"
if [[ "$OS" == "Linux" ]]; then
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/$ARCH/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
elif [[ "$OS" == "Darwin" ]]; then
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/$ARCH/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
fi
echo -e "${GREEN}β kubectl installed${NC}"
else
echo -e "${GREEN}β kubectl found${NC}"
fi
# Install kind if not present
if ! command -v kind &> /dev/null; then
echo -e "${YELLOW}βοΈ Installing kind...${NC}"
if [[ "$OS" == "Linux" ]]; then
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-$ARCH
elif [[ "$OS" == "Darwin" ]]; then
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-darwin-$ARCH
fi
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
echo -e "${GREEN}β kind installed${NC}"
else
echo -e "${GREEN}β kind found${NC}"
fi
# Check for existing clusters
if kind get clusters 2>/dev/null | grep -q "^netlab$"; then
echo -e "${YELLOW}β οΈ Cluster 'netlab' already exists.${NC}"
read -p "Delete and recreate? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
kind delete cluster --name netlab
else
echo "Using existing cluster."
kubectl cluster-info --context kind-netlab
exit 0
fi
fi
# Create kind cluster
echo ""
echo -e "${YELLOW}π¨ Creating kind cluster 'netlab'...${NC}"
cat <<EOF | kind create cluster --name netlab --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 80
hostPort: 80
protocol: TCP
- containerPort: 443
hostPort: 443
protocol: TCP
- role: worker
- role: worker
networking:
podSubnet: "10.244.0.0/16"
serviceSubnet: "10.96.0.0/12"
EOF
echo -e "${GREEN}β Cluster created${NC}"
# Set kubectl context
kubectl cluster-info --context kind-netlab
# Verify nodes
echo ""
echo "π Cluster Status:"
kubectl get nodes -o wide
# Create test namespaces
echo ""
echo -e "${YELLOW}π¦ Creating test namespaces...${NC}"
kubectl create namespace frontend || true
kubectl create namespace backend || true
kubectl create namespace database || true
kubectl label namespace frontend name=frontend --overwrite || true
kubectl label namespace backend name=backend --overwrite || true
kubectl label namespace database name=database --overwrite || true
echo -e "${GREEN}β Namespaces created${NC}"
# Install metrics-server (optional but useful)
echo ""
echo -e "${YELLOW}π Installing metrics-server...${NC}"
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# Patch metrics-server for kind
kubectl patch deployment metrics-server -n kube-system --type='json' -p='[
{
"op": "add",
"path": "/spec/template/spec/containers/0/args/-",
"value": "--kubelet-insecure-tls"
}
]' || true
echo -e "${GREEN}β metrics-server installed${NC}"
# Summary
echo ""
echo "=========================================="
echo -e "${GREEN}β
Setup Complete!${NC}"
echo "=========================================="
echo ""
echo "Cluster Info:"
echo " Name: netlab"
echo " Nodes: 3 (1 control-plane, 2 workers)"
echo " Pod CIDR: 10.244.0.0/16"
echo " Service CIDR: 10.96.0.0/12"
echo ""
echo "Quick Commands:"
echo " kubectl get nodes"
echo " kubectl get pods -A"
echo " kind get clusters"
echo ""
echo "Next Steps:"
echo " cd hour-01/"
echo " cat README.md"
echo " ./lab.sh"
echo ""
echo "To delete cluster when done:"
echo " kind delete cluster --name netlab"
echo ""
echo -e "${GREEN}Happy learning! π${NC}"