環境說明:node
主機名 | 操做系統版本 | ip | docker version | kubelet version | kubeadm version | kubectl version | flannel version | 備註 |
---|---|---|---|---|---|---|---|---|
master | Centos 7.6.1810 | 172.27.9.131 | Docker 18.09.6 | V1.14.2 | V1.14.2 | V1.14.2 | V0.11.0 | master主機 |
node01 | Centos 7.6.1810 | 172.27.9.135 | Docker 18.09.6 | V1.14.2 | V1.14.2 | V1.14.2 | V0.11.0 | node節點 |
node02 | Centos 7.6.1810 | 172.27.9.136 | Docker 18.09.6 | V1.14.2 | V1.14.2 | V1.14.2 | V0.11.0 | node節點 |
Centos7.6安裝詳見:Centos7.6操做系統安裝及優化全紀錄
Kubernetes這個名字源自希臘語,意思是「舵手」,也是「管理者」,「治理者」等詞的源頭。k8s是Kubernetes的簡稱(用數字『8』替代中間的8個字母『ubernete』)
linux
前言:本文經過kudeadm方式在centos7.6上安裝kubernetes v1.14.2集羣(目前centos和kubernetes都爲最新版),共分爲五個部分:
1、Docker安裝;
2、k8s安裝準備工做;
3、Master節點安裝;
4、Node節點安裝;
5、Dashboard安裝;
6、集羣測試。nginx
全部節點都須要安裝dockergit
[root@centos7 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@centos7 ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@centos7 ~]# yum list docker-ce --showduplicates | sort -r
[root@centos7 ~]# yum install docker-ce-18.09.6 docker-ce-cli-18.09.6 containerd.io
指定安裝的docker版本爲18.09.6,因爲該版本目前爲最新版,故能夠直接安裝,不用指定版本:github
yum install -y docker-ce docker-ce-cli containerd.io
[root@centos7 ~]# systemctl start docker [root@centos7 ~]# systemctl enable docker
[root@centos7 ~]# yum -y install bash-completion
[root@centos7 /]# source /etc/profile.d/bash_completion.sh
因爲Docker Hub的服務器在國外,下載鏡像會比較慢,能夠配置鏡像加速器。主要的加速器有:Docker官方提供的中國registry mirror、阿里雲加速器、DaoCloud 加速器,本文以阿里加速器配置爲例。web
登錄地址爲:https://cr.console.aliyun.com ,未註冊的能夠先註冊阿里雲帳戶docker
配置daemon.json文件apache
[root@centos7 ~]# mkdir -p /etc/docker [root@centos7 ~]# tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://v16stybc.mirror.aliyuncs.com"] } EOF
重啓服務json
[root@centos7 ~]# systemctl daemon-reload [root@centos7 ~]# systemctl restart docker
加速器配置完成bootstrap
[root@centos7 ~]# docker --version [root@centos7 ~]# docker run hello-world
經過查詢docker版本和運行容器hello-world來驗證docker是否安裝成功。
安裝Centos是已經禁用了防火牆和selinux並設置了阿里源。master和node節點都執行本部分操做。
[root@centos7 ~]# hostnamectl set-hostname master [root@centos7 ~]# more /etc/hostname master
退出從新登錄便可顯示新設置的主機名master
[root@master ~]# cat >> /etc/hosts << EOF 172.27.9.131 master 172.27.9.135 node01 172.27.9.136 node02 EOF
[root@master ~]# cat /sys/class/net/ens33/address [root@master ~]# cat /sys/class/dmi/id/product_uuid
保證各節點mac和uuid惟一
[root@master ~]# swapoff -a
若須要重啓後也生效,在禁用swap後還需修改配置文件/etc/fstab,註釋swap
[root@master ~]# sed -i.bak '/swap/s/^/#/' /etc/fstab
[root@master ~]# sysctl net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-iptables = 1 [root@master ~]# sysctl net.bridge.bridge-nf-call-ip6tables=1 net.bridge.bridge-nf-call-ip6tables = 1
[root@master ~]# cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF [root@master ~]# sysctl -p /etc/sysctl.d/k8s.conf
修改daemon.json,新增‘"exec-opts": ["native.cgroupdriver=systemd"’
[root@master ~]# more /etc/docker/daemon.json { "registry-mirrors": ["https://v16stybc.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"] }
[root@master ~]# systemctl daemon-reload [root@master ~]# systemctl restart docker
修改cgroupdriver是爲了消除告警:
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[root@master ~]# 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
- [] 中括號中的是repository id,惟一,用來標識不一樣倉庫
- name 倉庫名稱,自定義
- baseurl 倉庫地址
- enable 是否啓用該倉庫,默認爲1表示啓用
- gpgcheck 是否驗證從該倉庫得到程序包的合法性,1爲驗證
- repo_gpgcheck 是否驗證元數據的合法性 元數據就是程序包列表,1爲驗證
- gpgkey=URL 數字簽名的公鑰文件所在位置,若是gpgcheck值爲1,此處就須要指定gpgkey文件的位置,若是gpgcheck值爲0就不須要此項了
[root@master ~]# yum clean all [root@master ~]# yum -y makecache
[root@master ~]# yum list kubelet --showduplicates | sort -r
目前最新版是1.14.2,該版本支持的docker版本爲1.13.1, 17.03, 17.06, 17.09, 18.06, 18.09。
[root@master ~]# yum install -y kubelet-1.14.2 kubeadm-1.14.2 kubectl-1.14.2
若不指定版本直接運行‘yum install -y kubelet kubeadm kubectl’則默認安裝最新版即1.14.2,兩種方式結果一致。
- kubelet 運行在集羣全部節點上,用於啓動Pod和容器等對象的工具
- kubeadm 用於初始化集羣,啓動集羣的命令工具
- kubectl 用於和集羣通訊的命令行,經過kubectl能夠部署和管理應用,查看各類資源,建立、刪除和更新各類組件
啓動kubelet並設置開機啓動
[root@master ~]# systemctl enable kubelet && systemctl start kubelet
[root@master ~]# echo "source <(kubectl completion bash)" >> ~/.bash_profile [root@master ~]# source .bash_profile
Kubernetes幾乎全部的安裝組件和Docker鏡像都放在goolge本身的網站上,直接訪問可能會有網絡問題,這裏的解決辦法是從阿里雲鏡像倉庫下載鏡像,拉取到本地之後改回默認的鏡像tag。
[root@master ~]# more image.sh #!/bin/bash url=registry.cn-hangzhou.aliyuncs.com/google_containers version=v1.14.2 images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`) for imagename in ${images[@]} ; do docker pull $url/$imagename docker tag $url/$imagename k8s.gcr.io/$imagename docker rmi -f $url/$imagename done
url爲阿里雲鏡像倉庫地址,version爲安裝的kubernetes版本。
運行腳本image.sh,下載指定版本的鏡像
[root@master ~]# ./image.sh [root@master ~]# docker images
[root@master ~]# kubeadm init --apiserver-advertise-address 172.27.9.131 --pod-network-cidr=10.244.0.0/16
apiserver-advertise-address指定master的interface,pod-network-cidr指定Pod網絡的範圍,這裏使用flannel網絡方案。
記錄kubeadm join的輸出,後面須要這個命令將各個節點加入集羣中。
[root@master ~]# echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile [root@master ~]# source .bash_profile
本文全部操做都在root用戶下執行,若爲非root用戶,則執行以下操做:
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config
[root@master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
taint:污點的意思。若是一個節點被打上了污點,那麼pod是不容許運行在這個節點上面的
默認狀況下集羣不會在master上調度pod,若是偏想在master上調度Pod,能夠執行以下操做:
查看污點:
[root@master ~]# kubectl describe node master|grep -i taints Taints: node-role.kubernetes.io/master:NoSchedule
刪除默認污點:
[root@master ~]# kubectl taint nodes master node-role.kubernetes.io/master- node/master untainted
語法:
kubectl taint node [node] key=value[effect] 其中[effect] 可取值: [ NoSchedule | PreferNoSchedule | NoExecute ] NoSchedule: 必定不能被調度 PreferNoSchedule: 儘可能不要調度 NoExecute: 不只不會調度, 還會驅逐Node上已有的Pod
打污點
[root@master ~]# kubectl taint node master key1=value1:NoSchedule node/master tainted [root@master ~]# kubectl describe node master|grep -i taints Taints: key1=value1:NoSchedule
key爲key1,value爲value1(value能夠爲空),effect爲NoSchedule表示必定不能被調度
刪除污點:
[root@master ~]# kubectl taint nodes master key1- node/master untainted [root@master ~]# kubectl describe node master|grep -i taints Taints: <none>
刪除指定key全部的effect,‘-’表示移除全部以key1爲鍵的污點
同master節點
同master節點
如下操做master上執行
[root@master ~]# kubeadm token list TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS j5eoyz.zu0x6su7wzh752b3 <invalid> 2019-06-04T17:40:41+08:00 authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
發現以前初始化時的令牌已過時
[root@master ~]# kubeadm token create 1zl3he.fxgz2pvxa3qkwxln
[root@master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \ openssl dgst -sha256 -hex | sed 's/^.* //'
在node節點上分別執行以下操做:
[root@node01 ~]# kubeadm join 172.27.9.131:6443 --token 1zl3he.fxgz2pvxa3qkwxln --discovery-token-ca-cert-hash sha256:5f656ae26b5e7d4641a979cbfdffeb7845cc5962bbfcd1d5435f00a25c02ea50
[root@master ~]# wget https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml
若是鏈接超時,能夠多試幾回
sed -i 's/k8s.gcr.io/registry.cn-hangzhou.aliyuncs.com\/kuberneters/g' kubernetes-dashboard.yaml
因爲默認的鏡像倉庫網絡訪問不通,故改爲阿里鏡像
sed -i '/targetPort:/a\ \ \ \ \ \ nodePort: 30001\n\ \ type: NodePort' kubernetes-dashboard.yaml
配置NodePort,外部經過https://NodeIp:NodePort 訪問Dashboard,此時端口爲30001
cat >> kubernetes-dashboard.yaml << EOF --- # ------------------- dashboard-admin ------------------- # apiVersion: v1 kind: ServiceAccount metadata: name: dashboard-admin namespace: kube-system --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: dashboard-admin subjects: - kind: ServiceAccount name: dashboard-admin namespace: kube-system roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin EOF
建立超級管理員的帳號用於登陸Dashboard
[root@master ~]# kubectl apply -f kubernetes-dashboard.yaml
[root@master ~]# kubectl get deployment kubernetes-dashboard -n kube-system [root@master ~]# kubectl get pods -n kube-system -o wide [root@master ~]# kubectl get services -n kube-system
kubectl describe secrets -n kube-system dashboard-admin
令牌爲:
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4ta3Q5eDciLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiYzI2MjZiYTUtOTE5Ny0xMWU5LTk2OTQtMDAwYzI5ZDk5YmEzIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.XnBTkkMNQx_hKlTml7B-D1Ip-hp-WFi1mgjgx2E_SHbaRJluLN5qXWLsBAPl1Cgp-IY5ujg9pYePAZL2GDYyjnCaMiFmQsRW-zbNfkyYfPWje8MtCxJqoILTlCxsa5apwWsKdbsW_X8moMC4cM92hApQshn_-x-V7cxydEKuQPrB3PfQ7ReNMM5VCj4rexAh9Qr7I1wEHFa0KX1XaDRiedjWgXFKU7kWX2VQ3vwnfRdpKq_r0vlDSQRVdrMDPT1BHuBaUn8Gz-EdLR3qUy5vlZbzo1UFvxuL2enoZxntHZmnP1vg4552HROeEJhigdtRSbDvxHTEGWT3A67faha-Kg
請使用火狐瀏覽器訪問
經過令牌方式登陸
Dashboard提供了能夠實現集羣管理、工做負載、服務發現和負載均衡、存儲、字典配置、日誌視圖等功能。
[root@master ~]# kubectl run httpd-app --image=httpd --replicas=3 kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. deployment.apps/httpd-app created
經過命令行方式部署apache服務
cat >> nginx.yml << EOF apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx spec: replicas: 3 template: metadata: labels: app: nginx spec: restartPolicy: Always containers: - name: nginx image: nginx:latest EOF [root@master ~]# kubectl apply -f nginx.yml deployment.extensions/nginx created
經過配置文件方式部署nginx服務
[root@master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 47h v1.14.2 node01 Ready <none> 22h v1.14.2 node02 Ready <none> 6h55m v1.14.2
[root@master ~]# kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default httpd-app-6df58645c6-42zmc 1/1 Running 0 176m default httpd-app-6df58645c6-g6bkc 1/1 Running 0 176m default httpd-app-6df58645c6-kp8tl 1/1 Running 0 176m default nginx-9d4cf4f77-dft2f 1/1 Running 0 18m default nginx-9d4cf4f77-dztxq 1/1 Running 0 18m default nginx-9d4cf4f77-l9gdh 1/1 Running 0 18m kube-system coredns-fb8b8dccf-bxvrz 1/1 Running 1 47h kube-system coredns-fb8b8dccf-mqvd8 1/1 Running 1 47h kube-system etcd-master 1/1 Running 3 47h kube-system kube-apiserver-master 1/1 Running 3 47h kube-system kube-controller-manager-master 1/1 Running 4 47h kube-system kube-flannel-ds-amd64-lkh5n 1/1 Running 0 6h55m kube-system kube-flannel-ds-amd64-pv5ll 1/1 Running 1 24h kube-system kube-flannel-ds-amd64-wnn5g 1/1 Running 1 22h kube-system kube-proxy-42vb5 1/1 Running 3 47h kube-system kube-proxy-7nrfk 1/1 Running 0 6h55m kube-system kube-proxy-x7dmk 1/1 Running 1 22h kube-system kube-scheduler-master 1/1 Running 4 47h
[root@master ~]# kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE httpd-app 3/3 3 3 178m nginx 3/3 3 3 19m [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES httpd-app-6df58645c6-42zmc 1/1 Running 0 179m 10.244.0.6 master <none> <none> httpd-app-6df58645c6-g6bkc 1/1 Running 0 179m 10.244.1.2 node01 <none> <none> httpd-app-6df58645c6-kp8tl 1/1 Running 0 179m 10.244.2.2 node02 <none> <none> nginx-9d4cf4f77-dft2f 1/1 Running 0 20m 10.244.0.7 master <none> <none> nginx-9d4cf4f77-dztxq 1/1 Running 0 20m 10.244.2.3 node02 <none> <none> nginx-9d4cf4f77-l9gdh 1/1 Running 0 20m 10.244.1.3 node01 <none> <none> [root@master ~]#
能夠看到nginx和httpd的3個副本pod均勻分佈在3個節點上
[root@master ~]# kubectl describe deployments
[root@master ~]# kubectl get cs NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health":"true"}
至此完成Centos7.6下k8s(v1.14.2)集羣部署。
本文全部腳本和配置文件已上傳github:https://github.com/loong576/Centos7.6-install-k8s-v1.14.2-cluster.git
本文參考:
1.https://docs.docker.com/install/linux/docker-ce/centos;
2.https://kubernetes.io/docs/setup/independent/install-kubeadm;
3.https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm;
4.https://github.com/coreos/flannel;
5.https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG-1.14.md#external-dependencies;
6.https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/;