环境
[09:42:02 root@ceshi-01 ~ $]cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[09:41:17 root@ceshi-01 ~ $]kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:23:26Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:15:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
第一步:Installing kubeadm, kubelet and kubectl
您将在所有节点上安装以下软件包:
kubeadm 引导集群的命令
kubelet 在集群中所有计算机上运行的组件,并执行诸如启动 pod 和容器之类的操作
kubectl 用于与集群通讯的命令行
## 官方源(被墙)
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
## 阿里源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
## 安装并启动
## kubelet 将每隔几秒重启一次,因为他在等待 kubeadm 告诉他应该怎么做
yum install -y kubelet-1.15.2 kubeadm-1.15.2 kubectl-1.15.2
systemctl enable kubelet && systemctl start kubelet
systemctl status kubelet
## 让 kubectl 命令支持自动补全(临时生效)
source <(kubectl completion bash)
## 永久生效
echo "source <(kubectl completion bash)" >> ~/.bashrc
第二步:初始化集群(Master 节点)
1.使用阿里源初始化集群
如果要部署 Flannel 网络,需要在初始化集群的时候添加以下参数:--pod-network-cidr=10.244.0.0/16
kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16
## 如果已经使用官方源初始化集群并报错的话需要执行以下命令重置设置后重新初始化
kubeadm reset
## 执行成功后有如下提示
## 1.提示初始化成功
## 2.执行以下3条命令
## 3.告诉你应该向集群中部署一个 Pod 网络
## 4.可以有任意多的 worker 节点使用以下命令加入集群
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
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.30.112:6443 --token rfcsy4.og5y0otwoxwc2cue \
--discovery-token-ca-cert-hash sha256:0ffd11a2a1f3fe713ade639c64137ab07756d8825dd00ec32cbbae5d921cbba3
2.执行以下命令
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
3.在集群中部署pod网络
## 这里只是为了快速开始,所以部署了「Weave Net」网络(后面可以更换为其他网络)
## 更多详情请参阅:https://kubernetes.io/docs/concepts/cluster-administration/addons/#networking-and-network-policy
## kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
## 部署 Flannel 网络
## 如果要部署 Flannel 网络,需要在初始化集群的时候添加以下参数:--pod-network-cidr=10.244.0.0/16
## 参考:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml
## 使用以下命令查看状态
kubectl get nodes
kubectl get pods --all-namespaces
## nodes 状态要处于 Ready
## pod 要处于 running 状态
[root@k8s-02 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-02 Ready master 32m v1.15.2
[root@k8s-02 ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-bccdc95cf-2hh76 1/1 Running 0 32m
kube-system coredns-bccdc95cf-n68wk 1/1 Running 0 32m
kube-system etcd-k8s-02 1/1 Running 0 31m
kube-system kube-apiserver-k8s-02 1/1 Running 0 31m
kube-system kube-controller-manager-k8s-02 1/1 Running 0 31m
kube-system kube-proxy-kqjpj 1/1 Running 0 32m
kube-system kube-scheduler-k8s-02 1/1 Running 0 31m
kube-system weave-net-sxllx 2/2 Running 0 10m
4.从 Master 节点以外的计算机控制集群
## 该 admin.conf 为用户 提供了集群的超级用户权限,应谨慎使用此文件
## https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
scp root@<master ip>:/etc/kubernetes/admin.conf .
kubectl --kubeconfig ./admin.conf get nodes
第三步:加入集群(Worker 节点)
1.安装 kubeadm
## 阿里源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
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 kubeadm
systemctl enable kubelet.service
2.加入到集群
kubeadm join 192.168.30.112:6443 --token rfcsy4.og5y0otwoxwc2cue \
--discovery-token-ca-cert-hash sha256:0ffd11a2a1f3fe713ade639c64137ab07756d8825dd00ec32cbbae5d921cbba3
3.验证集群状态(Master 执行)
kubectl get nodes
[root@k8s-02 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-02 Ready master 46m v1.15.2
k8s-03 Ready <none> 79s v1.15.2
第四步:简单使用
## 创建名名称为 nginx 的容器,指定镜像为 nginx,副本数为5,暴露端口为80
kubectl run nginx --image=nginx --replicas=5 --port=80
## 扩容到10个副本
kubectl scale deploy/nginx --replicas=10
## 查看 Pods 标签
kubectl get pods --show-labels
## 查看 Pod 详细信息
kubectl describe pod/nginx-7c45b84548-7wvg8
## 查看所有 Pods 信息(包含运行在某个节点)
kubectl get pods -o wide
kubectl get deploy -o wide
## 发布应用
kubectl expose deployment nginx --port=80 --type=NodePort --target-port=80 --name=nginx-service
附录:
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/high-availability/
网友评论