Ubuntu部署kubernetes

  1. 环境准备

环境准备/说明

  • Master:k8s管理端 # 10.250.101.60
  • Node1: k8s节点 # 10.250.101.64
  • Node2: k8s节点 # 10.250.101.65
  • Node3: k8s节点 # 10.250.101.66

在所有节点Master节点部署k8s master

  • 安装工具包
sudo apt-get update && sudo apt-get install -y ca-certificates curl softwareproperties-common apt-transport-https curl

添加阿里云key

curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo aptkey add -

配置阿里源

sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF 
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main 
EOF

通过kubeadm方式部署

  • Master,Node1,Node2,Node3 安装kubelet,kubeadm,kubectl
sudo apt update; sudo apt-get install -y kubelet=1.18.2-00 kubeadm=1.18.2-00 kubectl=1.18.2-00  # 版本与kubernetes-version版本一致

初始化Master

sudo kubeadm init --kubernetes-version=1.18.2 \  # 指定版本1.18.2 最新 1.20
--apiserver-advertise-address=10.250.101.60 \    # master节点
--service-cidr=10.1.0.0/16 \                     # svc 的网络VIP # kubeproxy # ClusterIP
--pod-network-cidr=10.244.0.0/16                 # pod网段
# 输出
k8s@master:~$ sudo kubeadm init --kubernetes-version=1.18.2 --apiserver-advertise-address=192.168.19.100 \ 
--image-repository registry.aliyuncs.com/google_containers --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16 
...
...
[init] Using Kubernetes version: v1.18.2
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:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.19.100:6443 --token la82lq.j25bot0eopia3knp kubeadm join 192.168.19.100:6443 --token la82lq.j25bot0eopia3knp \    
--discovery-token-ca-cert-hash sha256:221335f0da68ce2395509d37f1abf5805b73a999ff4233f6c49d633aeb8fd63d
  • 组件说明
kubelet:# 管理docker服务 ,k8s中使用kubelet 管理docker api  在master # 本地的服务操作系统管理静态POD,所有节点都要运行。
api-server: api服务器  
scheduler:调度器
etcd: 数据库,(pod部署)保存所有数据  分存式数据 #kubernetes HA 使用,虚拟机部署etcd
kubeproxy: haproxy 实现 VIP
kubectl :  管理命令行工具
kubeproxy: haproxy 实现 VIP

配置身份认证

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

安装网络组件calico

wget https://docs.projectcalico.org/v3.11/manifests/calico.yaml 
  • 修改yaml文件,将CALICO_IPV4POOL_CIDR为10.244.0.0/16,与上述初始化的pod网络一致
    Ubuntu部署kubernetes
  • 部署calico网络插件
#: kubectl apply -f calico.yaml
#: kubectl get nodes ## master 节点此时为NotReady,网络节点完成部署后,master状态Ready
#: kubectl get pods -n kube-system
trnuser@k8s:~$ kubectl get pod -n kube-system 
NAME                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-59877c7fb4-lgf4z   1/1     Running   0          15d
calico-node-8rt4m                          1/1     Running   1          15d
calico-node-b4wgq                          1/1     Running   0          15d
calico-node-gjqwf                          1/1     Running   0          15d
calico-node-mdh22                          1/1     Running   0          15d
coredns-7ff77c879f-nvq2b                   1/1     Running   0          15d
coredns-7ff77c879f-rwwmm                   1/1     Running   0          15d
etcd-k8s                                   1/1     Running   0          15d
kube-apiserver-k8s                         1/1     Running   1          15d
kube-controller-manager-k8s                1/1     Running   1          15d
kube-proxy-45h2v                           1/1     Running   0          15d
kube-proxy-gd6gz                           1/1     Running   0          15d
kube-proxy-rwzg9                           1/1     Running   0          15d
kube-proxy-wjbv5                           1/1     Running   1          15d
kube-scheduler-k8s                         1/1     Running   1          15d
metrics-server-7f96bbcc66-vwpsj            1/1     Running   0          14d
  • 在node1/2/3上安装kubelet kubeadm kubectl, 版本与k8s版本匹配

添加Node节点

# 初始化时提示的内容,在各节点上运行
sudo kubeadm join 10.250.101.60:6443 --token la82lq.j25bot0eopia3knp --discovery-token-ca-cert-hash sha256:221335f0da68ce2395509d37f1abf5805b73a999ff4233f6c49d633aeb8fd63d 
trnuser@k8s:~$ kubectl get nodes
NAME    STATUS   ROLES    AGE   VERSION
k8s     Ready    master   15d   v1.18.2
node1   Ready    worker   15d   v1.18.2
node2   Ready    worker   15d   v1.18.2
node3   Ready    worker   15d   v1.18.2

配置kubectl命令补全

sudo apt install bash-completion -y
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
source ~/.bashrc
  • K8S集群图解
    Ubuntu部署kubernetes

kubectl常用命令

  • 查看kubernetes中所有资源对象
kubectl api-server
  • 查看yaml编辑帮助
kubectl explain pod.spec.containers
  • 生成yaml文件
kubectl run --image=nginx  --image-pull-policy=IfNotPresent --restart=Always  web2 --dry-run=client -o yaml 
  • 生产的pod不会调度到master节点
原因:master 被标记为NoSchedule
kubectl describe nodes master
###
Taints: node-role.kubernetes.io/master:NoSchedule  #污点 
###

Ubuntu部署kubernetes

上一篇:jQuery拼图滑块验证


下一篇:html里文本保留换行格式