使用kubeadm安裝k8s,比起之前一個個安裝組件,已經很是簡單了。只不過由於國內網絡問題,google的東西都被牆了,因此感受安裝起來比較費勁。因此安裝ks關鍵在如何拉取鏡像,有四個方法法解決:node
github上有一個圖形化安裝k8s的項目,已經得到CNCF官方認證。 圖像界面安裝k8slinux
機器名稱 | ip地址 | 角色 | yum安裝的組件 |
---|---|---|---|
k8s-master | 10.1.69.101 | master | docker、kubelet、kubeadm、kubectl |
k8s-node1 | 10.1.69.102 | node | docker、kubelet、kubeadm |
k8s-node2 | 10.1.69.103 | node | docker、kubelet、kubeadm |
友情提示:虛擬機下安裝,最好固定ip地址。特別是master節點,由於安裝時,須要使用ip地址,且不能用域名。若是下次ip地址變了,可能會致使集羣不可用git
yum install -y wget vim net-tools epel-release
複製代碼
swapoff -a
# 永久禁用,打開/etc/fstab註釋掉swap那一行。
sed -i 's/.*swap.*/#&/' /etc/fstab
複製代碼
# 臨時禁用selinux
setenforce 0
# 永久關閉 修改/etc/sysconfig/selinux文件設置
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
複製代碼
systemctl disable firewalld
systemctl stop firewalld
複製代碼
## 配置默認源
## 備份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
## 下載阿里源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
## 刷新
yum makecache fast
## 配置k8s源
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=0
EOF
## 重建yum緩存
yum clean all
yum makecache fast
yum -y update
複製代碼
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl enable docker
systemctl start docker
複製代碼
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
sudo systemctl daemon-reload sudo systemctl restart docker
複製代碼
沒有代理執行步驟5github
mkdir /etc/systemd/system/docker.service.d
touch /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://xxx"
Environment="HTTPS_PROXY=http://xxx"
Environment="NO_PROXY=localhost,127.0.0.1,localaddress,.localdomain.com"
systemctl daemon-reload && systemctl restart docker
複製代碼
## 查看集羣初始化所需鏡像及對應依賴版本號,列出的就是須要下載的鏡像
kubeadm config images list
複製代碼
#!/bin/bash
## 使用以下腳本下載國內鏡像,並修改tag爲google的tag
set -e
KUBE_VERSION=v1.15.0
KUBE_PAUSE_VERSION=3.1
ETCD_VERSION=3.3.10
CORE_DNS_VERSION=1.3.1
GCR_URL=k8s.gcr.io
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(kube-proxy:${KUBE_VERSION}
kube-scheduler:${KUBE_VERSION}
kube-controller-manager:${KUBE_VERSION}
kube-apiserver:${KUBE_VERSION}
pause:${KUBE_PAUSE_VERSION}
etcd:${ETCD_VERSION}
coredns:${CORE_DNS_VERSION})
for imageName in ${images[@]} ; do
docker pull $ALIYUN_URL/$imageName
docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done
複製代碼
## 配置/etc/profile文件,特別是https,xxx替換爲本身或本身公司的代理地址
http_proxy=xxx
https_proxy=xxx
HTTP_PROXY=xxx
HTTPS_PROXY=xxx
複製代碼
yum -y install kubelet kubeadm kubectl kubernetes-cni
systemctl enable kubelet && systemctl start kubelet
複製代碼
## master節點執行:
sudo kubeadm init \
--apiserver-advertise-address 10.1.69.101 \
--kubernetes-version=v1.15.0 \
--pod-network-cidr=10.244.0.0/16
複製代碼
友情提示:若是鏡像沒有提早拉取,這一步會比較慢。配置了代理的話,可執行:kubeadm config images pull, 單獨拉取鏡像chrome
若是是kubeadm reset卸載後再安裝,可能報錯,加入這個參數:--ignore-preflight-errors=alldocker
獲得回覆:shell
(...省略)
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
## 保存好該命令,丟了很差找回。節點加入時須要
kubeadm join 10.1.69.101:6443 --token ou5pvo.qseafc4s8licblzy \
--discovery-token-ca-cert-hash sha256:de9c10f11c50c074f212698b9d514fc12a9c1c4ffe70961aff89ac5e585f0663
複製代碼
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
複製代碼
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
## 查看flannal是否安裝成功
sudo kubectl -n kube-system get po -l app=flannel -o wide
複製代碼
其餘節點執行:json
kubeadm join 10.1.69.101:6443 --token ou5pvo.qseafc4s8licblzy \
--discovery-token-ca-cert-hash sha256:de9c10f11c50c074f212698b9d514fc12a9c1c4ffe70961aff89ac5e585f0663
複製代碼
sudo kubeadm reset
複製代碼
下載yaml文件到本地vim
wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
複製代碼
配置dashboard界面能夠在虛擬機以外訪問centos
配置准入控制(不然各類沒有權限訪問)
apiVersion: v1
kind: ServiceAccount
metadata:
name: dashboard
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: dashboard
subjects:
- kind: ServiceAccount
name: dashboard
namespace: kube-system
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
複製代碼
開始安裝
sudo kubectl apply -f kubenetes-dashboard.yaml
sudo kubectl appy -f dashboard-rbac.yaml
複製代碼
查看dashboard是否安裝成功
## 下載鏡像須要必定時間,看pod處於running表示安裝成功
kubectl get pod -n kube-system |grep kubernetes-dashboard
複製代碼
用Firefox查看dashboard頁面(chrome沒法訪問不安全的https)
注意:url中地址必須用https!!!
進入到輸入token界面
獲取token命令
## 獲取登錄界面token
kubectl -n kube-system describe $(kubectl -n kube-system \
get secret -n kube-system -o name | grep namespace) | grep token
複製代碼
進入dashboard首頁
若是dashboard的token過時,如下腳本從新生成config文件
#!/bin/bash
TOKEN=$(kubectl -n kube-system describe secret default| awk '$1=="token:"{print $2}')
kubectl config set-credentials kubernetes-admin --token="${TOKEN}"
複製代碼
save導出的時候沒有指定image的tag
kubeadm init時,apiserver的地址寫錯了
echo "1">/proc/sys/net/bridge/bridge-nf-call-iptables
kubeadm reset, 重置節點,從新加入