Kubeadm方式搭建K8S集群

Kubeadm方式搭建集群优缺点:

优点:
    简单优雅,支持高可用,升级方便
    
缺点:
    不易维护,文档不够细致

将master作为deploy节点,未指定节点时默认在master上进行操作。

建议deploy节点与其它节点配置ssh免密登录,配置过程参考:批量实现SSH免密登录


环境准备

环境准备工作请在所有节点进行。

  • 主机说明:
系统 ip 角色 cpu 内存 hostname
CentOS 7.8 192.168.30.128 master、deploy >=2 >=2G master
CentOS 7.8 192.168.30.129 node >=2 >=2G node1
CentOS 7.8 192.168.30.130 node >=2 >=2G node2
CentOS 7.8 192.168.30.131 node >=2 >=2G node3
  • 设置主机名:

以master为例,

hostnamectl set-hostname master
  • 安装依赖包:
yum update -y

yum install -y curl git iptables conntrack ipvsadm ipset jq sysstat libseccomp
  • 关闭防火墙、selinux和swap,重置iptables:
systemctl stop firewalld && systemctl disable firewalld

sed -i 's/=enforcing/=disabled/g' /etc/selinux/config && setenforce 0

iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT

swapoff -a

sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
  • 系统参数设置:
cat > /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
EOF

modprobe br_netfilter

sysctl -p /etc/sysctl.d/kubernetes.conf
  • 安装docker:
curl http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo

yum makecache fast

yum install -y docker-ce

systemctl enable docker && systemctl start docker

cat > /etc/docker/daemon.json <<EOF
{  
    "registry-mirrors": ["http://f1361db2.m.daocloud.io"],
    "exec-opts":["native.cgroupdriver=systemd"]
}
EOF

systemctl restart docker
  • 安装必要工具:
kubeadm     用于部署集群

bukelet     集群中各节点需要运行的组件,负责管理pod、容器的生命周期

kubectl     集群管理工具(master节点安装即可)
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

yum install -y kubeadm-1.18.3 kubelet-1.18.3 kubectl-1.18.3 --disableexcludes=kubernetes

systemctl enable kubelet && systemctl start kubelet

集群初始化

  • 集群初始化:
mkdir /software

vim /software/kubeadm-config.yaml
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
kubernetesVersion: v1.18.3
controlPlaneEndpoint: 192.168.30.128:6443
networking:
    podSubnet: 172.10.0.0/16
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kubeadm config images pull --kubernetes-version=v1.18.3 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers

kubeadm init --config=/software/kubeadm-config.yaml --upload-certs

初始化这一步如果报错:

error execution phase upload-config/kubelet: Error writing Crisocket information for the control-plane node: timed out waiting for the condition

解决:

swapoff -a

kubeadm reset -f

systemctl daemon-reload

systemctl restart kubelet

iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
mkdir ~/.kube

\cp /etc/kubernetes/admin.conf ~/.kube/config

kubectl get pods -n kube-system

NAME                             READY   STATUS    RESTARTS   AGE
coredns-546565776c-srxkq         0/1     Pending   0          60s
coredns-546565776c-w9fbs         0/1     Pending   0          60s
etcd-master                      1/1     Running   0          75s
kube-apiserver-master            1/1     Running   0          75s
kube-controller-manager-master   1/1     Running   0          75s
kube-proxy-qb7d5                 1/1     Running   0          60s
kube-scheduler-master            1/1     Running   0          75s
kubectl completion bash > ~/.kube/completion.bash.inc
 
echo 'source ~/.kube/completion.bash.inc' >> ~/.bash_profile

source ~/.bash_profile

注意备份上面初始化之后打印的join命令,这里分别是以master、node节点加入集群。

kubeadm join 192.168.30.128:6443 --token 1ndel7.xb623vep9pl5o6vl \
    --discovery-token-ca-cert-hash sha256:0e41f6020955c36970bf504cbfc0047941240dda57ebb9d85086706da14dcd1f \
    --control-plane --certificate-key 6518fe9f3eca5cb4a5860170d18c03109f54c94fba8ca7e5408a9aab5e598663

kubeadm join 192.168.30.128:6443 --token 1ndel7.xb623vep9pl5o6vl \
    --discovery-token-ca-cert-hash sha256:0e41f6020955c36970bf504cbfc0047941240dda57ebb9d85086706da14dcd1f

部署calico

  • 部署calico:
mkdir /etc/kubernetes/addons

vim /etc/kubernetes/addons/calico-rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: calico-kube-controllers
  namespace: kube-system
  
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: calico-kube-controllers
rules:
  - apiGroups: [""]
    resources:
      - nodes
    verbs:
      - watch
      - list
      - get
  - apiGroups: [""]
    resources:
      - pods
    verbs:
      - get
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - ippools
    verbs:
      - list
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - blockaffinities
      - ipamblocks
      - ipamhandles
    verbs:
      - get
      - list
      - create
      - update
      - delete
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - hostendpoints
    verbs:
      - get
      - list
      - create
      - update
      - delete
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - clusterinformations
    verbs:
      - get
      - create
      - update
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - kubecontrollersconfigurations
    verbs:
      - get
      - create
      - update
      - watch
      
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: calico-kube-controllers
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: calico-kube-controllers
subjects:
- kind: ServiceAccount
  name: calico-kube-controllers
  namespace: kube-system

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: calico-node
  namespace: kube-system

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: calico-node
rules:
  - apiGroups: [""]
    resources:
      - pods
      - nodes
      - namespaces
    verbs:
      - get
  - apiGroups: [""]
    resources:
      - endpoints
      - services
    verbs:
      - watch
      - list
      - get
  - apiGroups: [""]
    resources:
      - configmaps
    verbs:
      - get
  - apiGroups: [""]
    resources:
      - nodes/status
    verbs:
      - patch
      - update
  - apiGroups: ["networking.k8s.io"]
    resources:
      - networkpolicies
    verbs:
      - watch
      - list
  - apiGroups: [""]
    resources:
      - pods
      - namespaces
      - serviceaccounts
    verbs:
      - list
      - watch
  - apiGroups: [""]
    resources:
      - pods/status
    verbs:
      - patch
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - globalfelixconfigs
      - felixconfigurations
      - bgppeers
      - globalbgpconfigs
      - bgpconfigurations
      - ippools
      - ipamblocks
      - globalnetworkpolicies
      - globalnetworksets
      - networkpolicies
      - networksets
      - clusterinformations
      - hostendpoints
      - blockaffinities
    verbs:
      - get
      - list
      - watch
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - ippools
      - felixconfigurations
      - clusterinformations
    verbs:
      - create
      - update
  - apiGroups: [""]
    resources:
      - nodes
    verbs:
      - get
      - list
      - watch
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - bgpconfigurations
      - bgppeers
    verbs:
      - create
      - update
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - blockaffinities
      - ipamblocks
      - ipamhandles
    verbs:
      - get
      - list
      - create
      - update
      - delete
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - ipamconfigs
    verbs:
      - get
  - apiGroups: ["crd.projectcalico.org"]
    resources:
      - blockaffinities
    verbs:
      - watch
  - apiGroups: ["apps"]
    resources:
      - daemonsets
    verbs:
      - get

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: calico-node
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: calico-node
subjects:
- kind: ServiceAccount
  name: calico-node
  namespace: kube-system
vim /etc/kubernetes/addons/calico.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: calico-config
  namespace: kube-system
data:
  typha_service_name: "none"
  calico_backend: "bird"
  veth_mtu: "1440"
  cni_network_config: |-
    {
   
      "name": "k8s-pod-network",
      "cniVersion": "0.3.1",
      "plugins": [
        {
   
          "type": "calico",
          "log_level": "info",
          "datastore_type": "kubernetes",
          "nodename": "__KUBERNETES_NODE_NAME__",
          "mtu": __CNI_MTU__,
          "ipam": {
   
              "type": "calico-ipam"
          },
          "policy": {
   
              "type": "k8s"
          },
          "kubernetes": {
   
              "kubeconfig": "__KUBECONFIG_FILEPATH__"
          }
        },
        {
   
          "type": "portmap",
          "snat": true,
          "capabilities": {
   "portMappings": true}
        },
        {
   
          "type": "bandwidth",
          "capabilities": {
   "bandwidth": true}
        }
      ]
    }
  
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: bgpconfigurations.crd.projectcalico.org
spec:
  group: crd.projectcalico.org
  names:
    kind: BGPConfiguration
    listKind: BGPConfigurationList
    plural: bgpconfigurations
    singular: bgpconfiguration
  scope: Cluster
  versions:
  - name: v1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            properties:
              asNumber:
                format: int32
                type: integer
              logSeverityScreen:
                type: string
              nodeToNodeMeshEnabled:
                type: boolean
              serviceClusterIPs:
                items:
                  properties:
                    cidr:
                      type: string
                  type: object
                type: array
              serviceExternalIPs:
                items:
                  properties:
                    cidr:
                      type: string
                  type: object
                type: array
            type: object
        type: object
    served: true
    storage: true
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: bgppeers.crd.projectcalico.org
spec:
  group: crd.projectcalico.org
  names:
    kind: BGPPeer
    listKind: BGPPeerList
    plural: bgppeers
    singular: bgppeer
  scope: Cluster
  versions:
  - name: v1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            properties:
              asNumber:
                format: int32
                type: integer
              node:
                type: string
              nodeSelector:
                type: string
              peerIP:
                type: string
              peerSelector:
                type: string
            required:
            - asNumber
            - peerIP
            type: object
        type: object
    served: true
    storage: true
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: blockaffinities.crd.projectcalico.org
spec:
  group: crd.projectcalico.org
  names:
    kind: BlockAffinity
    listKind: BlockAffinityList
    plural: blockaffinities
    singular: blockaffinity
  scope: Cluster
  versions:
  - name: v1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            properties:
              cidr:
                type: string
              deleted:
                type: string
              node:
                type: string
              state:
                type: string
            required:
            - cidr
            - deleted
            - node
            - state
            type: object
        type: object
    served: true
    storage: true
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []
  
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: clusterinformations.crd.projectcalico.org
spec:
  group: crd.projectcalico.org
  names:
    kind: ClusterInformation
    listKind: ClusterInformationList
    plural: clusterinformations
    singular: clusterinformation
  scope: Cluster
  versions:
  - name: v1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            properties:
              calicoVersion:
                type: string
              clusterGUID:
                type: string
              clusterType:
                type: string
              datastoreReady:
                type: boolean
              variant:
                type: string
            type: object
        type: object
    served: true
    storage: true
status:
  acceptedNames:
    kind: ""
    plural: ""
  conditions: []
  storedVersions: []

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: felixconfigurations.crd.projectcalico.org
spec:
  group: crd.projectcalico.org
  names:
    kind: FelixConfiguration
    listKind: FelixConfigurationList
    plural: felixconfigurations
    singular: felixconfiguration
  scope: Cluster
  versions:
  - name: v1
    schema:
      openAPIV3Schema:
        properties:
          apiVersion:
            type: string
          kind:
            type: string
          metadata:
            type: object
          spec:
            properties:
              bpfConnectTimeLoadBalancingEnabled:
                type: boolean
              bpfDataIfacePattern:
                type: string
              bpfDisableUnprivileged:
                type: boolean
              bpfEnabled:
                type: boolean
              bpfExternalServiceMode:
                type: string
              bpfKubeProxyEndpointSlicesEnabled:
                type: boolean
              bpfKubeProxyIptablesCleanupEnabled:
                type: boolean
              bpfKubeProxyMinSyncPeriod:
                type: string
              bpfLogLevel:
                type: string
              chainInsertMode:
                type: string
              dataplaneDriver:
                type: string
              debugDisableLogDropping:
                type: boolean
              debugMemoryProfilePath:
                type
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
kubeadmKubernetes官方提供的一种部署Kubernetes集群的工具,它可以快速、简单地部署一个符合生产环境要求的Kubernetes集群。 下面是kubeadm部署K8s集群的步骤: 1. 准备好服务器环境:确保服务器的操作系统是Ubuntu 16.04/18.04或CentOS 7,并且每个节点有至少2GB的内存和2个CPU。 2. 安装Docker:Kubernetes需要Docker来运行容器,使用以下命令安装Docker: ``` $ sudo apt-get update $ sudo apt-get install -y docker.io ``` 3. 安装kubeadm等工具:使用以下命令安装kubeadmkubelet和kubectl: ``` $ sudo apt-get update && sudo apt-get install -y apt-transport-https curl $ curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - $ cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/ kubernetes-xenial main EOF $ sudo apt-get update $ sudo apt-get install -y kubelet kubeadm kubectl $ sudo apt-mark hold kubelet kubeadm kubectl ``` 4. 初始化Master节点:在Master节点上使用kubeadm init命令初始化Kubernetes集群,并将输出的kubeadm join命令保存以便后续使用。 ``` $ sudo kubeadm init --pod-network-cidr=10.244.0.0/16 ``` 5. 配置kubectl:将配置文件复制到当前用户的目录下,以便使用kubectl命令。 ``` $ mkdir -p $HOME/.kube $ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config $ sudo chown $(id -u):$(id -g) $HOME/.kube/config ``` 6. 安装网络插件:Kubernetes需要网络插件来实现Pod之间的网络通信,使用以下命令安装flannel网络插件: ``` $ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml ``` 7. 加入Worker节点:在Worker节点上使用刚才保存的kubeadm join命令加入Kubernetes集群。 ``` $ sudo kubeadm join <MASTER_IP>:<MASTER_PORT> --token <TOKEN> --discovery-token-ca-cert-hash <HASH> ``` 这样,一个基于kubeadm快速部署的Kubernetes集群搭建好了。如果需要更详细的说明,请参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值