Kubernetes 安裝筆記

-- Pageviews

Kubernetes 安裝筆記

用 CentOS 練習安裝 Kubernetes 的筆記。

Install Docker

1
yum install -y docker

Install kubeadm & kubelet & kubectl

1
2
3
4
5
6
7
8
9
10
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubeadm kubelet kubectl

Before Start Service

1
2
3
4
5
6
# 允許 containers 連到 host
setenforce 0
# Disable swap
swapoff -a
# 初始化 kubeadm
kubeadm init --pod-network-cidr=10.244.0.0/16
1
2
3
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Start Service

1
2
3
4
5
systemctl enable docker
systemctl start docker

systemctl enable kubelet
systemctl start kubelet

Check Service

1
2
kubectl version
kubectl get cs

Apply Master has Node

1
kubectl taint nodes --all node-role.kubernetes.io/master-

Install Pod network

1
2
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
systemctl restart kubelet

Create Pod

1
2
3
4
5
6
7
8
9
10
11
12
13
# vi lab1-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: lab1-pod
labels:
app: webserver
spec:
containers:
- name: lab1-container
image: httpd
ports:
- containerPort: 80
1
2
3
kubectl create -f lab1-pod.yml
kubectl get pods
# kubectl delete pods lab1-pod

Bind Service

1
2
3
kubectl expose pod lab1-pod --name=lab1-pod-service --type=NodePort --port=80
kubectl get services
# kubectl delete services lab1-pod-service

Bind Replication Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# vi lab2-replication-controller.yml
apiVersion: v1
kind: ReplicationController
metadata:
name: lab2-replication-controller
labels:
app: webserver
spec:
replicas: 3
selector:
app: lab2-pod
template:
metadata:
labels:
app: lab2-pod
spec:
containers:
- name: lab2-container
image: httpd
ports:
- containerPort: 80
1
2
3
4
5
6
kubectl create -f lab2-replication-controller.yml
kubectl get rc

kubectl scale --replicas=4 -f lab2-replication-controller.yml

# kubectl delete rc lab2-replication-controller

Ref