參考: http://docs.kubernetes.org.cn/459.html
https://blog.csdn.net/gui951753/article/details/83316976#_1
http://www.mamicode.com/info-detail-2544943.html
https://blog.csdn.net/nklinsirui/article/details/80581286#debian-ubuntu
https://cloud.tencent.com/info/852b3e3d1ad1bc020eaacd3bef724443.html
1. 規划
IP | 節點角色 | 工作職責 |
---|---|---|
172.10.30.100 | master | 對外暴露API,對內提供工作流的調度和配置 |
172.10.30.101 | node1 | 承載着k8s運行的實際任務 |
172.10.30.102 | node2 | 同node1相同 |
2. 部署前提
- 主機名稱解析,(在/etc/hosts文件編輯相關信息即可)
172.10.30.100 master
172.10.30.101 node1
172.10.30.102 node2
將上述配置文件拷貝到集群中的所有節點,包括master節點和node節點。
- 時間同步(使用chrony服務實現)
yum -y install chrony vim /etc/chrony.conf ~~~ server master #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst ~~~~ #注釋掉原有的server內容,把原有的時鍾同步服務設置為master結點上的時鍾同步。
- 關閉所有節點的iptables和firewalld以及selinux
iptables -F
systemctl stop firewalld
systemctl disable firewalld
sed -i '/^SELINUX=/s/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
-
禁止iptables對bridge數據進行處理
cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF sysctl -p /etc/sysctl.conf # 不起作用
sysctl -p /etc/sysctl.d/k8s.conf #這樣可以
- 關閉swap
swapoff -a sed 's/.*swap.*/#&/' /etc/fstab
3. docker 安裝
參考: https://docs.docker.com/install/linux/docker-ce/centos/#uninstall-old-versions
這里安裝特定的版本 docker-ce-18.06。
需要注意的是,Kubernetes 1.13已經針對Docker的1.11.1, 1.12.1, 1.13.1, 17.03, 17.06, 17.09, 18.06等版本做了驗證,最低支持的Docker版本是1.11.1,最高支持是18.06,而Docker最新版本已經是18.09
了,故我們安裝時需要指定版本為18.06.1-ce
:
#移除舊版docker
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine #依賴 yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 #docke-ce 官方yum 源 yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
#阿里的docke-ce yum源 二者用其一
wget -P /etc/yum.repos.d/ https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #從阿里雲獲取docker-ce的鏡像文件,-P指定下載文件存放的目錄 #查看docker版本 yum list docker-ce --showduplicates | sort -r
#安裝 yum install docker-ce-18.06.1.ce-3.el7 -y
systemctl start docker
systemctl enable docker
4. kubectl、kubelete、kubeadm安裝
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 setenforce 0
yum install -y kubelet kubeadm kubectl systemctl enable kubelet && systemctl start kubelet
5. 使用kubeadm創建一個單Master集群
5.1初始化Master節點
K8s的控制面板組件運行在Master節點上,包括etcd和API server(Kubectl便是通過API server與k8s通信)。
在執行初始化之前,我們還有一下3點需要注意:
1.選擇一個網絡插件,並檢查它是否需要在初始化Master時指定一些參數,比如我們可能需要根據選擇的插件來設置--pod-network-cidr
參數。參考:Installing a pod network add-on。
2.kubeadm使用eth0的默認網絡接口(通常是內網IP)做為Master節點的advertise address,如果我們想使用不同的網絡接口,可以使用--apiserver-advertise-address=<ip-address>
參數來設置。如果適應IPv6,則必須使用IPv6d的地址,如:--apiserver-advertise-address=fd00::101
。
3.使用kubeadm config images pull
來預先拉取初始化需要用到的鏡像,用來檢查是否能連接到Kubenetes的Registries。
Kubenetes默認Registries地址是k8s.gcr.io
,很明顯,在國內並不能訪問gcr.io,因此在kubeadm v1.13之前的版本,安裝起來非常麻煩,但是在1.13
版本中終於解決了國內的痛點,其增加了一個--image-repository
參數,默認值是k8s.gcr.io
,我們將其指定為國內鏡像地址:registry.aliyuncs.com/google_containers
,其它的就可以完全按照官方文檔來愉快的玩耍了。
其次,我們還需要指定--kubernetes-version
參數,因為它的默認值是stable-1
,會導致從https://dl.k8s.io/release/stable-1.txt
下載最新的版本號,我們可以將其指定為固定版本(最新版:v1.13.0)來跳過網絡請求。
現在,我們就來試一下:
# 使用calico網絡 --pod-network-cidr=192.168.0.0/16 sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.13.0 --pod-network-cidr=192.168.0.0/16
輸出

[init] Using Kubernetes version: v1.13.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 "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 [172.10.30.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 [172.10.30.100 127.0.0.1 ::1] [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [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 172.10.30.100] [certs] Generating "apiserver-kubelet-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 [apiclient] All control plane components are healthy after 26.004391 seconds [uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "master" as an annotation [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: 485yy9.azkhnftmz2mf9me5 [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy Your Kubernetes master 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/ You can now join any number of machines by running the following on each node as root: kubeadm join 172.10.30.100:6443 --token 485yy9.azkhnftmz2mf9me5 --discovery-token-ca-cert-hash sha256:165d19adeaac9bd84837367b414a45b01879dbb8f36092a32b957223904e9c30
這次非常順利的就部署成功了,如果我們想使用非root用戶操作kubectl
,可以使用以下命令,這也是kubeadm init
輸出的一部分:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
5.2 安裝網絡插件
參考: https://kubernetes.io/docs/concepts/cluster-administration/addons/
http://docs.kubernetes.org.cn/459.html
為了讓Pods間可以相互通信,我們必須安裝一個網絡插件,並且必須在部署任何應用之前安裝,CoreDNS也是在網絡插件安裝之后才會啟動的。
網絡的插件完整列表,請參考 Networking and Network Policy。
在安裝之前,我們先查看一下當前Pods的狀態:
[kuber@master ~]$ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-78d4cf999f-8z6d6 0/1 Pending 0 3m34s kube-system coredns-78d4cf999f-l4dhk 0/1 Pending 0 3m34s kube-system etcd-master 1/1 Running 0 2m54s kube-system kube-apiserver-master 1/1 Running 0 2m43s kube-system kube-controller-manager-master 1/1 Running 0 2m50s kube-system kube-proxy-4jmf5 1/1 Running 0 3m34s kube-system kube-scheduler-master 1/1 Running 0 2m35s
如上,可以看到CoreDND的狀態是Pending
,這是因為我們還沒有安裝網絡插件。
Calico是一個純三層的虛擬網絡方案,Calico 為每個容器分配一個 IP,每個 host 都是 router,把不同 host 的容器連接起來。與 VxLAN 不同的是,Calico 不對數據包做額外封裝,不需要 NAT 和端口映射,擴展性和性能都很好。
默認情況下,Calico網絡插件使用的的網段是192.168.0.0/16
,在init
的時候,我們已經通過--pod-network-cidr=192.168.0.0/16
來適配Calico,當然你也可以修改calico.yml
文件來指定不同的網段。
可以使用如下命令命令來安裝Canal
插件:詳細參考: https://docs.projectcalico.org/v3.4/getting-started/kubernetes/
####### [kuber@master ~]$ cat /etc/NetworkManager/conf.d/calico.conf [keyfile] unmanaged-devices=interface-name:cali*;interface-name:tunl* ######## kubectl apply -f \ https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/etcd.yaml ########## kubectl apply -f \ https://docs.projectcalico.org/v3.4/getting-started/kubernetes/installation/hosted/calico.yaml ###### [kuber@master ~]$ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system calico-etcd-7w8dl 1/1 Running 0 24m kube-system calico-kube-controllers-5d94b577bb-kswkt 1/1 Running 0 37m kube-system calico-node-vl9cf 1/1 Running 0 7m1s kube-system coredns-78d4cf999f-9cz26 1/1 Running 4 40m kube-system coredns-78d4cf999f-stf4z 1/1 Running 4 40m kube-system etcd-master 1/1 Running 0 39m kube-system kube-apiserver-master 1/1 Running 0 39m kube-system kube-controller-manager-master 1/1 Running 0 39m kube-system kube-proxy-snh7v 1/1 Running 0 40m kube-system kube-scheduler-master 1/1 Running 0 39m
如上,STATUS全部變為了Running
,表示安裝成功,接下來就可以加入其他節點以及部署應用了。
5.3 master隔離
默認情況下,由於安全原因,集群並不會將pods部署在Master節點上。但是在開發環境下,我們可能就只有一個Master節點,這時可以使用下面的命令來解除這個限制:
kubectl taint nodes --all node-role.kubernetes.io/master-
## 輸出
node/master untainted
5.4 加工作節點
要為群集添加工作節點,需要為每台計算機執行以下操作:
- SSH到機器
- 成為root用戶,(如: sudo su -)
- 運行上面的
kubeadm init
命令輸出的:kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>
如果我們忘記了Master節點的加入token,可以使用如下命令來查看:
kubeadm token list # 輸出 TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS 6pkrlg.8glf2fqpuf3i489m 22h 2018-12-07T13:46:33Z authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
默認情況下,token的有效期是24小時,如果我們的token已經過期的話,可以使用以下命令重新生成:
kubeadm token create
# 輸出
u2mt59.tyqpo0v5wf05lx2q
如果我們也沒有--discovery-token-ca-cert-hash
的值,可以使用以下命令生成:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' # 輸出 eebfe256113bee397b218ba832f412273ae734bd4686241fb910885d26efd222
現在,我們登錄到工作節點服務器,然后運行如下命令加入集群(這也是上面init
輸出的一部分):
kubeadm join 172.10.30.100:6443 --token 8gl99n.iu0m7c669vm0d1b1 --discovery-token-ca-cert-hash sha256:e3107f0be5fa44f939a6460b0c980a89e6d491dca89f5a44bc6923e40acc8e0b
等待一會,我們可以在Master節點上使用kubectl get nodes
命令來查看節點的狀態:
[kuber@master ~]$ kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 50m v1.13.1 node1 Ready <none> 56s v1.13.1
如上全部Ready
,大功告成,我們可以運行一些命令來測試一下集群是否正常。
5.5 測試
首先驗證kube-apiserver, kube-controller-manager, kube-scheduler, pod network 是否正常:
# 部署一個 Nginx Deployment,包含兩個Pod # https://kubernetes.io/docs/concepts/workloads/controllers/deployment/ kubectl create deployment nginx --image=nginx:alpine kubectl scale deployment nginx --replicas=2 # 驗證Nginx Pod是否正確運行,並且會分配192.168.開頭的集群IP kubectl get pods -l app=nginx -o wide # 輸出如下: NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-54458cd494-p8jzs 1/1 Running 0 31s 192.168.1.2 node1 <none> <none> nginx-54458cd494-v2m4b 1/1 Running 0 24s 192.168.1.3 node1 <none> <none>
再驗證一下kube-proxy
是否正常:
# 以 NodePort 方式對外提供服務 https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/ kubectl expose deployment nginx --port=80 --type=NodePort # 查看集群外可訪問的Port kubectl get services nginx # 輸出 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx NodePort 10.110.49.49 <none> 80:31899/TCP 4s # 可以通過任意 NodeIP:Port 在集群外部訪問這個服務,本示例中部署的2台集群IP分別是172.17.20.210和172.17.20.211 curl http://172.17.20.210:31899 curl http://172.17.20.211:31899
最后驗證一下dns, pod network是否正常:
# 運行Busybox並進入交互模式 kubectl run -it curl --image=radial/busyboxplus:curl # 輸入`nslookup nginx`查看是否可以正確解析出集群內的IP,已驗證DNS是否正常 [ root@curl-66959f6557-6sfqh:/ ]$ nslookup nginx # 輸出 Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: nginx Address 1: 10.110.49.49 nginx.default.svc.cluster.local # 通過服務名進行訪問,驗證kube-proxy是否正常 [ root@curl-66959f6557-6sfqh:/ ]$ curl http://nginx/ # 輸出如下: # <!DOCTYPE html> ---省略 # 分別訪問一下2個Pod的內網IP,驗證跨Node的網絡通信是否正常 [ root@curl-66959f6557-6sfqh:/ ]$ curl http://192.168.1.2/ [ root@curl-66959f6557-6sfqh:/ ]$ curl http://192.168.1.3/