部署环境服务器:腾讯云
操作系统版本:centos 7.2
1、修改系统配置:
|
1
2
3
|
[root@node1~]# cat /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables= 1
net.bridge.bridge-nf-call-iptables= 1
|
2、是配置生效:
执行sysctl -p/etc/sysctl.d/k8s.conf使修改生效。
3、设置主机名对应IP:
[root@node1~]# cat /etc/hosts
10.104.143.65 node1
10.186.54.177 node2
4、安装kuberete包,下载包,因为中国都是被屏蔽的、所以使用国人维护的库,下载包下来:https://yum.mritd.me/
# 添加 yum 源
|
1
2
3
4
5
6
7
8
|
[root@linux-node1src]
[mritdrepo]
name=MritdRepository
baseurl=https:
//yumrepo
.b0.upaiyun.com
/centos/7/x86_64
enabled=1
gpgcheck=1
gpgkey=https:
//mritd
.b0.upaiyun.com
/keys/rpm
.public.key
EOF
|
|
1
2
3
4
5
6
7
8
9
|
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
kubeadm-1.6.2-0.x86_64.rpm kubectl-1.6.2-0.x86_64.rpm kubelet-1.6.2-0.x86_64.rpm kubernetes-cni-0.5.1-0.x86_64.rpm
|
5、安装kubeletes包:
[root@linux-node1src]# yum localinstall kube*.rpm
6、我们先了解一下需要下载哪些包,相关版本都需要什么依赖关系,因为在国内包是被墙的,我们需要从第三方云下载相关的版本然后再安装:
| Image |
Ver |
Component |
| gcr.io/google_containers/kube-proxy-amd64 |
v1.6.2 |
Kubernetes |
| gcr.io/google_containers/kube-controller-manager-amd64 |
v1.6.2 |
Kubernetes |
| gcr.io/google_containers/kube-apiserver-amd64 |
v1.6.2 |
Kubernetes |
| gcr.io/google_containers/kube-scheduler-amd64 |
v1.6.2 |
Kubernetes |
| gcr.io/google_containers/etcd-amd64 |
3.0.17 |
Kubernetes |
| gcr.io/google_containers/pause-amd64 |
3.0 |
Kubernetes |
| gcr.io/google_containers/k8s-dns-sidecar-amd64 |
1.14.1 |
DNS |
| gcr.io/google_containers/k8s-dns-kube-dns-amd64 |
1.14.1 |
DNS |
| gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64 |
1.14.1 |
DNS |
7、下载相关的包,然后重命名tag,再删除之前的包:
|
1
2
3
4
5
6
7
|
[root@linux-node1src]
[root@linux-node1src]
docker pull registry.cn-hangzhou.aliyuncs.com
/kube_containers/
$imageName
docker tag registry.cn-hangzhou.aliyuncs.com
/kube_containers/
$imageNamegcr.io
/google_containers/
$imageName
docker rmi registry.cn-hangzhou.aliyuncs.com
/kube_containers/
$imageName
done
|
|
1
2
3
4
5
6
7
8
9
|
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
[root@linux-node1src]
|
8、初始化集群:
|
1
2
3
4
5
|
[root@node1~]
........
You cannow
join
any number of machines by running the following on each node
as root:
kubeadm
join
--token 50e5cc.8ca639f09da1fb6510.104.143.65:6443
|
#使用flannel网络的时候需要指定pod-network-cide,不然后面起不来的。
#控制token,这个是我们Node加入集群的凭证。
9、假如出错,我们就重新初始化集群,然后再生成一个token-id:(不重新执行这部略)
|
1
2
3
4
5
6
|
[root@node1~]
[root@node1~]
[root@node1~]
[root@node1~]
[root@node1~]
[root@node1~]
|
10、在1.6.2版本之后由于监听的是6443端口,不是之前的80,所以执行查看会报错:
|
1
2
3
4
|
[root@node1manifests]
Theconnection to the server localhost:8080 was refused - did you specify the righthost or port?
|
11、可以查看一下相关api-server文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@node1manifests]
apiVersion:v1
。。。。。。
image:gcr.io
/google_containers/kube-apiserver-amd64
:v1.6.2
livenessProbe:
failureThreshold: 8
httpGet:
host: 127.0.0.1
path:
/healthz
port: 6443
scheme: HTTPS
initialDelaySeconds: 15
timeoutSeconds: 15
[root@node1manifests]
tcp6 0 0:::6443 :::* LISTEN 64692
/kube-apiserve
|
12、设置一下环境变量:
|
1
2
3
|
[root@node1~]
exportKUBECONFIG=
/etc/kubernetes/admin
.conf
[root@node1~]
|
13、查看一下相关的pod状态:
|
1
2
3
4
5
6
7
8
9
|
[root@node1src]
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE
kube-system etcd-node1 1
/1
Running 0 4m 10.104.143.65 node1
kube-system kube-apiserver-node1 1
/1
Running 0 4m 10.104.143.65 node1
kube-system kube-controller-manager-node1 1
/1
Running 0 4m 10.104.143.65 node1
kube-system kube-dns-3913472980-zkqk1 0
/3
Pending 0 3m <none>
kube-system kube-proxy-tckxj 1
/1
Running 0 3m 10.104.143.65 node1
kube-system kube-scheduler-node1 1
/1
Running 0 4m 10.104.143.65 node1
|
14、查看集群节点的状态:
|
1
2
3
|
[root@node1src]
NAME STATUS AGE VERSION
node1 NotReady 3m v1.6.2
|
15、接下来我们配置网络,先下载flannl网络文件,然后配置:
|
1
2
|
[root@node1 src]
[root@node1 src]
|
##然后修改相关文件,加上自己网卡,不然多网卡经常网络起不来:
|
1
2
3
|
[root@node1 src]
command
: [
"/opt/bin/flanneld"
,
"--ip-masq"
,
"--kube-subnet-mgr"
,
"--iface=eth0"
]
|
16、为了启动快速,我们可以事先把flannel的镜像下载下来:
|
1
2
|
[root@node1 ~]
[root@node1 ~]
|
17、接下来我们启动flannel网络:
|
1
2
3
4
5
6
7
|
[root@node1 src]
clusterrole
"flannel"
created
clusterrolebinding
"flannel"
created
[root@node1 src]
serviceaccount
"flannel"
created
configmap
"kube-flannel-cfg"
created
daemonset
"kube-flannel-ds"
created
|
18、然后我们看一下pod相关状态:
|
1
2
3
4
5
6
7
8
9
|
[root@node1 src]
NAME READY STATUS RESTARTS AGE IP NODE
etcd-node1 1
/1
Running 0 10m 10.104.143.65 node1
kube-apiserver-node1 1
/1
Running 0 10m 10.104.143.65 node1
kube-controller-manager-node1 1
/1
Running 0 10m 10.104.143.65 node1
kube-dns-3913472980-zkqk1 3
/3
Running 0 10m 10.244.0.2 node1
kube-flannel-ds-xjckw 2
/2
Running 0 51s 10.104.143.65 node1
kube-proxy-tckxj 1
/1
Running 0 10m 10.104.143.65 node1
kube-scheduler-node1 1
/1
Running 0 10m 10.104.143.65 node1
|
19、接下来我们把node2加入集群:
|
1
2
3
4
|
[root@node2 ~]
[kubeadm] WARNING: kubeadm is
in
beta, pleasedo not use it
for
production clusters.
[preflight] Running pre-flight checks
。。。。。
|
20、在Node1上面查看:
|
1
2
3
4
|
[root@node1src]
NAME STATUS AGE VERSION
node1 Ready 26m v1.6.2
node2 Ready 2m v1.6.2
|
21、我们可以先测试一下DNS解析组件是否正常,因为这个和可能影响到后面的dashboard:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@node1 src]
If you don't see a
command
prompt, trypressing enter.
[ root@curl-57077659-f4wgb:/ ]$ nslookupkubernetes.default
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.
local
Name: kubernetes.default
Address 1: 10.96.0.1kubernetes.default.svc.cluster.
local
[ root@curl-57077659-f4wgb:/ ]$ nslookupkube-dns.kube-system
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.
local
Name: kube-dns.kube-system
Address 1: 10.96.0.10kube-dns.kube-system.svc.cluster.
local
|
22、正常之后,我们编写一个pod,然后测试一下运行情况:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@node1src]
apiVersion:extensions
/v1beta1
kind:Deployment
metadata:
name: my-nginx
spec:
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
|
###对外访问的时候,我们需要提供相关端口访问,故需要编写一个service:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@node1src]
apiVersion:v1
kind:Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
type
: NodePort
ports:
- port: 80
nodePort: 30062
protocol: TCP
selector:
run: my-nginx
|
23、启动正常,我们可以测试一下访问是否正常:
|
1
2
3
4
5
6
7
8
9
10
|
[root@node1src]
deployment
"my-nginx"
created
[root@node1src]
service
"my-nginx"
created
[root@node1src]
NAME READY STATUS RESTARTS AGE IP NODE
curl-57077659-f4wgb 1
/1
Running 1 19m 10.244.0.3 node1
my-nginx-858393261-2f2cq 1
/1
Running 0 41s 10.244.0.4 node1
my-nginx-858393261-frdtg 1
/1
Running 0 41s 10.244.1.2 node2
|
##查看一下相关service的情况:
|
1
2
3
4
|
[root@node1src]
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.96.0.1 <none> 443
/TCP
37m
my-nginx 10.110.89.147 <nodes> 80:30062
/TCP
1m
|
24、接下来就是测试是否正常了:
|
1
2
3
4
5
6
|
[root@node1src]
<!DOCTYPEhtml>
<html>
<
head
>
<title>Welcometo nginx!<
/title
>
.......
|
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@node1src]
<!DOCTYPEhtml>
<html>
<
head
>
<title>Welcometo nginx!<
/title
>
[root@node1src]
<!DOCTYPEhtml>
<html>
<
head
>
<title>Welcometo nginx!<
/title
>
<style>
|
25、##然后我们可以测试一下容器之间的相互连通性:
|
1
2
3
4
5
6
7
8
9
10
|
[root@node1src]
NAME READY STATUS RESTARTS AGE IP NODE
curl-57077659-f4wgb 1
/1
Running 1 23m 10.244.0.3 node1
my-nginx-858393261-2f2cq 1
/1
Running 0 4m 10.244.0.4 node1
my-nginx-858393261-frdtg 1
/1
Running 0 4m 10.244.1.2 node2
[root@node1src]
PING10.244.1.2 (10.244.1.2): 56 data bytes
64 bytesfrom 10.244.1.2: icmp_seq=0 ttl=62
time
=5.018 ms
|
26、接下来我们测试一下dns解析是否有问题,因为我们有curl节点了,直接用curl测试,有了dns内部测试,我们可以根据service 定义的访问即可:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@node1src]
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.96.0.1 <none> 443
/TCP
57m
my-nginx 10.110.89.147 <nodes> 80:30062
/TCP
21m
[root@node1src]
% Total % Received % Xferd AverageSpeed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<!DOCTYPE html>
<html>
<
head
>
<title>Welcometo nginx!<
/title
>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial,sans-serif;
}
<
/style
>
|
##我们看到测试dns也是能够完成安装的,到底玄机在哪里的,就是dns插件构成的结果,后续我们有时间再做进一步解析,今天kubernetes安装就先到这。
本文转自 小罗ge11 51CTO博客,原文链接:http://blog.51cto.com/xiaoluoge/1939561,如需转载请自行联系原作者