joephon / blog

极简博客
3 stars 2 forks source link

How to initial a kubernetes cluster by kubeadm #21

Open joephon opened 4 years ago

joephon commented 4 years ago

first of all

It's easy nowaday to build up a k8s cluster, so dont worry, lets move step by step

step 1 host with ubuntu

At the very beginning, you at least need a host which running ubuntu 16.04+ on it or what ever local host you have, but this tutorial is all about k8s with ubuntu stuff, so assume you've got those.

step 2 install docker ce

There are kinds of elder version of docker on ubuntu which called docker.io etc etc

We are not talking about the above metioned but going to install the docker ce version

Just follow the shell script below

# (Install Docker CE)
## Set up the repository:
### Install packages to allow apt to use a repository over HTTPS
apt-get update && apt-get install -y \
  apt-transport-https ca-certificates curl software-properties-common gnupg2
# Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
# Add the Docker apt repository:
add-apt-repository \
  "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"
# Install Docker CE
apt-get update && apt-get install -y \
  containerd.io=1.2.13-2 \
  docker-ce=5:19.03.11~3-0~ubuntu-$(lsb_release -cs) \
  docker-ce-cli=5:19.03.11~3-0~ubuntu-$(lsb_release -cs)

Finally, you still need a daemon config file for docker like that:

# Set up the Docker daemon
cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
EOF

This line "exec-opts": ["native.cgroupdriver=systemd"], is typically not required but here it is!

In order to make it works well with k8s, you'd better do it the same

This line "registry-mirrors": ["http://hub-mirror.c.163.com"] is also not required, for some reason in China mainland, you may need something magical like that : )

After the above all have done then restart docker service like that:

mkdir -p /etc/systemd/system/docker.service.d
# Restart Docker
systemctl daemon-reload
systemctl restart docker

step 3 install k8s

Befor install k8s, you may need to do these:

# disabled fire wall
sudo ufw disable
# disabled swap
sudo swapoff -a

Now you are ok to install k8s

apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF 
apt-get update
apt-get install -y kubelet kubeadm kubectl

Let's initialization the k8s

sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.0 --pod-network-cidr=10.244.0.0/16

Arg --image-repository registry.aliyuncs.com/google_containers is for China mainland issue, something magical you know~

Arg --kubernetes-version v1.18.0 is about which k8s version you want, here I choose v1.18.0, actually you can pick up whatever version you want if there it is.

Arg --pod-network-cidr=10.244.0.0/16 is important because that define which network cidr that k8s will use, typically there are kinds of cidr of k8s, if you do not name one, then it will use the default, and the 10.244.0.0/16 format means I choose flannel as my cidr

Then wait for minutes, if there is no any error occur that means you are almost there

# if you want to run as regular user, then do it below
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Finally, the last move: install flannel

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

All right check out the below command and make sure everything is fine~

kubectl get cs

kubectl get nodes

kubectl get pods -A