当前位置: 首页>前端>正文

K8S 中 ClusterIP 和 NodePort 的区别 k8s clusterrole

我们将部署三个节点的 Kubernetes Cluster。

k8s-master 是 Master,k8s-node1 和 k8s-node2 是 Node。

所有节点的操作系统均为 Ubuntu 16.04,当然其他 Linux 也是可以的。

官方安装文档可以参考 https://kubernetes.io/docs/setup/independent/install-kubeadm/

注意:Kubernetes 几乎所有的安装组件和 Docker 镜像都放在 goolge 自己的网站上,这对国内的同学可能是个不小的障碍。建议是:网络障碍都必须想办法克服,不然连 Kubernetes 的门都进不了。

安装 Docker

所有节点都需要安装 Docker。

apt-get update && apt-get install docker.io

安装 kubelet、kubeadm 和 kubectl

在所有节点上安装 kubelet、kubeadm 和 kubectl。

kubelet 运行在 Cluster 所有节点上,负责启动 Pod 和容器。

kubeadm 用于初始化 Cluster。

kubectl 是 Kubernetes 命令行工具。通过 kubectl 可以部署和管理应用,查看各种资源,创建、删除和更新各种组件。

apt-get update && apt-get install -y apt-transport-https
 
 
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
 
 
deb http://apt.kubernetes.io/ kubernetes-xenial main
 
 
EOF
 
 
apt-get updateapt-get install -y kubelet kubeadm kubectl
 
 
用 kubeadm 创建 Cluster
完整的官方文档可以参考 https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/
初始化 Master
在 Master 上执行如下命令:
 
 
kubeadm init --apiserver-advertise-address 192.168.56.105 --pod-network-cidr=10.244.0.0/16
 
 
--apiserver-advertise-address 指明用 Master 的哪个 interface 与 Cluster 的其他节点通信。如果 Master 有多个 interface,建议明确指定,如果不指定,kubeadm 会自动选择有默认网关的 interface。
--pod-network-cidr 指定 Pod 网络的范围。Kubernetes 支持多种网络方案,而且不同网络方案对 --pod-network-cidr 有自己的要求,这里设置为 10.244.0.0/16 是因为我们将使用 flannel 网络方案,必须设置成这个 CIDR。在后面的实践中我们会切换到其他网络方案,比如 Canal。
初始化过程如下:
① kubeadm 执行初始化前的检查。
② 生成 token 和证书。
③ 生成 KubeConfig 文件,kubelet 需要这个文件与 Master 通信。
④ 安装 Master 组件,会从 goolge 的 Registry 下载组件的 Docker 镜像,这一步可能会花一些时间,主要取决于网络质量。
⑤ 安装附加组件 kube-proxy 和 kube-dns。
⑥ Kubernetes Master 初始化成功。
⑦ 提示如何配置 kubectl,后面会实践。
⑧ 提示如何安装 Pod 网络,后面会实践。
⑨ 提示如何注册其他节点到 Cluster,后面会实践。
配置 kubectl
kubectl 是管理 Kubernetes Cluster 的命令行工具,前面我们已经在所有的节点安装了 kubectl。Master 初始化完成后需要做一些配置工作,然后 kubectl 就能使用了。
依照 kubeadm init 输出的第 ⑦ 步提示,推荐用 Linux 普通用户执行 kubectl(root 会有一些问题)。
我们为 ubuntu 用户配置 kubectl:
 
 
su - ubuntu
 
 
mkdir -p $HOME/.kube
 
 
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
 
 
sudo chown $(id -u):$(id -g) $HOME/.kube/config
 
 
为了使用更便捷,启用 kubectl 命令的自动补全功能。
 
 
echo "source <(kubectl completion bash)" >> ~/.bashrc
 
 
这样 ubuntu 用户就可以使用 kubectl 了。
下节我们将安装 Pod 网络并添加 k8s-node1 和 k8s-node2,完成集群部署。
CENTOS7.4部署k8s安装
1:设置阿里源
 
 
 
  cat <<EOF >  
  / 
  etc 
  / 
  yum.repos.d 
  / 
  kubernetes.repo 
 
 
 
 
  [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 
 
 
 

   wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
 
 
 

   wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  
 
 
 

   wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo  
 
 
 
2:设置selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config3:安装程序
yum install -y docker-ce kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet
systemctl enable docker.service
systemctl start docker.service
4:设置环境参数
vi /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1net.ipv4.ip_forward = 1
vm.swappiness=0sysctl --system
5:加载组件
modprobe br_netfilter
 
6:关闭swap
vi /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
 
7:kubeadm初始化
# kubeadm init --apiserver-advertise-address 192.168.169.31 --pod-network-cidr=10.244.0.0/16
W0624 18:24:52.059372 22813 version.go:98] could not fetch a Kubernetes version from the internet: unable to get URL "https://dl.k8s.io/release/stable-1.txt": Get https://dl.k8s.io/release/stable-1.txt: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
W0624 18:24:52.059515 22813 version.go:99] falling back to the local client version: v1.15.0
[init] Using Kubernetes version: v1.15.0
[preflight] Running pre-flight checks
 [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[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'
[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] Activating the kubelet service
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.169.31]
[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/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.169.31 127.0.0.1 ::1]
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.169.31 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
[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"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 16.003274 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: zn5f6v.4wl5l93ar5an3v0d
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[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
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxyYour 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/configYou 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.169.31:6443 --token zn5f6v.4wl5l93ar5an3v0d \

--discovery-token-ca-cert-hash sha256:4b4ebc498106b4f96ba752a7773d1ea03f67da5af3b08302a7a55b76c3010ed3

参考文档 https://www.jianshu.com/p/866f02f67578


https://www.xamrdz.com/web/2ag1962140.html

相关文章: