1.环境准备
一台或多台运行兼容deb / rpm的Linux操作系统的计算机;例如:Ubuntu或CentOS。
每台计算机2 GiB或更多的RAM-更少的空间留给您的应用程序。
用作master节点的计算机上至少有2个CPU。
群集中所有计算机之间的完全网络连接。您可以使用公共网络或专用网络。
1.1 安装docker
docker官网安装教程
下面是初始化环境脚本,可以直接复制,其中需要注意的是需要修改脚本的/etc/hosts下面的值,然后执行
#!/bin/bash
#1.2
#关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux:
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
#关闭swap:
swapoff -a 临时关闭
#添加主机名与IP对应关系(记得设置主机名,主机名跟ip是对相应的):
cat /etc/hosts
182.42.61.199 k8s-master
182.42.61.198 k8s-node1
#加载br_netfilter模块
modprobe br_netfilter
#将桥接的IPv4流量传递到iptables的链:
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
2.安装kubelet kubeadm kubectl
2.1 添加k8s仓库,官网的k8s仓库地址国内访问不了,这里我们用阿里云的仓库
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[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
[root@d0tihpxwtqddgpwm ~]yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
[root@d0tihpxwtqddgpwm ~]systemctl enable kubelet
3.部署master节点
kubeadm init \
--apiserver-advertise-address 182.42.61.199 \
--apiserver-bind-port=6443 \
--pod-network-cidr=10.244.0.0/16 \
--service-cidr=10.96.0.0/16 \
--kubernetes-version=v1.18.0 \
--image-repository registry.aliyuncs.com/google_containers \
--ignore-preflight-errors=Swap
[init] Using Kubernetes version: vX.Y.Z
[preflight] Running pre-flight checks
[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 "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [kubeadm-cp localhost] and IPs [10.138.0.4 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [kubeadm-cp localhost] and IPs [10.138.0.4 127.0.0.1 ::1]
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubeadm-cp kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.138.0.4]
[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 "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 31.501735 seconds
[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-X.Y" 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 "kubeadm-cp" as an annotation
[mark-control-plane] Marking the node kubeadm-cp as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node kubeadm-cp as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: <token>
[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 control-plane 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:
/docs/concepts/cluster-administration/addons/
You can now join any number of machines by running the following on each node
as root:
kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
- --apiserver-advertise-address 指定apiserver的地址,一般写为内网地址ip
- --apiserver-bind-port api绑定的端口
- --pod-network-cidr pod网络
- --service-cidr svc网络
- --kubernetes-version 指定k8s版本
- --image-repository 要拉取的镜像仓库地址
要使kubectl适用于您的非root用户,请运行以下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
或者,如果您是root用户,则可以运行:
export KUBECONFIG=/etc/kubernetes/admin.conf
4.部署calico网络
[root@d0tihpxwtqddgpwm ~]# mkdir -p /var/lib/calico
[root@d0tihpxwtqddgpwm ~]# cd /var/lib/calico
[root@d0tihpxwtqddgpwm ~]# touch nodename
[root@d0tihpxwtqddgpwm ~]# kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
[root@d0tihpxwtqddgpwm deploy]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-65f8bc95db-ppwg9 1/1 Running 1 25h
kube-system calico-node-gfsxs 1/1 Running 1 25h
kube-system coredns-7ff77c879f-rk7x8 1/1 Running 1 25h
kube-system coredns-7ff77c879f-v6ww5 1/1 Running 1 25h
kube-system etcd-d0tihpxwtqddgpwm.novalocal 1/1 Running 1 25h
kube-system kube-apiserver-d0tihpxwtqddgpwm.novalocal 1/1 Running 1 25h
kube-system kube-controller-manager-d0tihpxwtqddgpwm.novalocal 1/1 Running 1 25h
kube-system kube-proxy-7pp4p 1/1 Running 1 25h
kube-system kube-scheduler-d0tihpxwtqddgpwm.novalocal 1/1 Running 1 25h
[root@d0tihpxwtqddgpwm deploy]# kubectl get node
NAME STATUS ROLES AGE VERSION
d0tihpxwtqddgpwm.novalocal Ready master 25h v1.18.0
kubectl get node可以看到node节点状态为ready状态,证明master节点已经部署完成
5.添加work节点
kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
如果没有令牌,则可以通过在控制平面节点上运行以下命令来获取令牌:
kubeadm token list
输出类似于以下内容
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
8ewj1p.9r9hcjoqgajrj4gi 23h 2018-06-12T02:51:28Z authentication, The default bootstrap system:
signing token generated by bootstrappers:
'kubeadm init'. kubeadm:
default-node-token
如果没有的值--discovery-token-ca-cert-hash,则可以通过在master节点上运行以下命令链来获取它:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
输出类似于:
8cb2de97839780a412b93877f8507ad6c94f73add17d5d7058e91741c9d5ec78
如果是token过期了,可以执行kubeadm token create --print-join-command
然后复制输出的内容进行节点添加
6.部署dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
默认dashboard只能集群内部访问
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 30001(该处为添加的内容)
selector:
k8s-app: kubernetes-dashboard
创建service account并绑定默认cluster-admin管理员集群角色:
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
使用输出的token登录Dashboard
dashboard访问地址:https://NodeIP:30001
出现的问题:第一次部署的是v1.10.1版本的dashboard,发现每次登陆成功后都会报404重定向,最后发现是dashboard版本太低,切换到v2.0.3解决
浙公网安备 33010602011771号