centos7.4 系統機器兩臺:node
機器 | 主機名 |
---|---|
10.1.31.36 | kubernetes master |
10.1.31.24 | kubernetes node1 |
如下步驟在兩臺臺主機上都要操做。
須要pull docker鏡像,請先安裝docker。linux
[`https://github.com/kubernetes/kubernetes`]()
從CentOS OS存儲庫中使用docker,則docker版本可能較舊,沒法與Kubernetes v1.13.0及更高版本一塊兒使用。
這裏咱們安裝18.06.3版本git
yum install -y -q yum-utils device-mapper-persistent-data lvm2 > /dev/null 2>&1 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo > /dev/null 2>&1 yum install -y -q docker-ce-18.06.3.ce >/dev/null 2>&1
啓動dockergithub
systemctl enable docker && systemctl start docker docker version
若是系統開啓了防火牆則按以下步驟關閉防火牆docker
[root@localhost]# systemctl stop firewalld [root@localhost]# systemctl disable firewalld
[root@localhost]# setenforce 0 [root@localhost]# sed -i '/^SELINUX=/cSELINUX=disabled' /etc/sysconfig/selinux
或者編輯/etc/selinux/configapache
root@localhost]# vi /etc/selinux/config 將SELINUX=enforcing改成SELINUX=disabled,重啓後生效 查看狀態 [root@localhost]# sestatus SELinux status: disabled
kubeadm不支持swap
執行bootstrap
[root@localhost yum.repos.d]# swapoff -a #或者編輯/etc/fstab註釋掉swap [root@localhost yum.repos.d]# cat /etc/fstab # /etc/fstab # Created by anaconda on Fri Oct 18 00:03:01 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=1a73fb9d-095b-4302-aa87-4a49783c0133 / xfs defaults 0 0 UUID=e293a721-617e-4cdd-8cb9-d2e895427b03 /boot xfs defaults 0 0 #UUID=73e88a54-4574-4f0a-8a4f-d57d2bc2ee46 swap swap defaults 0 0
三臺主機分別設置master、node1 vim
[root@localhost kubernetes]# cat >> /etc/sysconfig/network << EOF hostname=master EOF [root@localhost kubernetes]# cat /etc/sysconfig/network # Created by anaconda hostname=master
或者centos
[root@localhost]#hostnamectl set-hostname master [root@localhost]#hostnamectl set-hostname node1 [root@localhost]#hostnamectl set-hostname node2
每臺主機上都執行api
[root@localhost]#echo "192.168.3.100 master" >> /etc/hosts [root@localhost]#echo "192.168.3.101 node1" >> /etc/hosts [root@localhost]#echo "192.168.3.102 node2" >> /etc/hosts
將橋接的IPv4流量傳遞到iptables的鏈
[root@localhost]#cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 ----net.bridge.bridge-nf-call-arptables = 1 net.ipv4.ip_forward = 1 EOF
使如上配置生效
[root@localhost]#sysctl --system
//配置國內tencent yum源地址、epel源地址、Kubernetes源地址
[root@localhost]#mkdir -p /etc/yum.repos.d/repos.bak [root@localhost]#mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repos.bak [root@localhost]#wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo [root@localhost]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo [root@localhost]#yum clean all && yum makecache //配置國內Kubernetes源地址 [root@localhost]#cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
指定版本號
[root@localhost]#yum install -y kubelet-1.16.0 kubeadm-1.16.0 kubectl-1.16.0 --disableexcludes=kubernetes
不指定版本號
install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
說明:–disableexcludes=kubernetes #禁用除kubernetes以外的倉庫,能夠不帶這個參數
啓動工具
[root@localhost]#systemctl enable kubelet [root@localhost]#systemctl start kubelet
其餘命令
systemctl status kubelet systemctl stop kubelet systemctl restart kubelet
##3# 十一、初始化獲取要下載的鏡像列表
[root@master ~]# cd /home [root@master home]# ls apache-zookeeper-3.5.6-bin jdk1.8.0_231 kafka_2.12-2.3.1 [root@master home]# kubeadm config images list W1115 18:05:31.432259 17146 version.go:101] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers) W1115 18:05:31.436956 17146 version.go:102] falling back to the local client version: v1.16.3 k8s.gcr.io/kube-apiserver:v1.16.3 k8s.gcr.io/kube-controller-manager:v1.16.3 k8s.gcr.io/kube-scheduler:v1.16.3 k8s.gcr.io/kube-proxy:v1.16.3 k8s.gcr.io/pause:3.1 k8s.gcr.io/etcd:3.3.15-0 k8s.gcr.io/coredns:1.6.2
執行下面命令,會在當前目錄生成kubeadm.conf文件
[root@master home]#kubeadm config print init-defaults > kubeadm.conf
kubeadm.conf這個配置文件默認會從google的鏡像倉庫地址k8s.gcr.io下載鏡像,國內下載不了。所以,咱們經過下面的方法把地址改爲國內的,好比用阿里的。
編輯kubeadm.conf文件,把imageRepository的value改爲下面的
imageRepository: registry.aliyuncs.com/google_containers
kubeadm.conf文件裏的kubeadm和k8s版本必須匹配,不能一高一低,若是不匹配,修改kubeadm.conf下面k8s的版本號便可
[root@master home]#vi kubeadm.conf
在kubeadm.conf所在目錄執行
[[root@master home]#kubeadm config images pull --config kubeadm.conf [root@master home]# docker images
鏡像下載好後,還須要tag下載好的鏡像,讓下載好的鏡像都是帶有 k8s.gcr.io 標識的,若是不打tag變成k8s.gcr.io,那麼後面用kubeadm安裝會出現問題,由於kubeadm裏面只認 google自身的模式。打tag後刪除帶有 registry.aliyuncs.com 標識的鏡像。
從新打tag
docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.16.0 k8s.gcr.io/kube-apiserver:v1.16.0 docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.16.0 k8s.gcr.io/kube-proxy:v1.16.0 docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.16.0 k8s.gcr.io/kube-controller-manager:v1.16.0 docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.16.0 k8s.gcr.io/kube-scheduler:v1.16.0 docker tag registry.aliyuncs.com/google_containers/etcd:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0 docker tag registry.aliyuncs.com/google_containers/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2 docker tag registry.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
執行
[root@master ~]# kubeadm init --kubernetes-version=v1.16.0 --pod-network-cidr= 10.244.0.0/16 --apiserver-advertise-address=192.168.3.100
執行成功結果
[init] Using Kubernetes version: v1.16.0 [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] Generating "ca" certificate and key [certs] Generating "apiserver" certificate and key [certs] apiserver serving cert is signed for DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.3.100] [certs] Generating "apiserver-kubelet-client" certificate and key [certs] Generating "front-proxy-ca" certificate and key [certs] Generating "front-proxy-client" certificate and key [certs] Generating "etcd/ca" certificate and key [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [master localhost] and IPs [192.168.3.100 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [192.168.3.100 127.0.0.1 ::1] [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "sa" key and public 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 [kubelet-check] Initial timeout of 40s passed. [apiclient] All control plane components are healthy after 209.010947 seconds [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.16" in namespace kube-system with the configuration for the kubelets in the cluster [upload-certs] Skipping phase. Please see --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: vmuuvn.q7q14t5135zm9xk0 [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: kubeadm join 192.168.3.100:6443 --token vmuuvn.q7q14t5135zm9xk0 \ --discovery-token-ca-cert-hash sha256:c302e2c93d2fe86be7f817534828224469a19c5ccbbf9b246f3695127c3ea611
記錄最後的輸出,node節點加入須要用到
kubeadm join 192.168.3.100:6443 --token vmuuvn.q7q14t5135zm9xk0 –discovery-token-ca-cert-hash sha256:c302e2c93d2fe86be7f817534828224469a19c5ccbbf9b246f3695127c3ea611
若是init失敗,重啓kubeadm再執行init
[root@master ~]# kubeadm reset 初始化成功後會在/etc/kubernetes/成下面的文件,若是沒有須要從新init
[root@master home]# ll /etc/kubernetes/ total 36 -rw------- 1 root root 5453 Nov 15 17:42 admin.conf -rw------- 1 root root 5485 Nov 15 17:42 controller-manager.conf -rw------- 1 root root 5457 Nov 15 17:42 kubelet.conf drwxr-xr-x. 2 root root 113 Nov 15 17:42 manifests drwxr-xr-x 3 root root 4096 Nov 15 17:42 pki -rw------- 1 root root 5437 Nov 15 17:42 scheduler.conf
注意:kubeadm reset是清理環境,執行後,master要從新init。若是node已經加入集羣,那麼node也要reset重啓kubelet並要從新join
[root@node1 home]#kubeadm reset [root@node1 home]#systemctl daemon-reload && systemctl restart kubelet [root@node1 home]# kubeadm join 192.168.3.100:6443 --token vmuuvn.q7q14t5135zm9xk0 \ --discovery-token-ca-cert-hash sha256:c302e2c93d2fe86be7f817534828224469a19c5ccbbf9b246f3695127c3ea611
當前用戶,執行如下步驟(系統由admin.conf文件啓動kubernetes,若是不配置會起不來)
root@master home]# mkdir -p $HOME/.kube [root@master home]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [root@master home]# chown $(id -u):$(id -g) $HOME/.kube/config
k8s 支持不少種網絡方案,在這裏使用的是經典的 flannel 方案,還有Calico方式。
查看flannel版本
https://github.com/coreos/flannel
執行命令開始安裝。注:下面這個地址是v0.11.0-amd6版本的
[root@master home]#kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml 安裝結果 clusterrole.rbac.authorization.k8s.io/flannel created clusterrolebinding.rbac.authorization.k8s.io/flannel created serviceaccount/flannel created configmap/kube-flannel-cfg created unable to recognize "https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml": no matches for kind "PodSecurityPolicy" in version "extensions/v1beta1" unable to recognize "https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml": no matches for kind "DaemonSet" in version "extensions/v1beta1" unable to recognize "https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml": no matches for kind "DaemonSet" in version "extensions/v1beta1" unable to recognize "https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml": no matches for kind "DaemonSet" in version "extensions/v1beta1" unable to recognize "https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml": no matches for kind "DaemonSet" in version "extensions/v1beta1" unable to recognize "https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml": no matches for kind "DaemonSet" in version "extensions/v1beta1"
若是一直pull不下來,默認的flannel配置文件拉取鏡像在國外,國內拉取失敗,不少網上文章沒注意這一步,致使flannel部署失敗,以下解決
訪問:https://blog.51cto.com/14040759/2492764
[root@master home]# mkdir flannel #把內容粘貼到下面的文本後保存 [root@master home]# vim kube-flannel.yml [root@master home]# kubectl apply -f flannel/kube-flannel.yml [root@master home]#
執行獲取pods列表命令,查看相關狀態
[root@master home]# kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-5644d7b6d9-5zsbb 0/1 Pending 0 6h17m kube-system coredns-5644d7b6d9-pvsm9 0/1 Pending 1 6h17m kube-system etcd-master 1/1 Running 0 6h16m kube-system kube-apiserver-master 1/1 Running 0 6h16m kube-system kube-controller-manager-master 1/1 Running 0 6h16m kube-system kube-flannel-ds-amd64-7x9fv 1/1 Running 0 6h16m kube-system kube-flannel-ds-amd64-n6kpw 1/1 Running 0 6h14m kube-system kube-proxy-8zl82 1/1 Running 0 6h14m kube-system kube-proxy-gqmxx 1/1 Running 0 6h17m kube-system kube-scheduler-master 1/1 Running 0 6h16m
查看主節點狀況
[root@master ~]# kubectl get nodes
使用kubeadm初始化的集羣,出於安全考慮Pod不會被調度到Master Node上,也就是說Master Node不參與工做負載,這樣Master的資源就浪費了。在Master節點上,能夠運行如下命令使其做爲一個工做節點
[root@master ~]#kubectl taint nodes --all node-role.kubernetes.io/master-
禁止master部署pod
[root@master ~]#kubectl taint nodes k8s node-role.kubernetes.io/master=true:NoSchedule
關於污點,網上資料參考:
使用kubeadm搭建的集羣默認就給 master 節點添加了一個污點標記,因此咱們看到咱們平時的 pod 都沒有被調度到 master 上去。
[root@master ~]#kubectl describe node master | grep Taints(注:master node name)
有一條關於 Taints 的信息:node-role.kubernetes.io/master:NoSchedule,就表示給 master 節點打了一個污點的標記,其中影響的參數是NoSchedule,表示 pod 不會被調度到標記爲 taints 的節點,除了 NoSchedule 外,還有另外兩個選項:
PreferNoSchedule:NoSchedule 的軟策略版本,表示儘可能不調度到污點節點上去
NoExecute:該選項意味着一旦 Taint 生效,如該節點內正在運行的 pod 沒有對應 Tolerate 設置,會直接被逐出
jion集羣
[root@node1 ~]# kubeadm join 10.1.31.36:6443 --token vmuuvn.q7q14t5135zm9xk0 \ --discovery-token-ca-cert-hash sha256:c302e2c93d2fe86be7f817534828224469a19c5ccbbf9b246f3695127c3ea611
若是在master初始化的時候沒有記下jion命令,在master上使用kubeadm token create --print-join-command查詢
$ kubectl delete node --all
$ for service in kube-apiserver kube-controller-manager kubectl kubelet kube-proxy kube-scheduler; do systemctl stop $service done $ yum -y remove kubernetes #if it's registered as a service
yum remove -y kubelet kubectl
rm -rf ~/.kube/ rm -rf /etc/kubernetes/ rm -rf /etc/systemd/system/kubelet.service.d rm -rf /etc/systemd/system/kubelet.service rm -rf /usr/bin/kube* rm -rf /etc/cni rm -rf /opt/cni rm -rf /var/lib/etcd rm -rf /var/etcd rm -rf /var/lib/cni/flannel/* && rm -rf /var/lib/cni/networks/cbr0/* && ip link delete cni0 rm -rf /var/lib/cni/networks/cni0/*
docker rm -f $(docker ps -qa) #刪除全部的數據卷 docker volume rm $(docker volume ls -q) #再次顯示全部的容器和數據卷,確保沒有殘留 docker ps -a docker volume ls
[root@master ~]# kubectl get nodes -o wide