该篇文章结合了网络上零散的的资料整理出来的,亲测可用有效,有问题欢迎大家可以提出来
1永久禁用swap分区,注释掉/etc/fstab中swap那一行
sudo vim /etc/fstab
2 关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
3 kubelet安装与启动
执行命令
1:
cat < /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
2:
yum install -y kubelet kubeadm kubectl
4、创建初始化配置文件init.yml
apiVersion: kubeadm.k8s.io/v1alpha3
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
pod-infra-container-image: registry.aliyuncs.com/google_containers/pause-amd64:3.1
apiVersion: kubeadm.k8s.io/v1alpha3
kind: ClusterConfiguration
imageRepository: registry.aliyuncs.com/google_containers
kubernetesVersion: v1.13.2
networking:
podSubnet: 10.244.0.0/16
5、初始化集群
正常日志显示如下
kubeadm init --config init.yml
[init] Using Kubernetes version: v1.13.2
[preflight] Running pre-flight checks
[WARNING Hostname]: hostname “dev-k8s” could not be reached
[WARNING Hostname]: hostname “dev-k8s”: lookup dev-k8s on 192.168.12.6:53: server misbehaving
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using ‘kubeadm config images pull’
[kubelet-start] Writing kubelet environment file with flags to file “/var/lib/kubelet/kubeadm-flags.env”
[kubelet-start] Writing kubelet configuration to file “/var/lib/kubelet/config.yaml”
[kubelet-start] Activating the kubelet service
[certs] Using certificateDir folder “/etc/kubernetes/pki”
[certs] Generating “ca” certificate and key
[certs] Generating “apiserver” certificate and key
[certs] apiserver serving cert is signed for DNS names [dev-k8s kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.12.149]
[certs] Generating “apiserver-kubelet-client” certificate and key
[certs] Generating “front-proxy-ca” certificate and key
[certs] Generating “front-proxy-client” certificate and key
[certs] Generating “etcd/ca” certificate and key
[certs] Generating “etcd/peer” certificate and key
[certs] etcd/peer serving cert is signed for DNS names [dev-k8s localhost] and IPs [192.168.12.149 127.0.0.1 ::1]
[certs] Generating “apiserver-etcd-client” certificate and key
[certs] Generating “etcd/server” certificate and key
[certs] etcd/server serving cert is signed for DNS names [dev-k8s localhost] and IPs [192.168.12.149 127.0.0.1 ::1]
[certs] Generating “etcd/healthcheck-client” certificate and key
[certs] Generating “sa” key and public key
[kubeconfig] Using kubeconfig folder “/etc/kubernetes”
[kubeconfig] Writing “admin.conf” kubeconfig file
[kubeconfig] Writing “kubelet.conf” kubeconfig file
[kubeconfig] Writing “controller-manager.conf” kubeconfig file
[kubeconfig] Writing “scheduler.conf” kubeconfig file
[control-plane] Using manifest folder “/etc/kubernetes/manifests”
[control-plane] Creating static Pod manifest for “kube-apiserver”
[control-plane] Creating static Pod manifest for “kube-controller-manager”
[control-plane] Creating static Pod manifest for “kube-scheduler”
[etcd] Creating static Pod manifest for local etcd in “/etc/kubernetes/manifests”
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory “/etc/kubernetes/manifests”. This can take up to 4m0s
[apiclient] All control plane components are healthy after 18.503429 seconds
[uploadconfig] storing the configuration used in ConfigMap “kubeadm-config” in the “kube-system” Namespace
[kubelet] Creating a ConfigMap “kubelet-config-1.13” in namespace kube-system with the configuration for the kubelets in the cluster
[patchnode] Uploading the CRI Socket information “/var/run/dockershim.sock” to the Node API object “dev-k8s” as an annotation
[mark-control-plane] Marking the node dev-k8s as control-plane by adding the label “node-role.kubernetes.io/master=’’”
[mark-control-plane] Marking the node dev-k8s as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: ibnc7j.575ekk8iobclo3a8
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] creating the “cluster-info” ConfigMap in the “kube-public” namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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
(
i
d
−
u
)
:
(id -u):
(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.12.149:6443 --token ibnc7j.575ekk8iobclo3a8 --discovery-token-ca-cert-hash sha256:540c62887cf0d7bc96ea2ea5ffcdacc024c9f43585bd39744e3d403ca77ff632
6 重新初始化(可选)
如果初始化出错或者想重新初始化,可以使用如下命令
$ kubeadm reset
7 配置集群
要开始使用群集,您需要以普通用户身份运行以下命令:
即配置授权信息,主要是为了保存相关的配置信息在用户目录下,这样不用每次都输入相关的认证信息。
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown
(
i
d
−
u
)
:
(id -u):
(id−u):(id -g) $HOME/.kube/config
运行命令查询节点
$ kubectl get node
NAME STATUS ROLES AGE VERSION
dev-k8s NotReady master 115m v1.13.2
$kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-78d4cf999f-68lsn 1/1 Pending 0 118m
kube-system coredns-78d4cf999f-cl5sr 1/1 Pending 0 118m
kube-system etcd-dev-k8s 1/1 Running 0 117m
kube-system kube-apiserver-dev-k8s 1/1 Running 0 117m
kube-system kube-controller-manager-dev-k8s 1/1 Running 0 117m
kube-system kube-proxy-4s9fr 1/1 Running 0 118m
kube-system kube-scheduler-dev-k8s 1/1 Running 0 117m
因为还没有安装网络,集群状态并不正常
8 配置网络
$kubectl apply -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml
$ kubectl get node
NAME STATUS ROLES AGE VERSION
dev-k8s Ready master 115m v1.13.2
$kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-78d4cf999f-68lsn 1/1 Running 0 118m
kube-system coredns-78d4cf999f-cl5sr 1/1 Running 0 118m
kube-system etcd-dev-k8s 1/1 Running 0 117m
kube-system kube-apiserver-dev-k8s 1/1 Running 0 117m
kube-system kube-controller-manager-dev-k8s 1/1 Running 0 117m
kube-system kube-proxy-4s9fr 1/1 Running 0 118m
kube-system kube-scheduler-dev-k8s 1/1 Running 0 117m
9 新增node
步骤一:重复上文步骤 1 、2、3
步骤二:执行如下脚本;
kubeadm join ${ip}:6443 --token
t
o
k
e
n
−
−
d
i
s
c
o
v
e
r
y
−
t
o
k
e
n
−
c
a
−
c
e
r
t
−
h
a
s
h
s
h
a
256
:
{token} --discovery-token-ca-cert-hash sha256:
token−−discovery−token−ca−cert−hashsha256:{密钥}
举例说明:
ip:master节点ip
token:master节点密钥token,用于node加入集群的凭证。若遗失可以按照下面的步骤找回
密钥:master节点密钥secret。若遗失可以执行下面的命名找回