docker筆記20-初始化k8s叢集
k8s的部署有多種方式,但我們採用kubeadm工具部署。
kubeadm官方地址:
一、環境
master,etcd: 172.16.1.100
node1: 172.16.1.101
node2: 172.16.1.102
k8s版本:1.11
二、前提
1、基於主機名通訊:/etc/hosts;
172.16.1.100 master 172.16.1.101 node01 172.16.1.102 node02
2、時間同步;
3、關閉firewalld和iptables.service,這兩個一定要禁用,因為k8s會自己設定iptables網路策略等;
systemctl stop iptables.service systemctl disable iptables.service systemctl stop firewalld.service systemctl disable firewalld.service
4、網路橋接全部設定為1
[root@k8s-master yum.repos.d]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables 1 [root@k8s-master yum.repos.d]# cat /proc/sys/net/bridge/bridge-nf-call-iptables 1
三、安裝配置步驟
1、下載k8s安裝包
下載kubernetes包:
我們為了方便,不使用上面安裝包的來安裝k8s,這裡只是讓大家瞭解一下。我們此次安裝使用kubeadm方式安裝。
2、準備好yum源(master和nodes都需要)
a) docker源
wget
b)k8s源
[root@k8s-master yum.repos.d]# cat k8s.repo [k8s] name=k8s repo baseurl= gpgcheck=1 gpgkey= enabled=1
[root@k8s-master yum.repos.d]# yum repolist
[root@k8s-master yum.repos.d]# wget [root@k8s-master yum.repos.d]# rpm --import yum-key.gpg
[root@k8s-master yum.repos.d]# wget [root@k8s-master yum.repos.d]# rpm --import rpm-package-key.gpg
3、首先安裝kublete、kubeadm、docker(在master上執行)
yum -y install docker-ce kubelet kubeadm kubectl (master上執行)
[root@k8s-master yum.repos.d]# rpm -ql kubelet /etc/kubernetes/manifests #清單目錄 /etc/sysconfig/kubelet #配置檔案 /etc/systemd/system/kubelet.service /usr/bin/kubelet #主程式
4、安裝代理(為了翻牆)
由於中國某種不可描述的原因,需要更改docker 預設拉取映象的源
root@k8s-master yum.repos.d]# vim /usr/lib/systemd/system/docker.service [Service] #表示訪問https服務時,透過下面的代理來訪問,本次這麼做的目的是為了能訪問外國的docer映象,要不會被牆,用完了再註釋掉,從而繼續使用國內 的映象 Environment="HTTPS_PROXY= Environment="NO_PROXY=127.0.0.0/8,172.16.0.0/16"
[root@k8s-master yum.repos.d]# systemctl daemon-reload [root@k8s-master yum.repos.d]# systemctl start docker
[root@k8s-master yum.repos.d]# docker info #能看到如下兩個 HTTPS Proxy: No Proxy: 127.0.0.0/8,172.16.0.0/16
[chenzx@sa ~]$ telnet 要確保這個埠是通的
5、執行kubeadm int初始化叢集(在master上)
該過程會做先決條件預檢、生成證書、私鑰、生成配置檔案、生成靜態pod的清單檔案並完成部署(addons)
[root@k8s-master yum.repos.d]# systemctl enable kubelet #首先只能設定為開機自啟動,但先不要不要手工啟動該服務(即使現在啟動也啟動不起來),等初始化完成了再啟動。
[root@k8s-master chenzx]# systemctl enable docker
[root@k8s-master chenzx]# kubeadm init --help
--apiserver-advertise-address:表示apiserver對外的地址是什麼,預設是0.0.0.0
--apiserver-bind-port:表示apiserver的埠是什麼,預設是6443
--cert-dir:載入證書的目錄,預設在/etc/kubernetes/pki
--config:配置檔案
--ignore-preflight-errors:在預檢中如果有錯誤可以忽略掉,比如忽略 IsPrivilegedUser,Swap.等
--kubernetes-version:指定要初始化k8s的版本資訊是什麼
--pod-network-cidr :指定pod使用哪個網段,預設使用10.244.0.0/16
--service-cidr:指定service元件使用哪個網段,預設10.96.0.0/12
[root@k8s-master chenzx]# cat /etc/sysconfig/kubelet #指定額外的初始化資訊,下面表示禁用作業系統的swap功能 KUBELET_EXTRA_ARGS="--fail-swap-on=false"
[root@k8s-master chenzx]# kubeadm init --kubernetes-version=v1.11.1 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap [preflight/images] Pulling images required for setting up a Kubernetes cluster ##表示開始拉取映象 [preflight/images] This might take a minute or two, depending on the speed of your internet connection [preflight/images] You can also perform this action in beforehand using 'kubeadm config images pull' ##如果你感覺網速慢,可以執行kubeadm config images pull命令把映象拖到本地 [certificates] Generated apiserver-kubelet-client certificate and key. ##可以看到生成一堆證書 [certificates] Generated sa key and public key. [certificates] Generated front-proxy-ca certificate and key. [certificates] Generated front-proxy-client certificate and key. [certificates] Generated etcd/ca certificate and key. [controlplane] wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml" ##yml控制給pod分多少cpu和記憶體 [controlplane] wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager. ###markmaster幫我們把此節點標記為主節點 [markmaster] Marking the node k8s-master as master by adding the label "node-role.kubernetes.io/master=''" [markmaster] Marking the node k8s-master as master by adding the taints [node-role.kubernetes.io/master:NoSchedule] ##bootstraptoken是引導令牌,讓其他nodes加入叢集時用的 [bootstraptoken] using token: as5gwu.ktojf6cueg0doexi [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials ##從k8s 1.11版開始,DNS正式被CoreDNS取代,它支援很多新的功能,比如資源的動態配置等 [addons] Applied essential addon: CoreDNS ##kube-proxy託管在K8S之上,負責生產service的iptables和ipvs規則,從k8s1.11開始預設支援ipvs [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 ##其他機器裝好包後,可以執行下面的命令來把nodes節點加入叢集,把下面的命令記得自己儲存起來,要不將來找不著就加不進去了 ##其實這麼設計的目的就是不是誰都能加入叢集的,需要拿著下面的令牌來加入 You can now join any number of machines by running the following on each node as root: kubeadm join 172.16.1.100:6443 --token as5gwu.ktojf6cueg0doexi --discovery-token-ca-cert-hash sha256:399a7de763b95e52084d7bd4cad71dc8fa1bf6dd453b02743d445eee59252cc5
[root@k8s-master chenzx]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE k8s.gcr.io/kube-proxy-amd64 v1.11.1 d5c25579d0ff 7 weeks ago 97.8MB k8s.gcr.io/kube-apiserver-amd64 v1.11.1 816332bd9d11 7 weeks ago 187MB k8s.gcr.io/kube-controller-manager-amd64 v1.11.1 52096ee87d0e 7 weeks ago 155MB k8s.gcr.io/kube-scheduler-amd64 v1.11.1 272b3a60cd68 7 weeks ago 56.8MB k8s.gcr.io/coredns 1.1.3 b3b94275d97c 3 months ago 45.6MB k8s.gcr.io/etcd-amd64 3.2.18 b8df3b177be2 4 months ago 219MB k8s.gcr.io/pause 3.1 da86e6ba6ca1 8 months ago 742kB
說明:pause可以做一個容器,這個容器不用啟動,pause可以使其他容器複製基礎的網路和儲存構件。
如果安裝出錯了,可以執行kubeadm reset命令進行重置,再重新執行kubeadm init...命令
注意:上面初始化中輸出的kubeadm join 172.16.1.100:6443 --token.....這句話,一定要貼上到記事本儲存好,因為以後要使用這個命令把node加入叢集,並且該命令無法復現,切記切記!!!
[root@k8s-master chenzx]# mkdir -p $HOME/.kube [root@k8s-master chenzx]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
6、在nodes節點上安裝k8s包(所有nodes節點上執行)
yum -y install docker-ce kubelet kubeadm (node上執行,nodes上可以不安裝kubectl)
7、檢視狀態資訊(在master上)
檢視元件資訊:
[root@k8s-master chenzx]# kubectl get cs NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health": "true"}
檢視節點資訊:
[root@k8s-master chenzx]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 51m v1.11.2
說明,:狀態為NotReady,是因為還缺flannel元件,沒有這個元件是沒法設定網路的。
8、安裝flannel網路元件(master上執行)
下載地址:
安裝flannel: [root@k8s-master chenzx]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
檢視當前master節點上kube-system名稱空間裡執行的所有pod狀態:
[root@k8s-master chenzx]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-78fcdf6894-6j6nt 0/1 Running 0 2h coredns-78fcdf6894-pnmjj 0/1 Running 0 2h etcd-k8s-master 1/1 Running 0 1h kube-apiserver-k8s-master 1/1 Running 0 1h kube-controller-manager-k8s-master 1/1 Running 0 1h kube-flannel-ds-amd64-txxw2 1/1 Running 0 1h kube-proxy-frkp9 1/1 Running 0 2h kube-scheduler-k8s-master 1/1 Running 0 1h
另外,以上所有pod必須保證都是running狀態的,如果哪個不是,可以透過類似如下命令檢視為什麼:
kubectl dscrible pods coredns-78fcdf6894-6j6nt -n kube-system
檢視flannel映象: [root@k8s-master chenzx]# docker images quay.io/coreos/flannel REPOSITORY TAG IMAGE ID CREATED SIZE quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 7 months ago 44.6MB
檢視nodes節點資訊,看到status這回變成ready了 [root@k8s-master chenzx]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 1h v1.11.2
檢視當前節點名稱空間:
[root@k8s-master chenzx]# kubectl get ns NAME STATUS AGE default Active 3h kube-public Active 3h kube-system Active 3h
9、執行kubeadm join(在node1和node2上執行,表示加入叢集中來)
該過程也會先檢查先決條件是否滿足需求,然後基於域共享的令牌認證方式完成master節點的認證,並完成本地的pod的資源安裝,包含以addons方法部署的kubbe-proxy、DNS。
1)在node1和node2上修改配置檔案並啟動服務:
[root@k8s-master chenzx]# vim /usr/lib/systemd/system/docker.service
[Service] Environment="HTTPS_PROXY= Environment="NO_PROXY=127.0.0.0/8,172.16.0.0/16"
[root@k8s-master chenzx]# vim /etc/sysconfig/kubelet
#指定額外的初始化資訊 KUBELET_EXTRA_ARGS="--fail-swap-on=false"
[root@k8s-node1 chenzx]# systemctl daemon-reload [root@k8s-node1 chenzx]# systemctl start docker [root@k8s-node1 chenzx]# systemctl enable docker [root@k8s-node1 chenzx]# systemctl enable kubelet
[root@k8s-node1 chenzx]# docker info
HTTPS Proxy: No Proxy: 127.0.0.0/8,172.16.0.0/16
[root@k8s-node1 chenzx]# kubeadm join 172.16.1.100:6443 --token as5gwu.ktojf6cueg0doexi --discovery-token-ca-cert-hash sha256:399a7de763b95e52084d7bd4cad71dc8fa1bf6dd453b02743d445eee59252cc5 --ignore-preflight-errors=Swap (注意:這個命令是在kubeadm init初始化中得到的)
[tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap... [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "k8s-node1" as an annotation This node has joined the cluster: * Certificate signing request was sent to master and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the master to see this node join the cluster.
[root@k8s-node1 chenzx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE k8s.gcr.io/kube-proxy-amd64 v1.11.1 d5c25579d0ff 7 weeks ago 97.8MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 7 months ago 44.6MB k8s.gcr.io/pause 3.1 da86e6ba6ca1 8 months ago 742kB
[root@k8s-master chenzx]# kubectl get nodes (master上看)
NAME STATUS ROLES AGE VERSION k8s-master Ready master 4h v1.11.2 k8s-node1 Ready <none> 55m v1.11.2
[root@k8s-master chenzx]# kubectl get pods -n kube-system -o wide (master上看)
NAME READY STATUS RESTARTS AGE IP NODE coredns-78fcdf6894-6j6nt 0/1 Running 0 4h <none> k8s-master coredns-78fcdf6894-pnmjj 0/1 Running 0 4h <none> k8s-master etcd-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master kube-apiserver-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master kube-controller-manager-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master kube-flannel-ds-amd64-87tqv 1/1 Running 0 57m 172.16.1.101 k8s-node1 kube-flannel-ds-amd64-txxw2 1/1 Running 0 3h 172.16.1.100 k8s-master kube-proxy-2rf4m 1/1 Running 0 57m 172.16.1.101 k8s-node1 kube-proxy-frkp9 1/1 Running 0 4h 172.16.1.100 k8s-master kube-scheduler-k8s-master 1/1 Running 0 3h 172.16.1.100 k8s-master
以上命令在node2上也執行一遍。
此時已經完成k8s的安裝。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28916011/viewspace-2213536/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Docker 與 K8S學習筆記(二十三)—— Kubernetes叢集搭建DockerK8S筆記
- Mac + Docker + K8S 本地搭建K8S叢集MacDockerK8S
- docker筆記41-ceph叢集的日常運維Docker筆記運維
- Docker 學習筆記(第十集:docker 叢集管理工具 swarm )Docker筆記Swarm
- 使用虛擬機器初始化 K8S 叢集虛擬機K8S
- k8s介紹及與docker搭建叢集K8SDocker
- MongoDB叢集運維筆記MongoDB運維筆記
- Docker Swarm:叢集DockerSwarm
- Docker Swarm叢集DockerSwarm
- 【docker-ce】k8s叢集docker容器異常重啟問題分析DockerK8S
- k8s筆記6--使用kubeadm快速部署k8s叢集 v1.19.4K8S筆記
- Docker Swarm 叢集搭建DockerSwarm
- Docker Swarm叢集初探DockerSwarm
- Docker搭建ELK叢集Docker
- docker部署mysql叢集DockerMySql
- docker搭建mysql叢集DockerMySql
- Docker Elasticsearch 叢集配置DockerElasticsearch
- Docker部署ElasticSearch叢集DockerElasticsearch
- docker-redis叢集DockerRedis
- Docker搭建Redis叢集DockerRedis
- Docker 搭建叢集 MongoDBDockerMongoDB
- Kubernetes(k8s)叢集部署(k8s企業級Docker容器集K8SDocker
- docker 1.12 版本 docker swarm 叢集DockerSwarm
- Redis學習筆記(十八) 叢集(下)Redis筆記
- Redis學習筆記(十七) 叢集(上)Redis筆記
- ZooKeeper學習筆記一:叢集搭建筆記
- Redis學習筆記八:叢集模式Redis筆記模式
- redis與叢集實用操作筆記Redis筆記
- k8s之叢集管理K8S
- 多k8s叢集管理K8S
- k8s 叢集升級K8S
- 刪除k8s叢集K8S
- docker下,極速搭建spark叢集(含hdfs叢集)DockerSpark
- 用Docker搭建RabbitMq的普通叢集和映象叢集DockerMQ
- (三)docker-Elasticsearch 叢集DockerElasticsearch
- 使用docker 搭建rabbitmq 叢集DockerMQ
- Docker Swarm 叢集搭建教程DockerSwarm
- Docker swarm叢集搭建教程DockerSwarm