Skip to content

antony@notes:~/kubernetes$ cat "CKAD-準備.md"

CKAD 準備

2024-05-08· kubernetes

CKAD 考前實作準備

練習環境準備

# 1. Download shell scripts and YAML files
$ wget https://training.linuxfoundation.org/cm/LFD259/LFD259_V2024-02-22_SOLUTIONS.tar.xz \
--user=LFtraining --password=Penguin2014

$ tar -xvf LFD259_V2024-02-22_SOLUTIONS.tar.xz

Exercise 2.3: Create a Basic Pod

$ cd LFD259/SOLUTIONS/s_02

$ cat basic.yaml
apiVersion: v1
kind: Pod
metadata:
  name: basicpod
spec:
  containers:
  - name: webcont
    image: nginx

#  Create the new pod using the recently created YAML file
$ kubectl create -f basic.yaml
pod/basicpod created

$ kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
basicpod   1/1     Running   0          63s

# Shut down the pod and verify it is no longer running.
$ kubectl delete pod basicpod
pod "basicpod" deleted

$ kubectl get pods
No resources found in default namespace.

# We will now configure the pod to expose port 80. This configuration does not interact with the container to determine what port to open. We have to know what port the process inside the container is using, in this case port 80 as a web server. Add two lines to the end of the file
$ echo -e "    ports:\n    - containerPort: 80" >> basic.yaml

$ cat basic.yaml
apiVersion: v1
kind: Pod
metadata:
  name: basicpod
spec:
  containers:
  - name: webcont
    image: nginx
    ports:
    - containerPort: 80

# Create the pod and verify it is running.
$ kubectl create -f basic.yaml
pod/basicpod created

$ kubectl get pods -o wide
NAME       READY   STATUS    RESTARTS   AGE   IP            NODE   NOMINATED NODE   READINESS GATES
basicpod   1/1     Running   0          6s    10.244.1.14   k1w2   <none>           <none>

$ curl http://10.244.1.14
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

$ kubectl delete pod basicpod
pod "basicpod" deleted

# We will now create a simple service to expose the pod to other nodes and pods in the cluster. The service YAML will have the same four sections as a pod, but different spec configuration and the addition of a selector. Again, copy of the example from the tarball instead of typing the file by hand.
$ cat basicservice.yaml
apiVersion: v1
kind: Service
metadata:
  name: basicservice
spec:
  selector:
    type: webserver
  ports:
  - protocol: TCP
    port: 80
    
# We will also add a label to the pod and a selector to the service so it knows which object to communicate with
$ cat basic.yaml
apiVersion: v1
kind: Pod
metadata:
  name: basicpod
  labels:                 #<-- Add this line
    type: webserver       #<-- and this line which matches selector
spec:
  containers:
  - name: webcont
    image: nginx
    ports:
    - containerPort: 80

$ kubectl create -f basic.yaml
$ kubectl create -f basicservice.yaml
service/basicservice created

$ kubectl get pods,svc
NAME           READY   STATUS    RESTARTS   AGE
pod/basicpod   1/1     Running   0          55s

NAME                   TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/basicservice   ClusterIP   10.98.0.176   <none>        80/TCP    10s
service/kubernetes     ClusterIP   10.98.0.1     <none>        443/TCP   10d

$ curl http://10.98.0.176
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

$ kubectl delete -f basic.yaml
pod "basicpod" deleted

$ kubectl delete -f basicservice.yaml
service "basicservice" deleted

# We will now expose the service to outside the cluster as well. Delete the service, edit the file and add a type declaration
$ sed -i "8i \ \ type: NodePort" basicservice.yaml
$ cat basicservice.yaml
apiVersion: v1
kind: Service
metadata:
  name: basicservice
spec:
  selector:
    type: webserver
  type: NodePort
  ports:
  - protocol: TCP
    port: 80

$ kubectl create -f basicservice.yaml
service/basicservice created
[
$ kubectl get svc
NAME           TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
basicservice   NodePort    10.98.0.178   <none>        80:32011/TCP   2s
kubernetes     ClusterIP   10.98.0.1     <none>        443/TCP        10d

$ kubectl get pods basicpod --no-headers -o custom-columns=HostIP:.status.hostIP
172.22.1.16

$ curl http://172.22.1.16:32011
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>