阅读 86

kubeadm方式安装kubernetes

1. 使用kubeadm方式安装

1.1 环境准备介绍

角色 IP地址 操作系统 配置 kubernetes docker
master 192.168.199.100 centos7.x 2核/2G/50G 1.18.0 18.06.3
node1 192.168.199.101 centos7.x 2核/2G/50G 1.18.0 18.06.3
node2 192.168.199.102 centos7.x 2核/2G/50G 1.18.0 18.06.3

1.2 准备工作

1.2.1 关闭防火墙

systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld

1.2.2 设置主机名

hostnamectl set-hostname k8s-master
hostnamectl set-hostname k8s-node1
hostnamectl set-hostname k8s-node2

1.2.3 关闭selinux

sed -i 's/enforcing/disabled/' /etc/selinux/config   永久
setenforce 0 临时
getenforce 查看当前selinux 状态

1.2.4 关闭swap分区

sed -ri 's/.*swap.*/#&/' /etc/fstab  注释swap行
swapoff -a 临时关闭 重启无效
 lsblk 查看swap关闭情况

1.2.5 将桥接的IPv4流量传递到iptables的链

cat > /etc/modules-load.d/k8s.conf << EOF
br_netfilter
EOF
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
# 生效
sysctl --system

1.2.6 k8s-master执行: 推荐dns解析

cat >> /etc/hosts << EOF
192.168.199.100 k8s-master k8s-api.local.cn
192.168.199.101 k8s-node1
192.168.199.102 k8s-node2
EOF

2. 每个节点安装Docker、kubeadm、kubelet和kubectl

2.1 安装docker 18.0.6.3

 wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.3.ce-3.el7
mkdir -p /etc/docker
cat >/etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl enable docker && systemctl start docker
docker version
docker info 查看是否修改成功

2.2 安装kubeadm、kubelet和kubectl

2.2.1 跟换国内源

cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

2.2.2 安装kubeadm、kubelet和kubectl

yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet

2.2.3 部署k8s的Master节点

kubeadm init \
  --apiserver-advertise-address=192.168.199.100 \
  --image-repository registry.aliyuncs.com/google_containers \
  --control-plane-endpoint k8s-api.local.cn \
  --kubernetes-version v1.18.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16 \
  --token-ttl 0
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/

You can now join any number of control-plane nodes by copying certificate authorities
and service account keys on each node and then running the following as root:

  kubeadm join k8s-api.local.cn:6443 --token 2l64r1.7kf53k1aux25kbdl \
    --discovery-token-ca-cert-hash sha256:75a43008d91f5fbc23e0caedb6952fbb800fbe3204c7e3c386f9a6aa1031b805 \
    --control-plane 

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join k8s-api.local.cn:6443 --token 2l64r1.7kf53k1aux25kbdl \
    --discovery-token-ca-cert-hash sha256:75a43008d91f5fbc23e0caedb6952fbb800fbe3204c7e3c386f9a6aa1031b805 

有效期默认24H 复制到node节点执行 
kubeadm token create --print-join-command 再次生成
kubeadm token create --ttl 0 --print-join-command 永久有效
kubectl get nodes 查看加入信息

3. 安装 Pod 网络插件(CNI)

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[root@hk ~]# 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.13.1-rc1
        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.13.1-rc1
        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

4.部署k8s的node节点

kubeadm join k8s-api.local.cn:6443 --token 2l64r1.7kf53k1aux25kbdl \
    --discovery-token-ca-cert-hash sha256:75a43008d91f5fbc23e0caedb6952fbb800fbe3204c7e3c386f9a6aa1031b805 
测试
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
访问地址:http://NodeIP:Port

5. 修改k8s 集群从iptables为 ipvsadm

[root@k8s-master ~]# kubectl get configmap -n kube-system
NAME                                 DATA   AGE
coredns                              1      43h
extension-apiserver-authentication   6      43h
kube-flannel-cfg                     2      43h
kube-proxy                           2      43h
kubeadm-config                       2      43h
kubelet-config-1.18                  1      43h
[root@k8s-master ~]# kubectl edit cm kube-proxy -n kube-system
搜索mode 修改如下
 mode: "ipvs"
kubectl delete pods -l k8s-app=kube-proxy -n kube-system  杀掉kube-proxy 容器会自动创建新的
node节点
yum install  ipvsadm -y
ipvsadm -Ln 查看

作者:挑战_bae7

原文链接:https://www.jianshu.com/p/4f53b7004bbc

文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐