├── COPYRIGHT.md
├── README.md
├── docs
├── 01-prerequisites.md
├── 02-generating-tls-certificate.md
├── 03-generating-kubenetes-configuration-file.md
├── 04-generating-data-encryption-key.md
├── 05-tip-n-trick.md
├── 06-transfer-file.md
├── 07-install_etcd_cluster.md
├── 08-install_kubernetes_control_plane.md
├── 09-loadbalancer.md
├── 10-install-worker-node.md
├── 11-kubernetes-object-authorization.md
├── 12-kubectl-remote-access.md
├── 13-pod-network.md
├── 14-dns-for-services-and-pods.md
├── 15-test.md
└── 16-install-metallb.md
└── images
├── README.md
├── kube-component.png
└── ss1.png
/COPYRIGHT.md:
--------------------------------------------------------------------------------
1 | # Copyright
2 |
3 | 
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | > เอกสารฉบับนี้ อ้างอิงจาก Kubernetes The Hard Way ที่ถูกพัฒนาโดย [Kelsey Hightower](https://github.com/kelseyhightower/kubernetes-the-hard-way) และอีกฉบับที่ถูกพัฒนาโดย [Mumshad Mannambeth](https://github.com/mmumshad/kubernetes-the-hard-way)
2 | # Kubernetes The Hard Way - CentOS
3 | ใน Repository นี้เป็นเอกสารและข้อมูลติดตั้ง Kubernetes แบบ Kubernetes The Hard Way ตามแนวทางของ Kelsey Hightower จากข้่อมูลในวันที่ 20 กันยายน 2563 แนวของ Kelsey Hightower จะอัางถึงระบบที่ถูกสร้างอยู่ใน Google Cloud Platform ทั้งหมด โดยใช้ Kubernetes v1.18.6 และติดตั้งบนระบบปฏิบัติการ Ubuntu ผู้เขียนได้พัฒนาและปรับปรุงให้ทันสมัยมากขึ้น และปรับให้ใช้อยู่บนพื้นฐานของระบบปฏิบัติการ CentOS 8 โดยมีเจตนาจะเผยแพร่ให้กับบุคคลทั่วไปที่สนใจศึกษาการติดตั้ง Kubernetes
4 | # Copyright
5 |
6 | 
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
7 | # รายละเอียดของ Kubernetes Cluster
8 | - Kubernetes 1.19.2
9 | - Docker 19.3.13
10 | - cni v0.8.7
11 | - etcd v3.4.13
12 | - NGINX v1.14.1 (Loadbalancer)
13 | - ชุด IP ภายใน Cluster 10.96.0.0/24
14 | - Cluster DNS 10.96.0.10
15 | # สารบัญ Kubernetes The Hard Way - CentOS
16 | [01. รายละเอียดในการติดตั้ง](docs/01-prerequisites.md)
17 |
18 | [02. แนวคิดในการสร้าง TLS Certificate ใน Kubernetes และ สร้าง Certificate Authority กับ TLS Certificate](docs/02-generating-tls-certificate.md)
19 |
20 | [03. สร้าง Kubernetes Configuration Files สำหรับ Authentication](docs/03-generating-kubenetes-configuration-file.md)
21 |
22 | [04. สร้าง Data Encryption Config และ Key](docs/04-generating-data-encryption-key.md)
23 |
24 | [05. เคล็ดไม่ลับ เล็ก ๆ น้อย ๆ](docs/05-tip-n-trick.md)
25 |
26 | [06. โอนถ่ายไฟล์ที่สร้างไปยัง Virtual Machine](docs/06-transfer-file.md)
27 |
28 | [07. ติดตั้ง etcd](docs/07-install_etcd_cluster.md)
29 |
30 | [08. ติดตั้ง Kubernetes Control Plane](docs/08-install_kubernetes_control_plane.md)
31 |
32 | [09. ติดตั้ง Loadbalancer สำหรับ master node](docs/09-loadbalancer.md)
33 |
34 | [10, ติดตั้ง Kubernetes Worker Nodes](docs/10-install-worker-node.md)
35 |
36 | [11. Kubernetes Object Authorization](docs/11-kubernetes-object-authorization.md)
37 |
38 | [12. สร้าง kubeconfig สำหรับ remote access](docs/12-kubectl-remote-access.md)
39 |
40 | [13. ติดตั้ง Network สำหรับ Pod](docs/13-pod-network.md)
41 |
42 | [14. ติดตั้ง DNS สำหรับ Services และ Pods](docs/14-dns-for-services-and-pods.md)
43 |
44 | [15. ทดสอบการทำงาน](docs/15-test.md)
45 |
46 | # ขอบคุณ
47 | - ขอบคุณโอกาสที่ได้เรียนรู้จาก MFEC Public Company Limited
48 | - ขอบคุณ @Sirinat Oam Paphatsirinatthi และ @Saritrat Jirakulphondchai จาก @KubeOps Skills ที่พาเดินก้าวแรกให้เรียนรู้
49 | - ขอบคุณ @Sukkarin ที่เป็นเพื่อนคุย เพื่อนคิด ช่วยหาคำตอบตอนที่คิดไม่ออก
50 | - ขอบคุณทุกคนที่เป็นกำลังใจ และฟังเสียงบ่นใน Facebook
51 |
52 | # สุดท้าย
53 | เอกสารชุดนี้เขียนจากการลองผิด ลองถูก ตามเอกสารที่อ้างอิงของผู้เขียนเอง และเห็นว่าเอกสารชุดนี้ยังไม่มีชุดที่เป็นภาษาไทยจึงได้พัฒนาเอกสารชุดนี้ขึ้นมา ผู้เขียนยินดีรับฟังทุกความคิดเห็นในเชิงสร้างสรรค์ เพื่อช่วยกันพัฒนาเอกสารฉบับนี้ให้เป็นประโยชน์กับผู้ที่ต้องการศึกษา
54 |
55 | ด้วยจิตคารวะ
56 |
--------------------------------------------------------------------------------
/docs/01-prerequisites.md:
--------------------------------------------------------------------------------
1 | # รายละเอียดในการติดตั้ง
2 | ในขั้นตอนนี้เป็นขั้นตอนการเตรียม Virtual Machine สำหรับติดตั้ง Kubernetes และ Admin Workstation สำหรับเป็นเครื่องมือในการเตรียมข้อมูล และใช้ติดตั้ง Kubernetes ให้พร้อมในการติดตั้ง Kubernetes แบบ The Hard Way
3 | ## Admin Workstation
4 | ในกรณีของผู้เขียนใช้ macOS version 10.15.6 เป็นเครื่องหลักในการติดตั้ง ดังนั้นรายละเอียดในส่วนนี้ ก็จะอ้างอิงกับ macOS แต่เครื่องมือที่จำเป็นก็สามารถติดตั้งในระบบปฏิบัติการ Linux ได้ เครื่องมือจะถูกติดตั้งผ่าน [Homebrew](https://brew.sh/) ที่เป็นเครื่องมือ Package Manager ที่ช่วยอำนวยความสะดวกในการติดตั้ง software เพิ่มเติมใน macOS
5 | ### ติดตั้ง cfssl และ kubectl
6 | ```
7 | brew install cfssl kubectl
8 | ```
9 | > ตรวจสอบการติดตั้ง
10 | ```
11 | $ cfssl version
12 | Version: dev
13 | Runtime: go1.14
14 | $ cfssljson --version
15 | Version: dev
16 | Runtime: go1.14
17 | $ kubectl version --client
18 | Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.2", GitCommit:"f5743093fd1c663cb0cbc89748f730662345d44d", GitTreeState:"clean", BuildDate:"2020-09-16T21:51:49Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"darwin/amd64"}
19 | ```
20 | นอกจาก software ที่ต้องติดตั้งเพิ่มเติม ก็ยังใช้ software ที่ถูกติดตั้งมาพร้อมกับ macOS อยู่แล้วได้แก่ terminal, ssh client, ssh-keygen
21 | ### สร้าง Key Pair สำหรับ Authentication เครื่อง Virtual Machine
22 | ```
23 | $ ssh-keygen -t ed25519
24 | Generating public/private ed25519 key pair.
25 | Enter file in which to save the key (/Users/drs/.ssh/id_ed25519):
26 | Enter passphrase (empty for no passphrase):
27 | Enter same passphrase again:
28 | Your identification has been saved in id_ed25519.
29 | Your public key has been saved in id_ed25519.pub.
30 | The key fingerprint is:
31 | SHA256:4Ox72ZR/DwqpVCu0LtmN+KQ0BWGgyYI7G6x9QejOYHs drs@Tyche6c.local
32 | The key's randomart image is:
33 | +--[ED25519 256]--+
34 | | ..o |
35 | |.. + . . |
36 | |o = . o |
37 | |.+ . o o |
38 | |=o. . o S .. |
39 | |oO. o o ooo |
40 | |o.+E. ++=*+. . |
41 | | .. .+B=oo..... |
42 | | o++ .. ..|
43 | +----[SHA256]-----+
44 | ```
45 | ## Virtual Machine
46 | ใน lab ของผู้เขียนใช้ [oVirt](https://www.ovirt.org/) เป็นเครื่องมือในการจัดการ ซึ่งแต่ละคนอาจจะใช้เครื่องมือที่ต่างกัน เช่น VirtualBox, VMWare Workstation, VMWare vSphere, VMWare vCenter, OpenStack, Azure Stack หรือจะเป็น Instance ที่ Public Cloud ก็ได้ ในการติดตั้งครั้งนี้ต้องการ Virtual Machine ทั้งหมด 6 เครื่องดังนี้
47 | |Item|Hostname|IP|Function|
48 | |----|--------|--|--------|
49 | |1|api.example.com|192.168.254.60|Loadbalancer|
50 | |2|master0.example.com|192.168.254.61|Master|
51 | |3|master1.example.com|192.168.254.62|Master|
52 | |4|master2.example.com|192.168.254.63|Master|
53 | |5|node0.example.com|192.168.254.64|Worker|
54 | |6|node1.example.com|192.168.254.65|Worker|
55 | > - ทุกเครื่องมี 2 vCPU, Memory 2 GB, HDD 10 GB
56 | > - ทุกเครื่องมีระบบปฏิบัติการ CentOS 8.2
57 | > - ติดตั้ง Docker ที่ Worker Node ทั้งสองเครื่อง
58 | > - ทุกเครื่องสามารถใช้ SSH Key-Based Authentication ได้
59 | ### ตัวอย่างการโอนถ่าย Public Key ไปยังเครื่อง Virtual Machine เพื่อให้สามารถทำ SSH Key-Based Authentication ได้
60 | ```
61 | $ ssh-copy-id root@192.168.254.60
62 | /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/Users/drs/.ssh/id_ed25519.pub"
63 | /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
64 | /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
65 | root@192.168.254.60's password:
66 |
67 | Number of key(s) added: 1
68 |
69 | Now try logging into the machine, with: "ssh 'root@192.168.254.60'"
70 | and check to make sure that only the key(s) you wanted were added.
71 | ```
72 | ### ตรวจสอบ Docker ที่ติดตั้งใน Worker Node
73 | ```
74 | # docker version
75 | Client: Docker Engine - Community
76 | Version: 19.03.13
77 | API version: 1.40
78 | Go version: go1.13.15
79 | Git commit: 4484c46d9d
80 | Built: Wed Sep 16 17:03:45 2020
81 | OS/Arch: linux/amd64
82 | Experimental: false
83 |
84 | Server: Docker Engine - Community
85 | Engine:
86 | Version: 19.03.13
87 | API version: 1.40 (minimum version 1.12)
88 | Go version: go1.13.15
89 | Git commit: 4484c46d9d
90 | Built: Wed Sep 16 17:02:21 2020
91 | OS/Arch: linux/amd64
92 | Experimental: false
93 | containerd:
94 | Version: 1.2.13
95 | GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
96 | runc:
97 | Version: 1.0.0-rc10
98 | GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
99 | docker-init:
100 | Version: 0.18.0
101 | GitCommit: fec3683
102 | ```
103 | **Next>** [แนวคิดในการสร้าง TLS Certicate ใน Kubernetes และ สร้าง Certificate Authority กับ TLS Certificate](02-generating-tls-certificate.md)
104 |
--------------------------------------------------------------------------------
/docs/02-generating-tls-certificate.md:
--------------------------------------------------------------------------------
1 | # แนวคิดในการสร้าง TLS Certificate ใน Kubernetes และ สร้าง Certificate Authority กับ TLS Certificate
2 | ในการสื่อสารกับ Kubernetes ทั้งภายในและภายนอก Cluster เรื่องความปลอดภัยในการสื่อสารเป็นเรื่องที่ได้รับการออกแบบให้มีความปลอดภัย และ [TLS (Transport Layer Security)](https://en.wikipedia.org/wiki/Transport_Layer_Security) ก็เป็นวิธีการที่ถูกเลือกใช้ในการจัดการเรื่องนี้ ส่วนเรื่องของการ Authentication ของ client ในการขอเชื่อมต่อก็จะใช้วิธีการที่เรียกว่า [Mutual Authentication แบบ Certificated based](https://en.wikipedia.org/wiki/Mutual_authentication) ดังนั้นจึงจำเป็นต้องสร้าง key pair จำนวนมากในการติดตั้ง Kubernetes
3 | ## ต้องสร้าง Key Pair ที่ไหนบ้าง
4 | ในการใช้งาน TLS ส่วนของการ Authenitation ใน TLS ใช้ [Public-Key Cryptography](https://en.wikipedia.org/wiki/Public-key_cryptography) ในส่วนนี้จึงสร้าง key pair โดยหลักคิดก็คือว่า component ตัวไหนทำหน้าที่เป็น Server ในการให้บริการก็ต้องสร้าง key pair ให้ และ component ไหนทำหน้าที่เป็น client ในการขอใช้บริการก็ต้องสร้าง key pair ให้
5 | 
6 | - วงกลมสีฟ้าที่มีอักษร S แสดงในองค์ประกอบนั้นทำหน้าที่เป็น Server ที่ทำหน้าที่ให้บริการ service ในหน้าที่ของตัวเอง
7 | - วงกลมสีฟ้าที่มีอักษร C แสดงในองค์ประกอบนั้นทำหน้าที่เป็น Client ต้องขอใช้บริการใน service ที่ต้องการ
8 | > ในเอกสาร version แรกจะเป็นไปตามแนวทางที่ผู้เขียนอ้างอิงถึงจะมีจุดสังเกตุอยู่ 2 จุด
9 | > - client key pair จาก kube-apiserver ไปยัง etcd จะใช้ key pair ของ etcd ในการเชื่อมต่อ
10 | > - client key pair จาก kube-apiserver ไปยัง kubelet จะใช้ key pair ของ kube-apiserver ในการเชื่อมต่อ
11 | > - client key pair จาก kubelet ไปยัง kube-apiserver จะใช้ key pair ของ kubelet ในการเชื่อมต่อ
12 |
13 | ## สร้าง CA (Certificate Authority) [Admin Workstation]
14 | ประเด็นหนึ่งในการทำ Mutual Authentication แบบ Certificated based คือ Ceriticate ในฝั่ง Client จะต้องมี Digital Signature ที่ถูก sign จาก CA เดียวกับฝั่ง Server ดังนั้นการสร้าง CA key pair จึงเป็นขั้นตอนแรก และเครื่องมือที่จะช่วยในการสร้าง key pair ก็คือ `cfssl`
15 | ```
16 | {
17 |
18 | cat > ca-config.json < ca-csr.json < เนื่องจากผู้เขียน มีความเห็นส่วนตัวเรื่องการตั้งชื่อไฟล์ของ Key และ Certificate ว่าการตั้งไฟล์ที่ลงท้ายด้วย .key และ .crt ทำให้ผู้เขียนจัดการไฟล์ได้สะดวกกว่า
61 | ## สร้าง Client และ Server Key Pair
62 | ในส่วนนี้จะเป็นการ Key Pair สำหรับส่วนประกอบต่าง ๆ ที่จำเป็นในการติดตั้ง Kubernetes
63 | ### สร้าง Admin Client Key Pair
64 | ```
65 | {
66 | cat > admin-csr.json < ซึ่งจะกำหนดที่ CommonName และ Organization ใน Certificate Attributes
98 | ```
99 | {
100 | declare -A NODE_IP
101 | NODE_IP[node0]="192.168.254.64"
102 | NODE_IP[node1]="192.168.254.65"
103 |
104 |
105 | for instance in node0 node1; do
106 | cat > ${instance}-csr.json < ในส่วนของการสร้าง Kubelet Client Certificate ทดสอบบน zsh ใน macOS แล้วใช้งานได้ แต่ยังไม่ได้สอบใน bash
142 | ### สร้าง Controller Manager Client Certificate
143 | ```
144 | {
145 |
146 | cat > kube-controller-manager-csr.json < kube-proxy-csr.json < kube-scheduler-csr.json < kubernetes-csr.json < service-account-csr.json < etcd-server-csr.json <** [สร้าง Kubernetes Configuration Files สำหรับ Authentication](03-generating-kubenetes-configuration-file.md)
348 |
349 | **** [สร้าง Data Encryption Config และ Key](04-generating-data-encryption-key.md)
140 |
141 | ** encryption-config.yaml <** [เคล็ดไม่ลับ เล็ก ๆ น้อย ๆ](05-tip-n-trick.md)
29 |
30 | **** [โอนถ่ายไฟล์ที่สร้างไปยัง Virtual Machine](06-transfer-file.md)
5 |
6 | **** [ติดตั้ง etcd](07-install_etcd_cluster.md)
21 |
22 | ** eth0 เป็นชื่อ network interface card ซึ่งอาจจะต่างกันไป
36 | ## สร้าง `etcd.service` สำหรับ systemd [all master node]
37 | ```
38 | {
39 | cat < ผลการทดสอบ
89 | ```
90 | 41cf2dc7f859bfed, started, master2, https://192.168.254.63:2380, https://192.168.254.63:2379, false
91 | 8283bd82ad10dcb8, started, master1, https://192.168.254.62:2380, https://192.168.254.62:2379, false
92 | fcae0ac3856fb516, started, master0, https://192.168.254.61:2380, https://192.168.254.61:2379, false
93 | ```
94 | **Next>** [ติดตั้ง Kubernetes Control Plane](08-install_kubernetes_control_plane.md)
95 |
96 | ** eth0 เป็นชื่อ network interface card ซึ่งอาจจะต่างกันไป
33 | ## สร้าง `kube-apiserver.service` สำหรับ systemd [all master node]
34 | ```
35 | cat < ผลการทดสอบ
143 | ```
144 | [+]ping ok
145 | [+]log ok
146 | [+]etcd ok
147 | [+]informer-sync ok
148 | [+]poststarthook/start-kube-apiserver-admission-initializer ok
149 | [+]poststarthook/generic-apiserver-start-informers ok
150 | [+]poststarthook/start-apiextensions-informers ok
151 | [+]poststarthook/start-apiextensions-controllers ok
152 | [+]poststarthook/crd-informer-synced ok
153 | [+]poststarthook/bootstrap-controller ok
154 | [+]poststarthook/rbac/bootstrap-roles ok
155 | [+]poststarthook/scheduling/bootstrap-system-priority-classes ok
156 | [+]poststarthook/priority-and-fairness-config-producer ok
157 | [+]poststarthook/start-cluster-authentication-info-controller ok
158 | [+]poststarthook/start-kube-aggregator-informers ok
159 | [+]poststarthook/apiservice-registration-controller ok
160 | [+]poststarthook/apiservice-status-available-controller ok
161 | [+]poststarthook/kube-apiserver-autoregistration ok
162 | [+]autoregister-completion ok
163 | [+]poststarthook/apiservice-openapi-controller ok
164 | [+]shutdown ok
165 | healthz check passed
166 | ```
167 | **Next>** [ติดตั้ง Loadbalancer สำหรับ master node](09-loadbalancer.md)
168 |
169 | **> /etc/nginx/nginx.conf
20 | systemctl enable --now nginx
21 | ```
22 | **Next>** [ติดตั้ง Kubernetes Worker Nodes](10-install-worker-node.md)
23 |
24 | **ผลการทดสอบ
115 | ```
116 | NAME STATUS ROLES AGE VERSION
117 | node0 NotReady 10m v1.19.0
118 | node1 NotReady 6m7s v1.19.0
119 | ```
120 | > ผลที่ได้ สถานะของ node จะยังเป็น Not Ready เนื่องจาก ในส่วนของ Network ยังไม่ได้ถูกกำหนดค่า
121 |
122 | **Next>** [Kubernetes Object Authorization](11-kubernetes-object-authorization.md)
123 |
124 | **** [สร้าง kubeconfig สำหรับ remote access](12-kubectl-remote-access.md)
23 |
24 | **** [ติดตั้ง Network สำหรับ Pod](13-pod-network.md)
24 |
25 | ** ผลการทดสอบ
17 | ```
18 | NAME READY STATUS RESTARTS AGE
19 | weave-net-c8wgm 1/2 Running 0 35s
20 | weave-net-lxwpn 1/2 Running 0 35s
21 | ```
22 | > จะพบว่ามี pod ที่ชื่อว่า weave-net-xxxxx ที่มีสถานะ Running ทำงานอยู่
23 | ## ตรวจสอบสถานะของ Worker Node [master0]
24 | ```
25 | kubectl get nodes
26 | ```
27 | > ผลการทดสอบ
28 | ```
29 | NAME STATUS ROLES AGE VERSION
30 | node0 Ready 14m v1.19.0
31 | node1 Ready 11m v1.19.0
32 | ```
33 | **Next>** [ติดตั้ง DNS สำหรับ Services และ Pods](14-dns-for-services-and-pods.md)
34 |
35 | ** ผลการทดสอบ จะพบ pod ชื่อว่า coredns-xxxxxxxxxx-xxxxx สถานะ Running อยู่
16 | ```
17 | NAME READY STATUS RESTARTS AGE
18 | coredns-7bf4bd64bd-zgtnf 1/1 Running 0 14s
19 | weave-net-c8wgm 2/2 Running 0 3m39s
20 | weave-net-lxwpn 2/2 Running 0 3m39s
21 | ```
22 | > จะพบว่ามี pod ที่ชื่อว่า coredns-xxxxxxxxxx-xxxxx สถานะ Running อยู่
23 | ### ทดสอบการทำงานของ CoreDNS [master0]
24 | ```
25 | ]# kubectl run dnstest --image centos -- sleep 36000
26 | pod/dnstest created
27 | ]# kubectl get pods
28 | NAME READY STATUS RESTARTS AGE
29 | dnstest 1/1 Running 0 50s
30 | ]# kubectl exec -it dnstest -- /bin/bash
31 | [root@dnstest /]# yum install -y bind-utils
32 | [root@dnstest /]# dig kubernetes.default.svc.cluster.local
33 |
34 | ; <<>> DiG 9.11.13-RedHat-9.11.13-6.el8_2.1 <<>> kubernetes.default.svc.cluster.local
35 | ;; global options: +cmd
36 | ;; Got answer:
37 | ;; WARNING: .local is reserved for Multicast DNS
38 | ;; You are currently testing what happens when an mDNS query is leaked to DNS
39 | ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8809
40 | ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
41 | ;; WARNING: recursion requested but not available
42 |
43 | ;; OPT PSEUDOSECTION:
44 | ; EDNS: version: 0, flags:; udp: 4096
45 | ; COOKIE: 1f618ef8f0a39ff7 (echoed)
46 | ;; QUESTION SECTION:
47 | ;kubernetes.default.svc.cluster.local. IN A
48 |
49 | ;; ANSWER SECTION:
50 | kubernetes.default.svc.cluster.local. 5 IN A 10.96.0.1
51 |
52 | ;; Query time: 0 msec
53 | ;; SERVER: 10.96.0.10#53(10.96.0.10)
54 | ;; WHEN: Wed Sep 23 15:04:06 UTC 2020
55 | ;; MSG SIZE rcvd: 129
56 |
57 | ```
58 | **Next>** [ทดสอบการทำงาน](15-test.md)
59 |
60 | **
7 |
8 |
9 | [root@master0 ~]# kubectl run pingtest -it --image centos -- /bin/bash
10 | If you don't see a command prompt, try pressing enter.
11 |
12 | [root@pingtest /]# ping -c 3 10.32.0.2
13 | PING 10.32.0.2 (10.32.0.2) 56(84) bytes of data.
14 | 64 bytes from 10.32.0.2: icmp_seq=1 ttl=64 time=1.49 ms
15 | 64 bytes from 10.32.0.2: icmp_seq=2 ttl=64 time=0.666 ms
16 | 64 bytes from 10.32.0.2: icmp_seq=3 ttl=64 time=0.699 ms
17 |
18 | --- 10.32.0.2 ping statistics ---
19 | 3 packets transmitted, 3 received, 0% packet loss, time 17ms
20 | rtt min/avg/max/mdev = 0.666/0.952/1.493/0.383 ms
21 |
22 |
23 | [root@pingtest /]# exit
24 | exit
25 | Session ended, resume using 'kubectl attach pingtest -c pingtest -i -t' command when the pod is running
26 | ```
27 | ## ทดสอบสร้าง Deployment
28 | ```
29 | [root@master0 ~]# echo "
30 | apiVersion: apps/v1
31 | kind: Deployment
32 | metadata:
33 | name: dp1
34 | spec:
35 | replicas: 5
36 | selector:
37 | matchLabels:
38 | run: dp-hello
39 | template:
40 | metadata:
41 | name: my-app
42 | labels:
43 | run: dp-hello
44 | spec:
45 | containers:
46 | - name: my-hello
47 | image: nginxdemos/nginx-hello:plain-text
48 | " | kubectl apply -f -
49 |
50 |
51 | [root@master0 ~]# kubectl get pods
52 | NAME READY STATUS RESTARTS AGE
53 | dnstest 1/1 Running 0 17m
54 | dp1-6fc549dc58-4l686 1/1 Running 0 9s
55 | dp1-6fc549dc58-8pj9v 1/1 Running 0 9s
56 | dp1-6fc549dc58-rd7hp 1/1 Running 0 9s
57 | dp1-6fc549dc58-trcf8 1/1 Running 0 9s
58 | dp1-6fc549dc58-vgb5d 1/1 Running 0 9s
59 | pingtest 1/1 Running 1 8m29s
60 | ```
61 | ## ทดสอบสร้าง Service แบบ NodePort
62 | ```
63 | [root@master0 ~]# echo "
64 | apiVersion: v1
65 | kind: Service
66 | metadata:
67 | name: svc-np
68 | spec:
69 | type: NodePort
70 | selector:
71 | run: dp-hello
72 | ports:
73 | - port: 9090
74 | protocol: TCP
75 | targetPort: 8080
76 | nodePort: 31111
77 | " | kubectl apply -f -
78 |
79 |
80 | [root@master0 ~]# kubectl get services
81 | NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
82 | kubernetes ClusterIP 10.96.0.1 443/TCP 34m
83 | svc-np NodePort 10.96.0.178 9090:31111/TCP 9s
84 |
85 |
86 | [root@master0 ~]# curl http://node0.example.com:31111
87 | Server address: 10.44.0.3:8080
88 | Server name: dp1-6fc549dc58-rwmxr
89 | Date: 23/Sep/2020:15:25:40 +0000
90 | URI: /
91 | Request ID: a819dfc9ec38a94e5a8d4bcea85a0f07
92 |
93 |
94 | [root@master0 ~]# curl http://node1.example.com:31111
95 | Server address: 10.44.0.4:8080
96 | Server name: dp1-6fc549dc58-fngm2
97 | Date: 23/Sep/2020:15:25:44 +0000
98 | URI: /
99 | Request ID: 8ad1b8407f1b7c2759830b04547b5b49
100 |
101 |
102 | [root@master0 ~]# kubectl exec -it pingtest -- /bin/bash
103 |
104 |
105 | [root@pingtest /]# curl http://svc-np:9090
106 | Server address: 10.32.0.5:8080
107 | Server name: dp1-6fc549dc58-zgrm5
108 | Date: 23/Sep/2020:15:27:01 +0000
109 | URI: /
110 | Request ID: 2afe05ccb6eb274e4a40f96394a81a39
111 |
112 |
113 | [root@pingtest /]# curl http://svc-np.default.svc.cluster.local:9090
114 | Server address: 10.44.0.4:8080
115 | Server name: dp1-6fc549dc58-fngm2
116 | Date: 23/Sep/2020:15:27:12 +0000
117 | URI: /
118 | Request ID: 01d1a7c51d4e9a537fa69320e2075c1e
119 | ```
120 |
121 |
--------------------------------------------------------------------------------
/docs/16-install-metallb.md:
--------------------------------------------------------------------------------
1 | # ติดตั้ง Metallb เมื่อต้องการใช้ Service แบบ LoadBalancer
2 | หากต้องการใช้ Service แบบ Loadlbalancer จำเป็นต้องติดตั้ง software เพิ่มเติม ผู้เขียนเลือก Metallb เป็นตัวอย่างในเอกสารชุดนี้
3 | ## แก้ไข Kube-Proxy Configuration และ Restart kube-proxy service [all worker node]
4 | ```
5 | [root@node0 kube-proxy]# cat /var/lib/kube-proxy/kube-proxy-config.yaml
6 | kind: KubeProxyConfiguration
7 | apiVersion: kubeproxy.config.k8s.io/v1alpha1
8 | clientConnection:
9 | kubeconfig: "/var/lib/kube-proxy/kubeconfig"
10 | mode: "ipvs"
11 | ipvs:
12 | staticARP: true
13 | clusterCIDR: "10.0.0.0/16"
14 |
15 |
16 | systemctl restart kube-proxy
17 | ```
18 | ## ติดตั้ง Metallb และกำหนดค่า ConfigMap สำหรับ Metallb
19 | ```
20 | kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.3/manifests/namespace.yaml
21 | kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.3/manifests/metallb.yaml
22 | # On first install only
23 | kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
24 | [root@kube-master ~]# cat metallb-configmap.yml
25 | apiVersion: v1
26 | kind: ConfigMap
27 | metadata:
28 | namespace: metallb-system
29 | name: config
30 | data:
31 | config: |
32 | address-pools:
33 | - name: default
34 | protocol: layer2
35 | addresses:
36 | - 192.168.254.200-192.168.254.250
37 |
38 | kubectl create -f metallb-configmap.yml
39 | ```
40 | ## ทดสอบการทำงานของ Metallb โดยการสร้าง Service แบบ Loadbalancer
41 | ```
42 | [root@kube-master ~]# cat nginx-deployment.yml
43 | apiVersion: apps/v1
44 | kind: Deployment
45 | metadata:
46 | name: nginx-deployment
47 | spec:
48 | selector:
49 | matchLabels:
50 | app: nginx
51 | replicas: 2
52 | template:
53 | metadata:
54 | labels:
55 | app: nginx
56 | spec:
57 | containers:
58 | - name: nginx
59 | image: nginx:latest
60 | ports:
61 | - containerPort: 80
62 | kubectl apply -f nginx-deployment.yml
63 |
64 | [root@kube-master ~]# cat nginx-service.yml
65 | apiVersion: v1
66 | kind: Service
67 | metadata:
68 | name: nginx
69 | spec:
70 | type: LoadBalancer
71 | selector:
72 | app: nginx
73 | ports:
74 | - port: 80
75 | name: http
76 |
77 | kubectl apply -f nginx-service.yml
78 |
79 |
80 | [root@kube-master ~]# kubectl get services
81 | NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
82 | nginx LoadBalancer 10.109.67.153 192.168.254.200 80:32199/TCP 27m
83 | ```
84 | > จะพบว่า มี Service ขนิดที่เป็น LoadBalancer และมีค่าของ External-IP อยู่
85 |
--------------------------------------------------------------------------------
/images/README.md:
--------------------------------------------------------------------------------
1 | # Images
2 |
--------------------------------------------------------------------------------
/images/kube-component.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rdamrong/Kubernetes-The-Hard-Way-CentOS/272ac6ca2c15c8199aa220ece20e7feae3edad47/images/kube-component.png
--------------------------------------------------------------------------------
/images/ss1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rdamrong/Kubernetes-The-Hard-Way-CentOS/272ac6ca2c15c8199aa220ece20e7feae3edad47/images/ss1.png
--------------------------------------------------------------------------------