kubernetes--集群环境搭建

 本文将搭建一套master-node-node k8s集群环境,需要准备三台至少cpu > 2G, mem > 2G的虚拟机,步骤如下:

1. 在所有节点配置hosts并节点间的连接性

#分别在三个节点配置host,示例为master节点的配置

root@k8s-master01:~# hostnamectl set-hostname k8s-master01
root@k8s-master01:~# cat /etc/hosts
127.0.0.1    localhost
# The following lines are desirable for IPv6 capable hosts
::1    localhost    ip6-localhost    ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
127.0.1.1    s02g07017.cloud.em160.tbsite.net    s02g07017

172.18.8.211 k8s-master01
172.18.8.210 k8s-node01
172.18.8.212 k8s-node02

#确保节点间的连接性

root@k8s-master01:~# ping k8s-node1
root@k8s-master01:~# ping k8s-node01
root@k8s-master01:~# ping k8s-node02

2. 在所有节点安装所需应用

#安装依赖包

root@k8s-master01:~# apt-get update
root@k8s-master01:~# apt-get install conntrack ipvsadm ipset jq iptables curl sysstat libseccomp2 wget vim net-tools git

#关闭防火墙

root@k8s-master01:~# systemctl stop firewalld
root@k8s-master01:~# iptables -F  ;  service iptables save 

#disable swap分区,以免pod运行在swap中

root@k8s-master01:~# setenforce 0 && sed -i ‘s/^SELINUX=.*/SELINUX=disabled/‘ /etc/selinux/config
setenforce: SELinux is disabled

#配置kubernetes.conf,调整内核参数,【可选

root@k8s-master01:/home/whq# cat > kubernetes.conf <<EOF
> net.bridge.bridge-nf-call-iptables=1 # 开启网桥模式
> net.bridge.bridge-nf-call-ip6tables=1 # 开启网桥模式
> net.ipv4.ip_forward=1
> net.ipv4.tcp_tw_recycle=0
> vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它
> vm.overcommit_memory=1 # 不检查物理内存是否够用
> vm.panic_on_oom=0 # 开启 OOM
> fs.inotify.max_user_instances=8192
> fs.inotify.max_user_watches=1048576
> fs.file-max=52706963
> fs.nr_open=52706963
> net.ipv6.conf.all.disable_ipv6=1 # 关闭IPV6协议
> net.netfilter.nf_conntrack_max=2310720
> EOF
root@k8s-master01:/home/whq# cp kubernetes.conf  /etc/sysctl.d/kubernetes.conf
root@k8s-master01:/home/whq# sysctl -p /etc/sysctl.d/kubernetes.conf 

#kube-proxy开启ipvs的前置条件

root@k8s-master01:/home/whq# modprobe br_netfilter
root@k8s-master01:/home/whq# vi /etc/modules   添加以下内容

modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
#modprobe -- nf_conntrack_ipv4
modprobe -- nf_conntrack

root@k8s-master01:/home/whq# chmod 755 /etc/modules
root@k8s-master01:/home/whq# bash /etc/modules
modprobe: FATAL: Module nf_conntrack_ipv4 not found in directory /lib/modules/5.4.0-77-generic
root@k8s-master01:/home/whq# chmod 755 /etc/modules && bash /etc/modules && lsmod | grep -e ip_vs -e nf_conntrack
ip_vs_sh               16384  0
ip_vs_wrr              16384  0
ip_vs_rr               16384  0
ip_vs                 155648  6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack          139264  1 ip_vs
nf_defrag_ipv6         24576  2 nf_conntrack,ip_vs
libcrc32c              16384  2 nf_conntrack,ip_vs

#安装docker

root@k8s-master01:/home/whq#  sudo apt-get install \
>     apt-transport-https \
>     ca-certificates \
>     curl \
>     gnupg-agent \
>     software-properties-common
root@k8s-master01:/home/whq# sudo add-apt-repository \
>    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
>   $(lsb_release -cs) \
>   stable"
root@k8s-master01:/home/whq# sudo apt-get install docker-ce docker-ce-cli containerd.io
root@k8s-master01:/home/whq# sudo add-apt-repository    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
root@k8s-master01:/home/whq# cat > /etc/docker/daemon.json <<EOF
> {
>   "registry-mirrors": ["https://fxr0bix2.mirror.aliyuncs.com"],
>   "exec-opts": ["native.cgroupdriver=systemd"],
>   "log-driver": "json-file",
>   "log-opts": {
>     "max-size": "100m"
>   }
> }
> EOF
root@k8s-master01:/home/whq# mkdir -p /etc/systemd/system/docker.service.d
root@k8s-master01:/home/whq# systemctl daemon-reload && systemctl restart docker && systemctl enable docker
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker

#安装kubeadm, kubectl, kubelet

root@k8s-master01:/usr/local/dockerjdk8# curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
OK
root@k8s-master01:/usr/local/dockerjdk8# sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF
> deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
> EOF
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
root@k8s-master01:/usr/local/dockerjdk8# sudo apt-get update
root@k8s-master01:/usr/local/dockerjdk8# sudo apt-get install -y kubelet kubeadm kubectl

#下载必须镜像到master&nodes

root@k8s-master01:~# kubeadm config images list                                                                      #查看需下载哪些镜像
root@k8s-master01:~# sudo kubeadm init --image-repository=fxr0bix2.mirror.aliyuncs.com  #执行完这条后发现coredns还是没有下载成功
root@k8s-master01:~# docker pull coredns/coredns                                                                     #手动下载并更新tag
root@k8s-master01:~# docker tag coredns/coredns:latest registry.aliyuncs.com/google_containers/coredns:v1.8.4
root@k8s-master01:~# sudo kubeadm reset  #仅在nodes执行,此目的是在nodes上下载需要的镜像包,记得删除对应的文件(提示中有),另外可以自己通过pull image然后更改tag的方式来下载 

3. master配置kubeadm

#初始化kubeadm

root@k8s-master01:~# kubeadm init --image-repository=fxr0bix2.mirror.aliyuncs.com --ignore-preflight-errors=ImagePull --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.22.0
[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‘
st for registry.aliyuncs.com/google_containers/coredns:v1.8.4 not found: manifest unknown: manifest unknown
, error: exit status 1
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
ster.local] and IPs [10.96.0.1 172.18.8.211]
[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/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master01 localhost] and IPs [172.18.8.211 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master01 localhost] and IPs [172.18.8.211 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-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
[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] Starting the kubelet
[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"
p to 4m0s
[apiclient] All control plane components are healthy after 8.506664 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.22" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
rnetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node k8s-master01 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: l7k6l0.68injj34gkc1pi7p
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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 172.18.8.211:6443 --token l7k6l0.68injj34gkc1pi7p \
    --discovery-token-ca-cert-hash sha256:d103ddd3e24e279a725af4e84095535b327f2f815fa226ea2c31ed573b16e76d 

#将上行kubeadmin join记录下来

#配置kube

root@k8s-master01:~# mkdir -p $HOME/.kube
root@k8s-master01:~# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
root@k8s-master01:~# sudo chown $(id -u):$(id -g) $HOME/.kube/config
root@k8s-master01:~# kubectl get node
NAME           STATUS     ROLES                  AGE   VERSION
k8s-master01   NotReady   control-plane,master   75s   v1.22.0

#部署flannel

root@k8s-master01:~# cd /home/whq/
root@k8s-master01:/home/whq# cd install-k8s/
root@k8s-master01:/home/whq/install-k8s# cd plugin/flannel/

#从官网下载的kube-flannel.yml   https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
root@k8s-master01:/home/whq/install-k8s/plugin/flannel# cat kube-flannel.yml 

---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: psp.flannel.unprivileged
  annotations:
    seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
    seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
    apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
    apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
  privileged: false
  volumes:
  - configMap
  - secret
  - emptyDir
  - hostPath
  allowedHostPaths:
  - pathPrefix: "/etc/cni/net.d"
  - pathPrefix: "/etc/kube-flannel"
  - pathPrefix: "/run/flannel"
  readOnlyRootFilesystem: false
  # Users and groups
  runAsUser:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  # Privilege Escalation
  allowPrivilegeEscalation: false
  defaultAllowPrivilegeEscalation: false
  # Capabilities
  allowedCapabilities: [‘NET_ADMIN‘, ‘NET_RAW‘]
  defaultAddCapabilities: []
  requiredDropCapabilities: []
  # Host namespaces
  hostPID: false
  hostIPC: false
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  # SELinux
  seLinux:
    # SELinux is unused in CaaSP
    rule: ‘RunAsAny‘
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups: [‘extensions‘]
  resources: [‘podsecuritypolicies‘]
  verbs: [‘use‘]
  resourceNames: [‘psp.flannel.unprivileged‘]
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-system
  labels:
    tier: node
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-system
  labels:
    tier: node
    app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
      hostNetwork: true
      priorityClassName: system-node-critical
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni
        image: quay.io/coreos/flannel:v0.14.0
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
        image: quay.io/coreos/flannel:v0.14.0
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            cpu: "100m"
            memory: "50Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN", "NET_RAW"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg

root@k8s-master01:/home/whq/install-k8s/plugin/flannel# kubectl apply -f kube-flannel.yml 
Warning: policy/v1beta1 PodSecurityPolicy is deprecated in v1.21+, unavailable in v1.25+
podsecuritypolicy.policy/psp.flannel.unprivileged configured
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created

root@k8s-master01:/home/whq/install-k8s/plugin/flannel# kubectl get pod -n kube-system #全部为running表示成功
NAME READY STATUS RESTARTS AGE
coredns-7f6cbbb7b8-5s4f2 0/1 Running 0 82m
coredns-7f6cbbb7b8-7zw47 1/1 Running 0 82m
etcd-k8s-master01 1/1 Running 2 82m
kube-apiserver-k8s-master01 1/1 Running 2 82m
kube-controller-manager-k8s-master01 1/1 Running 0 82m
kube-flannel-ds-np6xp 1/1 Running 0 52m
kube-proxy-pnq8b 1/1 Running 0 82m
kube-scheduler-k8s-master01 1/1 Running 3 82m

root@k8s-master01:/home/whq/install-k8s/plugin/flannel# kubectl get node  #ready表示成功

NAME STATUS ROLES AGE VERSION
k8s-master01 Ready control-plane,master 101m v1.22.0

4. Node配置kubeadm

#执行master kubeadm init时的join命令,让node加入集群

root@k8s-node01:~# kubeadm join 172.18.8.211:6443 --token l7k6l0.68injj34gkc1pi7p \
> --discovery-token-ca-cert-hash sha256:d103ddd3e24e279a725af4e84095535b327f2f815fa226ea2c31ed573b16e76d
[preflight] Running pre-flight checks
^C
535b327f2f815fa226ea2c31ed573b16e76d
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with ‘kubectl -n kube-system get cm kubeadm-config -o yaml‘
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run ‘kubectl get nodes‘ on the control-plane to see this node join the cluster.

#配置kube

root@k8s-node01:~# mkdir -p $HOME/.kube
root@k8s-node01:~# chown $(id -u):$(id -g) $HOME/.kube/config

#查看node状态

root@k8s-node01:~# kubectl get node  #全部为ready状态即可

NAME STATUS ROLES AGE VERSION
k8s-master01 Ready control-plane,master 103m v1.22.0
k8s-node01 Ready <none> 2m25s v1.22.0
k8s-node02 Ready <none> 4m30s v1.22.0

5. 尝试创建pod

root@k8s-master01:/home/whq/install-k8s/plugin/flannel# kubectl run nginx-off --image=nginx
pod/nginx-off created
root@k8s-master01:/home/whq/install-k8s/plugin/flannel# kubectl get pod --all-namespaces -o wide
TES
default       nginx-off                              1/1     Running   0          3m9s   192.168.2.50   k8s-node01     <none>           <none>
kube-system   coredns-7f6cbbb7b8-5s4f2               1/1     Running   0          118m   192.168.0.73   k8s-master01   <none>           <none>
kube-system   coredns-7f6cbbb7b8-7zw47               1/1     Running   0          118m   192.168.0.74   k8s-master01   <none>           <none>
kube-system   etcd-k8s-master01                      1/1     Running   2          118m   172.18.8.211   k8s-master01   <none>           <none>
kube-system   kube-apiserver-k8s-master01            1/1     Running   2          118m   172.18.8.211   k8s-master01   <none>           <none>
kube-system   kube-controller-manager-k8s-master01   1/1     Running   0          118m   172.18.8.211   k8s-master01   <none>           <none>
kube-system   kube-flannel-ds-8w45l                  1/1     Running   0          19m    172.18.8.212   k8s-node02     <none>           <none>
kube-system   kube-flannel-ds-np6xp                  1/1     Running   0          88m    172.18.8.211   k8s-master01   <none>           <none>
kube-system   kube-flannel-ds-z7t6x                  1/1     Running   0          17m    172.18.8.210   k8s-node01     <none>           <none>
kube-system   kube-proxy-g7bp5                       1/1     Running   0          19m    172.18.8.212   k8s-node02     <none>           <none>
kube-system   kube-proxy-njjb8                       1/1     Running   0          17m    172.18.8.210   k8s-node01     <none>           <none>
kube-system   kube-proxy-pnq8b                       1/1     Running   0          118m   172.18.8.211   k8s-master01   <none>           <none>
kube-system   kube-scheduler-k8s-master01            1/1     Running   3          118m   172.18.8.211   k8s-master01   <none>           <none>

root@k8s-master01:/home/whq/install-k8s/plugin/flannel# ping 192.168.2.50  #查看连接性

来自 192.168.2.50 的回复: 字节=32 时间<1ms TTL=128

kubernetes--集群环境搭建

上一篇:【K8s教程】Nginx Ingress 控制器 TLS/HTTPS 说明


下一篇:gson的特殊用法