centos7 部署 k8s 集群

1、環(huán)境準(zhǔn)備

先準(zhǔn)備一臺centos7虛擬機(jī)

2、安裝docker-ce

官方文檔

# 卸載原來的docker
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

# 安裝依賴
sudo yum update -y && sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
  
# 添加官方y(tǒng)um庫
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
    
# 安裝docker
sudo yum install docker-ce docker-ce-cli containerd.io

# 安裝指定版本docker
yum list docker-ce --showduplicates | sort -r
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

# 查看docker版本
docker --version

# 開機(jī)啟動
systemctl enable --now docker

或者使用腳本一鍵安裝

curl -fsSL "https://get.docker.com/" | sh
systemctl enable --now docker

修改docker cgroup驅(qū)動,與k8s一致,使用systemd

# 修改docker cgroup驅(qū)動:native.cgroupdriver=systemd
cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}
EOF

systemctl restart docker  # 重啟使配置生效

3、安裝 kubelet kubeadm kubectl

官方文檔

安裝kubernetes的時(shí)候,需要安裝kubelet, kubeadm等包,但k8s官網(wǎng)給的yum源是packages.cloud.google.com,國內(nèi)訪問不了,此時(shí)我們可以使用阿里云的yum倉庫鏡像。

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
       http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

# 關(guān)閉SElinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

# 安裝kubelet kubeadm kubectl
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

systemctl enable --now kubelet  # 開機(jī)啟動kubelet

# centos7用戶還需要設(shè)置路由:
yum install -y bridge-utils.x86_64
modprobe  br_netfilter  # 加載br_netfilter模塊,使用lsmod查看開啟的模塊
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system  # 重新加載所有配置文件

systemctl disable --now firewalld  # 關(guān)閉防火墻

# k8s要求關(guān)閉swap  (qxl)
swapoff -a && sysctl -w vm.swappiness=0  # 關(guān)閉swap
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab  # 取消開機(jī)掛載swap

4、準(zhǔn)備所需鏡像

kubeadm config images pull # 拉取集群所需鏡像,這個需要翻墻

# --- 不能翻墻可以嘗試以下辦法 ---
kubeadm config images list # 列出所需鏡像

k8s.gcr.io/kube-apiserver:v1.15.3
k8s.gcr.io/kube-controller-manager:v1.15.3
k8s.gcr.io/kube-scheduler:v1.15.3
k8s.gcr.io/kube-proxy:v1.15.3
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1


# 根據(jù)所需鏡像名字先拉取國內(nèi)資源
docker pull mirrorgooglecontainers/kube-apiserver:v1.14.1
docker pull mirrorgooglecontainers/kube-controller-manager:v1.14.1
docker pull mirrorgooglecontainers/kube-scheduler:v1.14.1
docker pull mirrorgooglecontainers/kube-proxy:v1.14.1
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd:3.3.10
docker pull coredns/coredns:1.3.1  # 這個在mirrorgooglecontainers中沒有

# 修改鏡像tag
docker tag mirrorgooglecontainers/kube-apiserver:v1.14.1 k8s.gcr.io/kube-apiserver:v1.15.3
docker tag mirrorgooglecontainers/kube-controller-manager:v1.14.1 k8s.gcr.io/kube-controller-manager:v1.15.3
docker tag mirrorgooglecontainers/kube-scheduler:v1.14.1 k8s.gcr.io/kube-scheduler:v1.15.3
docker tag mirrorgooglecontainers/kube-proxy:v1.14.1 k8s.gcr.io/kube-proxy:v1.15.3
docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker tag coredns/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1

# 刪除原來的鏡像
docker rmi mirrorgooglecontainers/kube-apiserver:v1.14.1
docker rmi mirrorgooglecontainers/kube-controller-manager:v1.14.1
docker rmi mirrorgooglecontainers/kube-scheduler:v1.14.1
docker rmi mirrorgooglecontainers/kube-proxy:v1.14.1
docker rmi mirrorgooglecontainers/pause:3.1
docker rmi mirrorgooglecontainers/etcd:3.3.10
docker rmi coredns/coredns:1.3.1

使用虛擬機(jī)的可以做完以上步驟后,使用Vbox進(jìn)行克隆。,一臺做Master,另一臺做Node
修改hostname

vim /etc/hostname
systemctl restart systemd-hostnamed

5、使用kubeadm創(chuàng)建集群

# 初始化Master(Master需要至少2核)此處會各種報(bào)錯,異常...成功與否就在此
kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.15.3 --apiserver-advertise-address=192.168.83.881.14.1
# --apiserver-advertise-address Master節(jié)點(diǎn)IP
# --pod-network-cidr 指定pod網(wǎng)絡(luò)子網(wǎng),使用fannel網(wǎng)絡(luò)必須使用這個CIDR
# 初始化結(jié)果:
[init] Using Kubernetes version: v1.14.1
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Using existing etcd/ca certificate authority
[certs] Using existing etcd/server certificate and key on disk
[certs] Using existing etcd/peer certificate and key on disk
[certs] Using existing etcd/healthcheck-client certificate and key on disk
[certs] Using existing apiserver-etcd-client certificate and key on disk
[certs] Using existing ca certificate authority
[certs] Using existing apiserver certificate and key on disk
[certs] Using existing apiserver-kubelet-client certificate and key on disk
[certs] Using existing front-proxy-ca certificate authority
[certs] Using existing front-proxy-client certificate and key on disk
[certs] Using the existing "sa" key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 21.503375 seconds
[upload-config] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.14" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --experimental-upload-certs
[mark-control-plane] Marking the node master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: w2i0mh.5fxxz8vk5k8db0wq
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

#每個機(jī)器創(chuàng)建的master以下部分都不同,需要自己保存好-qxl
kubeadm join 192.168.200.25:6443 --token our9a0.zl490imi6t81tn5u \
    --discovery-token-ca-cert-hash sha256:b93f710eb9b389a69f0cd0d6dcf7c82e389a68f009eb6b2028f69d54b099de16 

普通用戶設(shè)置權(quán)限

  # Master端:
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
  # Node端:
  mkdir -p $HOME/.kube
  # 復(fù)制Master端配置文件$HOME/.kube/config到同級目錄,否則后面kubectl get nodes會出現(xiàn)如下錯誤
  # The connection to the server localhost:8080 was refused - did you specify the right host or port?
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

應(yīng)用flannel網(wǎng)絡(luò)

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

node加入機(jī)器

# node1:
kubeadm join 192.168.200.25:6443 --token w2i0mh.5fxxz8vk5k8db0wq \
    --discovery-token-ca-cert-hash sha256:65e82e987f50908f3640df7e05c7a91f390a02726c9142808faa739d4dc24252 
# node2:
kubeadm join 192.168.200.25:6443 --token w2i0mh.5fxxz8vk5k8db0wq \
    --discovery-token-ca-cert-hash sha256:65e82e987f50908f3640df7e05c7a91f390a02726c9142808faa739d4dc24252 

輸出日志:

[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.14" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
# master:
kubectl get pods --all-namespaces
# ---輸出信息---
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE
kube-system   coredns-fb8b8dccf-rn8kd          1/1     Running   0          170m
kube-system   coredns-fb8b8dccf-slwr4          1/1     Running   0          170m
kube-system   etcd-master                      1/1     Running   0          169m
kube-system   kube-apiserver-master            1/1     Running   0          169m
kube-system   kube-controller-manager-master   1/1     Running   0          169m
kube-system   kube-flannel-ds-amd64-l8c7c      1/1     Running   0          130m
kube-system   kube-flannel-ds-amd64-lcmxw      1/1     Running   1          117m
kube-system   kube-flannel-ds-amd64-pqnln      1/1     Running   1          72m
kube-system   kube-proxy-4kcqb                 1/1     Running   0          170m
kube-system   kube-proxy-jcqjd                 1/1     Running   0          72m
kube-system   kube-proxy-vm9sj                 1/1     Running   0          117m
kube-system   kube-scheduler-master            1/1     Running   0          169m
# ---輸出信息---


kubectl get nodes
# ---輸出信息---
NAME     STATUS   ROLES    AGE    VERSION
master   Ready    master   171m   v1.14.1
node1    Ready    <none>   118m   v1.14.1
node2    Ready    <none>   74m    v1.14.1
# ---輸出信息---

排錯

journalctl -f  # 當(dāng)前輸出日志
journalctl -f -u kubelet  # 只看當(dāng)前的kubelet進(jìn)程日志

出于安全考慮,默認(rèn)配置下Kubernetes不會將Pod調(diào)度到Master節(jié)點(diǎn)。如果希望將k8s-master也當(dāng)作Node使用,可以執(zhí)行如下命令:

kubectl describe node localhost
#輸出:Taints:             node-role.kubernetes.io/master:NoSchedule(這個污點(diǎn)表示默認(rèn)情況下master節(jié)點(diǎn)將不會調(diào)度運(yùn)行Pod,即不運(yùn)行工作負(fù)載。)
#可以部署到master
kubectl taint node k8s-master node-role.kubernetes.io/master=:NoSchedule-

其中k8s-master是主機(jī)節(jié)點(diǎn)hostname如果要恢復(fù)Master Only狀態(tài),執(zhí)行如下命令:

#不會部署到master
kubectl taint node k8s-master node-role.kubernetes.io/master:NoSchedule

注意:kubeadm初始化的Kubernetes集群,master節(jié)點(diǎn)也被打上了一個node-role.kubernetes.io/master=的label,標(biāo)識這個節(jié)點(diǎn)的角色為master。

給Node設(shè)置Label和設(shè)置污點(diǎn)是兩個不同的操作。

實(shí)踐:Kubernetes master節(jié)點(diǎn)不運(yùn)行工作負(fù)載

Kubernetes集群的Master節(jié)點(diǎn)是十分重要的,一個高可用的Kubernetes集群一般會存在3個以上的master節(jié)點(diǎn),為了保證master節(jié)點(diǎn)的穩(wěn)定性,一般不推薦將業(yè)務(wù)的Pod調(diào)度到master節(jié)點(diǎn)上。 下面將介紹一下我們使用Kubernetes調(diào)度的Taints和和Tolerations特性確保Kubernetes的Master節(jié)點(diǎn)不執(zhí)行工作負(fù)載的實(shí)踐。

我們的Kubernetes集群中總共有3個master節(jié)點(diǎn),節(jié)點(diǎn)的名稱分別為k8s-01、k8s-02、k8s-03。 為了保證集群的穩(wěn)定性,同時(shí)提高master節(jié)點(diǎn)的利用率,我們將其中一個節(jié)點(diǎn)設(shè)置為node-role.kubernetes.io/master:NoSchedule,另外兩個節(jié)點(diǎn)設(shè)置為node-role.kubernetes.io/master:PreferNoSchedule,這樣保證3個節(jié)點(diǎn)中的1個無論在任何情況下都將不運(yùn)行業(yè)務(wù)Pod,而另外2個載集群資源充足的情況下盡量不運(yùn)行業(yè)務(wù)Pod。

kubectl taint nodes k8s-01 node-role.kubernetes.io/master=:NoSchedule

kubectl taint nodes k8s-02 node-role.kubernetes.io/master=:PreferNoSchedule

kubectl taint nodes k8s-03 node-role.kubernetes.io/master=:PreferNoSchedule

原文地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容