This page looks best with JavaScript enabled

10分钟搭建kubernetes

 ·  ☕ 2 min read

简单记录一下使用kubeadm安装指定版本的 kubernetes

step 1

准备好一台linux机器,这里是以ubuntu 18.04为例,进行如下配置

修改镜像源

更改默认镜像源加快速度

deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic main restricted

deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic-updates main restricted

deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic universe
deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic-updates universe

deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic multiverse
deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic-updates multiverse

deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic-backports main restricted universe multiverse


deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic-security main restricted
deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic-security universe
deb http://mirrors.huaweicloud.com/repository/ubuntu/ bionic-security multiverse

添加docker-ce和kubernetes的镜像源

1
2
3
4
#curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add
#curl http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable

安装docker-ce和kubernetes组件

这里安装的版本是 docker 18.09 和kubernetes 1.14.8

 apt install 18.09.9~3-0~ubuntu-bionic
 apt install kubectl=1.14.8-00
 apt install kubelet=1.14.8-00
 apt install kubeadm=1.14.8-00

优化系统参数,并开启ipvs模块

1
2
3
4
$ cat /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
1
2
3
4
5
6
7
$ cat /etc/modules-load.d/ipvs.conf
# /etc/modules-load.d
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack

安装ipvsadm

apt install ipvsadm

重启后检查ipvs模块如下

1
2
3
4
5
6
7
8
$ lsmod|grep ip_vs
ip_vs_sh               16384  0
ip_vs_wrr              16384  0
ip_vs_rr               16384  0
ip_vs                 151552  6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_defrag_ipv6         20480  1 ip_vs
nf_conntrack          131072  8 xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_ipv4,nf_nat,ipt_MASQUERADE,nf_nat_ipv4,nf_conntrack_netlink,ip_vs
libcrc32c              16384  3 nf_conntrack,nf_nat,ip_vs

优化docker

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ cat /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": [""],
  "insecure-registries" : [""],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "300m"
  },
  "storage-driver": "overlay2"
}

step 2

配置kubeadm

输出默认配置文件

kubeadm config print init-defaults

修改配置,如下

 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
$ cat kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1beta1
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 10.55.55.10
  bindPort: 6443
nodeRegistration:
  taints:
  - effect: PreferNoSchedule
    key: node-role.kubernetes.io/master
---
apiVersion: kubeadm.k8s.io/v1beta1
kind: ClusterConfiguration
kubernetesVersion: v1.14.8
networking:
  podSubnet: 10.244.0.0/16
apiServer:
  certSANs:
  - 10.55.55.10
  - 192.168.2.100
  - 111.111.111.123
  - kube.example.com
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs

开始安装

1
kubeadm init --config kubeadm.yaml

如果拉去进行失败,可以使用下面的脚本拉取

1
2
3
4
5
6
7
#!/bin/bash
for i in `kubeadm config images list`; do
  imageName=${i#k8s.gcr.io/}
  docker pull registry.aliyuncs.com/google_containers/$imageName
  docker tag registry.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
  docker rmi registry.aliyuncs.com/google_containers/$imageName
done;

安装完成后,配置kubectl

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

安装网络插件

https://kubernetes.io/docs/concepts/cluster-administration/addons/

kubecrtl apply -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml

kubectl apply -f kube-flannel.yml

至此,基本安装完成,然后检查集群状态是否ready

1
2
3
4
$ kubectl get node
NAME        STATUS   ROLES    AGE   VERSION
dev-k8s-1   Ready    master   13m   v1.14.8

镜像源替换列表

global proxy in China format example
dockerhub(docker.io) dockerhub.azk8s.cn dockerhub.azk8s.cn/<repo-name>/<image-name>:<version> dockerhub.azk8s.cn/microsoft/azure-cli:2.0.61 dockerhub.azk8s.cn/library/nginx:1.15
gcr.io gcr.azk8s.cn gcr.azk8s.cn/<repo-name>/<image-name>:<version> gcr.azk8s.cn/google_containers/hyperkube-amd64:v1.13.5
quay.io quay.azk8s.cn quay.azk8s.cn/<repo-name>/<image-name>:<version> quay.azk8s.cn/deis/go-dev:v1.10.0
Share on

tux
WRITTEN BY
tux
devops

What's on this Page