一,安装kubernetes前的准备工作
安装前的准备工作(master\worker都要进行)
参见:
https://www.cnblogs.com/architectforest/p/13141743.html
说明:以上这些准备工作需要在worker 节点机器上操作一遍
kubernetes的master节点的安装(需在master节点机器上操作):
参见:
https://www.cnblogs.com/architectforest/p/13153053.html
演示例子说明:机器共两台:
master节点机器: ip: 192.168.219.130, hostname: kubemaster
woker 节点机器: ip: 192.168.219.136, hostname: node1
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,在worker节点安装kubernetes
1,新建kubernetes的repo
[root@node1 ~]# vi /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
2,安装三大件:
[root@node1 ~]# dnf install kubectl kubelet kubeadm
安装完成后查看版本:
[root@node1 ~]# kubelet --version Kubernetes v1.18.3
[root@node1 ~]# kubeadm version kubeadm version: &version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40",
GitTreeState:"clean", BuildDate:"2020-05-20T12:49:29Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
[root@node1 ~]# kubectl version Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40",
GitTreeState:"clean", BuildDate:"2020-05-20T12:52:00Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"} The connection to the server localhost:8080 was refused - did you specify the right host or port?
3,使kubelet服务可以自启动
[root@node1 ~]# systemctl enable kubelet
三,把worker节点加入到cluster
1,得到join命令:要在master节点上执行:
[root@kubemaster ~]# kubeadm token create --print-join-command W0618 15:07:30.243762 115106 configset.go:202] WARNING: kubeadm cannot validate component configs
for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io] kubeadm join 192.168.219.130:6443 --token cts238.khb7z4qwu1h6iens \
--discovery-token-ca-cert-hash sha256:c718e29ccb1883715489a3fdf53dd810a7764ad038c50fd62a2246344a4d9a73
worker节点的ip要加入到master节点的防火墙:
[root@kubemaster ~]# firewall-cmd --permanent --zone=trusted --add-source=192.168.219.136 success [root@kubemaster ~]# firewall-cmd --reload success
2,回到worker节点上执行:
[root@node1 ~]# kubeadm join 192.168.219.130:6443 --token cts238.khb7z4qwu1h6iens \
--discovery-token-ca-cert-hash sha256:c718e29ccb1883715489a3fdf53dd810a7764ad038c50fd62a2246344a4d9a73
看到如下信息表示加入到cluster成功:
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.
3,查看woker节点上新增加的image
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE calico/node v3.14.1 04a9b816c753 3 weeks ago 263MB calico/pod2daemon-flexvol v3.14.1 7f93af2e7e11 3 weeks ago 112MB calico/cni v3.14.1 35a7136bc71a 3 weeks ago 225MB registry.aliyuncs.com/google_containers/kube-proxy v1.18.3 3439b7546f29 4 weeks ago 117MB registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 4 months ago 683kB
4,备注:
error execution phase preflight: couldn‘t validate the identity of the API Server:
could not find a JWS signature in the cluster-info ConfigMap for token ID “xgt21h"
这个错误提示表示token已过期
四,在master节点上查看node
[root@kubemaster ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION kubemaster Ready master 26h v1.18.3 node1 Ready <none> 2m57s v1.18.3
已经看到了node1节点,说明加入到cluster成功
五,在master和node1节点上安装一个nginx容器,并查看效果:
编辑rc文件
[root@kubemaster k8s]# vi nginx-rc.yaml
内容:
apiVersion: v1 kind: ReplicationController metadata: name: nginx-demo spec: replicas: 2 selector: app: nginx-demo template: metadata: labels: app: nginx-demo spec: containers: - name: nginx-demo image: nginx ports: - containerPort: 80
创建rc
[root@kubemaster k8s]# kubectl apply -f nginx-rc.yaml
replicationcontroller/nginx-demo created
查看效果:
[root@kubemaster k8s]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-demo-b5vwt 1/1 Running 0 15m 172.16.141.16 node1 <none> <none> nginx-demo-j8n7d 1/1 Running 0 15m 172.16.141.13 node1 <none> <none>
用curl访问:
[root@kubemaster ~]# curl http://172.16.141.13 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
生成service文件
[root@kubemaster k8s]# vi nginx-svc.yaml
内容:
apiVersion: v1 kind: Service metadata: name: nginx-demo spec: type: NodePort ports: - port: 80 nodePort: 30020 selector: app: nginx-demo
创建service
[root@kubemaster k8s]# kubectl apply -f nginx-svc.yaml
service/nginx-demo created
从浏览器访问服务:
六,查看kubernetes的版本
[root@kubemaster ~]# kubelet --version Kubernetes v1.18.3 [root@kubemaster ~]# kubeadm version kubeadm version: &version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40",
GitTreeState:"clean", BuildDate:"2020-05-20T12:49:29Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64”}
七,查看linux的版本
[root@kubemaster ~]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core) [root@kubemaster ~]# uname -r 4.18.0-193.el8.x86_64
linux(centos8):安装kubernetes worker节点并加入到kubernetes集群(kubernetes 1.18.3)