Kubernetes 1.12.2版,使用docker 映象安裝
kubernetes 1.12.2版本安裝
起碼準兩臺機器:master,node
ip: master:192.168.1.220
node1:192.168.1.221
node2:192.168.1.205
環境:centos7
配置:2核4G起
注意事項:
docker版本最高支援18.06,高於要此版本報錯
kubernetes12.2+docker-ce18.06.1ce
(一)環境配置:
在master和node 端執行:
1:安全策略規則配置
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
iptables -F
iptables -t nat -F
iptables -I FORWARD -s 0.0.0.0/0 -d 0.0.0.0/0 -j ACCEPT
yum -y install ntp
ntpdate pool.ntp.org
systemctl start ntpd
systemctl enable ntpd
2:核心設定:
關閉selinux
vim /etc/sysconfig/selinux
SELINUX=disable
修改核心引數
$vim /etc/sysctl.conf
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-arptables = 1
vm.swappiness=0
關閉swap
swapoff -a
註釋自動掛載
vim /etc/fstab
關閉selinux
vim /etc/selinux/config
儲存修改核心引數
sysctl -p
確保以下兩個檔案裡面顯示值為1:
[[email protected] ~]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables
1
[[email protected] ~]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables
每個節點都修改下面值,
[[email protected] ~]# vim /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="fail-swap-on=false"
3:域名解析,免密登入,時間同步
a:
[[email protected]
[[email protected] ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.220 k8m1
192.168.1.221 k8n1
192.168.1.205 k8n2
b:
[[email protected] ~]# vim /etc/hosts
[[email protected] ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ldQKudY4N7CV+VrQV6qCOeEC5KlKIeLk7F8tR/cm8kI [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| . ..= ..|
| o . +.=.o .. |
|oo + .Oo+ .. |
|B .. . .*== o. |
| =. .oS+.+. |
|o. oE..o. |
|.. o.+ . o |
| . . o.o o |
| . .. |
+----[SHA256]-----+
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa k8n1
[[email protected] ~]# ssh-copy-id -i ~/.ssh/id_rsa k8n2
c:master和node端分別執行
[[email protected] ~]# yum -y install ntp
[[email protected] ~]# ntpdate pool.ntp.org
10 Nov 11:07:32 ntpdate[50259]: step time server 193.228.143.22 offset 14.725020 sec
[[email protected] ~]# systemctl start ntpd
[[email protected] ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[[email protected] ~]#
(二)安裝docker:安裝官方要求安裝
配置docker-CE源 :master node都要配置
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
$sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
$ yum list docker-ce --showduplicates | sort -r
$yum install docker-ce-18.06.1.ce
備註:目前kubernetes1.12.2支援docker版本最多18.06,docker版本已經更新到18.9了,
所以不能指只有yum安裝最新版,要指定版本型號
$systemctl start docker
$systemctl enable docker
$systemctl status docker
執行下面命令配置加速器:
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
然後重啟docker
$systemctl restart docker
配置阿里雲:
[[email protected] yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#yum repolist
(三)配置kubernetes源:
vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes Rope
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
enabled=1
[[email protected] yum.repos.d]# yum repolist
已載入外掛:fastestmirror
kubernetes | 1.4 kB 00:00:00
kubernetes/primary | 37 kB 00:00:00
Loading mirror speeds from cached hostfile
- base: mirrors.163.com
- epel: mirrors.aliyun.com
- extras: mirrors.aliyun.com
- updates: mirrors.aliyun.com
kubernetes 272/272
源標識 源名稱 狀態
base/7/x86_64 CentOS-7 - Base 9,911
docker-ce-stable/x86_64 Docker CE Stable - x86_64 25
epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 12,696
extras/7/x86_64 CentOS-7 - Extras 434
kubernetes Kubernetes Rope 272
updates/7/x86_64 CentOS-7 - Updates 1,614
repolist: 24,952
在master端安裝:
#yum install -y kubelet kubeadm kubectl
kubeadm init --help #檢視初始化命令幫助
kubeadm config images list
使用下面這條命令:檢視到所需要都安裝元件有以下這些:
kubeadm config images list
服務元件:
k8s.gcr.io/kube-apiserver:v1.12.2
k8s.gcr.io/kube-controller-manager:v1.12.2
k8s.gcr.io/kube-scheduler:v1.12.2
k8s.gcr.io/kube-proxy:v1.12.2
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.2.24
k8s.gcr.io/coredns:1.2.2
這下做元件怎麼安裝能?當然是去dockerHub
使用下面命令docker pull下來:
docker pull mirrorgooglecontainers/kube-apiserver-amd64:v1.12.2
docker pull mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.2
docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.12.2
docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.12.2
docker pull mirrorgooglecontainers/pause-amd64:3.1
docker pull mirrorgooglecontainers/etcd-amd64:3.2.24
docker pull kuberneter/coredns:1.2.2
給下載下來都映象元件tag上和服務元件同樣都標籤:
docker tag mirrorgooglecontainers/kube-apiserver-amd64:v1.12.2 k8s.gcr.io/kube-apiserver:v1.12.2
docker tag mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.2 k8s.gcr.io/kube-controller-manager:v1.12.2
docker tag mirrorgooglecontainers/kube-scheduler-amd64:v1.12.2 k8s.gcr.io/kube-scheduler:v1.12.2
docker tag mirrorgooglecontainers/kube-proxy-amd64:v1.12.2 k8s.gcr.io/kube-proxy:v1.12.2
docker tag mirrorgooglecontainers/pause-amd64:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd-amd64:3.2.24 k8s.gcr.io/etcd:3.2.24
docker tag kuberneter/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2
初始化叢集,只在master端執行: ()注意修改為master地址)
kubeadm init \
--kubernetes-version=v1.12.2 \
--pod-network-cidr=10.244.0.0/16 \
--apiserver-advertise-address=192.168.1.220
master初始化之後會出現以下token,要複製下來儲存好,加node要用:
kubeadm join 192.168.1.220:6443 --token o4bmhn.12ppkvve2cecgk6m --discovery-token-ca-cert-hash sha256:03b9e1d1979288194bcf694c12a85217236319cfdf1b89aae803e430796c611b
如果上面都token忘記了 可以通過以下命令獲得:
kubeadmin token list
此時root使用者還不能使用kubelet控制叢集需要,配置下環境變數
對於非root使用者
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
啟動faannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
root 使用者執行以下配置環境變數
export KUBECONFIG=/etc/kubernetes/admin.conf
也可以直接放到~/.bash_profile
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
kubectl get pods --all-namespaces
node節點要安裝都軟體:
#yum install -y kubelet kubeadm kubectl
[[email protected] ~]# vim /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
node節點要安裝的docker映象:
docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.12.2
docker pull mirrorgooglecontainers/pause-amd64:3.1
docker pull kuberneter/coredns:1.2.2
docker pull mirrorgooglecontainers/etcd-amd64:3.2.24 #node媏的ETCD可以安裝,
也可以不安裝需要做ETCD 叢集,安裝就是
打標籤
docker tag mirrorgooglecontainers/kube-proxy-amd64:v1.12.2 k8s.gcr.io/kube-proxy:v1.12.2
docker tag mirrorgooglecontainers/pause-amd64:3.1 k8s.gcr.io/pause:3.1
docker tag kuberneter/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2
執行之前上面保留下來都token:就會自己加入叢集
kubeadm join 192.168.1.220:6443 --token o4bmhn.12ppkvve2cecgk6m --discovery-token-ca-cert-hash sha256:03b9e1d1979288194bcf694c12a85217236319cfdf1b89aae803e430796c611b
[[email protected] ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8m1 Ready master 22h v1.12.2
k8n1 Ready <none> 22h v1.12.2
k8n2 Ready <none> 37m v1.12.2
[[email protected] ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-576cbf47c7-jqcn8 1/1 Running 0 22h
kube-system coredns-576cbf47c7-rqqvk 1/1 Running 0 22h
kube-system etcd-k8m1 1/1 Running 0 22h
kube-system kube-apiserver-k8m1 1/1 Running 0 22h
kube-system kube-controller-manager-k8m1 1/1 Running 0 22h
kube-system kube-flannel-ds-amd64-ck799 1/1 Running 1 22h
kube-system kube-flannel-ds-amd64-nkdzg 1/1 Running 0 38m
kube-system kube-flannel-ds-amd64-tjp27 1/1 Running 0 22h
kube-system kube-proxy-8b7ds 1/1 Running 0 38m
kube-system kube-proxy-dmswb 1/1 Running 0 22h
kube-system kube-proxy-pm7wt 1/1 Running 0 22h
kube-system kube-scheduler-k8m1 1/1 Running 0 22h
OK,檢視節點可能啟動比較慢,要稍等,如果加不進去 要排查問題。。
(如果初始化叢集出現問題,可以使用下面命令進行清理進行重新初始化:kubeadm reset)