1)首先部署nginx pod 和復(fù)制器---------------------------------------------------------------------
[root@k8s-master ~]
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io
/nginx
latest 3448f27c273f 8 days ago 109.4 MB
通過下面命令發(fā)現(xiàn)apiVersion版本是v1
[root@k8s-master ~]
# curl -s -L http://182.48.115.237:8080/api/v1beta1/version | python -mjson.tool
{
"apiVersion"
:
"v1"
,
.......
}
開始創(chuàng)建pod單元
[root@k8s-master ~]
# mkdir -p /home/kubermange && cd /home/kubermange
[root@k8s-master kubermange]
# vim nginx-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-controller
spec:
replicas: 2
#即2個(gè)備份
selector:
name: nginx
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: docker.io
/nginx
ports:
- containerPort: 80
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 create -f nginx-rc.yaml
replicationcontroller
"nginx-controller"
created
由于kubernetes要去gcr.io下載gcr.io
/google_containers/pause
鏡像,,然后下載nginx鏡像,所以所創(chuàng)建的Pod需要等待一些時(shí)間才能處于running狀態(tài),。
然后查看pods清單
[root@k8s-master kubermange]
# kubectl -s http://k8s-master:8080 get pods
NAME READY STATUS RESTARTS AGE
nginx-controller-f0j9c 0
/1
ContainerCreating 0 1m
nginx-controller-v219k 0
/1
ContainerCreating 0 1m
可以使用describe 命令查看pod所分到的節(jié)點(diǎn):
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 describe pod nginx-controller-f0j9c
Name: nginx-controller-f0j9c
Namespace: default
Node: k8s-node-1
/182
.48.115.238
.......
同理,,查看另一個(gè)pod
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 describe pod nginx-controller-v219k
Name: nginx-controller-v219k
Namespace: default
Node: k8s-node-2
/182
.48.115.239
.......
由上可以看出,這個(gè)復(fù)制器啟動了兩個(gè)Pod,,分別運(yùn)行在182.48.115.238和182.48.115.239這兩個(gè)節(jié)點(diǎn)上了,。到這兩個(gè)節(jié)點(diǎn)上查看,發(fā)現(xiàn)已經(jīng)有nginx應(yīng)用容器創(chuàng)建了,。
2)部署節(jié)點(diǎn)內(nèi)部可訪問的nginx service------------------------------------------------------------------------
Service的
type
有ClusterIP和NodePort之分,,缺省是ClusterIP,這種類型的Service只能在集群內(nèi)部訪問,。配置文件如下:
[root@k8s-master kubermange]
# vim nginx-service-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service-clusterip
spec:
ports:
- port: 8001
targetPort: 80
protocol: TCP
selector:
name: nginx
然后執(zhí)行下面的命令創(chuàng)建service:
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 create -f ./nginx-service-clusterip.yaml
service
"nginx-service-clusterip"
created
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443
/TCP
2h
nginx-service-clusterip 10.254.163.249 <none> 8001
/TCP
24s
驗(yàn)證service的可訪問性(訪問節(jié)點(diǎn)):
上面的輸出告訴我們這個(gè)Service的Cluster IP是10.254.163.249,,端口是8001。那么我們就來驗(yàn)證這個(gè)PortalNet IP的工作情況:
ssh
登錄到節(jié)點(diǎn)機(jī)上驗(yàn)證(可以提前做
ssh
無密碼登錄的信任關(guān)系,,當(dāng)然也可以不做,,這樣驗(yàn)證時(shí)要手動輸入登錄密碼)
[root@k8s-master kubermange]
# ssh 182.48.115.238 curl -s 10.254.163.249:8001 //或者直接到節(jié)點(diǎn)機(jī)上執(zhí)行"curl -s 10.254.163.249:8001"
The authenticity of host
'182.48.115.238 (182.48.115.238)'
can't be established.
ECDSA key fingerprint is 4c:24:35:e0:35:00:86:05:94:a2:9e:f9:22:b0:90:b7.
Are you sure you want to
continue
connecting (
yes
/no
)?
yes
Warning: Permanently added
'182.48.115.238'
(ECDSA) to the list of known hosts.
<!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:///"
><
/a
>.<br/>
Commercial support is available at
<a href=
"http:///"
><
/a
>.<
/p
>
<p><em>Thank you
for
using nginx.<
/em
><
/p
>
<
/body
>
<
/html
>
同理驗(yàn)證到另外一個(gè)節(jié)點(diǎn)機(jī)上的service的可訪問性也是ok的
[root@k8s-master kubermange]
# ssh 182.48.115.239 curl -s 10.254.163.249:8001
由此可見,從前面部署復(fù)制器的部分可以知道nginx Pod運(yùn)行在182.48.115.238和182.48.115.239這兩個(gè)節(jié)點(diǎn)上,。
從這兩個(gè)節(jié)點(diǎn)上訪問我們的服務(wù)來體現(xiàn)Service Cluster IP在所有集群節(jié)點(diǎn)的可到達(dá)性,。
3)部署外部可訪問的nginx service-------------------------------------------------------------------
下面我們創(chuàng)建NodePort類型的Service,這種類型的Service在集群外部是可以訪問,。下表是本文用的配置文件:
[root@k8s-master kubermange]
# vim nginx-service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service-nodeport
spec:
ports:
- port: 8000
targetPort: 80
protocol: TCP
type
: NodePort
selector:
name: nginx
執(zhí)行下面的命令創(chuàng)建service:
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 create -f ./nginx-service-nodeport.yaml
service
"nginx-service-nodeport"
created
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443
/TCP
2h
nginx-service-clusterip 10.254.163.249 <none> 8001
/TCP
13m
nginx-service-nodeport 10.254.146.68 <nodes> 8000:31298
/TCP
22s
使用下面的命令獲得這個(gè)service的節(jié)點(diǎn)級別的端口:
[root@k8s-master kubermange]
# kubectl -s http://182.48.115.237:8080 describe service nginx-service-nodeport 2>/dev/null | grep NodePort
Type: NodePort
NodePort: <
unset
> 31298
/TCP
驗(yàn)證service的可訪問性(訪問節(jié)點(diǎn)):
上面的輸出告訴我們這個(gè)Service的節(jié)點(diǎn)級別端口是31298,。下面我們驗(yàn)證這個(gè)Service的工作情況:
[root@k8s-master kubermange]
# curl 182.48.115.238:31298
<!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:///"
><
/a
>.<br/>
Commercial support is available at
<a href=
"http:///"
><
/a
>.<
/p
>
<p><em>Thank you
for
using nginx.<
/em
><
/p
>
<
/body
>
<
/html
>
同理驗(yàn)證到另外一個(gè)節(jié)點(diǎn)機(jī)上的service的可訪問性也是ok的
[root@k8s-master kubermange]
# curl 182.48.115.239:31298
----------------------------------------------------------
登錄另外兩個(gè)節(jié)點(diǎn)機(jī)上,發(fā)現(xiàn)已經(jīng)創(chuàng)建了nginx應(yīng)用容器
[root@k8s-node-1 ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
620d9171a42c docker.io
/nginx
"nginx -g 'daemon off"
19 minutes ago Up 19 minutes k8s_nginx.3d610115_nginx-controller-f0j9c_default_eaa0497b-3be5-11e7-a8a1-52540030ba6d_a6cde2e2
01facbbbe7cb registry.access.redhat.com
/rhel7/pod-infrastructure
:latest
"/pod"
19 minutes ago Up 19 minutes k8s_POD.a8590b41_nginx-controller-f0j9c_default_eaa0497b-3be5-11e7-a8a1-52540030ba6d_d2dd947d
[root@k8s-node-2 ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
308749352e55 docker.io
/nginx
"nginx -g 'daemon off"
30 minutes ago Up 30 minutes k8s_nginx.3d610115_nginx-controller-v219k_default_eaa02644-3be5-11e7-a8a1-52540030ba6d_7d54d433
cde94e406f9a registry.access.redhat.com
/rhel7/pod-infrastructure
:latest
"/pod"
30 minutes ago Up 30 minutes