Note: I use centos7 system to build

First, basic environment preparation

Stop firewalld systemctl disable Firewalld systemctl disable Firewalld **selinux** sed -i 's/enforcing/disabled/' /etc/selinux/config # Vim /etc/sysctl.d/k8s.conf # Add net.bridge.bridge -NF-call-ip6tables = 1 Net.bridge. bridge -nF-call-iptables = 1 net.ipv4.ip_forward = 1Copy the code

2. Docker environment preparation

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo yum list Docker - ce - showduplicates | sort - r # version installation specified yum install docker - ce - yum source 20.10.7 # add ali cloud, Cat <<EOF > /etc/docker/daemon.json {"registry-mirrors": [" https://xxxx.mirror.aliyuncs.com "]} EOF # start docker systemctl enable docker && systemctl start dockerCopy the code

Install kubeadm, Kubelet and Kubectl

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 yum install - y kubelet - 1.21.3 kubeadm - 1.21.3 Kubectl - 1.21.3 systemctl enable kubeletCopy the code

Iv. Single machine deployment

Hostname hostnamectl set-hostname master cat >> /etc/hosts << EOF 192.168.137.200 master EOF # Initialize master kubeadm Init \ - apiserver - advertise - address = 192.168.137.200 \ - image - repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.21.3 \ --service-cidr=10.96.0.0/12 \ -- pod-neto-cidr =10.244.0.0/16 Docker pull registry.aliyuncs.com/google_containers/coredns:1.8.0 docker tag Registry.aliyuncs.com/google_containers/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns:v1.8.0 docker rmi Registry.aliyuncs.com/google_containers/coredns:1.8.0 # for other than the root user create execute permissions mkdir -p $HOME /. Kube sudo cp - I The/etc/kubernetes/admin. Conf. $HOME/kube/config sudo chown $(id - u) : $(id - g) $HOME /. Kube/config # root, you can run: Export KUBECONFIG = / etc/kubernetes/admin. Conf # remove stain kubectl the describe the node master | grep Taints kubectl taint nodes Master node - role. Kubernetes. IO # / master - the CNI network deployment plug-in kubectl apply - f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml # to check the running status kubectl get the pods - n kube-systemCopy the code

V. Cluster deployment

Hostnamectl set-hostname master # node1 / node2 hostname cat >> /etc/hosts << EOF 192.168.137.200 master 192.168.137.201 node1 192.168.137.202 node2 EOF # Initialize master kubeadm init \ - apiserver - advertise - address = 192.168.137.200 \ - image - repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.21.3 \ --service-cidr=10.96.0.0/12 \ -- pod-neto-cidr =10.244.0.0/16 mkdir -p $HOME/. Kube sudo Cp - I/etc/kubernetes/admin. Conf. $HOME/kube/config sudo chown $(id - u) : $(id - g) $HOME /. Kube/config # the root user, you can run: Export KUBECONFIG = / etc/kubernetes/admin. Conf kubectl get initialized nodes # Node kubeadm join 192.168.137.200:6443 - token 0bgtnj.6xl01261s02wqc7z \ --discovery-token-ca-cert-hash Sha256: # 6553 e9758f5eb18d81e793e936fffb7a168c943a6429de0834e1946033ce6f80 possible ERROR [ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1 kubeadm reset echo 1 > /proc/sys/net/ipv4/ip_forward Network plugin (Master) kubectl apply - f # https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml Kubectl get Pods -n kube-systemCopy the code

Ipvs configuration

# # to install ipvs yum - y install ipvsadm ipset permanent cat > / etc/sysconfig/modules/ipvs modules < < EOF modprobe - ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack_ipv4 EOF / etc/sysconfig/modules/ipvs. # modules perform/bin/bash script file/etc/sysconfig/modules/ipvs modules # check to see if the corresponding module loaded successfully lsmod | grep Kubectl edit configmap kube-proxy -n kube-systemCopy the code

Delete the original kube-proxy

Delete pod kubectl delete pod kube-proxy-* -n kube-system # delete pod kubectl get pod -n Kube-system # Check logs to see if there is an ipvSCopy the code

The Dashboard UI monitors usage

Making address github.com/kubernetes/…

# # download wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml editor file open port visit 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: 31443 selector: k8s-app: Kubernetes - # dashboard Settings roleRef super users in accordance with the following changes - apiVersion:. Rbac authorization. K8s. IO/v1 kind: ClusterRoleBinding metadata: name: kubernetes-dashboard-minimal namespace: kube-system roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - kind: ServiceAccount name: kubernetes-dashboard namespace: Kubectl -n kubernetes-dashboard describe secret $(kubectl). Kube-system -- # start kubectl apply -f recommended - n kubernetes - dashboard get secret | grep admin - user | awk '} {print $1) # will display a token string and then to landCopy the code

Simple SpringBoot application deployment

vim k8s-springboot.yaml apiVersion: v1 kind: Namespace metadata: name: k8s-springboot --- apiVersion: v1 kind: Service metadata: name: springboot-demo-nodeport namespace: k8s-springboot spec: type: NodePort ports: - port: 8080 targetPort: 8080 nodePort: 30001 selector: app: springboot-demo --- apiVersion: apps/v1 kind: Deployment metadata: name: springboot-demo namespace: k8s-springboot spec: selector: matchLabels: app: springboot-demo replicas: 1 template: metadata: labels: app: springboot-demo spec: containers: - name: springboot-demo image: Huzhihui/springboot: 1.0.0 ports: - containerPort: 8080 # deploy kubectl apply -f k8s-springboot.yaml # Deploy kubectl get Pods --all-namespacesCopy the code

Use proxy to access the service

Use the kubectl proxy command to make the API server listen on port 8001: $kubectl proxy Starting to serve on 127.0.0.1:8001 $kubectl proxy --port=8009 Starting to serve on 127.0.0.1:1 $kubectl proxy --port=8009 Starting to serve on $curl -x GET -l http://master:8009/ <h3>Unauthorized</h3> # Set API server to receive requests from all hosts: $kubectl proxy --address='0.0.0.0' --accept-hosts='^*$' --port=8009 Starting to serve on [::]:8009  $ curl -X GET -L http://k8s-master:8009/ { "paths": [ "/api", "/api/v1", ... ] }} http://k8s-master :8009/ API /v1/namespaces/[namespace-name]/services/[service-name]/proxy # = '0.0.0.0' kubectl proxy - address - accept - hosts = '$' ^ * - port = 8009 > & 1 & 2Copy the code

Ingress use

Plug-in address kubernetes. Making. IO/ingress – ngi…

Install the wget plugin https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.48.1/deploy/static/provider/cloud/deploy.yaml # Edit the file to change the following version, because the image can not be pulled in China, so change vim deploy.yamlCopy the code

Run kubectl apply -f deploy. Yaml as soon as the following controller is successful and leave the other two alone

I’ve already created the Springboot-demo-nodeport service, so I’ll just use it

K8s – springboot – ingress. Yaml files

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: k8s-springboot-ingress
  namespace: k8s-springboot
spec:
  rules:
    - host: k8s-springboot.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: springboot-demo-nodeport
                port:
                  number: 8080
Copy the code

To view ingress, run the kubectl get ingress –all-namespaces command

Editing the hosts file

Vim /etc/hosts # add IP 192.168.137.200k8s-springboot.com to the following lineCopy the code

Visit the curl http://k8s-springboot.com

So far the use of simple finished, I also just learn, there are a lot of do not understand, but also hope that the leaders to teach