Kubernetes 從入門到進階 第一個Helloworld程式
本文將使用kubernetes-ansible進行安裝,由於其kubernetes-ansible更新很快,細小的地方又發生了一定變化,簡單記錄一下,同時由於特殊原因,一般網內使用者都無法正常執行k8s。所以這個系列準備寫一些咱們自己能用的例子來學習k8s的基礎知識。
構成說明
演示程式使用下列的叢集構成。
No | type | IP | OS |
---|---|---|---|
1 | master | 192.168.32.131 | CENTOS7.2 |
2 | etcd | 192.168.32.131 | CENTOS7.2 |
3 | minion | 192.168.32.132 | CENTOS7.2 |
3 | minion | 192.168.32.133 | CENTOS7.2 |
3 | minion | 192.168.32.134 | CENTOS7.2 |
Step 1:安裝ansible
在192.168.32.131上安裝ansible
[root@host131 local]# yum -y install epel-release
[root@host131 local]# yum -y install ansible
- 1
- 2
確認安裝
[root@host131 local]# ansible --version
ansible 2.1.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
[root@host131 local]#
- 1
- 2
- 3
- 4
- 5
Step 2:設定ssh通路和ansible
分別在4臺機器上生成ssh的key
設定4臺機器的/etc/hosts
設定4臺機器ssh通路
在ansible所安裝的機器上,追加機器資訊到/etc/ansible/hosts中
確認ansible正常動作
Step 3:下載contrib
[[email protected] local]# cd ..
[[email protected] /]# mkdir -p /local; cd /local
[[email protected] local]# git clone https://github.com/kubernetes/contrib
Cloning into 'contrib'...
remote: Counting objects: 30938, done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 30938 (delta 12), reused 0 (delta 0), pack-reused 30907
Receiving objects: 100% (30938/30938), 32.69 MiB | 153.00 KiB/s, done.
Resolving deltas: 100% (14922/14922), done.
Checking out files: 100% (16114/16114), done.
[[email protected] local]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
Step 4:生成kubernetes-ansible設定檔案
按照構成說明中的4臺機器構成,設定inventory檔案
[root@host131 inventory]# pwd
/local/contrib/ansible/inventory
[root@host131 inventory]# ll
total 4
drwxr-xr-x. 2 root root 20 Aug 29 20:06 group_vars
-rw-r--r--. 1 root root 132 Aug 29 20:06 localhost.ini
[root@host131 inventory]# cp localhost.ini inventory
[root@host131 inventory]# vi inventory
[root@host131 inventory]# cat inventory
[masters]
host131
[nodes]
host132
host133
host134
[etcd]
host131
[root@host131 inventory]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
設定inventory/group_vars/all.yml,將kube-ui開啟,從default的false該為true
85 # Turn to false to disable the kube-ui addon for this cluster
86 kube-ui: false
Step 6:安裝所需package
依賴關係整理的真不錯,只有一個package在github的說明中雖然沒有提到,但是實際是需要提前安裝的
No | package | 機器 | 安裝命令 |
---|---|---|---|
1 | python-netaddr | 192.168.32.131 | yum -y install python-netaddr |
Step 7:執行安裝檔案
執行安裝檔案deploy-cluster.sh
[root@host131 ansible]# pwd
/local/contrib/ansible
[root@host131 ansible]# cd scripts/ && ./deploy-cluster.sh
- 1
- 2
- 3
Step 8:確認kubernetes
確認node
確認services
確認版本
[root@host131 scripts]# kubectl version
Client Version: version.Info{Major:"1", Minor:"2", GitVersion:"v1.2.0", GitCommit:"ec7364b6e3b155e78086018aa644057edbe196e5", GitTreeState:"clean"}
Server Version: version.Info{Major:"1", Minor:"2", GitVersion:"v1.2.0", GitCommit:"ec7364b6e3b155e78086018aa644057edbe196e5", GitTreeState:"clean"}
[root@host131 scripts]#
[root@host131 scripts]# kubectl get nodes
NAME STATUS AGE
host132 Ready 2m
host133 Ready 2m
host134 Ready 2m
[root@host131 scripts]# kubectl get service
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 12m
[root@host131 scripts]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
Hello world示例
事前準備
因為pod在建立的時候會用到一個helper的映象,以前叫pause-amd64,在k8s1.2之後都是叫做pause,因為它需要從gcr.io下載,所以大部分使用者都無法下載,所以pod的狀態會一直是ContainerCreating。我把這個放到一個docker使用者都可以訪問的地方,你也可以使用各種方法,只要取到對應的版本,將其部署到各個節點即可。在本文示例中,如下操作將在4個node都同樣作一下。
[root@host131 ~]# docker search liumiaocn
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/liumiaocn/pause-amd64 gcr.io/google_containers/pause:2.0: Kubern... 0 [OK]
[root@host131 ~]# docker pull docker.io/liumiaocn/pause-amd64
Using default tag: latest
Trying to pull repository docker.io/liumiaocn/pause-amd64 ...
latest: Pulling from docker.io/liumiaocn/pause-amd64
8f216013977d: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:51dbbd8636156e778a30c9eaf481b6e751552aac4fc654795d58cdbfb516d25e
Status: Downloaded newer image for docker.io/liumiaocn/pause-amd64:latest
[root@host131 ~]# docker tag docker.io/liumiaocn/pause-amd64 gcr.io/google_containers/pause:2.0
[root@host131 ~]# docker images |grep 2.0
gcr.io/google_containers/pause 2.0 c5147e5f6708 Less than a second ago 350.2 kB
[root@host131 ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
建立一個deployment
以前的RC現在逐漸會被deployment所替代,但是yml寫法基本沒有變法,不過cli的方式下有了明顯的變化。用如下的命令建立一個執行起2個例項的redmine,埠為80。為了提高速度,可以事前在node的各節點將redmine的映象提前pull下來。
kubectl run redminemgnt --image=redmine --replicas=2 --port=80
- 1
確認結果
[root@host131 ~]# kubectl run redminemgnt --image=redmine --replicas=2 --port=80
deployment "redminemgnt" created
[root@host131 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
redminemgnt-298222028-85fi9 1/1 Running 0 3m
redminemgnt-298222028-tfqi2 1/1 Running 0 3m
[root@host131 ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
redminemgnt 2 2 2 2 4m
[root@host131 ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
expose對外的port
export對外的port,以使得其他機器可以訪問
[root@host131 ~]# kubectl expose deployment redminemgnt --port=80 --type=LoadBalancer
service "redminemgnt" exposed
[root@host131 ~]#
- 1
- 2
- 3
確認2個例項如何啟動在3個node上
通過如下可以看到,當我們把2個redmine的例項啟動在3個node上的時候,k8s自動的進行的分配到了host133和host134,關於分配策略可以自己通過調整scale進行實驗確認以加深理解。
[root@host131 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE NODE
redminemgnt-298222028-85fi9 1/1 Running 0 10m host134
redminemgnt-298222028-tfqi2 1/1 Running 0 10m host133
[root@host131 ~]#
- 1
- 2
- 3
- 4
- 5
確認deploy
[[email protected] ~]# kubectl describe deployment redminemgnt
Name: redminemgnt
Namespace: default
CreationTimestamp: Tue, 30 Aug 2016 00:10:46 -0400
Labels: run=redminemgnt
Selector: run=redminemgnt
Replicas: 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
OldReplicaSets: <none>
NewReplicaSet: redminemgnt-298222028 (2/2 replicas created)
Events:
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
13m 13m 1 {deployment-controller } Normal ScalingReplicaSet Scaled up replica set redminemgnt-298222028 to 2
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
確認pod
describe命令把各個組成部分清晰的列出來,所以對我們加深理解有很好的作用
[[email protected] ~]# kubectl describe pod redminemgnt-298222028-85fi9
Name: redminemgnt-298222028-85fi9
Namespace: default
Node: host134/192.168.32.134
Start Time: Tue, 30 Aug 2016 00:10:47 -0400
Labels: pod-template-hash=298222028,run=redminemgnt
Status: Running
IP: 172.16.64.4
Controllers: ReplicaSet/redminemgnt-298222028
Containers:
redminemgnt:
Container ID: docker://61e5320fc6877707c9cabeed8a272a41515d2a4ccf63f137d2eff760741e3865
Image: redmine
Image ID: docker://sha256:55ccd586ad119ceeb784ac924a15dd1060fa935beced3ce32e74967a0bc0ffac
Port: 80/TCP
QoS Tier:
cpu: BestEffort
memory: BestEffort
State: Running
Started: Tue, 30 Aug 2016 00:11:56 -0400
Ready: True
Restart Count: 0
Environment Variables:
Conditions:
Type Status
Ready True
Volumes:
default-token-8209b:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-8209b
Events:
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- -------- ------ -------
13m 13m 1 {default-scheduler } Normal Scheduled Successfully assigned redminemgnt-298222028-85fi9 to host134
12m 12m 1 {kubelet host134} spec.containers{redminemgnt} Normal Pulling pulling image "redmine"
12m 12m 1 {kubelet host134} spec.containers{redminemgnt} Normal Pulled Successfully pulled image "redmine"
12m 12m 1 {kubelet host134} spec.containers{redminemgnt} Normal Created Created container with docker id 61e5320fc687
12m 12m 1 {kubelet host134} spec.containers{redminemgnt} Normal Started Started container with docker id 61e5320fc687
[[email protected] ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
這樣第一個簡單的kubernetes的Helloworld就完成了,接下我們會更加詳細的學習k8s的其他基礎知識。
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net