-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathKubernetes-Centos-7-Cluster Setup
173 lines (112 loc) · 4.98 KB
/
Kubernetes-Centos-7-Cluster Setup
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
#####################RUN ON MASTER ONLY###################
#!/bin/bash
#Disable swap
swapoff -a
cp /etc/fstab /etc/fstab.backup
# Comment /etc/fstab
# https://unix.stackexchange.com/questions/295537/how-do-i-comment-lines-in-fstab-using-sed
# https://stackoverflow.com/questions/8488253/how-to-force-cp-to-overwrite-without-confirmation/38649556
awk '/[/]swap/{$0="#"$0} 1' /etc/fstab >/etc/fstab.tmp && yes | mv /etc/fstab.tmp /etc/fstab 2>&1 > /dev/null
#Install Docker and Kubernetes on all servers.
#keyboard_arrow_up
#The first thing that we are going to do is use SSH to log in to all machines. Once we have logged in, we need to elevate privileges using sudo
#Disable SELinux.
setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
#Enable the br_netfilter module for cluster communication.
modprobe br_netfilter
echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
#Ensure that the Docker dependencies are satisfied.
yum install -y yum-utils device-mapper-persistent-data lvm2
#Add the Docker repo and install Docker.
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
#Set the cgroup driver for Docker to systemd, then reload systemd, enable and start Docker
sed -i '/^ExecStart/ s/$/ --exec-opt native.cgroupdriver=systemd/' /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl enable docker --now
#Add the repo for Kubernetes.
cat << EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
#Install Kubernetes.
yum install -y kubelet kubeadm kubectl
#Enable Bridging
#Create the k8s.conf file:
cat << EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
#Enable the kubelet service. The kubelet service will fail to start until the cluster is initialized, this is expected.
systemctl start kubelet && systemctl enable kubelet
#*Note: Complete the following section on the MASTER ONLY!
#Initialize the cluster using the IP range for Flannel.
kubeadm init --pod-network-cidr=10.244.0.0/16
#Copy the kubeadmn join command that is in the output. We will need this later.
#Exit sudo and copy the admin.conf to your home directory and take ownership.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#Deploy Flannel.
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# END
#Check the cluster state.
kubectl get pods --all-namespaces
kubectl get nodes
#####################RUN ON MASTER ONLY###################
### Note Scroll copy the token & paste on all worker nodes to join the cluster, just like in docker-swarm ###
### TEST ###
#Create and scale a deployment using kubectl.
#Create a simple deployment.
kubectl create deployment nginx --image=nginx
#Inspect the pod.
kubectl get pods
#Scale the deployment.
kubectl scale deployment nginx --replicas=4
#nspect the pods. You should now have 4.
kubectl get pods
kubectl expose deployment nginx --type=NodePort --port=80
#####################RUN ON WORKER NODES ONLY###################
#!/bin/bash
#Install Docker and Kubernetes on all servers.
#keyboard_arrow_up
#The first thing that we are going to do is use SSH to log in to all machines. Once we have logged in, we need to elevate privileges using sudo
#Disable SELinux.
setenforce 0
sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
#Enable the br_netfilter module for cluster communication.
modprobe br_netfilter
echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
#Ensure that the Docker dependencies are satisfied.
yum install -y yum-utils device-mapper-persistent-data lvm2
#Add the Docker repo and install Docker.
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce
#Set the cgroup driver for Docker to systemd, then reload systemd, enable and start Docker
sed -i '/^ExecStart/ s/$/ --exec-opt native.cgroupdriver=systemd/' /usr/lib/systemd/system/docker.service
systemctl daemon-reload
systemctl enable docker --now
#Add the repo for Kubernetes.
cat << EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
#Install Kubernetes.
yum install -y kubelet kubeadm kubectl
#Enable the kubelet service. The kubelet service will fail to start until the cluster is initialized, this is expected.
systemctl enable kubelet
#####################RUN ON WORKER NODES ONLY###################