k8s安装

基本环境配置

注意:本次折腾是在centos系统下

  • 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
1
2
  • 关闭sawp
swapoff -a
vi /etc/fstab
# 注释下面行
......
#/dev/mapper/centos-swap swap                    swap    defaults        0 0
......
"/etc/fstab" 14L, 694C written
1
2
3
4
5
6
7
  • 禁用selinux
setenforce 0
vi /etc/selinux/config
SELINUX=disabled
1
2
3
  • 安装docker
cd /etc/yum.repos.d/
wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install docker-ce
sudo systemctl start docker
sudo systemctl enable docker
1
2
3
4
5
  • 添加kubernetes.repo
cat >> /etc/yum.repos.d/kubernetes.repo <<EOF 
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
			 http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
1
2
3
4
5
6
7
8
9
10
  • centos7用户还需要设置路由
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
1
2
3
4

修改完成后执行sysctl --system应用。

  • 安装kubernetes包

参考链接

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet
1
2

修改/etc/sysconfig/kubelet,加入:

KUBELET_EXTRA_ARGS=--fail-swap-on=false
1

完成后启动kubelet服务

systemctl enable kubelet.service
systemctl start kubelet.service
1
2

以上操作需要在所有节点执行。

初始化集群

# 指定k8s版本
# kubeadm init --kubernetes-version v1.13.0 --pod-network-cidr=10.244.0.0/16
# 安装最新版本,会从k8s.gcr.io下载k8s所需要的镜像,国内网络是下载不了的。
kubeadm init --pod-network-cidr=10.244.0.0/16
1
2
3
4

两种方式下载k8s所需要的镜像

  • 方法一
[root@base ~]# kubeadm config images list
W0813 23:29:24.324521   25028 version.go:98] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
W0813 23:29:24.324612   25028 version.go:99] falling back to the local client version: v1.15.1
k8s.gcr.io/kube-apiserver:v1.15.1
k8s.gcr.io/kube-controller-manager:v1.15.1
k8s.gcr.io/kube-scheduler:v1.15.1
k8s.gcr.io/kube-proxy:v1.15.1
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1

#!/bin/bash
images=(
    kube-apiserver:v1.15.1
    kube-controller-manager:v1.15.1
    kube-scheduler:v1.15.1
    kube-proxy:v1.15.1
    pause:3.1
    etcd:3.3.10
    coredns:1.3.1
)

for imageName in ${images[@]} ; do
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  • 方法二

    Kubernetes v1.14 中可以使用--image-repository参数指定自定义的镜像仓库,而不需要手动docker pulldocker tag准备容器镜像,非常实用的一个参数。

    kubeadm init --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.81.101
    
    kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
    
    1
    2
    3

初始化之后会弹出以下类似的语句,标明客户端加入master的命令

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 192.168.10.21:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:564e3ba4b76649e981300fcea9e4400b759f91a02f4a968e035ada454f3a1d2e

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

主要提示:

1. 推荐非root账号使用`kubectl`命令。本文使用hall账号。
2. 需要在集群中创建pod network。本文使用flannel, 初始化命令中的`--pod-network-cidr=10.244.0.0/16`是使用flannel的必备参数,详情见官方英文文档。
3. 显示了其它节点加入集群的命令。
1
2
3

然后执行一下命令

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
1
2
3

查看node

kubectl get nodes
1
[root@k8s-master ~]# kubectl get nodes
NAME         STATUS     ROLES    AGE    VERSION
k8s-master   NotReady   master   121m   v1.15.1
k8s-slave    NotReady   <none>   113m   v1.15.1
1
2
3
4

这里状态为NotReady是因为还没有进行pod network安装

安装详情查看:https://juejin.im/post/5c9a49ace51d456c9d78dbef#heading-1

注意:

如果执行下面命令提示端口报错的是,可以加上 --ignore-preflight-errors=all

kubeadm init --config kubeadm-init.yaml
1

安装dashboard

github地址:https://github.com/kubernetes/dashboard

注意:在master上操作

官方说明:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
1

由于在国内,上面命令是无法下载k8s.gcr.io的镜像的,所以可以修改为阿里云的镜像

wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
1

然后修改kubernetes-dashboard.yaml,把里面的k8s.gcr.io 改为registry.cn-hangzhou.aliyuncs.com/google_containers

然后执行

kubectl apply -f kubernetes-dashboard.yaml 
1

参考链接:

https://www.cnblogs.com/harlanzhang/p/10045975.html

https://cloud.tencent.com/developer/article/1115558

https://192.168.31.78:30001/
1

Token:

eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi0ybGs4cyIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjZlNzEyNTc4LWU0NWQtNGM0Ni04YzNmLTUwZjQ0NGQzZDEwMCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.aVYbffMpeNa1sY8mOHCiDaFrRa0lWQtgl3scV_5xinxPvA5CzEYQItryAPQ_ceRQFj6GGv994Q4jpAfoTdDyJbhQYsEN3MP8apo7bOR94MWIh57lf8ftIUDBV46c15Lc55SYp9MDinxEEmmioYFSl4yJ0FiHH73rq0vE21ARyc5b_hquGwVh5vMStlU1GtiPSXzTmfLQlDLtXI-mqP_hPHLnmjTs0dInvzbshHKQuS5emkfStYJoWbBMx5bdxgA1OmlLptkDK73CUsk5ygFB13CcF4nobxHfxMYRSx1S-bx4mGJDHryl38ddU0YCkJpqDut582djkEj0dRxMEb0Jwg
1

常用命令

kubectl get nodes
kubectl get pods --namespace=kube-system

kubectl get deployment kubernetes-dashboard -n kube-system
 
kubectl get pods -n kube-system -o wide
 
kubectl get services -n kube-system
kubectl get pods -o wide --all-namespaces
kubectl describe pod kubernetes-dashboard -n kube-system
1
2
3
4
5
6
7
8
9
10

参考资料

kubernetes 1.13 全新安装指南

Kubeadm创建Kubernetes集群

部署Kubernetes cluster

kubenetes 1.15.0 安装脚本